Write your first Ros2 program

Create a ROS2 Workspace

Create and set up a Ros2 Workpace
-Ros2 Workpace: The place where you write all your code for a Ros2 application and where you will compile this code
Ros2_For_Beginners2-1
(1)Go to home directory and create a folder named ros2_ws & In this folder create a src folder (souce folder)
-src:All the code and packages created will be in the source folder
(2)Build the workspace
Ros2_For_Beginners2-2
-Have a successful build and because we didn’t put anything in the source, we have 0 packages installed
(3)After build, we have three more folders
Ros2_For_Beginners2-3
-log: log of the build process (4)Inside the install, setup.bash script exists
Ros2_For_Beginners2-4
(5)Source either local_setup.bash or setup.bash
Source local_setup.bash
Ros2_For_Beginners2-5
-Can use whatever I created in the workspace
-Difference between local_setup.bash and setup.bash:
local_setup.bash can simply source that workspace which we can overlay(ros2_ws/install)
setup.bash will set up the entire workspace (ros2_ws/install + global ros2 installation)
Source setup.bash
Ros2_For_Beginners2-7
Ros2_For_Beginners2-6
-Can be sure own Ros2 functionalities can be used
-Can be sure use my own functionalities added in the workspace

Create a Python package

To create a Ros2 node, a package is needed
Packages are allowed to seperate your code into reusable blocks
Each package is an independent unit
ex.
Camera pkg: package that handles a camera
Hardware Control pkg: package for wheels of a robot
Motion planning pkg: package that will handle motion planning for the robot in the environment
(1)Create a python pkg in your new ros2 ws
Ros2_For_Beginners2-8
Navigate src dir of your workspace
Ros2_For_Beginners2-9
Create my_py_pkg and add an argument to specify which kind of package you want
ros2 pkg create : Command to create a package
--build-type ament_python
-There are differences between a python package and a c++ package
-Used a build type argument with ament's python
-ament is the build system and ament's python means you want a python package
--build-type ament_python
--dependencies
-dependencies: packages this new package relies on
-rclpy: Ros2 python library
-Many folders and files created
![Ros2_For_Beginners2-10](https://github.com/growingpenguin/growingpenguin.github.io/assets/110277903/3f9e4b5b-1260-4404-94f5-a0efa08a7eac)
(2)See what's in the src folder
Opened the source directory of the ros2 workspace on vs code
my_py_pkg: we are going to put in all your python nodes
test: containing test files
![Ros2_For_Beginners2-11](https://github.com/growingpenguin/growingpenguin.github.io/assets/110277903/524a0a35-a957-414a-b694-a2226932214f)![Ros2_For_Beginners2-12](https://github.com/growingpenguin/growingpenguin.github.io/assets/110277903/85ad904e-6490-4c25-9e97-2b4e7e019584)
package.xml: C++ or Python package will contain a package.xml file
-First is the name of information
Next, dependencies, rclpy specified when creating the pkg
-When wanting to create another dependency, add another dependency tag below that one
Last, ament_python build type for the pkg
setup.cfg & setup.py: when we create a ros2 node, it will be useful to install the node in your environment
(3)Building workspace
Error:
![Ros2_For_Beginners2-13](https://github.com/growingpenguin/growingpenguin.github.io/assets/110277903/b469899c-5bfe-4c20-a762-a0ba233f51d7)
Solution:
![Ros2_For_Beginners2-14](https://github.com/growingpenguin/growingpenguin.github.io/assets/110277903/11ded559-9e61-4297-9414-80f919ad7bc2)
![Ros2_For_Beginners2-15](https://github.com/growingpenguin/growingpenguin.github.io/assets/110277903/aed9743b-e862-4367-9075-fca10d645061)
Previous setuptools version was 59.6.0, but things are not working right, so we need to downgrade it to 58.2.0
Error resolved:
![Ros2_For_Beginners2-16](https://github.com/growingpenguin/growingpenguin.github.io/assets/110277903/7008fc08-6194-4c05-8659-92a8d25f4d67)
(4)Build a particular package
![Ros2_For_Beginners2-17](https://github.com/growingpenguin/growingpenguin.github.io/assets/110277903/10e7cd4c-57e6-4835-9147-7a13c9447eed)
colcon build --packages-select : Build/Compile a certain package
(5)Python package is ready to host any Python node

Create a C++ package

(1)Navigate src folder
Ros2_For_Beginners2-18
my_py_pkg, python package exists
my_py_pkg exists
(2)Create C++ package
Ros2_For_Beginners2-19
pkg create my_cpp_pkg: Where we put all our C++ code
–build-type ament_cmake: Will create a C++ pkg
–dependencies rclcpp: Add dependency rclcpp
rclpy for python and rclcpp for C++, which allows you to use all the ros2 functionalities
Ros2_For_Beginners2-20
New packages made
(3)See what’s in the src folder
Ros2_For_Beginners2-21
package.xml: C++ or Python package will contain a package.xml file
-First is the informations
Next, dependencies, rclcpp library specified when creating the pkg
-When wanting to create another dependency, add another dependency tag below that one
Last, ament_cmake build type flag for the pkg
CMakelists.txt: Where the code is compiled
We also have the dependency for rclcpp
If wanting to add a new dependency for your C++ pkg, you need to add it in packages.xml and CMakeLists.txt
(4)Building workspace
Ros2_For_Beginners2-22
(5)Build a particular package
Ros2_For_Beginners2-23
Only build cpp package
(6)Ready to write your first node inside the packages

What is a Ros2 Node?

Node: sub-part of your application and should have a single purpose
-Applications will contain many nodes which will be put into packages
-Nodes will communicate with each other
-Real life example
package: independent unit of your application
-Empty package will not do anything
-Nodes will be created inside the packages
Camera pkg:
Handle a camera as an independent unit
Camera driver: Driver for the camera to be able to program it and get frames from it
Image processing: Program that will take those frames and do some image processing work
Additional program: Any other program related to the camera we are using
-All those programs in blue are nodes
-Each node can be launched seperately
-Launch the driver and then image processing node
-Nodes will communicate using ros2 communication functionalities
-camera pkg is filled with all the nodes we need
-sometimes it can be quite hard to know if you should put two nodes in the same pkg or not
ex.Image processing node could be part of the other pkg which only handles image processing for any camera
-Many other processing nodes can be added and this new pkg will communicate with any other camera pkg which includes drivers
=> In this example, image processing is specific to the camera we’re talking about and both drivers and image processing nodes are using some common dependencies
Motion planning pkg:
Motion planning node: Compute motion planning for your robot
Path correction node: Modify the motion planning due to external factors
-Can make two nodes inside different packages communicate together
ex.Can link image processing node to the path correction node
Hardware Control pkg: Has an independent unit which will control the hardware of the robot that can be wheels, robotic arm joint, or anything else
Drivers: Control motors
Main Control Loop Node: Drivers are controlled by the main control loop node
State Publisher Node: Position data which is coming back from the motor encoders is sent back to the control loop for precise control and is also published by the state publisher node
Process:
(1)Analyze frames coming from the camera and send an analysis of the environment to the path correction node
(2)Path correction node will be able to notify the motion planning node
(3)Motion planning node from the motion planning pkg will send computed trajectories to the main control loop node inside the hardware control pkg
(4)Hardware status of the robot is published in both the motion planning node and path correction node are getting those messages