Article Directory
roscpp different demo demo
topic_demo
The basic model is similar, you can refer to the template.
Function Description: two Node, a release message simulated GPS (custom format, including coordinates and operating state), the message is accepted and processed further (to calculate the distance from the origin).
step:
- package
- msg
- talker.cpp
- listener.cpp
- CMakeList.txt & package.xml
1. package
$ cd ~/catkin_ws/src
$ catkin_create_pkg topic_demo roscpp rospy std_msgs
2. msg
$ cd topic_demo/
$ mkdir msg
$ cd msg
$ vi gps.msg
# gps.msg
float32 x
float32 y
string state
When you finish creating msg, the compiler generates a ~/catkin_ws/devel/include/topic_demo/gps.h
file. Write code include into it
#include<topic_demo/gps.h>
topic_demo::gps msg;
3. talker.cpp
#include <ros/ros.h>
#include <topic_demo/gps.h>
int main(int argc, char** argv){
ros::init(argc, argv, "talker" ); //解析参数,命名节点
ros::NodeHandle nh; //创建句柄, 实例化node
topic_demo::gps msg; //创建gps消息
msg.x = 1.0;
msg.y = 1.0;
msg.state = "working";
ros::Publisher pub = nh.advertise<topic_demo::gps>("gps_info", 1); //用句柄操作创建publisher,<需要publish的类型>
ros::Rate loop_rate(1.0); //定义循环发布的频率
while(ros::ok()){
msg.x = 1.03 * msg.x;
msg.y = 1.01 * msg.y;
ROS_INFO( "Talker: GPS: x = %f, y = %f", msg.x , msg.y);
pub.publish(msg); //发布消息
loop_rate.sleep();
}
return 0;
}
4. listener.cpp
#include <ros/ros.h>
#include <topic_demo/gps.h>
#include <std_msgs/Float32.h>
void gpsCallback(const topic_demo::gps::ConstPtr &msg) //ConstPtr &msg定义于gps.h文件中
{
std_msgs::Float32 distance;
distance.data = sqrt(pow(msg ->x, 2), pow(msg -> y, 2));
ROS_INFO( " Listener: Distance to origin = %f, state = %s" , distanace.data, msg -> state.c_str() );
}
int main(int argc, char** argv){
ros::init(argc, argv, " listener");
ros::NodeHandle n;
ros::Subscribe sub = n.subscribe( "gps_info", 1, gpsCallback); // 'gpsCallback'为该回调函数的指针,接受到消息后会通过该函数处理
ros::spin(); //反复调用当前可触发的回调函数(阻塞)
return 0;
}
Tips: ros :: spinOnce () action with spin, but it is non-blocking.
5. CMakeLists.txt
Then you can compile (catkin_make) and run (rosrun) a ~
service_demo
The basic model is similar, you can refer to the template.
Function Description: two Node, a release request (custom format), other receive and process the message and return information.
step:
- package
- msg
- server.cpp
- client.cpp
- CMakeList.txt & package.xml
1. package
$ cd ~/catkin_ws/src
$ catkin_create_pkg service_demo roscpp rospy std_msgs
2. srv
$ cd service_demo/
$ mkdir srv
$ vi Greeting.srv
# Greeting.srv
string name
int32 age
---
string feedback
When you finish creating srv, the compiler generates ~/catkin_ws/devel/include/service_demo/Greeting.h
, .../GreetingRequest.h
, .../GreetingResponse.h
file. Write code include into it
3. server.cpp
#include <ros/ros.h>
#include <service_demo/Greeting.h>
bool handle_function(service::demo::Greeting::Request &req, service::demo::Greeting::Response &res){
//显示请求信息
ROS_INFO( "Request from %s with age %d" . req.name.c_str(), req.age();
//处理请求,结果写入response
res.feedback = "Hi" + req.name + ". I' m server!" '
//返回true,正确处理了请求
return true;
}
int main(int argc, char** argv){
ros::init(argc, argv, "greetings_server" ); //解析参数,命名节点
ros::NodeHandle nh; //创建句柄, 实例化node
ros::ServiceServer service = nh.advertiseService("greetings",handle_function); //提供服务,后者同样是函数指针。
ros::spin();
return 0;
}
4. client.cpp
#include <ros/ros.h>
#include <service_demo/Greeting.h>
int main(int argc, char** argv){
ros::init(argc, argv, " greetings_server" ); //解析参数,命名节点
ros::NodeHandle nh;
ros::ServiceClient client = nh.serviceClient<service_demo::Greeting>( "greetings" ); //“greetings”是发送的服务名
service_demo::Greeting srv;
srv.request.name = "HAN" ;
srv.request.age = "20" ;
if (client.call(srv)){ //call 的返回值就是上面Handle_function 的返回值。
ROS_INFO( " Feedback from server : %s." , srv.response.feedback );
}
else{
ROS_ERROR ( "Failed to call service greetings.") ;
return 1;
}
return 0;
}
5. CMakeLists.txt & package.xml
Then you can compile (catkin_make) and run (rosrun) a ~
param_demo
Two kinds of API: ros :: param and ros :: NodeHandle
Setting the parameters of ways:
- In the code
#include <ros/ros.h>
int main(int argc, char** argv){
ros::init(argc,argv, "greetings_server");
ros::NodeHandle nh;
int param1, param2, param3, param4, param5;
//获取参数
ros::param::get ("param1", param1);
nh.getParam ("param2", param2);
nh.param ("param3", param3, 123); //123是默认值
//设置参数
ros::param::set( "param4" , param4);
nh.setParam("param5", param5);
//检查参数是否存在
ros::param::has("param5");
nh.hasParam("param6");
//删除参数
ros::param::del("param5");
nh.deleteParam("param6");
return 0;
}
- In the launch file
Please indicate the source.
This paper summarizes the Chinese University of MOOC "Android OS Getting Started"
link: Link .
Pictures from the video capture program