简体   繁体   English

如何使用Python和ROS在Naoqi中订阅事件?

[英]How to subscribe to event in Naoqi using Python and ROS?

How can I subscribe to an event using ALMemory 's subscribeToEvent function, which requires the correct Python scope while using ROS (rospy) that initiates modules outside my code? 如何使用ALMemorysubscribeToEvent函数subscribeToEvent事件,当使用ROS(rospy)来初始化代码之外的模块时,该函数需要正确的Python作用域?

This question is similar to this Naoqi subscription question , but differs in the sense that rospy was not used, which is critical to the implementation. 该问题类似于此Naoqi订阅问题 ,但不同之处在于未使用rospy,这对实现至关重要。

My current code is as such; 我当前的代码就是这样;
mainBehaviour.py mainBehaviour.py

from nao.nao import Nao


class mainBehaviour(AbstractBehaviour):
    def init(self):
        global mainBehaviourMethod
        mainBehaviourMethod = self
        self.nao.setCallOnFall("mainBehaviourMethod", "robotHasFallen")

    def update(self):
        print("Update")

    def robotHasFallen(self, eventName, val, subscriberIdentifier):
        print("FALLING")

nao.py o

from naoqi import ALProxy, ALModule, ALBroker
import rospy
from math import radians
import weakref
class Nao(object):

    def __init__(self):
        self.nao_ip = rospy.get_param("nao_ip")
        self.port = 9559
        self.memory_proxy = ALProxy("ALMemory", self.nao_ip, self.port)

    def setCallOnFall(self, module, method):
        self.memory_proxy.subscribeToEvent("robotHasFallen", module, method)

What I want is that mainBehaviour.py, using nao.py, has its function robotHasFallen fire when the robot has fallen. 我想要的是使用nao.py的mainBehaviour.py具有其功能,当机器人掉落时会robotHasFallen However, the current code does not produce this behaviour (it ignores any fall), but does not produce an error either. 但是,当前代码不会产生此行为(它会忽略任何掉落),但是也不会产生错误。 I attempted to implement this using this ALMemory tutorial . 我试图使用此ALMemory教程来实现此目的 However, this tutorial makes use of a single python file, in which methods aren't instantiated by ROS. 但是,本教程使用单个python文件,其中ROS不会实例化方法。 Therefore I cannot make use of the line pythonModule = myModule("pythonModule") . 因此,我无法使用pythonModule = myModule("pythonModule") I attempted to still obtain this Python scope (on which the answer to the previously linked question states "the python variable must have the same name than the module name you create"), by declaring a global variable pointing to self . 我试图通过声明一个指向self的全局变量,来获得这个Python范围(先前链接的问题的答案指出“ python变量必须与您创建的模块名称具有相同的名称”)。

How can I create the desired behaviour, detecting a fallen robot using subscribeToEvent , using ROS with its consequences of not initiating modules myself and therefore not being able to pass its constructor? 我该如何创建所需的行为,使用subscribeToEvent检测掉落的机器人,使用ROS,其后果是无法自行启动模块,因此无法传递其构造函数?
I cannot install any further libraries because I use a university computer. 我使用的是大学计算机,因此无法安装其他任何库。

Your example code uses the "naoqi" library, but it's now more convenient to use the "qi" library (you can get it with "pip install qi", it's already present on your robot in versions 2.1 or above). 您的示例代码使用了“ naoqi”库,但是现在更方便地使用“ qi”库(您可以通过“ pip install qi”获得它,它已经存在于2.1版或更高版本的机器人中)。 In that version you can directly pass a callback, see here for a helper library that allows you to do events.connect("MyALMemoryKey", my_callback) (you pass the function and not just it's name - and it doesn't care where the function comes from). 在该版本中,您可以直接传递回调,请参见此处的帮助程序库,该库可让您执行events.connect("MyALMemoryKey", my_callback) (您传递的函数不仅是函数的名称,而且它不在乎功能来自)。

Under the hood, it does this: 在后台,它执行以下操作:

ALMemory.subscriber("MyALMemoryKey").signal.connect(my_callback)

(note that here ALMemory is a Service (qi framework) and not a module (naoqi framework). (请注意,此处ALMemory是服务 (qi框架),而不是模块 (naoqi框架)。

You can directly use that helper lib (see the doc here , and some samples using it here ), or use the same logic in your code (it's not very complicated, once you have a working example to start from). 您可以直接使用该帮助程序库(请参见此处的文档,以及一些使用示例的示例),或在代码中使用相同的逻辑(一旦您有了一个可行的示例,它就不会很复杂)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM