carla-ros cross-machine communication (ros, carla_ros_bridge installation, development board communication)

carla-ros communication

1. ros, carla_ros_bridge installation

1.ros installation steps

The ros version selection should correspond to the ubnutu system version. It is recommended to use ros2 (communication is more convenient)
Correspondence between ROS and operating system versions: Correspondence between ROS and operating system versions_Corresponding versions of ros version and ubuntu_Wanqilinxi’s blog-CSDN blog

(1) Configure system software source

Open “Software and Updates”, enter the “Ubuntu Software” page, and allow universe, restricted, and multiverse, that is, check these three items, as shown in the figure below.

(2)Add secret key
Open the terminal and enter the following command to add the secret key:

sudo apt install curl gnupg2 -y
curl -s https://gitee.com/ohhuo/rosdistro/raw/master/ros.asc | sudo apt-key add -

(3)Set software source
Enter the following command to add the repository to the sources list:

sudo sh -c 'echo "deb [arch=$(dpkg --print-architecture)] http://packages.ros.org/ros2/ubuntu $(lsb_release -cs) main" > / etc/apt/sources.list.d/ros2-latest.list'

(4) Install ros

sudo apt update
sudo apt install ros-foxy-desktop

Note: The foxy version is installed here. To install other versions, you need to replace the version number.
(5)Install the automatic completion tool

sudo apt install python3-argcomplete -y

(6) Set environment variables

echo "source /opt/ros/foxy/setup.bash" >> ~/.bashrc
source.bashrc

(7) Check the installed ros version

printenv | grep -i ROS

(8)Test run
After ros2 is installed, you can run the official demo to verify whether the installation is completed: open two terminals and execute two instructions respectively, the first is used to publish topic messages, and the second is used to subscribe to topic messages.

ros2 run demo_nodes_cpp talker
ros2 run demo_nodes_cpp listener

2. carla_ros_bridge installation steps

The Carla_ros_bridge version selection should correspond to the Carla version, carla-simulator/ros-bridge: ROS bridge for CARLA Simulator (github.com)
(1) Download the corresponding version of carla_ros_bridge source code (be sure to pay attention to the version)

(2) Unzip the installation package

mkdir -p ~/carla-ros-bridge/project/src
//Copy the decompressed folder ros-bridge to the ~/carla-ros-bridge/project/src directory


(3) Install dependencies

cd ~/carla-ros-bridge/project
Rosdepc init
Rosdepc update
rosdepc install --from-paths src --ignore-src -r

rosdepc installation: rosdepc is the domestic version of rosdep. Generally, rosdep will encounter network connection problems. It is recommended to use rosdepc.
Installation command: wget http://fishros.com/install -O fishros & amp; & amp; . fishros
After running, select 4 in the terminal to install.
(4)Compilation

cd ~/carla-ros-bridge/project
colcon build

After compilation, the build, install, and log folders will be generated.
Note: There may be dependencies missing during the compilation process, and you need to download them manually.

2. carla-ros cross-machine communication

1. Communication between pc side ros and x3 board side ros

To achieve cross-machine communication, ROS1 needs to configure a single Master or multiple Master network environment, which is too troublesome; ROS2 can achieve communication as long as multiple machines are on the same LAN, but some detailed settings still need to be paid attention to.
The X3 board end is installed with the Ros.Bot officially provided by Horizon, which is essentially a simplified version of ros2-foxy, but does not support the display functions of ros2 (rviz, pcl, etc.).
(1) PC and x3 board are in the same LAN
If you are using a VM virtual machine or a vritual box virtual machine, you need to set the network connection mode to bridge mode. Communication cannot occur in NAT mode.

Let the PC and x3 board connect to the same wireless network, check that the network IP is in the same network segment, and test whether the two-way ping can be successful. If only the PC side can ping the x3 board side, you need to turn off the PC firewall settings.

(2) Test communication

PC terminal input: ros2 run examples_rclcpp_minimal_subscriber subscriber_member_function
X3 terminal input: source /opt/tros/setup.bash
ros2 run examples_rclcpp_minimal_publisher publisher_member_function

The above two instructions can also be replaced to achieve mutual publication and subscription:

The result shown in the above figure shows that cross-machine communication of ros can be carried out.

2. Communication between carla on the PC and ros on the PA (taking Deep Blue Academy control algorithm pid as an example)

Shenlan Academy provides open source code (c++, carla0.9.13) that implements the ros-carla control algorithm: gxf1002/ShenLanAcademy-PlanningControl: Vertical control: PID; Horizontal control: Stanely \ LQR \ MPC (github.com)
(1) Download the decompression and compilation function package
Download the function package from the above website and decompress it. Use the decompressed carla-ros-bridge directory as the workspace and compile colcon build in this directory.

(2) Run the algorithm
Step one: start carla
In the carla installation directory, run ./CarlaUE4.sh

Step 2: Configure environment variables

