ROS independent mapping navigation car project practice

Basic tutorial link:

Introduction · Autolabor-ROS Robot Introductory Course “ROS Theory and Practice” Zero Basic Tutorialicon-default.png?t=N7T8http://www.autolabor.com.cn/book/ ROSTutorials/

1: Robot platform design

The main components of the robot include:

Actuator: The main body is assembled from acrylic plates. Two DC motors drive the driving wheels and two universal wheels that maintain balance to achieve robot walking. Since the actuator is relatively simple, it will not be introduced separately.

Drive system: battery, arduino and motor drive module;

Control system: Raspberry Pi;

Sensing system: encoder, single-line lidar;

1. Arduino basics of robot platform design

The arduino mega 2560 is used, and the software part is Arduino IDE.

1.1 Send data from arduino to computer through serial port
/*
 * Requirement: Send data from arduino to computer through serial port
 * accomplish:
 * 1.Set the baud rate in setup
 * 2. Use Serial.print or Serial.println() in setup or loop to send data
 *
 *
 *
 */
void setup() {
  Serial.begin(57600);
  Serial.println("setup");
}

void loop() {
  delay(3000);
  Serial.print("loop");
  Serial.print(" ");
  Serial.println("hello");
}
1.2 Send data from computer to Arduino through serial port
/*
 * Requirement: Send data from computer to arduino through serial port
 * accomplish:
 * 1.Set the baud rate in setup
 * Receive the sent data in 2.loop and print it
 *
 *
 *
 */
char num;
void setup() {
  Serial.begin(57600);
}

void loop() {
  if(Serial.available() > 0){
    num = Serial.read();
    Serial.print("I accept:");
    Serial.println(num);
  }
}

2. Motor drive for robot platform design

The motor used in the robot platform is a DC reduction motor, and the motor drive board is a circuit board based on L298P.

2.1 DC reduction motor

It mainly consists of three parts: reduction gearbox, motor body and encoder.

Encoder parameters: M1: motor power supply + (can be reversed by swapping with M2), GND: encoder power supply -, C2: signal line, C1: signal line, VCC: encoder power supply +, M2: motor power supply – (and M1 swap can be reversed).

2.2 Motor driver board

A motor driver board optimized based on L298P is used.

2.3 Basic Motor Control Implementation
  1. M1 and M2 of the left motor correspond to pin 4 (DIRA) and pin 5 (PWMA). Pin 4 controls steering and pin 5 outputs PWM. M1 and M2 of the right motor correspond to pin 6 (PWMB) and pin 7 (DIRB). Pin 7 controls steering and pin 6 outputs PWM.

  2. The motor speed can be controlled through PWM.

 digitalWrite(DIRA,HIGH);
  analogWrite(PWMA,100);
  /*
   * Notice: 
   * 1. The motor steering can be controlled by setting DIRA to HIGH or LOW, but which flag bit is used for forward or reverse rotation needs to be judged according to the demand. The steering is relative.
   * 2. The value of PWM is [0,255], this value can be set by yourself.
   *
   */
2.4 PID control theory

P: If the vehicle speed control in the above scenario is implemented, a simple implementation method is: determine the target speed, obtain the current speed, and use (target speed-current speed)*a certain coefficient to calculate the result as output PWM, then obtain the current speed, use (target speed-current speed)*certain coefficient to calculate the result as the output PWM and output… In this loop, in the above model, the speed regulation implementation is a Closed loop, each cycle will calculate the PWM value to be output based on the difference between the current speed and the target speed, multiplied by a fixed coefficient, and the coefficient is called the ratio.

I: In the above model algorithm, there is a steady-state error between the final speed and the expected speed, which means that the final result may never reach expectations. The solution is to use integral I. Each time the speed is adjusted, the output PWM also accumulates the results calculated based on the integral I to eliminate the static error.

D: When the I value is set too large, “overspeeding” may occur. After overspeeding, multiple adjustments may be required, causing system oscillation. To solve this situation, D differential can be used. When the speed is closer to the target speed, D will exert more force in the opposite direction, weakening P’s control and acting like a “damping”. System oscillation can be reduced through the use of D.

