Monitor the Turtlesim emulator to send data to the actual robot--20

Source: Internet
Author: User

Abstract: Original Blog: Reproduced please indicate the source: http://www.cnblogs.com/zxouxuewei/

1.0. This tutorial teaches you to write the actual Ros program and control your robot. The PC-side Ubuntu+ros is used. Terminal in order to be able to use low-cost mileage meter, gyroscope, accelerometer, ultrasonic sensors, using STM32 controller. The two communicate through serial port, interactive data. All code has to be carefully studied. The premise is that you learn about the topics, messages, nodes and other nouns under Ros in ros.wiki or books.

2.0. Since Ros is officially using the Tuitlesim emulator, we are doing it again in this way. Start by creating your own Feature Pack in the Ros workspace:

Catkin_creat_pkg zxwtest_package std_msgs rospy roscpp (Feature Pack dependent std_msgs Rospy roscpp)

Then create a hello_node.cpp under ZXWTEST_PACKAGE/SRC

2.1. Modify the compilation option configuration required for the Catkin_make. Open the CMakeLists.txt file in the Zxwtest_package directory via vim or your favorite editor, load your own compilation options, and modify the following:

Add_executable (Hello_node src/hello.cpp)                      target_link_libraries (Hello_node ${catkin_libraries})         

2.2. Now it's time to start writing code. A Ros code that belongs to me.

Vim  Hello_node.cpp

The code is as follows: The code is simple, I hope you digest

#include"Ros/ros.h"//Add the Ros core header file#include"geometry_msgs/twist.h"//contains geometry_msgs::twist message header file #include<stdlib.h>intMainintargcChar**argv) {Ros::init (ARGC,ARGV,"Publish_zhouxuewei");    Initialize ROS node Ros::nodehandle node_handle; Ros::P ublisher Pub= Node_handle.advertise<geometry_msgs::twist> ("Turtle1/cmd_vel", +); Ros::rate Loop_rate (Ten);  while(Ros::ok ()) {geometry_msgs::twist msg; Msg.linear.x=Double(rand ())/Double(Rand_max); Assigning a value to a variable in a message Msg.angular.z=2*Double(rand ())/Double(Rand_max)-1;            Pub.publish (msg); Publish Message Ros_info_stream ("sending random velocity command!");    Ros Adjustable log output loop_rate.sleep (); }}

2.3. Compile the code: in the top-level directory of your workspace:

Catkin_make

If there is no error, everything is fine, and you will see the corresponding output:

2.4. Test the Code: (This program, as you can see, generates a random number between 1 and 1, controlling Turtlesim movement)

Roscorerosrun Turtlesim Turtlesim_noderosrun zxwtest_package Hello_node

View the node block diagram:

Rqt_graph

3.0. Below teaches you how to subscribe to the message posted on the topic by the node, the following code subscribes to the message on the Turtle1/pose topic. and output using Ros logs.

3.1. Create a new listen_node.cpp in ZXWTEST_PACKAGE/SRC

The code is as follows:

#include <ros/ros.h>#include<turtlesim/Pose.h>#include<iomanip>voidPosemessagereceived (ConstTurtlesim::P ose&msg) {Ros_info_stream (Std::setprecision (2) << std::fixed<<"position= ("<< msg.x <<","<< msg.y <<")"<<"*direction="<<msg.theta);}intMainintArgvChar**argc) {Ros::init (ARGV,ARGC,"Listener_pose");    Ros::nodehandle Node_handle; Ros::subscriber Sub= Node_handle.subscribe ("Turtle1/pose", +,&posemessagereceived);    Ros::spin (); return 0;}

3.2. Compile the code. The first thing is to modify the compilation properties, through Vim or your favorite editor to open the CMakeLists.txt file in the Zxwtest_package directory, load their own compilation options, modified as follows:

Add_executable (Listen_node src/listen_node.cpp)                      target_link_libraries (Listen_node.cpp ${catkin_LIBRARIES })  

To the top-level directory of your workspace:

Catkin_make

If there is no error, everything is fine, and you will see the corresponding output:

Built Target Listen_node

3.3. Test the code:

Roscorerosrun Turtlesim Turtlesim_node
Rosrun Turtlesim Turtle_teteop_keyrosrun zxwtest_package Listen_node

When you control the Turtlrsim via the Keyboard control node, the corresponding output is as follows:

View the node block diagram:

Rqt_graph

4.0. The following procedures in the Ros robot application is very much, PC end through the serial port data to the terminal execution. We subscribe to the Turtlesim moving angular velocity and line speed information on the/turtle1/cmd_vel topic, which is distributed to our robot, so that he can also follow the Turtlesim through the keyboard to control the movement.

4.1. Create a new send_serial_node.cpp in ZXWTEST_PACKAGE/SRC

The code is as follows:

#include <ros/ros.h>#include"geometry_msgs/twist.h"#include<iomanip>#include<stdio.h>#include<string.h>#include<unistd.h>#include<fcntl.h>#include<errno.h>#include<termios.h>voidSerial_send_data (Constgeometry_msgs::twist&msg) {        intI,fd,iret; structTermios options_old, options; Charbuf[2]; buf[0] = (Char) msg.linear.x; buf[1] = (Char) Msg.angular.z; printf ("buf[0] =%d\n", buf[0]); printf ("buf[1] =%d\n", buf[1]); FD= Open ("/dev/ttyusb0", O_RDWR | O_noctty |o_ndelay); if(FD <0) {printf ("[%s-%d] Open Error!!! \ n", __file__, __line__); } fcntl (FD, F_SETFL,0); /*********************************************************/tcgetattr (FD,&options); Options_old=options; Options.c_lflag&= ~ (Icanon | ECHO | Echoe | ISIG);/*Input*/Options.c_oflag&= ~opost;/** Select Original output **/        /** * Set the new options for the port ... **/tcsetattr (FD, Tcsanow,&options); /*********************************************************/IRet= Write (fd, &AMP;BUF,sizeof(BUF)); if(IRet <0) {printf ("[%s-%d] Write error!!! \ n", __file__, __line__); } tcsetattr (FD, Tcsanow,&options_old); Close (FD);}intMainintArgvChar**argc) {Ros::init (ARGV,ARGC,"Serial_send");    Ros::nodehandle Node_handle; Ros::subscriber Sub= Node_handle.subscribe ("/turtle1/cmd_vel", +,&serial_send_data);    Ros::spin (); return 0;}

4.2. Compile the code. The first thing is to modify the compilation properties, through Vim or your favorite editor to open the CMakeLists.txt file in the Zxwtest_package directory, load their own compilation options, modified as follows:

Add_executable (Send_serial_node src/send_serial_node.cpp)                      target_link_libraries (Send_serial_node.cpp ${ Catkin_libraries})  

To the top-level directory of your workspace:

Catkin_make

If there is no error, everything is fine, and you will see the corresponding output:

Built Target Send_serial_node

4.3. Test the code:

Roscorerosrun Turtlesim Turtlesim_node
Rosrun Turtlesim Turtle_teteop_keyrosrun zxwtest_package Send_serial_node

Connect your computer and your robot through the serial port, in the keyboard control node Terminal mobile robot, the PC-side output is as follows:

The robot side also moves after receiving the data.

View the node block diagram:

Rqt_graph

Monitor the Turtlesim emulator to send data to the actual robot--20

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.