Ros clients provide some column library files for user development.
Roscpp Client (c + + client)
Create a new package first
And then catkin_make a bit.
Under the new package directory, create a message file msg
Then we need to generate the message foo.h so that we can call it later, we go into the package.xml inside to modify,
And then go back to CMakeLists.txt.
Compile it below,
Let's write the code below:
We've written the make file before, and look at
With this file, we can open the project in Eclipse and execute the file first:
Open Eclipse below
Then File->import,
Below to write a source file in Src, called move_publisher.cpp
and write a move_subscriber.cpp.
first of all to control Turtlesim, his topic topic is/turtle1/cmd_vel message type Geometry_msgs/twist.
Write a publisher below
/* * Move_publisher.cpp *
* Created On:jan 16, 2017
* Author:gary * *
#include <ros/ros.h>//header file for messages
#include <geometry_msgs/Twist.h>
Be sure to add a command line argument, argc refers to the number of arguments
argv for the specific parameters
int main (int argc,char **argv)
{
Initialize, Move_publisher for node name
Ros::init (ARGC,ARGV, "Move_publisher");
Define a handle
Ros::nodehandle N;
Defines a Publisher object pub, specifying the desired type of geometry_msgs::twist
The published topic is turtle1/cmd_vel and the buffer size is Ros::P ublisher pub=n.advertise<geometry_msgs::twist> ("Turtle1/cmd_vel", 1000);
Instantiate a Message object
Geometry_msgs::twist tw;
and assign it to a value of angular velocity, respectively.
tw.angular.z=2;
tw.angular.x=0;
tw.angular.y=0;
Line Speed Assignment
tw.linear.x=2;
tw.linear.y=0;
tw.linear.z=0;
The message structure above has been defined, and the message is published below
Specify the frequency of the release, allowing the loop frequency to be 10HZ at the moment
Ros::rate rate (10); Ros::ok () If you press CTRL + C, it will return a value of false
while (Ros::ok ())
{//Specify the message to be published
Pub.publish (TW);
Spin () is a fixed wait message here, into the loop
Spin_once () loop once execution callback function
Ros::spin (); To be changed to Ros::spinonce () to run correctly
}
Rate.sleep ();
}
Modify the CMakeLists.txt below,
Add a row
Add_executable (Move_publisher src/move_publisher.cpp)
And
Target_link_libraries (Move_publisher
${catkin_libraries}
)
Save it, compile it below,
Catkin_make, after compiling correctly, run
Rosrun Turtle_move Move_publisher
Then write a subscriber:
Add the new message to the Subscriber and write the publisher and receiver together.
Then modify the CMakeLists.txt
Add the following:
Add_executable (Move_subscriber src/move_subscriber.cpp)
Target_link_libraries (Move_publisher
${catkin_libraries}
)
To compile again
Catkin_make, no problem, and then run it right.
Then write a timer:
Similar to creating a topic subscription
And then change it in the CMakeLists.txt,
Tim Plus
Add_executable (Timer src/timer.cpp)
Target_link_libraries (Timer
${catkin_libraries}
)
It is then compiled correctly, then run, and the results are correct.
Lesson four 4, Ros client