3. Chassis Realization of Robot Platform Design

Ros also provides an encapsulated module: ros_arduino_bridge. This module is composed of lower computer driver and upper computer control. Through this module, you can realize your own robot platform more quickly and conveniently.

3.1 Introduction to ros_arduino_bridge

This function package contains the Arduino library and the ROS driver package used to control Arduino. It is designed to be a complete solution for running Arduino-controlled robots under ROS.

  • If you only install and debug the slave computer, you do not need to install the ROS system, as long as you have the Arduino development environment;

  • The host computer debugging is suitable for ROS Indigo and higher versions, but the latest version noetic is not supported yet, so the host computer needs to use other versions of ROS (melodic is recommended);

In the ros_arduino_bridge architecture,

Although the structure is complex, it only focuses on two parts:

  • ros_arduino_bridge/ros_arduino_firmware/src/libraries/ROSArduinoBridge
  • ros_arduino_bridge/ros_arduino_python/config/arduino_params.yaml

The former is a firmware package implementation on the Arduino side, which needs to be modified and uploaded to the Arduino circuit board;

The latter is a configuration file on the ROS side. The relevant driver has been encapsulated. We only need to modify the configuration information.

Overall, using ros_arduino_bridge can greatly improve our development efficiency.

4. Control system of robot platform design

ROS is a distributed design framework. For control systems of small or micro robot platforms, a multi-processor implementation strategy can be selected. The specific implementation is “PC + Embedded”. The embedded system (Raspberry Pi) is used as the control system of the robot body, while the PC realizes remote monitoring. The former realizes data collection and direct chassis control, while the latter realizes graphics remotely. display and function calculations.

4.1 Control system implementation_distributed framework

Connect the Raspberry Pi to the wireless network and configure a static IP for the Raspberry Pi (originally a dynamic IP, obtained automatically).

(1) ROS distributed communication has certain requirements for network configuration. First, ensure that different computers are in the same network. It is best to set fixed IPs respectively. If it is a virtual machine, you need to change the network adapter to bridge mode;

(2) Modify the configuration file, modify the /etc/hosts files of different computers, and add the other party’s IP address and computer name to the file.

#Host side:

IP of the slave machine Computer name of the slave machine

#Slave side:
Host IP Host computer name

After the settings are completed, you can use the ping command to test whether the network communication is normal.

IP address view name: ifconfig

Computer name view: hostname

(3) Configure the IP of the host and slave machines, and add the following to ~/.bashrc of the host and slave machines:

export ROS_MASTER_URI=http://Host IP:11311
export ROS_HOSTNAME=Host IP

4.2 Control system to achieve _ssh remote connection

See the tutorial for specific implementation.

4.3 Control system implementation_install ros_arduino_bridge

The ROS function package of ros_arduino_bridge mainly uses ros_arduino_python, and the program entry is the arduino.launch file in the launch directory of the package.

You need to load a configuration file in yaml format. This file has provided a template in the config directory. The configuration file mainly contains some parameter values.

5. Sensors for robot platform design

5.1 Sensor_radar usage

The Silan A1 single-line lidar is used.

USB view command:

ll /dev/ttyUSB*

Authorization (add the current user to the dialout group, similar to arduino):

sudo usermod -a -G dialout your_user_name

It will take effect only after restarting.

You need to download the relevant radar driver package and compile it, and start the launch file to start the radar, and the radar will start publishing topics.

5.2 Sensor_Camera Usage

USB view command:

ll /dev/video

Software preparation:

Install the USB camera software package with the following command:

sudo apt-get install ros-ROS version-usb-cam

Or you can download the source code directly from github:

git clone https://github.com/ros-drivers/usb_cam.git

The software package contains the launch file used for startup, including: the camera’s startup node usb_cam, and the node image_view that displays image data in a graphical window.

5.3 Sensor integration

