0% found this document useful (0 votes)
69 views13 pages

ROS 2 Notes Saket

ROS2

Uploaded by

Mahesh Babu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views13 pages

ROS 2 Notes Saket

ROS2

Uploaded by

Mahesh Babu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

ROS 2 Notes Date: 21 Jan 2025

Create the project here:


ros_ws/src/{Project Folder Name}

For creating a Python file in the project workspace:


touch python_folder_name.py

Well this ain’t enough, we have to make this Python file into executive
mode:
chmod +x python_folder_name.py​
[For confirmation, type ls and check if the python file name is changed to green from white
color.]

Now change cd to src and open visual studio code there:


cd ../.. {This one cd to src}
code . {This one opens the visual code}

Well this is a basic code in visual studio in Python:

Then Do this:
Node: It is like a Robot Program or a task we are defining. (They can either publish or
subscribe to a Topic)
Topic: It is which two nodes communicate with each other. (Way to communicate
between different nodes.)

Here oval enclosed text are nodes, there are two nodes here, one opens a turtle simulator
(turtlesim) and the other actually moves (teleop) the turtle when we press keys, the other
example is a publisher node and a subscriber node.

Creating a Publisher:
Don’t forget to add these things: ​

Dependencies in package.xml: ​

And don’t forget to do this in setup.py:

After all these programming, do this in terminal:


To Run:

Result: ​

Similarly write the code for Subscriber:

Do the same here (To install the code): ​

To run the code:


Now here is a new code of a closed loop of a Publisher and a
subscriber:
Now to run this code: ​

Lil complex function/code for the same: (Just an added if-else condition to avoid hitting of
turtle)

Result after running:

Now we are writing a Code with Server and Client (It is


extension of the previous code bro):
URDF With ROS

Hello

ROS2 Action
Start:

Create a ros2_ws using mkdir,


Create a new dir called src
Now using colcon build command, create build, install, log folders along with src
Update bashrc
In src now create a package: ros2 pkg create {name}
Now cd to {name}
Remove unnecessary using this command: rm -r include/ src/
Now create new directories: mkdir msg srv action
Come cd src and open VScode using “code .” command.

That’s it do code in the VScode now.

Add these codes under this in your xml package: ​


<buildtool_depend>ament_cmake</buildtool_depend>
Code to be added:

<buildtool_depend> rosidl_default generators</build_tool_depend>


<exec_depend>rosidl_default_runtime</exec_depend>
<member_of_group>rosidl_interface_packaged</member_of_group>

Edit your CMake code to this: ​

cmake_minimum_required(VERSION 3.8)
project(my_robot_interfaces)
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rosidl_default_generators REQUIRED)

rosidl_generate_interfaces(${PROJECT_NAME}

)
ament_export_dependecies(rosidl_default_runtime)

ament_package()

A Simple Mini Project:

Create another pkg called actions_py, but use this syntax: ​


ros2 pkg create actions_py --build-type ament_python

Create another python file inside actions_py here it is count_until_server.


This is the Project code bro:

Server Code is below:


Client Code is below: ​
And as we always do, don’t forget to do change this in setup.py code bro:
Result: ​

You might also like