Skip to content

This repo contains files related to my teaching sessions on ROS

Notifications You must be signed in to change notification settings

m00ke5h/ROS-session-dreadnought

Repository files navigation

ROS Session

By - Mookesh Dash


ROS programmes work in the form of nodes

Now what are nodes?

Let's understand it with a simple example...

Screenshot-20210828013634-600x313

  • Sachin and Carryminati are two users signed in to Youtube.
  • Sachin wants to receive Carryminati's videos in his feed and therefore, he subscribes to Carryminati.
  • Now Carryminati publishes the video so that Sachin, who has subscribed to him can watch it.
  • Therefore,
    • Sachin is a subscriber,
    • Carryminati is a publisher , and,
    • YouTube is the medium which allows them to communicate.

Now coming to ROS terms,

  • rosnode - Sachin & Carryminati. The publishers and subscribers. They are programmes running inside ROS.
  • roscore - YouTube. The medium that allows nodes to communicate. We can call it the master node which every node registers itself to.
  • rostopic - Vines. The topic on which two nodes communicate.
  • rosmsg - Video or '.mp4'. The type of message communicated between the nodes.

Enough talk! Let's see a real pubsub programme...

First we have to initialize the roscore, so that nodes can communicate.

$ roscore

Go into your catkin_ws, scripts folder and create a new package.

$ cd ~/catkin_ws/src
$ catkin_create_pkg pubsub std_msgs rospy roscpp

catkin_create_pkg creates a package named pubsub that depends on std_msgs, rospy and roscpp.

Then, we have to build the packages in the catkin_ws and source the generated setup file to add our workspace to the ROS environment.

$ cd ~/catkin_ws
$ catkin_make
$ source devel/setup.bash

Make a script folder, (I have named it src) inside your pubsub package and move into it. Create two python files named carry.py and sachin.py

$ cd src/pubsub/
$ cd src/
$ touch carry.py sachin.py

Open your carry.py file in a text editor, and paste the following code:-

#!/usr/bin/python3

import rospy # Importing the ROS client library for python
from std_msgs.msg import String # Importing the String message type

def publish_video():
    pub = rospy.Publisher('vines', String, queue_size=1) # Subscribing to a topic
    rospy.init_node('carryminati', anonymous=True) # Intitializing the node
    rate = rospy.Rate(1) # Frequency of sent messages. Here 1 means 1Hz
    iteration = 1
    while not rospy.is_shutdown(): 
        video = f"Carry's video number {iteration}"
        pub.publish(video) # Publishing the message
        print(video)
        iteration += 1
        rate.sleep()

if __name__ == '__main__':
    try:
        publish_video()
    except rospy.ROSInterruptException:
        pass

Next open your sachin.py file in a text editor, and paste the following code:-

#!/usr/bin/python3

import rospy # Importing the ROS client library for python
from std_msgs.msg import String # Importing the String message type

def callback(data): 
    # Declaring a callback function. data is the message we receive from the publisher
    print(f"I saw {data.data}") # Printing it to console
    
def viewer():
    rospy.init_node('sachin', anonymous=True) # Intitializing the node
    rospy.Subscriber('vines', String, callback) # Subscribing to a topic
    rospy.spin() # keeps your node from exiting until the node has been shutdown

if __name__ == '__main__':
    viewer()

Save both the files, and in your bash make both the files executable by running:-

$ chmod +x carry.py sachin.py

Now go back to your catkin_ws and build your packages again.

$ cd ~/catkin_ws && catkin_make && source devel/setup.bash

Next, run your scripts using

$ rosrun pubsub carry.py
$ rosrun pubsub sachin.py

Our nodes are up and running!

Let's inspect our nodes and topics...

$ rosnode list
$ rostopic list

rosseshss

There's a nice way to check how the nodes and topics are working in a graphical way

$ rosrun rqt_graph rqt_graph

rosseshrqt

And indeed! carryminati and sachin are our two nodes, and the topic they are interacting with each other on is about vines.

That's it for today's sesh. I hope y'all have understood how nodes interact with each other via topics and if any doubts at all feel free to contact me anytime!

About

This repo contains files related to my teaching sessions on ROS

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages