【问题标题】:How to pass output from a python program into a processing program如何将python程序的输出传递给处理程序
【发布时间】:2016-09-27 07:57:46
【问题描述】:

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

【问题讨论】:

  • 你能给出你想要达到的目标的伪代码吗?
  • 这取决于 python 程序将其输出发送到哪里。通常它可能会写入标准输出(使用print),因此处理程序将读取其标准输入,我们将使用匿名管道传递。这并不总是合适的,这里没有足够的信息来做出明确的决定。

标签: python raspberry-pi processing gyroscope


【解决方案1】:

我以前从未使用过 Sense HAT,但我猜它在幕后使用了 I2C。从理论上讲,应该可以使用 I2C io library 重新实现 Processing 中的代码,但在实践中,查看 sense-hat library uses RTIMU 和所有花哨的过滤功能可能需要相当多的努力。

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

  1. pipe the output 从 python 程序到 Processing 的 stdin 并解析通过的内容
  2. 使用套接字。

第二个选项应该更简单,我可以想到多个选项:

  1. 原始 UDP 套接字
  2. OSC 使用 PyOSC 用于 Python,oscP5 用于处理
  3. 使用 WebSockets

我再次推荐第二个选项:UDP 非常快,并且 OSC 在此基础上使其可以向东传递带有参数的消息。

Python 脚本会:

  • 投票方向数据
  • 通过类似/orientation 的消息分享方向值

处理草图将:

  • 成为 OSC Server 服务器并等待数据
  • 从收到的/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 并运行处理程序,否则您可能会收到有关 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安装它

【讨论】:

    【解决方案2】:

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

    OUTPUT="$(python your_program.py)"
    print($OUTPUT)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-30
      • 1970-01-01
      • 1970-01-01
      • 2015-05-19
      • 2018-01-14
      • 2014-01-12
      相关资源
      最近更新 更多