Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Artificial Vision and Language Processing for Robotics

You're reading from   Artificial Vision and Language Processing for Robotics Create end-to-end systems that can power robots with artificial vision and deep learning techniques

Arrow left icon
Product type Paperback
Published in Apr 2019
Publisher Packt
ISBN-13 9781838552268
Length 356 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (3):
Arrow left icon
Gonzalo Molina Gallego Gonzalo Molina Gallego
Author Profile Icon Gonzalo Molina Gallego
Gonzalo Molina Gallego
Unai Garay Maestre Unai Garay Maestre
Author Profile Icon Unai Garay Maestre
Unai Garay Maestre
Álvaro Morena Alberola Álvaro Morena Alberola
Author Profile Icon Álvaro Morena Alberola
Álvaro Morena Alberola
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Artificial Vision and Language Processing for Robotics
Preface
1. Fundamentals of Robotics FREE CHAPTER 2. Introduction to Computer Vision 3. Fundamentals of Natural Language Processing 4. Neural Networks with NLP 5. Convolutional Neural Networks for Computer Vision 6. Robot Operating System (ROS) 7. Build a Text-Based Dialogue System (Chatbot) 8. Object Recognition to Guide a Robot Using CNNs 9. Computer Vision for Robotics Appendix

Chapter 6: Robot Operating System (ROS)


Activity 6: Simulators and Sensor

Solution

  1. 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
  2. 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:

  3. 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
  4. 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  
  5. 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:

    Figure 6.10: The first sequence of the execution of activity nodes

    Figure 6.11: The second sequence of the execution of activity nodes

    Figure 6.12: The third sequence of the execution of activity nodes

    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.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image