我正在從ROS的turtlesim教程中進行分配。使用roslaunch設置參數的初始值
的啓動文件ex74.launch
啓動4個節點:
-the turtlesim node (animates the movement of the turtle)
- pubvel (publishes random angular and linear velocity commands)
- vel_filter (subscribes to the topic on which pubvel publishes. This node filters the angular velocities and publishes only the messages with an angular velocity smaller than the parameter max_ang_vel)
- vel_printer (prints the filtered velocities)
的腳本和啓動文件在我的問題的最後給出。
目標是設置max_ang_vel
的初始值並從命令行更改它。
然而,當我運行的啓動文件,我得到以下錯誤:
也都角速度和線速度從vel_filter
和vel_printer
均爲0
誰能幫我解決這個?
在此先感謝!
ex74.launch
<launch>
<node
pkg="turtlesim"
type="turtlesim_node"
name="turtlesim"
/> ## launch animation of turtle
<node
pkg="me41025_74"
type="pubvel"
name="publish_velocity"
required="true"
launch-prefix="xterm -e"
output="screen"
/> ## launch pubvel
<node
pkg="me41025_74"
type="vel_filter"
name="filter_velocity"
required="true"
launch-prefix="xterm -e"
output="screen"
> ## launch vel_filter
<param name="max_ang_vel" value="0.1" />
</node>
<node
pkg="me41025_74"
type="vel_printer"
name="print_velocity"
launch-prefix="xterm -e"
output="screen"
/> ## launch vel_printer
</launch>
pubvel
#include <ros/ros.h>
#include <geometry_msgs/Twist.h>
#include <stdlib.h>
int main(int argc, char**argv){
ros::init(argc, argv, "publish_velocity");
ros::NodeHandle nh;
ros::Publisher pub = nh.advertise<geometry_msgs::Twist>("turtle1/cmd_vel", 1000);
srand(time(0));
ros::Rate rate(2);
int count_pubvel = 1;
while(ros::ok()){
geometry_msgs::Twist msg;
msg.linear.x = double(rand())/double(RAND_MAX);
msg.angular.z = 2*double(rand())/double(RAND_MAX) - 1;
pub.publish(msg);
ROS_INFO_STREAM("Sending random velocity command:"<<" linear="<<msg.linear.x<<" angular="<<msg.angular.z);
rate.sleep();
}
}
vel_filter
#include <ros/ros.h>
#include <geometry_msgs/Twist.h>
#include <turtlesim/Pose.h>
#include <stdlib.h>
float linx, angZ;
void filterVelocityCallback(const geometry_msgs::Twist& msg){
//Using the callback function just for subscribing
//Subscribing the message and storing it in 'linx' and 'angZ'
linx = msg.linear.x; angZ = msg.angular.z;
}
int main(int argc, char **argv){
ros::init(argc, argv, "filter_velocity");
ros::NodeHandle nh;
ros::Subscriber sub = nh.subscribe("turtle1/cmd_vel",1000,&filterVelocityCallback);
ros::Publisher pub = nh.advertise<geometry_msgs::Twist>("turtle1/pose", 1000);
const std::string PARAM_NAME = "~max_ang_vel";
double maxAngVel;
bool ok = ros::param::get(PARAM_NAME, maxAngVel);
if(!ok) {
ROS_FATAL_STREAM("Could not get parameter " << PARAM_NAME);
exit(1);
}
ros::Rate rate(2);
while(ros::ok()){
geometry_msgs::Twist msg;
msg.linear.x = linx; msg.angular.z = angZ;
if (angZ < maxAngVel){
ROS_INFO_STREAM("Subscriber velocities:"<<" linear="<<linx<<" angular="<<angZ);
pub.publish(msg);
}
rate.sleep();
}
}
vel_printer
#include <ros/ros.h>
#include <geometry_msgs/Twist.h>
#include <iomanip>
void printVelocityReceived(const geometry_msgs::Twist& msg){
ROS_INFO_STREAM("Filtered velocities:"<<" linear="<<msg.linear.x<<" angular="<<msg.angular.z);
}
int main(int argc, char **argv){
ros::init(argc, argv, "print_velocity");
ros::NodeHandle nh;
ros::Subscriber sub = nh.subscribe("turtle1/pose",1000,&printVelocityReceived);
ros::spin();
}
你好maetulj。感謝您的答覆。第二部分似乎現在工作,但第一部分給出了一個錯誤:a)在Pose ::後被顯示。我該如何解決這個問題? – user7845839
hey @ user7845839你可能會寫出編譯器的完整錯誤嗎?因爲我檢查了turtlesim :: Pose應該有ConstPtr定義... – maetulj