Before all the use of keyboard control Turtlebot walk, so want to write a paragraph code can let Turtlebot himself, after all, the future work is to let the robot on the map can achieve self-path planning to reach the designated destination.
After starting Turtlebot, in the command line Rostopic list We can see all the current topic, as shown below
We cmd_vel this topic when we use the keyboard to control Turtlebot, but we didn't find it in the image above. After a review and comparison, in the official code Kobuki_keyop/launch/keyop.launch file can be seen <remap from= "Keyop/cmd_vel" to= "mobile_base/commands/ Velocity ", the keyop/cmd_vel mapped to the mobile_base/commands/velocity, and this topic is there. So in order to implement our idea of controlling Turtlebot with code, we need to send a message of type geometry_msgs/twist to the topic, which contains the line speed and angular velocity of the motion.
It's a lot easier to implement when the analysis is correct.
Create a package first
Catkin_create_pkg turtle_move roscpp geometry_msgs TF
One of the official explanations for ROSCPP is:
Roscpp is a C + + implementation of ROS. IT provides a client library that enables C + + programmers to quickly interface with ROS Topics, Services, and Parameters. Roscpp are the most widely used Ros client library and are designed to being the High-performance library for ROS.
Here is the Code section, creating a move_turtle_goforward.cpp program in TURTLE_MOVE/SRC with code that lets the robot go straight
#include <ros / ros.h>
#include <signal.h>
#include <geometry_msgs / Twist.h>
ros :: Publisher cmdVelPub;
void shutdown (int sig)
{
cmdVelPub.publish (geometry_msgs :: Twist ()); // Stop the robot
ROS_INFO ("move_turtle_goforward ended!");
ros :: shutdown ();
}
int main (int argc, char ** argv)
{
ros :: init (argc, argv, "move_turtle_goforward"); // Initiate ROS, which allows ROS to perform name remapping through the command line
ros :: NodeHandle node; // Create a handle for the node of this process
cmdVelPub = node.advertise <geometry_msgs :: Twist> (/ mobile_base / commands / velocity, 1); // Post a message of geometry_msgs / Twist on / mobile_base / commands / velocity topic
ros :: Rate loopRate (10); // ros :: Rate object allows you to specify the frequency of self-looping
signal (SIGINT, shutdown);
ROS_INFO ("move_turtle_goforward cpp start ...");
geometry_msgs :: Twist speed; // Control signal carrier Twist message
while (ros :: ok ())
{
speed.linear.x = 0.1; // Set the line speed to 0.1m / s, positive for forward, negative for backward
speed.angular.z = 0; // Set angular velocity to 0rad / s, positive for left turn, negative for right turn
cmdVelPub.publish (speed); // Send the command just set to the robot
loopRate.sleep (); // sleep until a frequency cycle time
}
return 0;
}
Rotation is also the same, just need to set the line speed of 0, the angular velocity is set to 0.5.
Back to the top, add two sentences at the end of the CMakeList.txt file:
Add_executable (Move_turtle_goforward src/move_turtle_goforward.cpp)
Target_link_libraries (Move_turtle_goforward ${catkin_libraries})
Compile the Typing
Catkin_make
Start the robot, I'm using Kobuki here.
Roslaunch Kobuki_node Minimal.launch
and run the Move_turtle_goforward program we wrote.
Rosrun Turtle_move Move_turtle_goforward
Get. Can see Turtlebot straight, keyboard press CTRL + C END program let Turtlebot stop.
Finally thanked Teng elder brother's guidance.