The so-called integration mainly involves optimizing the startup of chassis, radar, and camera-related nodes and realizing the association between the robot chassis and the odometer, radar, and camera through coordinate transformation. The implementation steps are as follows:

  1. Write launch files for integration; (can run multiple nodes at the same time)
  2. Publish TF coordinate transformation; (establish a link between different coordinate systems)

Two: Implementation of physical robot navigation

1. Preparation

1.1 Function Pack Installation:

Install the function package required for navigation on the robot:

  • Install the gmapping package (for building maps): sudo apt install ros--gmapping

  • Install the map service package (for saving and reading maps): sudo apt install ros--map-server

  • Install navigation package (used for positioning and path planning): sudo apt install ros--navigation

Create a new function (customized package name, such as nav) and import dependencies: gmapping map_server amcl move_base

1.2 Create function packages related to robot models

Create a feature package:

catkin_create_pkg mycar_description urdf xacro.

2. Navigation to achieve SLAM mapping

2.1 Introduction to gmapping

gmapping is one of the more commonly used and mature SLAM algorithms in the ROS open source community. gmapping can draw two-dimensional raster maps based on mobile robot odometry data and lidar data.

The installation of gmapping was also introduced earlier. The command is as follows:

sudo apt install ros--gmapping

2.2 gmapping node description

The core node in the gmapping function package is: slam_gmapping. In order to facilitate the call, you need to first understand the topics subscribed by the node, published topics, services and related parameters.

(1) Subscribed Topic

tf (tf/tfMessage)

  • Used for coordinate transformation messages between radar, chassis and odometer.

scan(sensor_msgs/LaserScan)

  • Radar information required for SLAM.

(2)Published Topic

map_metadata(nav_msgs/MapMetaData)

  • Map metadata, including map width, height, resolution, etc., this message will be updated regularly.

map(nav_msgs/OccupancyGrid)

  • Map raster data is generally displayed graphically in rviz.

~entropy(std_msgs/Float64)

  • Robot attitude distribution entropy estimation (the larger the value, the greater the uncertainty).

(3)Service

dynamic_map(nav_msgs/GetMap)

  • Used to obtain map data.

2.3 Write launch files related to gmapping nodes

gmapping is a downloaded function package. We cannot modify its source code. We can only modify some of its internally set parameters and set the parameters by writing the launch file related to the gmapping node.

Some of the main parameters are: radar topic name, chassis coordinate system, odometer coordinate system, map update frequency, effective distance of laser detection, etc.

3. Navigation Implementation Map Service

3.1 Introduction to map_server

The map_server function package provides two nodes: map_saver and map_server. The former is used to save the raster map to disk, and the latter reads the raster map from the disk and provides it as a service.

The installation of map_server was also introduced earlier. The command is as follows:

sudo apt install ros--map-server

3.2 Use of map_saver and map_server nodes

(1)map_saver

Subscribed topics:

map(nav_msgs/OccupancyGrid)

  • Subscribe to this topic to generate map files.

If used, you can write a launch file.

As a result, two files will be generated under the specified path, xxx.pgm and xxx.yaml:

xxx.pgm is essentially a picture, which can be opened directly using a picture viewing program.

xxx.yaml saves the metadata information of the map and is used to describe the image.

(2) map_server

Posted topics:

map_metadata(nav_msgs/MapMetaData)

  • Publish map metadata.

map(nav_msgs/OccupancyGrid)

  • map data.

Serve

static_map(nav_msgs/GetMap)

  • Get maps through this service.

parameter

?frame_id (string, default: “map”)

  • Map coordinate system.

4. Navigation to achieve positioning

The amcl function package is provided in ROS’s navigation function package set navigation, which is used to implement robot positioning in navigation.

4.1 Introduction to amcl

AMCL (adaptive Monte Carlo Localization) is a probabilistic positioning system for 2D mobile robots. It implements the adaptive (or KLD sampling) Monte Carlo positioning method and can use particle filters to calculate the robot’s position based on existing maps.

amcl has been integrated into the navigation package. Navigation installation is also introduced earlier. The command is as follows:

