ROS learning 02 -Guangdong

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/
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值