02. Linux, ROS alapismeretek
Elmélet
ROS 1 → ROS 2
- ROS 2 was rewritten from scratch
- More modular architecture
- Improved support for real-time systems
- Support for multiple communication protocols
- Better interoperability with other robotic systems
- Focus on standardization and industry collaboration
- No ROS Master
- No Devel space
rclpy,rclcpp- More structured code (
Nodeclass) - Different build system
- Platforms: Windows, OS X, Linux
ROS principles
ROS node

- Executable part of ROS:
- python scripts
- compiled C++ code
- A process that performs computation
- Inter-node communication:
- ROS topics (streams)
- ROS parameter server
- Remote Procedure Calls (RPC)
- ROS services
- ROS actions
- Meant to operate at a fine-grained scale
- Typically, a robot control system consists of many nodes, like:
- Trajectory planning
- Localization
- Read sensory data
- Process sensory data
- Motor control
- User interface
- etc.
ROS workspace
Colcon workspace
A folder where packages are modified, built, and installed.
- Source space:
- Source code of colcon packages
- Space where you can extract/checkout/clone source code for the packages you want to build
- Build space
- Colcon is invoked here to build packages
- Colcon and CMake keep intermediate files here
- Install space:
- Each package will be installed here; by default each package will be installed into a separate subdirectory
- Log space:
- Contains various logging information about each colcon invocation
ROS package principle
Enough functionality to be useful, but not too much that the package is heavyweight and difficult to use from other software.
ROS dependencies
After cloning a new package, use the following command to install depenencies:
rosdep install --from-paths src --ignore-src -r -y
ROS package
- Main unit to organize software in ROS
- Buildable and redistributable unit of ROS code
- Consists of (in the case of Python packages):
package.xmlfile containing meta information about the package- name
- version
- description
- dependencies
- etc.
setup.pycontaining instructions for how to install the packagesetup.cfgis required when a package has executables, so ros2 run can find them/<package_name>- a directory with the same name as your package, used by ROS 2 tools to find your package, contains__init__.py- Anything else
ros2 run turtlesim turtlesim_node
CMake
For CMake packages (C++), the package contents will be different.
ROS build system---Colcon
- System for building software packages in ROS
Linux principles

