keyboard key teleop not registering movement: Getting started

asked 2017-02-08 15:10:10 -0500

cerebraldad gravatar image

I'm trying to get a keyboard controller working (teleop I think its called):

code:

#!/usr/bin/env python

import rospy
from std_msgs.msg import String
from geometry_msgs.msg import Twist

key_mapping = { 'w': [0, 1], 'x': [0, -1],
            'a': [-1, 0], 'd': [1, 0],
            's': [0, 0] }
#g_last_twist = None
g_vels_scales = [0.1, 0.1]

def keys_cb(msg, twist_pub):
    global g_last_twist
    if len(msg.data) == 0 or not key_mapping.has_key(msg.data[0]):
        return #unknown key
    vels = key_mapping[msg.data[0]]
    g_last_twist.angular.z = vels[0] * g_vels_scales[0]
    g_last_twist.linear.x = vels[1] * g_vels_scales[1]
    twist_pub.publish(g_last_twist)

if __name__ == '__main__':
    rospy.init_node('keys_to_twist')
    twist_pub = rospy.Publisher('cmd_vel', Twist, queue_size=1)
    rospy.Subscriber('keys', String, keys_cb, twist_pub)
    g_last_twist= Twist()
    if rospy.has_param('~linear_scale'):
        g_vels_scales[1] = rospy.get_param('~linear_scale')
     else:
         rospy.logwarn("linear scale not provided; usint %.1f" %g_vels_scales[1])
if rospy.has_param('~angualr_scale'):
    g_vels_scales[0] = rospy.get_param('~angular_scale')
else: 
    rospy.logwarn("angular scale not provided; using %.1f" %g_vels_scales[0])
rate = rospy.Rate(10)
while not rospy.is_shutdown():
    twist_pub.publish(g_last_twist)
    rate.sleep()

I can't seem to get the keys to work and publish the right twist. all of my dependencies are in the package.xml and the make file. Everything compiles and runs, but there is no "key event"

I want to expand on this exaple i got from a text book, but cant get it to do what it advertises.

any help would be appreciated.

edit retag flag offensive close merge delete

Comments

Are you just running this node? You are subscribing to the keys topic which actually requires another node publishing this. I guess this should then be the node actually capturing the keys...

You need to have the other node running your capture your key presses yourself....

mgruhler gravatar image mgruhler  ( 2017-02-09 01:45:45 -0500 )edit