如何将python程序的输出传递给处理程序

时间:2016-09-27 07:57:47

标签: python raspberry-pi processing gyroscope

我正在通过python程序读取陀螺仪(sense-hat)的方向,该程序可以输出字符串中的位置。我正在尝试将此数据用作Processing程序中的输入,以使其根据陀螺仪的方向进行交互。我如何让Processing与python程序接口?

2 个答案:

答案 0 :(得分:1)

我之前从未使用过Sense HAT,但我猜测它是在幕后使用I2C。理论上应该可以使用它I2C io library重新实现Processing中的代码,但实际上可能需要花费很多精力,查看sense-hat library uses RTIMU以及所有花哨的过滤就是它自己的。

要让您的Python程序与Processing交谈,您至少有两个选择:

  1. pipe the output从python程序进入Processing stdin并解析通过
  2. 传来的内容
  3. 使用套接字。
  4. 第二个选项应该更简单,我可以想到多个选项:

    1. 原始UDP套接字
    2. OSC使用PyOSC表示Python,oscP5表示处理
    3. 使用WebSockets
    4. 我再次推荐第二个选项: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并运行Processing sketch,否则您可能会收到有关OSCClient无法连接的Python错误。想法是Processing成为OSC服务器而Python Script是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
      5. 安装

答案 1 :(得分:0)

我在bash脚本中使用了以下代码片段来从python程序中获取输出。我希望这有帮助。

OUTPUT="$(python your_program.py)"
print($OUTPUT)
相关问题