Control robot movement
This section will introduce how to control the robot to send messages to the robot by writing python code to make the robot move. We will demonstrate through the following three points:
- Control the linear motion of the robot
- Control the robot steering movement
- Write a keyboard remote control program
This section will learn how to use the functions of ROS Service, and you can also refer to the ROS Wiki tutorial on Service Writing a Simple Service and Client
Control the linear motion of the robot
What we want to achieve is: control the robot to move up and down along the x-axis through a python program
code description
There are two codes in the function package we provide, one is linear_move.py
, and the other is linear_move_by_srv.py
.
linear_move.py
is to pass parameters by changing the parameter configuration of the code.linear_move_by_srv.py
is to pass parameters by calling the Service interface.
Here is a brief description of a function
(trans, rot) = self.tf_listener.lookupTransform(self.frame_odom, self.frame_base, rospy.Time(0))
This is a listener for tf transformation. Using this function, we can get the pose transformed from the first parameter to the second parameter. The three parameters are the time we want to make the transformation rospy.Time(0)
is to get the latest transformation.
Here we get the pose of the transition between odom
and base_link
, which is also the pose of the robot moving from the origin, so as to obtain the current state.
For the rest, please refer to the comments in the code.
Our code is stored in the directory /home/handsfree/handsfree/handsfree_ros_ws/src/handsfree/handsfree_tutorials/script/2_base_control
.
run linear_move.py
Open the terminal and start the robot driver node
roslaunch handsfree_hw handsfree_hw.launch
Normally, it will display:
Open the second terminal and run the python file (put your robot in an open place, the robot will move forward 0.5 meters)
rosrun handsfree_tutorials linear_move.py
At the same time, the terminal will display:
You can also modify the code in
linear_move.py
to change the distance the robot moves:You can also run the launch file in the terminal (the robot will move forward 0.5 meters)
roslaunch handsfree_tutorials linear_move_by_para.launch
At the same time, the terminal will display:
You can also modify the parameters in
linear_move.launch
to change the distance and speed of the robot, in the/launch
directory.
Run linear_move_by_srv.py
Open the terminal and start the robot driver node (if it was opened before, please ignore this step)
roslaunch handsfree_hw handsfree_hw.launch
Open a second terminal, run the python file (place your robot in an open space)
rosrun handsfree_tutorials linear_move_by_srv.py
Normally, it will display:
Open the third terminal, and call the service interface through the terminal to make the robot move. 0.5 is the specific distance, and you can also modify it to other values.
rosservice call /move_distance 0.5
Normally, it will display:
Tips: If the number of calls is too large and you want the robot to stop, select the second terminal with the mouse and use Ctrl+C to stop the robot.
You can also try by running the launch file, Ctrl+C to stop the process of the second terminal, and run the launch file in this terminal.
roslaunch handsfree_tutorials linear_move_by_srv.launch
Normally, it will display:
Select the third terminal before and call the service interface again
rosservice call /move_distance 0.5
Normally, it will display:
Control the robot's steering movement
What we want to achieve is: control the robot to rotate left and right along the z axis through a python program
code description
This is similar to the code for linear motion, except that angular.z
is sent when sending the speed message, which is the yaw angle message.
There are three codes here, namely radian_turn.py
, radian_turn_by_srv.py
, radian_turn_direct_by_srv
radian_turn.py
passes parameters by changing the parameter configuration of the code.radian_turn_by_srv.py
andradian_turn_direct_by_srv.py
pass parameters by calling the Service interface.radian_turn.py
andradian_turn_by_srv.py
will only rotate between -180~180 degrees, (for example: if you run these two codes and pass parameter 6.28, the robot will not move, but runradian_turn_direct_by_srv .py
, the robot will go around)
Run radian_turn.py
Open the terminal and start the robot driver node (if it was opened before, please ignore this step)
roslaunch handsfree_hw handsfree_hw.launch
Open a second terminal and run the python file (put your robot in an open place and the robot will rotate pi/2 radians)
rosrun handsfree_tutorials radian_turn.py
At the same time, the terminal will display:
You can also modify the code in
radian_turn.py
to change the distance the robot moves forward (math.pi is pi in mathematics):You can also run the launch file in the terminal (the robot rotates 180 degrees)
roslaunch hands free_tutorials radian_turn.launch
At the same time, the terminal will display:
You can also modify the code in
radian_turn.launch
to change the speed of the robot rotation and the rotation target angle, in the/launch
directory.
Run radian_turn_by_srv.py
Open the terminal and start the robot driver node (if it was opened before, please ignore this step)
roslaunch handsfree_hw handsfree_hw.launch
Open a second terminal and run the python file (put your robot in an open place)
rosrun handsfree_tutorials radian_turn_by_srv.py
Normally, it will display:
Open the third terminal and call the service interface through the terminal to make the robot move. 3.14 will make the robot turn 180 degrees in place
rosservice call /turn_radian 3.14
Normally, it will display:
You can also try by running the launch file, Ctrl+C to stop the process of the second terminal, and run the launch file in this terminal
roslaunch hands free_tutorials radian_turn_by_srv.launch
Normally, it will display:
Select the third terminal before and call service again
rosservice call /turn_radian 3.14
Normally, it will display:
Run radian_turn_direct_by_srv.py
Running this code is similar to running radian_turn_by_srv.py
Open the terminal and start the robot driver node (if it was opened before, please ignore this step)
roslaunch handsfree_hw handsfree_hw.launch
Open a second terminal, run the python file (place your robot in an open space)
rosrun handsfree_tutorials radian_turn_direct_by_srv.py
Open the third terminal and call the service interface through the terminal to make the robot rotate
rosservice call /turn_radian 6.28
Write a keyboard remote control program
Open the terminal and start the robot driver node (if it was opened before, please ignore this step)
roslaunch handsfree_hw handsfree_hw.launch
Open the second terminal and run the python keyboard remote control program. The uppercase key is only valid for Gemini-O universal wheels and other products.
rosrun handsfree_tutorials teleop_twist_keyboard.py
For related programming skills, please refer to the comments of the source code.