1. Edit hello.py
class HelloDrone(Node):
def __init__(self):
# node's name
super().__init__('hello_drone')
# Publishing VL53L0X x6 data — mock
self.pub_perimeter = self.create_publisher(
Float32MultiArray, # type of data
'/drone/perimeter', # name of param
10 # save in store if not readed
)
def tick():
...
One moment about ROS 2: to build nodes, I use a venv for all packages. But to run nodes — I have to deactivate the venv, because ROS 2 doesn’t play nice with venv. See venv ↔ ROS 2 / colcon.
2. Rebuild + run
3. Look at the topics
ros2 topic list
4. Read data from a topic
ros2 topic echo /drone/altitud
5. Frequency
ros2 topic hz /drone/perimeter
You should see ~1 Hz — because our timer fires once per second:
average rate: 1.000 min: 1.000s max: 1.000s std dev: 0.00044s window: 2
average rate: 1.000 min: 1.000s max: 1.000s std dev: 0.00035s window: 4
Adding a new node (sensor_monitor.py)
- Create the node
sensor_monitor.py. - Add to
CMakeLists.txt, after# Register nodes:
install(PROGRAMS
drone_sim/sensor_monitor.py
RENAME sensor_monitor
DESTINATION lib/${PROJECT_NAME}
)
- For testing, set one critical param in
hello.py:
perimeter.data = [1.2, 0.25, 1.5, 2.0, 1.1, 0.9]
Launcher
Create simulation/src/drone_sim/launch/drone.launch.py.
Build → run:
ros2 launch drone_sim drone.launch.py
The launcher usually shows up in the terminal without color. You can turn that on:
RCUTILS_COLORIZED_OUTPUT=1