ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A answers.ros.org
Ask Your Question
0

Printing and searching python namespaces from rospy.get_param()

asked 2016-03-10 12:15:35 -0600

Derkabub gravatar image

updated 2016-03-10 14:32:57 -0600

gvdhoorn gravatar image

I am catkin-izing, updating, and making revisions to the nxt ROS packages. I have hit an issue that seems basic but I could use some help to get through it. Searches for accessing Python dictionaries haven't helped much.

The code:

host = rospy.get_param("~host", None)
sock = nxt.locator.find_one_brick(host)
b = sock.connect()

config = rospy.get_param("~"+ns)
components = []
for c in config:
    rospy.loginfo("Creating %s with name %s on %s",c['type'],c['name'],c['port'])
    if c['type'] == 'motor':
        components.append(Motor(c, b))
    elif c['type'] == 'touch':
        components.append(TouchSensor(c, b))
    elif c['type'] == 'ultrasonic':
        components.append(UltraSonicSensor(c, b))
    elif c['type'] == 'color':
        components.append(ColorSensor(c, b))
    elif c['type'] == 'intensity':
        components.append(IntensitySensor(c, b))
    elif c['type'] == 'gyro':
        components.append(GyroSensor(c, b))
    elif c['type'] == 'accelerometer':
        components.append(AccelerometerSensor(c, b))
    else:
        rospy.logerr('Invalid sensor/actuator type %s'%c['type'])

My issue is with the c['text'] in the for loop. When I print config[c] it prints the value of the key. Printing 'c' alone prints the key. However, when c['text'] is called ROS fires back an error:

TypeError: string indices must be intergers, not str

The namespace (returned into the config variable) can have several sensors set up by a .yaml file.

Thank you for your time.

edit retag flag offensive close merge delete

Comments

What does print(config) give you?

eric-wieser gravatar image eric-wieser  ( 2016-03-10 23:35:35 -0600 )edit

Where is the official source for the nxt packages? The source link on the wiki page is dead

eric-wieser gravatar image eric-wieser  ( 2016-03-10 23:50:39 -0600 )edit

I am not sure how to report the source link being broken issue. There is more on this at sig/NXT. Between the ros wiki and the sig/NXT I figured out indiviuals working with this and selected a repository to grab the variant of ros nxt.

Derkabub gravatar image Derkabub  ( 2016-03-11 12:20:50 -0600 )edit

As for your previous comment: If I use rospy.loginfo() to print 'config' to screen it prints the entire dictionary, which in my case only pertains to one sensor or a single dictonary. There are several sensors.

Derkabub gravatar image Derkabub  ( 2016-03-11 12:25:38 -0600 )edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2016-03-10 23:36:45 -0600

for c in config iterates over the keys of the config, not the values. I think you want:

host = rospy.get_param("~host", None)
sock = nxt.locator.find_one_brick(host)
b = sock.connect()

config = rospy.get_param("~"+ns)
components = []
for k, c in config.items():
    # do something with k - it might be useful
    rospy.loginfo("Creating %s with name %s on %s",c['type'],c['name'],c['port'])
    if c['type'] == 'motor':
        components.append(Motor(c, b))
    elif c['type'] == 'touch':
        components.append(TouchSensor(c, b))
    elif c['type'] == 'ultrasonic':
        components.append(UltraSonicSensor(c, b))
    elif c['type'] == 'color':
        components.append(ColorSensor(c, b))
    elif c['type'] == 'intensity':
        components.append(IntensitySensor(c, b))
    elif c['type'] == 'gyro':
        components.append(GyroSensor(c, b))
    elif c['type'] == 'accelerometer':
        components.append(AccelerometerSensor(c, b))
    else:
        rospy.logerr('Invalid sensor/actuator type %s'%c['type'])
edit flag offensive delete link more
0

answered 2016-03-15 12:31:50 -0600

Derkabub gravatar image

updated 2016-03-15 12:32:40 -0600

It seems I was simply making an error setting up my .yaml. In my .yaml that sets up the parameters I had "-type" but I was supposed to have "- type". Fixing this repaired the immediate issues. Thanks for your help!

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2016-03-10 12:15:35 -0600

Seen: 303 times

Last updated: Mar 15 '16