简体   繁体   中英

roslibpy Messages don't hold more than one keyvalue pair

I'm working on sending input from a gamepad over rosbridge, using the inputs python module to get input from the gamepad. Each gamepad event has three pieces of information, the event type (afaik for telling between analog vs button input), the event code (the name of the button/joystick), and event state (analog values). If I send each of these pieces of information separately as their own Messages things work fine, but I'm trying to send all three in one message. Roslibpy's source and readthedocs both claim that messages are just dictionaries and can be treated as such, but if I try to send a message with more than on keyvalue pair I get error messages on the rosbridge server's console. EDIT: I'm using roslibpy because the talker node is on windows 7 and I'm sending information to a ROS Melodic server on ubuntu 18.04 via rosbridge.

In the example below, if I call talker.publish() with a Message containing a single keyvalue pair dictionary, the message sends successfully. With multiple pairs in the dictionary as shown below, I get errors.

the talker node:

from __future__ import print_function
import time

import roslibpy

from inputs import get_gamepad

def main():
    client = roslibpy.Ros(host='localhost', port=9090)
    client.run()

    talker = roslibpy.Topic(client, '/chatter', 'std_msgs/String')

    while client.is_connected:
        events = get_gamepad()
        for event in events:
            talker.publish(roslibpy.Message({'data': str(event.ev_type), 'btype': str(event.code), 'value': str(event.state) }))
            print('Sending message...\n')

    talker.unadvertise()

    client.terminate()

if __name__ == "__main__":
    main()

the listener node:

from __future__ import print_function
import roslibpy

def main():
    client = roslibpy.Ros(host='localhost', port=9090)
    client.run()

    listener = roslibpy.Topic(client, '/chatter', 'std_msgs/String')
    listener.subscribe(lambda message: out(message))

    try:
        while True:
            pass
    except KeyboardInterrupt:
        client.terminate()

def out( dict ):

    print('input type: ' + dict['data'])
    print('input button: ' + dict['btype'])
    print('button value: ' + dict['value'])


if __name__ == "__main__":
    main()

The error messages (there are many of them but the only thing that changes is publish:/chatter[a counter]):

[ERROR] [1570653818.070547]: [Client 26] [id: publish:/chatter:35] publish: Message type std_msgs/String does not have a field btype
[ERROR] [1570653818.070547]: [Client 26] [id: publish:/chatter:36] publish: Message type std_msgs/String does not have a field btype

Is this an exercise in futility and I should just try concatenating all of the gamepad event information into a single string, or is there something I'm missing and it really is possible to use a dictionary with more than one pair in a ros message?

Few things. One, have you seen Joy ? It's a standard node for reading events from a gamepad and publishing as a ROS sensor_msgs/Joy message.

Second, you shouldn't use roslibpy, you should use rospy . See those examples.

Lastly, you'll have far more success (after using rospy) if you try to use standard message types defined by ROS. If you still have problems after trying to use rospy (and joy_node?), make a new question.

I may have found the information I was looking for. The Docs for ROS message types show that while each message type can be treated as a dictionary, it has a hard definition of what keyvalue pairs and data types can be put into each message type. I wasn't aware of this, and was working under the assumption that I could have multiple entries with any name in any message type as long as they were the correct data type. So in the context of my question specifically, it seems that it would be best to concatenate the three pieces of information given by the gamepad into a single string (because std_msgs/String only holds a single string named data) and have the listener break the string apart again after receiving the message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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