2.jetbot_ros简单键盘控制

关于jetbot_ros代码,代码仓库位于:
https://github.com/dusty-nv/jetbot_ros
我们根据github上的描述,很容易测试oled和电机的功能是否正常。

然后你就会发现,官方提供的例程真的是要多简单有多简单。这次我也简单加了一个脚本,用于处理键盘控制jetbot小车。
我在scripts文件夹下新添加,jetbot_teleop.py文件,内容如下:

#!/usr/bin/env python

import  os
import  sys
import  tty, termios

import rospy

from geometry_msgs.msg import Twist
from std_msgs.msg import Float32

cmd = Twist()
pub = rospy.Publisher('jetbot_ros/cmd_raw', Twist, queue_size=1)


def keyboardLoop():
    rospy.init_node('teleop_read')
    rate = rospy.Rate(rospy.get_param('~hz', 1))
    
    walk_vel_ = rospy.get_param('walk_vel', 0.5)
    max_tv = walk_vel_

    print "Reading from keyboard"
    print "Use wasd keys to control the robot"
    print "Press Caps to move faster"
    print "Press q to quit"

    while not rospy.is_shutdown():
        fd = sys.stdin.fileno()
        old_settings = termios.tcgetattr(fd)
        old_settings[3] = old_settings[3] & ~termios.ICANON & ~termios.ECHO
        try :
            tty.setraw( fd )
            ch = sys.stdin.read( 1 )
        finally :
            termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)

        if ch == 'w':
            max_tv = walk_vel_
            speed_left = 1
            speed_right = 1
            turn = 0
        elif ch == 's':
            max_tv = walk_vel_
            speed_left = -1
            speed_right = -1
            turn = 0
        elif ch == 'a':
            max_tv = walk_vel_
            speed_left = 0
            speed_right = 1
            turn = 1
        elif ch == 'd':
            max_tv = walk_vel_
            speed_left = 1
            speed_right = 0
            turn = -1
        elif ch == 'e':
            speed_left = 0
            speed_right = 0
            turn = -1
        elif ch == 'q':
            exit()
        else:
            max_tv = walk_vel_
            max_rv = yaw_rate_
            speed = 0
            turn = 0

        cmd.linear.x = speed_left * max_tv
        cmd.linear.y = speed_right * max_tv
        # cmd.angular.z = turn * max_rv
        pub.publish(cmd)
        rate.sleep()

def stop_robot():
    cmd.linear.x = 0.0
    cmd.angular.z = 0.0
    speed_left = 0
    speed_right = 0
    turn = 0
    pub.publish(cmd)

# def start():
#     global pub
#     rospy.init_node('teleop_read')
#     # Set rospy to exectute a shutdown function when exiting       
#     rate = rospy.Rate(rospy.get_param('~hz', 1))
#     rospy.Subscriber("/cmd_vel", Twist, callback)
#     rospy.spin()

if __name__ == '__main__':
    try:
        keyboardLoop()
    except rospy.ROSInterruptException:
        pass

修改相应的jetbot_motors.py,如下两处:

# raw L/R motor commands (speed, speed)
def on_cmd_raw(msg):
	rospy.loginfo(rospy.get_caller_id() + ' left_vel=%s', msg.linear.x)
	rospy.loginfo(rospy.get_caller_id() + ' right_vel=%s', msg.linear.y)
	set_speed(motor_left_ID, msg.linear.x)
	set_speed(motor_right_ID, msg.linear.y)
	rospy.Subscriber('~cmd_dir', String, on_cmd_dir)
	rospy.Subscriber('jetbot_ros/cmd_raw', Twist, on_cmd_raw)
	rospy.Subscriber('~cmd_str', String, on_cmd_str)

虽然代码简陋了一点,但是好歹可以控制小车正常行动了。
运行说明:
w:forward
d:backward
a:left
d:right
e:stop

git代码提交在:
https://github.com/Top222Sin/jetbot_ros_control
csdn资源也已经提交,地址后续补充。

欢迎各位大神进行功能补充。

为本文作者打个赏吧,支持一下总结出更好的文章。

你可能感兴趣的:(Jetson,Nano)