Presentation is loading. Please wait.

Presentation is loading. Please wait.

Robotic Perception and Action

Similar presentations


Presentation on theme: "Robotic Perception and Action"— Presentation transcript:

1 Robotic Perception and Action
Turtlesim: Publisher/Subscriber Examples

2 Writing a Publisher node
What is Turtlesim? Writing a Publisher node ROS Publisher Program Catkin Executing Publisher Program Publisher Result Writing a Subscriber node ROS Subscriber Program Executing Subscriber Program Subscriber Result References Index

3 Turtlesim is a tool made for teaching ROS and ROS packages
In order to become more familiar with the ROS concepts about Publisher/Subscriber nodes, we use the simple simulator Turtlesim Turtlesim is a tool made for teaching ROS and ROS packages To open the turtlesim: Open a 1st terminal and activate ROS by digiting: > roscore Open a 2° terminal and run the command: > rosrun turtlesim turtlesim_node The simulator window is opened with the turtle simulated in the center position. What is Turtlesim?

4 The turtlesim package has a single node called turtlesim_node that presents the following topics:
Published Topics turtleX/cmd_vel (geometry_msgs/Twist) It is the linear and angular command velocity for turtleX. The turtle will execute a velocity command for 1 second then time out. Subscribed Topics turtleX/pose (turtlesim/Pose) The x, y, theta, linear velocity, and angular velocity of turtleX. In red there are the topic names and in blue the topic data types What is Turtlesim?

5 Let’s investigate the data types by finding them on Google or directly on the website geometry_msgs/Twist What is Turtlesim?

6 What is Turtlesim? turtlesim/Pose Remark Pose = Position + Orientation
In this case we have a 2D pose: Position: x, y directions Orientation: theta angle What is Turtlesim?

7 Move the turtle: send command velocity -> Publisher
Ok. Now we know how it is composed the turtlesim package, node and related topics. It’s time to create program files in order to use these information to: Move the turtle: send command velocity -> Publisher Acquire pose data (odometry) from turtle -> Subscriber So, open a terminal and go to the workspace directory and let’s create a new package called turtle_pkg in where we will create the publisher/subscriber nodes by following the commands: > cd ~/catkin_ws > source /opt/ros/indigo/setup.bash > source devel/setup.bash > cd ~/catkin_ws/src > catkin_create_pkg --rosdistro indigo turtle_pkg roscpp ROS Publisher/Subscriber

8 Now it’s time to understand the concepts about Publisher program code:
Move the turtle: send command velocity -> Publisher Once the package is created with success, go inside the turtle_pkg directory and in the directory src open with gedit a file called pubvel.cpp. > cd ~/catkin_ws/src/turtle_pkg/src > gedit pubvel.cpp In it copy the following code: ROS Publisher Program

9 // This program publishes randomly − generated velocity messages for turtlesim.
#include <ros/ros.h> #include <geometry_msgs/Twist.h> // For geometry_msgs ::Twist #include <stdlib.h> // For rand () and RAND_MAX int main (int argc, char **argv) { // Initialize the ROS system and become a node. ros::init(argc, argv, "publish_velocity") ; ros::NodeHandle nh; // Create a publisher object. ros::Publisher pub = nh.advertise<geometry_msgs::Twist>("turtle1/cmd_vel", 1000); // Seed the random number generator. srand(time(0)); ROS Publisher Program

