The page introduces the usage of teleop_twist_keyboard, including how to control the motion of a robot using the keyboard.
...
Environment Setup
software platform
- Ubuntu 20.04 X86
- ROS Noetic
hardware platform
Choose any one below:
- Slamware Kit (User Robot System based on Slamware Solution)
- robot base systems like Apollo/Ares/Athena
...
Sample Code Download
ROS-例程下载
...
Compile and Run
- Download the Slamware ROS SDK along with ROS examples.
- Place the 'src' folder from the ROS SDK into an empty working directory, such as 'catkin_ws'. Also, put the 'slamware_ros_sample' folder from the ROS examples into the 'src' folder.
Code Block |
---|
|
mkdir -p catkin_ws/src
cd catkin_ws/src |
- Initialize the workspace using the Catkin tool.
Code Block |
---|
|
catkin_init_workspace |
- Compile.
Code Block |
---|
|
cd ..
catkin_make |
- Set up workspace environment.
Code Block |
---|
|
source devel/setup.bash |
- Launch the slamware_ros_sdk_server_node node. Select the corresponding IP according to the mode.
Code Block |
---|
|
roslaunch slamware_ros_sdk slamware_ros_sdk_server_node.launch ip_address:=10.6.128.141 // Station mode
// If in AP mode, the IP address is 192.168.11.1 |
- Open another terminal, repeat step 5, and start the teleop_twist_keyboard_node node. Follow the prompts and use the W/S/A/D keys to control the robot to move forward/backward/left/right accordingly.
Code Block |
---|
|
roslaunch slamware_ros_sample teleop_twist_keyboard.launch |
...
Code Description
Code Block |
---|
language | py |
---|
title | Define keyboard buttons |
---|
|
moveBindings = {
'w':(1.0, 0.0),
's':(-1.0, 0.0),
'a':(0.0, 1.0),
'd':(0.0, -1.0)
} |
Code Block |
---|
language | py |
---|
title | Define a publisher, specify the topic name and type |
---|
|
pub = rospy.Publisher('/cmd_vel', Twist, queue_size = 1) |
Code Block |
---|
language | py |
---|
title | Initialize node |
---|
|
rospy.init_node('teleop_twist_keyboard_node') |
Code Block |
---|
language | py |
---|
title | When pressing keys, assign the velocity to linearX and angularZ |
---|
|
key = getKey()
print(key)
if key in moveBindings.keys():
linearX = moveBindings[key][0]
angularZ = moveBindings[key][1]
else:
linearX = 0.0
angularZ = 0.0
if (key == '\x03'):
break |
Code Block |
---|
language | py |
---|
title | Publish messages according to the required type of topic |
---|
|
twist = Twist()
twist.linear.x = linearX
twist.angular.z = angularZ
pub.publish(twist) |