2016-09-27 68 views
1

我正在通過一個可以輸出字符串位置的python程序讀取陀螺儀(sense-hat)的方向。我正在嘗試將這些數據用作處理程序中的一個輸入,以便根據陀螺儀的方向進行交互。我將如何獲得處理與Python程序接口?如何將python程序的輸出傳遞到處理程序

+0

安裝它,你可以請給你想要達到什麼僞代碼呢? –

+3

它取決於python程序在哪裏發送它的輸出。通常它可能會寫入標準輸出(使用'print'),因此Processing程序將讀取其標準輸入,並且我們將使用匿名管道傳遞。這並不總是合適的,這裏沒有足夠的信息來做出明確的決定。 – cdarke

回答

1

我從來沒有用過Sense HAT,但我猜它是在幕後使用I2C。從理論上講,應該可以使用I2C io library來重新實現Processing中的代碼,但實際上可能需要付出相當大的努力,查看sense-hat library uses RTIMU以及它自己所做的所有幻想過濾。

爲了讓您的Python程序交談處理您至少有兩種選擇:

  1. pipe the output從Python程序中處理的stdin和解析什麼是通過
  2. 使用套接字來。

第二個選項應該是簡單,我能想到的多發地點:使用PyOSC爲Python和oscP5進行處理

  • 使用的WebSockets
    1. 原始UDP套接字
    2. OSC

      我建議第二個選項再次:UDP是非常快,OSC的頂部,這使得它向東傳遞帶有參數的消息。

      的Python腳本將:通過消息

      • 輪詢方位數據
      • 份額取向值等/orientation

      的處理草圖將:

      • 是OSC服務器服務器並等待數據
      • 從接收到的/orientation消息獲取3個浮動參數和繪製

      下面是在Python一個未測試概念證明發件人腳本的:

      import time 
      from sense_hat import SenseHat 
      from OSC import OSCClient, OSCMessage 
      
      #update 60 times a second -> feel free to adjust this what works best 
      delay = 1.0/60.0 
      # sense hat 
      sense = SenseHat() 
      # OSC client -> Processing 
      client = OSCClient() 
      client.connect(("localhost", 12000)) 
      
      
      while True: 
          # read sense hat 
          orientation = sense.get_orientation_degrees() 
          print("p: {pitch}, r: {roll}, y: {yaw}".format(**orientation)) 
          # send data to Processing 
          client.send(OSCMessage("/orientation", [orientation["pitch"],orientation["roll"],orientation["yaw"] ])) 
          # wait 
          time.sleep(delay) 
      

      和處理接收機:

      import oscP5.*; 
      import netP5.*; 
      
      OscP5 oscP5; 
      
      float pitch,roll,yaw; 
      
      void setup() { 
          size(400,400,P3D); 
          frameRate(25); 
          /* start oscP5, listening for incoming messages at port 12000 */ 
          oscP5 = new OscP5(this,12000); 
      } 
      
      
      void draw() { 
          background(0); 
          text("pitch: " + pitch + "\nroll: " + roll + "\nyaw: " + yaw,10,15); 
      } 
      
      /* incoming osc message are forwarded to the oscEvent method. */ 
      void oscEvent(OscMessage message) { 
          message.print(); 
          if(message.checkAddrPattern("/orientation")==true) { 
          /* check if the typetag is the right one. -> expecting float (pitch),float (roll), float (yaw)*/ 
          if(message.checkTypetag("fff")) { 
           pitch = message.get(0).floatValue(); 
           roll = message.get(1).floatValue(); 
           yaw = message.get(2).floatValue(); 
          } 
          } 
      } 
      

      注意您需要安裝PyOSC並運行Pr請先手動繪製草圖,否則您可能會收到有關OSCClient無法連接的Python錯誤。這個想法是處理成爲一個OSC服務器,Python腳本是一個OSCClient,服務器需要可供客戶端連接。 (你可以讓Python腳本,如果你想要一個OSC服務器和處理草圖如果你的作品好客戶端)

      要安裝PyOSC嘗試:

      sudo pip install pyosc 
      

      否則:

      cd ~/Downloads 
      wget https://pypi.python.org/packages/7c/e4/6abb118cf110813a7922119ed0d53e5fe51c570296785ec2a39f37606d85/pyOSC-0.3.5b-5294.tar.gz 
      tar xvzf pyOSC-0.3.5b-5294.tar.gz 
      cd pyOSC-0.3.5b-5294 
      sudo python setup.py install 
      

      再次,上述未經測試,但其想法是:

      1. 下載庫
      2. 將其解壓縮
      3. 導航到解壓縮文件夾
      4. 通過sudo python setup.py install
    0

    我在bash腳本中使用了下面的代碼片段來從python程序獲取輸出。我希望這有幫助。

    OUTPUT="$(python your_program.py)" 
    print($OUTPUT) 
    
    相關問題