Simple 2D robot simulator

summary

STDR is a simple 2D robot simulator. Its installation and startup commands are as follows.

install
sudo apt-get install ros-$ROS_DISTRO-stdr-simulator
 start-up
roslaunch stdr_launchers server_with_map_and_gui_plus_robot.launch

After startup, you can see a two-dimensional plan. Here, it looks like this.


What does this simulator mean? I won't introduce the map. I can understand it if I'm not stupid. Let's talk about the robot part. There is a small circle in the lower left corner of the maze, which represents an abstract robot; The red line represents the simulated laser ray of the hypothetical lidar sensor, with a range; The green line represents the simulated sonar signal, which also has a range.
The theme for controlling robots is / robot0/cmd_vel, we can control the movement of the robot by publishing messages on this topic.

topic information
rostopic info /robot0/cmd_vel
 Message information
rosmsg show geometry_msgs/Twist

Command line control robot movement

First control

rostopic pub -r 2 /robot0/cmd_vel geometry_msgs/Twist '{linear: {x: 0.5, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 0.0}}'


At this time, the control robot moves forward at the speed of 0.5m/s and stops when encountering obstacles, but the speed is still 0.5m/s and needs to be reset to zero, that is, the following command.

rostopic pub -r 2 /robot0/cmd_vel geometry_msgs/Twist '{linear: {x: 0.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 0.0}}'

Second control

rostopic pub -r 2 /robot0/cmd_vel geometry_msgs/Twist '{linear: {x: 0.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 0.1}}'

This command means to make the robot rotate counterclockwise, which can be seen by the human eye. When you see that it is almost 90 degrees, quickly execute the zeroing command line, as follows.

rostopic pub -r 2 /robot0/cmd_vel geometry_msgs/Twist '{linear: {x: 0.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 0.0}}'

So the robot will stop rotating, like this.


Third control

rostopic pub -r 2 /robot0/cmd_vel geometry_msgs/Twist '{linear: {x: 0.5, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 0.0}}'

Still let it move forward at the speed of 0.5m/s, and it will stop when it encounters an obstacle.


Well, let's show so much. Command control is too troublesome. In fact, it is more convenient to use ROS node control.

Programmable control of robot movement

New package stdr_control to create a new cpp file stdr_open_loop_commander.cpp, don't ask me why the name is so disgusting. It's written in the book. I'm too lazy to change it myself.

#include <ros/ros.h>
#include <geometry_msgs/Twist.h>Simulator via cmd_vel
int main(int argc, char **argv) {
    ros::init(argc, argv, "stdr_commander"); 
    ros::NodeHandle n; 
    ros::Publisher twist_commander = n.advertise<geometry_msgs::Twist>("/robot0/cmd_vel", 1);
    double sample_dt = 0.01; //Sampling time and how often control information is released
    double speed = 1.0; // Forward speed
    double yaw_rate = 0.5; //angular velocity
    double time_3_sec = 3.0; // Moving time
    //initialization
    geometry_msgs::Twist twist_cmd;
    twist_cmd.linear.x=0.0;
    twist_cmd.linear.y=0.0;    
    twist_cmd.linear.z=0.0;
    twist_cmd.angular.x=0.0;
    twist_cmd.angular.y=0.0;
    twist_cmd.angular.z=0.0;   
    ros::Rate loop_timer(1/sample_dt);
    double timer=0.0;//Control movement time
    //Return to zero
    for (int i=0;i<10;i++) {
      twist_commander.publish(twist_cmd);
      loop_timer.sleep();
    }
    //forward
    twist_cmd.linear.x=speed; 
    while(timer<time_3_sec) {
          twist_commander.publish(twist_cmd);
          timer+=sample_dt;
          loop_timer.sleep();
          }
    //Zero and rotate
    twist_cmd.linear.x=0.0;
    twist_cmd.angular.z=yaw_rate;
    timer=0.0;//Retiming
    while(timer<time_3_sec) {
          twist_commander.publish(twist_cmd);
          timer+=sample_dt;
          loop_timer.sleep();
          }
    //Zero and move forward
    twist_cmd.angular.z=0.0;
    twist_cmd.linear.x=speed; 
    timer=0.0; //Retiming
    while(timer<time_3_sec) {
          twist_commander.publish(twist_cmd);
          timer+=sample_dt;
          loop_timer.sleep();
          }
    //Return to zero
    twist_cmd.angular.z=0.0; 
    twist_cmd.linear.x=0.0; 
    for (int i=0;i<10;i++) {
      twist_commander.publish(twist_cmd);
      loop_timer.sleep();
    }               
}

Write cmakelists Txt file.



The video is not easy to show. Let's take a screenshot.

Keywords: ROS

Added by BloodyMind on Mon, 17 Jan 2022 20:57:10 +0200