Skip to content

ROS Test Node for CARLA First Walkthrough

Pavlo Mospan edited this page Nov 11, 2021 · 7 revisions

This Wiki page is loosely intertwined with CARLA ROS Bridge Tutorial but gives a first overview on how to work with CARLA, first steps on running it and experiencing it the first hand.

Link To Oficial Carla-Ros-Bridge Repo

0.9.10.1 Version

Link To Oficial Carla Documentation

0.9.10 Version

Start Carla

In the terminal go inside carla_0.9.10.1 folder and run the following command to execute the package file and start the simulation:

./CarlaUE4.sh # on Linux
CarlaUE4.exe  # on Windows

Carla Start Page

Internet is full of creative ideas on how to also start CARLA simulator using additional prefixes, that supposedly could give extra functionality, like implicitly specifying number of fps or op OpenGl.

DISPLAY= ./CarlaUE4.sh -opengl -carla-server -benchmark -fps=10

Unfortunately or not, in our use case it doesn't run properly, but is really no matter. if one desires, he could open window with predefined resolution. Hopefully, it is pointless, since window size could be changed any time by drag-and-drop

./CarlaUE4.sh -windowed -ResX-320 -ResY-240

Also, CARLA package provides different ready-to-go examples, that allow control over the CARLA world through it's Python API. For instance, span a car to drive it manually

# in the carla_0.9.10.1 folder
cd PythonAPI/examples
python3 manual_control.py

Carla Manual Control

The are following. controls available:

Use ARROWS or WASD keys for control.

W            : throttle
S            : brake
A/D          : steer left/right
Q            : toggle reverse
Space        : hand-brake
P            : toggle autopilot
M            : toggle manual transmission
,/.          : gear up/down
CTRL + W     : toggle constant velocity mode at 60 km/h

L            : toggle next light type
SHIFT + L    : toggle high beam
Z/X          : toggle right/left blinker
I            : toggle interior light

TAB          : change sensor position
` or N       : next sensor
[1-9]        : change to sensor [1-9]
G            : toggle radar visualization
C            : change weather (Shift+C reverse)
Backspace    : change vehicle

R            : toggle recording images to disk

CTRL + R     : toggle recording of simulation (replacing any previous)
CTRL + P     : start replaying last recorded simulation
CTRL + +     : increments the start time of the replay by 1 second (+SHIFT = 10 seconds)
CTRL + -     : decrements the start time of the replay by 1 second (+SHIFT = 10 seconds)

F1           : toggle HUD
H/?          : toggle help
ESC          : quit

Or you can spawn, for instance, other 20 vehicles in Carla (The will also appear in ROS-Bridge simulator)

cd /carla_0.9.10.1/PythonAPI/examples
python3 spawn_npc.py -n 20

Start Ros-Bridge

The are following features, that Rot-Bridge for CARLA provides:

  • Provide Sensor Data (Lidar, Semantic lidar, Cameras (depth, segmentation, rgb, dvs), GNSS, Radar, IMU)
  • Provide Object Data (Transforms (via tf), Traffic light status, Visualization markers, Collision, Lane invasion)
  • Control AD Agents (Steer/Throttle/Brake)
  • Control CARLA (Support synchronous mode, Play/pause simulation, Set simulation parameters)

Launch the first node, that spawns randomly a vehicle and lets us manually control it. Hint : if you cannot drive, press B to allow manually control or address the built-in help message via H

roslaunch carla_ros_bridge carla_ros_bridge_with_example_ego_vehicle.launch

ROS Bridge Manuall Control

List all the topics, the above node opens for you:

rostopic list

Each node has it’s own topics, but the topics itself a can be opened by many nodes. So, run the command above to check which topics are available to you by this node. Topics are named buses over which nodes exchange messages. Topics have anonymous publish/subscribe semantics, which decouples the production of information from its consumption. In general, nodes are not aware of who they are communicating with. Instead, nodes that are interested in data subscribe to the relevant topic; nodes that generate data publish to the relevant topic. There can be multiple publishers and subscribers to a topic. Topics are intended for unidirectional, streaming communication.

In other words, topics allow us to push data to or read data from.

One of the topics listed from above command would be of carla_msgs.CarlaEgoVehicleControl, which means we can make the car moving.

 rostopic pub /carla/ego_vehicle/vehicle_control_cmd carla_msgs/CarlaEgoVehicleControl "{throttle: 1.0, steer: 0.0}" -r 10

What this actually says: Rostopic publish (pub stands for publish) to this topic vehicle_control_cmd (cmd stands for command) and then we specify the massage format. Command above makes the car drive forward with no steering whatsoever.

Hint : If the car doesn't start driving check if you set manually control to auto by pressing B again.

Listening to Topics

There is a 50/50 well written tutorial on how to write subscribers and listeners

So, what you basically need to know is the topics your ode opens. For instance, EgoVehicle node has such interesting topic as /carla/ego_vehicle/camera/depth/front/camera_info. Folder tests_scripts has the whole example code (as well as /carla/ego_vehicle/vehicle_status example). These are the most important milestones:

from sensor_msgs.msg import CameraInfo 

and

rospy.Subscriber("/carla/ego_vehicle/camera/depth/front/camera_info", CameraInfo, callback)

Simply execute the subscriber code, while node is running

python3 subscriber_camera.py

Output would look like this :

[INFO] [1636572059.543557, 1789.727963]: /pavlo_listener_14335_1636572036854Received header: 
  seq: 420
  stamp: 
    secs: 1789
    nsecs: 677963044
  frame_id: "ego_vehicle/camera/depth/front"
height: 70
width: 400
distortion_model: "plumb_bob"
D: [0.0, 0.0, 0.0, 0.0, 0.0]
K: [200.00000000000003, 0.0, 200.0, 0.0, 200.00000000000003, 35.0, 0.0, 0.0, 1.0]
R: [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]
P: [200.00000000000003, 0.0, 200.0, 0.0, 0.0, 200.00000000000003, 35.0, 0.0, 0.0, 0.0, 1.0, 0.0]
binning_x: 0
binning_y: 0
roi: 
  x_offset: 0
  y_offset: 0
  height: 0
  width: 0
  do_rectify: False

To make a long story short

  1. Start Carla : ./CarlaUE4.sh
    • Add npc's, drive a car, etc. by executing corresponding package inside carla_0.9.10.1/PythonAPI/examples directory
  2. Start ROS-Bridge for Carla roslaunch carla_ros_bridge carla_ros_bridge_with_example_ego_vehicle.launch
  3. Check available topics rostopic list
  4. Run subscriber, e.g. python3 subscriber_camera.py