Chapter 6: Robot Operating System (ROS)
Activity 6: Simulators and Sensor
Solution
We start by creating the packages and files:
cd ~/catkin_ws/src catkin_create_pkg activity1 rospy sensor_msgs cd activity1 mkdir scripts cd scripts touch observer.py touch movement.py chmod +x observer.py chmod +x movement.py
This is the implementation of the image obtainer node:
Note
Add the aforementioned code to the observer.py file.
#!/usr/bin/env python import rospy from sensor_msgs.msg import Image import cv2 from cv_bridge import CvBridge class Observer: Â Â Â Â bridge = CvBridge() Â Â Â Â counter = 0 Â Â Â Â def callback(self, data): Â Â Â Â Â Â Â Â if self.counter == 20: Â Â Â Â Â Â Â Â Â Â Â Â cv_image = self.bridge.imgmsg_to_cv2(data, "bgr8") Â Â Â Â Â Â Â Â Â Â Â Â cv2.imshow('Image',cv_image) Â Â Â Â Â Â Â Â Â Â Â Â cv2.waitKey(1000) Â Â Â Â Â Â Â Â Â Â Â Â cv2.destroyAllWindows() Â Â Â Â Â Â Â Â Â Â Â Â self.counter = 0 Â Â Â Â Â Â Â Â else: Â Â Â Â Â Â Â Â Â Â Â Â self.counter += 1 Â Â Â Â def observe(self): Â Â Â Â Â Â Â Â rospy.Subscriber('/camera/rgb/image_raw', Image, self.callback) Â Â Â Â Â Â Â Â rospy.init_node('observer', anonymous=True) Â Â Â Â Â Â Â Â rospy.spin() if __name__ == '__main__': Â Â Â Â obs = Observer() Â Â Â Â obs.observe()
As you can see, this node is very similar to the one in Exercise 21, Publishers and Subscribers. The only differences are:
A counter is used for showing only one image of twenty received.
We enter 1000 (ms) as the Key() parameter so that each image is shown for a second.
This is the implementation of the movement node:
#!/usr/bin/env python import rospy from geometry_msgs.msg import Twist def move(): Â Â Â Â pub = rospy.Publisher('/mobile_base/commands/velocity', Twist, queue_size=1) Â Â Â Â rospy.init_node('movement', anonymous=True) Â Â Â Â move = Twist() Â Â Â Â move.angular.z = 0.5 Â Â Â Â rate = rospy.Rate(10) Â Â Â Â while not rospy.is_shutdown(): Â Â Â Â Â Â Â Â pub.publish(move) Â Â Â Â Â Â Â Â rate.sleep() if __name__ == '__main__': Â Â Â Â try: Â Â Â Â Â Â Â Â move() Â Â Â Â except rospy.ROSInterruptException: Â Â Â Â Â Â Â Â pass
To execute the file, we will execute the code mentioned here.
Note
Add this code to observer the .py file.
cd ~/catkin_ws source devel/setup.bash roscore roslaunch turtlebot_gazebo turtlebot_world.launch rosrun activity1 observer.py rosrun activity1 movement.py
Run both nodes and check the system functioning. You should see the robot turning on itself while images of what it sees are shown. This is a sequence of the execution:
The output will look like this:
Note
The output will look similar but not exactly look as the one mentioned in figures 6.10, 6.11, and 6.12.
Congratulations! You have completed the activity and at the end, you will have an output which is like figures 6.8, 6.9, and 6.10. By completing this activity successfully, you have been able to implement and work with nodes that let you subscribe to a camera which will show images in the virtual environment. You also learned to rotate a robot on itself that lets you view these images.