- (Was) the only OS supported by ROS
- Security
- Efficieny
- Open-source
- Community support
- User freedom
- Distributions: Ubuntu, Linux Mint, Debian, etc.
- Terminal usage more dominant
Suggestion
Install Terminator terminal emulator:
sudo apt update
sudo apt install terminator
Linux commands
See some basic commands below:
- Run as administrator with
sudo - Manual of command
man, e.g.man cp - Package management
apt, e.g.apt update,apt install - Navigation
cd - List directory contents
ls - Create file
touch - Copy file
cp - Move file
mv - Remove file
rm - Make directory
mkdir - Remove directory
rmdir - Make a file executable
chmod +x <filename> - Safe restart: Crtl + Alt + PrtScr + REISUB
- If not sure, just google the command
Environmental setup file
- setup.bash
- generated during init process of a new workspace
- extends shell environment
- ROS can find any resources that have been installed or built to that location
source ~/ros2_ws/install/setup.bash
Gyakorlat
1: Linux gyakorlás
-
Hozzunk létre egy új mappát a home könyvtárban a
mkdirparancs segítségével.mkdir ~/test_folder
-
Navigáljunk a létrehozott mappába a
cdparancs használatával.cd ~/test_folder
-
Hozzunk létre egy új Python fájlt a
touchparancs segítségével, nevezzük el "hello.py"-nek.touch hello.py
-
Nyissuk meg a "hello.py" fájlt a Gedit szövegszerkesztővel, használjuk a
geditparancsot.gedit hello.py
-
Írjunk egy egyszerű Python programot a "hello.py" fájlba, például:
#!/usr/bin/python3 print("Hello, World!")
-
Mentés után készítsünk másolatot a "hello.py" fájlból egy másik néven (pl.: "hello_copy.py") a
cpparancs segítségével.cp hello.py hello_copy.py
-
Ellenőrizzük, hogy a másolat elkészült-e az
lsparancs használatával.ls
-
Futtassuk a "hello.py" fájlt a
python3előtaggal.python3 hello.py
-
Készítsünk egy shell szkriptfájlt, majd nyissuk is meg a Gedit segítségével. Nevezzük el "myscript.sh"-nek.
gedit myscript.sh
-
Írjunk egy egyszerű shell szkriptet a
myscript.shfájlba, például:#!/bin/bash echo "I make another copy of hello.py!" cp hello.py another_hello_copy.py
-
Adjunk futtatási jogosultságot a
myscript.shfájlnak achmodparancs segítségével.chmod +x myscript.sh
-
Futtassuk a
myscript.shfájlt a./előtaggal../myscript.sh
-
Ellenőrizzük, hogy az újabb másolat elkészült-e az
lsparancs használatával.ls
2: Turtlesim
-
Indítsuk el a
turtlesim_node-ot és aturtle_teleop_keynode-ot az alábbi parancsokkal, külö-külön terminál ablakokban:ros2 run turtlesim turtlesim_noderos2 run turtlesim turtle_teleop_keyTip
Terminator-ban
Ctrl-Shift-O,Ctrl-Shift-Ebillentyű kombinációkkal oszthatjuk tovább az adott ablakot.Ctrl-Shift-Wbezárja az aktív ablakot.Futtatás megszakítása
Ctrl-C
-
Az alábbi ROS parancsok futtatása hasznos információkkal szolgálhat:
ros2 wtf ros2 node list ros2 node info /turtlesim ros2 topic list ros2 topic info /turtle1/cmd_vel ros2 interface show geometry_msgs/msg/Twist ros2 topic echo /turtle1/cmd_vel
-
Az alábbi paranccsal indítsuk el az
rqt_gui-t:ros2 run rqt_gui rqt_gui
-
Jelenítsük meg a futó node-okat és topic-okat
rqt_gui-ban: Plugins → Introspection → Node Graph.
-
Publikáljunk a
/turtle1/cmd_veltopic-ba szintén azrqt_guifelhasználásával: Plugins → Topics → Message Publisher.
3: ROS 2 workspace létrehozása
-
Hozzunk létre új ROS2 workspace-t
ros2_wsnévvel.mkdir -p ~/ros2_ws/src
4: ROS 2 package létrehozása
-
Hozzunk létre új ROS2 package-et
ros2_coursenévvel és egy Hello World-del.cd ~/ros2_ws/src ros2 pkg create --build-type ament_python --node-name hello ros2_courseSzintaxis
ros2 pkg create --build-type ament_python <package_name>
-
Build-eljük a workspace-t.
cd ~/ros2_ws colcon build --symlink-installSymlink
A
--symlink-installopció az Install space-be belinkeli a forrás script-eket, így módosítás után nem kell újra build-elnünk.
-
A
~/.bashrcfájl végére illesszük be az alábbi sorokat:source ~/ros2_ws/install/setup.bash export ROS_DOMAIN_ID=12Importálás QtCreator-ba
New file or project → Other project → ROS Workspace. Válasszuk ki a Colcon-t, mint Build System, és a
ros2_ws-t, mint Worksapce path.Importálás CLion-ba
Állítsuk be a Python interpretert Python 3.10-re,
/usr/bin/python3.10. Adjuk hozzá akövetkező elérési utat:/opt/ros/humble/lib/python3.10/site-packages. Hozzuk létre acompile_commands.jsonfájlt a~/ros2_ws/buildkönyvtárban az alábbi tartalommal:[ ]
-
Teszteljük a Hello World működését:
ros2 run ros2_course hello
5: Publisher implementálása Python-ban
-
Navigáljunk a
ros2_ws/src/ros2_course/ros2_coursemappába és hozzuk létre atalker.pyfájlt az alábbi tartalommal.import rclpy from rclpy.node import Node from std_msgs.msg import String class Talker(Node): def __init__(self): super().__init__('talker') self.publisher_ = self.create_publisher(String, 'chatter', 10) timer_period = 0.5 # seconds self.timer = self.create_timer(timer_period, self.timer_callback) self.i = 0 def timer_callback(self): msg = String() msg.data = 'Hello World: %d' % self.i self.publisher_.publish(msg) self.get_logger().info('Publishing: "%s"' % msg.data) self.i += 1 def main(args=None): rclpy.init(args=args) talker = Talker() rclpy.spin(talker) # Destroy the node explicitly # (optional - otherwise it will be done automatically # when the garbage collector destroys the node object) talker.destroy_node() rclpy.shutdown() if __name__ == '__main__': main()
-
A
setup.pyfájlban adjunk meg egy új entry point-on:'talker = ros2_course.talker:main',
-
Build-eljük és futtassuk a node-ot:
cd ~/ros2_ws colcon build --symlink-install ros2 run ros2_course talker
-
Ellenőrizzük le a node kimenetét a
ros2 topic echoparancs vagy azrqt_guihasználatával.
6: Subscriber implementálása Python-ban
-
Navigáljunk a
ros2_ws/src/ros2_course/ros2_coursemappába és hozzuk létre alistener.pyfájlt az alábbi tartalommal.import rclpy from rclpy.node import Node from std_msgs.msg import String class Listener(Node): def __init__(self): super().__init__('listener') self.subscription = self.create_subscription( String, 'chatter', self.listener_callback, 10) self.subscription # prevent unused variable warning def listener_callback(self, msg): self.get_logger().info('I heard msg: "%s"' % msg.data) def main(args=None): rclpy.init(args=args) listener = Listener() rclpy.spin(listener) # Destroy the node explicitly # (optional - otherwise it will be done automatically # when the garbage collector destroys the node object) listener.destroy_node() rclpy.shutdown() if __name__ == '__main__': main()
-
A
setup.pyfájlban adjunk meg egy új entry point-on:'listener = ros2_course.listener:main',
-
Build-eljük és futtassuk mind a 2 node-ot:
cd ~/ros2_ws colcon build --symlink-install ros2 run ros2_course talkerros2 run ros2_course listener
-
Az
rqt_guihasználatával jeleníttessük meg a futó rendszer node-jait és topic-jait:ros2 run rqt_gui rqt_gui