sudo apt install ros--navigation

4.2 amcl node description

The core node in the amcl function package is: amcl.

Subscribed Topic

scan(sensor_msgs/LaserScan)

  • LiDAR data.

tf(tf/tfMessage)

  • Coordinate transformation message.

initialpose(geometry_msgs/PoseWithCovarianceStamped)

  • Used to initialize the mean and covariance of the particle filter.

map(nav_msgs/OccupancyGrid)

  • Get map data.

Published Topic

amcl_pose(geometry_msgs/PoseWithCovarianceStamped)

  • Robot pose estimation in the map.

particlecloud(geometry_msgs/PoseArray)

  • The pose estimation set can be subscribed to PoseArray in rviz and then graphically display the robot’s pose estimation set.

tf(tf/tfMessage)

  • Post the conversion from odom to map.

Service

global_localization(std_srvs/Empty)

  • Initialize the global positioning service.

request_nomotion_update(std_srvs/Empty)

  • A service that manually performs updates and publishes updated particles.

set_map(nav_msgs/SetMap)

  • Service for manually setting up new maps and attitude.

Service called

static_map(nav_msgs/GetMap)

  • Call this service to get map data.

4.3 Coordinate transformation

The odometer itself can also assist the robot in positioning. However, the odometer has a cumulative error and may cause positioning errors in some special circumstances (wheel slippage). amcl can estimate the posture of the robot in the map coordinate system and combine it with the odometer. Improve positioning accuracy.

5. Navigation to implement path planning

The move_base function package is provided in the ROS navigation function package set navigation to implement this function.

5.1 Introduction to move_base

The move_base function package provides path planning implementation based on action. move_base can control the robot chassis to move to the target position according to the given target point, and will continuously feedback the robot’s own posture and the status information of the target point during the movement. . As mentioned before, move_base mainly consists of global path planning and local path planning.

move_base has been integrated into the navigation package. Navigation installation is also introduced earlier. The command is as follows:

sudo apt install ros--navigation

5.2 move_base node

The core node in the move_base function package is: move_base.

See the tutorial for details.

5.3 Cost Map

The map in ROS is actually a picture. This picture has metadata such as width, height, resolution, etc. The gray value in the picture is used to represent the probability of the existence of obstacles. However, the map constructed by SLAM cannot be used directly in navigation. On top of it, some auxiliary information needs to be added to the map, such as obstacle data obtained from time to time, expansion zones added based on static maps, and other data.

There are two cost maps: global_costmap (global cost map) and local_costmap (local cost map). The former is used for global path planning, and the latter is used for local path planning.

Both cost maps can be superimposed in multiple layers, generally with the following levels:

  • Static Map Layer: Static map layer, a static map constructed by SLAM.

  • Obstacle Map Layer: Obstacle map layer, obstacle information sensed by sensors.

  • Inflation Layer: Inflation layer, inflating (outward expansion) on the above two layers of maps to prevent the robot’s shell from hitting obstacles.

  • Other Layers: Custom costmap.

Multiple layers can be freely matched as needed.

5.4 Collision algorithm

The gray value of the raster in the cost map is expressed as follows:

  • Fatal obstacle: The grid value is 254. At this time, the obstacle overlaps with the center of the robot, and a collision will inevitably occur;

  • Inscribed obstacle: The grid value is 253. At this time, the obstacle is within the inscribed circle of the robot, and a collision will inevitably occur;

  • Circumscribed obstacle: The grid value is [128,252]. At this time, the obstacle is within the circumscribed circle of its robot and is at the critical point of collision, and a collision may not occur;

  • Non-free space: The grid value is (0,127]. At this time, the robot is near an obstacle and belongs to a dangerous warning area. If it enters this area, a collision may occur in the future;

  • Free area: The grid value is 0, where the robot can pass freely;

  • Unknown area: The grid value is 255, and it has not yet been determined whether there are obstacles.

The setting of expansion space can refer to non-free space.

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. CS entry skill treeLinux introductionFirst introduction to Linux37723 people are learning the system