10 ROS Publisher Program // Loop at 2Hz until the node is shut down.
ros::Rate rate(2); while (ros::ok()){ // Create and fill in the message. The other four // fields, which are ignored by turtlesim , default to 0. geometry_msgs:: Twist msg; msg.linear.x = double(rand())/double(RAND_MAX); msg.angular.z = 2*double(rand())/double(RAND_MAX)-1; // Publish the message. pub.publish(msg); // Send a message to rosout with the details. ROS_INFO_STREAM("Sending random velocity command: " <<"linear="<<msg.linear.x <<"angular="<<msg.angular.z); // Wait until it's time for another iteration. rate.sleep(); } ROS Publisher Program

11 After that, open with gedit the CMakeLists
After that, open with gedit the CMakeLists.txt file in the previous folder and copy before the end of file: In this way we have created the Node file pubvel related to the .cpp file pubvel.cpp. add_executable(pubvel src/pubvel.cpp) target_link_libraries(pubvel ${catkin_LIBRARIES}) ROS Publisher Program

12 To build your package we have to return to the root directory of our workspace:
> cd /home/student/catkin_ws or > cd ~/catkin_ws Build and create executable for all packages: > catkin_make If all is ok, it will create other 2 directories called build and devel: Now you update your environment: > source devel/setup.bash NB: catkin is the ROS build system to generate exec, libraries, and interfaces. Catkin

13 Executing Publisher Program
When all of those build steps are complete, your new ROS program is ready to execute using rosrun just like any other ROS program. Open a terminal and launch the roscore command > roscore Open a second terminal and launch the turtlesim simulator > rosrun turtlesim turtlesim_node Open a third terminal and launch our example, the command is: > rosrun turtle_pkg pubvel NB: If the package is not visible, try to restart the shell or run the command: > rospack find turtle_pkg or rospack profile find turtle_pkg If all is ok, it will produce as result: Executing Publisher Program

14 Publisher Result

15 ROS Subscriber Program
Now it’s time to understand the concepts about Subscriber program code: Acquire pose data (odometry) from turtle -> Subscriber As the publisher, go inside the turtle_pkg directory and in the directory src open with gedit a file called subpose.cpp. > cd ~/catkin_ws/src/turtle_pkg/src > gedit subpose.cpp In it copy the following code: ROS Subscriber Program

16 ROS Subscriber Program
// This program subscribes to turtle1/pose and shows its messages on the screen. #include <ros/ros.h> #include <turtlesim/Pose.h> #include <iomanip> // for std::setprecision and std::fixed /* A callback function. Executed each time a new pose message arrives. void function_name(const package_name::type_name &msg) { ... } The package_name and type_name are the same as for publishing: They refer to the message class for the topic to which we plan to subscribe. */ void poseMessageReceived(const turtlesim::Pose& msg){ // POSE = POSITION + ANGULAR DIRECTION ROS_INFO_STREAM(std::setprecision(2)<<std::fixed <<"position =("<<msg.x<<", "<<msg.y<<")" // position <<"direction="<<msg.theta); // direction ROS Subscriber Program

17 ROS Subscriber Program
int main (int argc, char **argv){ // Initialize the ROS system and become a node . ros::init(argc, argv, "subscribe_to_pose"); ros::NodeHandle nh; // Create a subscriber object.1000 is the queue size ros::Subscriber sub = nh.subscribe("turtle1/pose", 1000, &poseMessageReceived); // Let ROS take over. ros::spin(); } ROS Subscriber Program

18 After that, open with gedit the CMakeLists
After that, open with gedit the CMakeLists.txt file in the previous folder and copy before the end of file: In this way we have created the Node file subpose related to the .cpp file subpose.cpp. add_executable(subpose src/subpose.cpp) target_link_libraries(subpose ${catkin_LIBRARIES}) ROS Subscriber Program

19 To build your package we have to return to the root directory of our workspace:
> cd /home/student/catkin_ws or > cd ~/catkin_ws Build and create executable for all packages: > catkin_make If all is ok, it will create other 2 directories called build and devel: Now you update your environment: > source devel/setup.bash NB: catkin is the ROS build system to generate exec, libraries, and interfaces. Catkin

20 Executing Subpose Program
When all of those build steps are complete, your new ROS program is ready to execute using rosrun just like any other ROS program. Open a terminal and launch the roscore command > roscore Open a second terminal and launch the turtlesim simulator > rosrun turtlesim turtlesim_node Open a third terminal and launch our example, the command is: > rosrun turtle_pkg subpose Open a fourth terminal and launch the turtle_teleop in order to move with the keyboard our turtle. The command is: > rosrun turtlesim turtle_teleop_key NB: If the package is not visible, try to restart the shell or run the command: > rospack find turtle_pkg or rospack profile find turtle_pkg If all is ok, it will produce as result: Executing Subpose Program

21 Subscriber Result

22 turtle_pkg tree

23 A Gentle Introduction to ROS (Jason M. O’Kane) Wiki ROS Tutorials
Turtlesim Tutorial A Gentle Introduction to ROS (Jason M. O’Kane) Wiki ROS Tutorials  Slides course (Roi Yehoshua) References


Download ppt "Robotic Perception and Action"

Similar presentations


Ads by Google