msg
• Header.msg
• sequence: basic counter
• time stamp: time of publication
• frame ID: robot coordinate frame for data
add xml when you want to use .cpp with Defining Variable-Length Messages
<run_depend>custom_msgs</run_depend>
<build_depend>custom_msgs</build_depend>
and add this when using self-defined msg
<build_depend>message_generation</build_depend><run_depend>message_runtime</run_depend>
the publisher
#include <ros/ros.h>
//next line requires a dependency on custom_msgs within package.xml
#include <custom_msgs/VecOfDoubles.h> //this is the message type we are testing
int main(int argc, char **argv) {
ros::init(argc, argv, "vector_publisher"); // name of this node
ros::NodeHandle n; // two lines to create a publisher object that can talk to ROS
ros::Publisher my_publisher_object = n.advertise<custom_msgs::VecOfDoubles>("vec_topic", 1);
custom_msgs::VecOfDoubles vec_msg; //create an instance of this message type
double counter=0;
ros::Rate naptime(1.0); //create a ros object from the ros “Rate” class; set 1Hz rate
vec_msg.dbl_vec.resize(3); //manually resize it to hold 3 doubles
//After setting the size, one can access elements of this array conventionally, e.g.
vec_msg.dbl_vec[0]=1.414;
vec_msg.dbl_vec[1]=2.71828;
vec_msg.dbl_vec[2]=3.1416;
//Alternatively, one can use the vector member function “push_back()” to append data to an existing array, e.g.:
vec_msg.dbl_vec.push_back(counter); // this makes the vector longer, to hold additional data
while(ros::ok()) {
counter+=1.0;
vec_msg.dbl_vec.push_back(counter);//adding another symbol
my_publisher_object.publish(vec_msg);
naptime.sleep();
}
}
vec_msg.dbl_vec.push_back(counter);//adding another symbol
subscriber
#include<ros/ros.h>
#include <custom_msgs/VecOfDoubles.h> //this is the message type we are testing
void myCallback(const custom_msgs::VecOfDoubles& message_holder)
{
std::vector <double> vec_of_doubles = message_holder.dbl_vec; //can copy contents of message to a C++ vector like this
int nvals = vec_of_doubles.size(); //ask the vector how long it is
for (int i=0;i<nvals;i++) {
ROS_INFO("vec[%d] = %f",i,vec_of_doubles[i]); //print out all the values
}
ROS_INFO("\n");
}
int main(int argc, char **argv)
{
ros::init(argc,argv,"vector_subscriber"); //default name of this node
ros::NodeHandle n; // need this to establish communications with our new node
ros::Subscriber my_subscriber_object= n.subscribe("vec_topic",1,myCallback);
ros::spin();
return 0; // should never get here, unless roscore dies
}
actionlib
to show
$ rostopic echo /example_action/result
to cancel
$ rostopic pub /example_action/cancel actionlib_msgs/GoalID ‘{}’
rosparam
WHAT is IN
** jnt1_gains.yaml **
joint1_gains: {p: 7.0, i: 8.0, d: 9.0}
.launch
<launch>
<rosparam command="load" file="$(find example_parameter_server)/launch/jnt1_gains.yaml" />
</launch>
.cpp
#include <ros/ros.h>
int main(int argc, char **argv) {
ros::init(argc, argv, "param_reader"); // name of this node will be "minimal_publisher"
ros::NodeHandle nh; // two lines to create a publisher object that can talk to ROS
double P_gain,D_gain,I_gain;
if (nh.getParam("/joint1_gains/p", P_gain)) {
ROS_INFO("proportional gain set to %f",P_gain);
}
else
{
ROS_WARN("could not find parameter value /joint1_gains/p on parameter server");
}
if (nh.getParam("/joint1_gains/d", D_gain)) {
ROS_INFO("derivative gain set to %f",D_gain);
}
else
{
ROS_WARN("could not find parameter value /joint1_gains/d on parameter server");
}
if (nh.getParam("/joint1_gains/i", I_gain)) {
ROS_INFO("integral gain set to %f",I_gain);
}
else
{
ROS_WARN("could not find parameter value /joint1_gains/i on parameter server");
}
}
in termimal
rosparam get /joint1_gains/