If you have tried the previous example, there is no feeling that every time the robot to move and input instructions in the terminal, this is too cumbersome, there is no way through the keyboard to control the movement of the robot? The answer room of course. I studied several other robot keyboard control code, or some harvest, finally transplanted to the SmartCar, the experiment was successful.
First, create a control pack
First, we create a separate package for keyboard control:
Roscreate-pkg smartcar_teleop rospy geometry_msgs std_msgs roscpp
If you have forgotten how to build a package, please refer to: http://www.ros.org/wiki/ROS/Tutorials/CreatingPackage
Second, simple message release
In the robot simulation, the main control robot Mobile is in the robot simulation, the main control robot Mobile is Twist () structure, if we program this structure through the program released into topic, nature can control the robot. Let's try the simple Python first.
In the previous simulations, we used a message release at the command line, and now we need to convert these commands into Python code and encapsulate them in a separate node. For the previous command line, we can easily write the following control code under the Smartcar_teleop/scripts folder:
#!/usr/bin/env python
import roslib roslib.load_manifest (' Smartcar_teleop ')
import rospy from
geometry _msgs.msg import Twist from
std_msgs.msg import String
class Teleop:
def __init__ (self):
pub = Rospy. Publisher (' Cmd_vel ', Twist)
rospy.init_node (' smartcar_teleop ')
rate = Rospy. Rate (Rospy.get_param (' ~hz ', 1))
self.cmd = None
cmd = Twist ()
cmd.linear.x = 0.2 cmd.linear.y
= 0< C14/>cmd.linear.z = 0
cmd.angular.z = 0
cmd.angular.z = 0
cmd.angular.z = 0.5 self.cmd
= cmd
While isn't Rospy.is_shutdown ():
str = "Hello World%s"% rospy.get_time ()
rospy.loginfo (str)
Pub.publish (Self.cmd)
Rate.sleep ()
if __name__ = = ' __main__ ': Teleop ()