export PYTHONPATH=$PYTHONPATH:<path-to-carla>/PythonAPI/carla/dist/carla-<carla_version_and_arch>.egg

Is the carla installation path.

carla-.egg selects the corresponding version and architecture.

Step 3: Add the function package path in the terminal
cd to the workspace carla-ros-bridge and enter source /install/laocl_setup.sh in the terminal

Step 4: launch the carla-ros bridge control interface (taking the pid algorithm as an example)

ros2 launch carla_shenlan_bridge_ego_vis carla_bridge_ego_vehilce.launch.py

The following screen appears, indicating that Carla and Ros can communicate:

Step 5: Run the control algorithm node
Open a new terminal under the workspace carla-ros-bridge and enter:

source /install/laocl_setup.sh
ros2 run carla_shenlan_pid_controller carla_shenlan_pid_controller_node

After running the node, you can see the vehicle running. The pid control algorithm adjusts the longitudinal speed of the vehicle and outputs real-time speed, speed error, angle error and other information in the terminal.

3. Cross-machine communication between x3 board-side ros and pc-side carla

The implementation of the above two communication methods provides the possibility of cross-machine communication. When the carla-ros bridge control interface is started, the topic content that the x3 board end and the PC side ros can accept and subscribe to are the same, so it is published on the x3 board end. The vehicle control information is given to the vehicle_control_cmd topic. On the PC side, you can subscribe to the information in the vehicle_control_cmd topic. Through this method, cross-machine communication is achieved.
Step 1: Start Carla on PC (same as above)
Step 2: Start the carla-ros bridge control interface on the PC (same as above)
Step 3: Run the control algorithm node on the x3 board (same as above)
Use ros2 topic list to view the topics received by the X3 board and run the node:

Cross-machine communication time: the time it takes to publish a message to the topic vehicle_control_cmd in the statistical pid algorithm

It can be seen that the time to publish information to the topic vehicle_control_cmd is negligible, indicating that the cross-machine communication method will not affect the efficiency of program operation.

3. Function package, message type

1. carla_ros_bridge

carla_ros_bridge is the communication tool between carla and ros officially provided by carla, and is applicable to both ros1 and ros2. Essentially, it is a ros workspace composed of multiple function packages, including all basic function packages and function packages that implement your own algorithms.
For an explanation of the basic function package, please refer to the official website:

Take the Deep Blue Academy pid control algorithm as an example:

Among them, carla_shenlan_project_1 is the function package added by the developer himself, including the .cpp file and header file that implements the control algorithm. If other algorithms are implemented, the .cpp file and header file need to be modified and replaced.

2. carla message type (communication interface)

Message is an interface definition method in ros. Carla officially defines the message type msgs (carla_msga folder) related to Carla simulation.
For example, CarlaEgoVehicleControl.msg defines the name and data type of the information contained in the message (int32, float32, bool, etc.). The information published and subscribed to the topic must be the same as the information in the message type defined by the topic. Details Content reference official website: CARLA messages reference – CARLA Simulator

4. ros communication related content

1. Node

The responsibility of a node is to perform certain specific tasks. From the perspective of a computer operating system, it is also called a process; each node is an executable file that can run independently, such as executing a certain python program, or executing C++ The results generated by compilation are all considered as running a node; the node functions are different. Depending on the system design, it may be located in computer A or computer B, or it may run in the cloud. This is called distributed, that is, it can Distributed on different hardware carriers; each node needs to have a unique name. When we want to find a certain node, or want to query the status of a certain node, we can query it by the name of the node.

2. Topic

Topics are the bridge for transferring data between nodes.
(1)Publish and subscribe model
The characteristic of topic data transmission is from one node to another node. The object that sends data is called a publisher, and the object that receives data is called a subscriber. Each topic needs to have a name, and the transmitted data also needs to have a fixed name. data type. Supports many-to-many communication.

(2)Asynchronous communication
The so-called asynchronous means that after the publisher sends the data, it does not know when the subscriber will receive it. The asynchronous feature also makes the topic more suitable for some periodically released data, such as sensor data, motion control instructions, etc. , if there are some instructions with strong logic, such as modifying a certain parameter, it is not appropriate to use topic transmission.
(3)Message interface
In ROS, the description format of topic communication data is called a message, which corresponds to the concept of data structure in programming languages. For example, an image data here will include the length and width pixel values of the image, the RGB of each pixel, etc., which have standard definitions in ROS.
Message is an interface definition method in ROS, which has nothing to do with programming language. We can also define it ourselves through files with .msg suffix. With such an interface, various nodes are like building blocks, through various interfaces Splice them together to form a complex system.

3. Service

Service is a synchronous communication effect where you ask and I answer.
From the perspective of the service implementation mechanism, this form of question-and-answer is called the client/server model, referred to as the CS model. When the client needs certain data, it sends request information for a specific service, and the server After the client receives the request, it will process it and feedback the response information.