2017-06-01 38 views
1

我正在從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的初始值並從命令行更改它。

然而,當我運行的啓動文件,我得到以下錯誤:

link to error

也都角速度和線速度從vel_filtervel_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(); 
} 

回答

0

在代碼中有兩個錯誤。

1.)您收到的錯誤在那裏,因爲您正在監聽錯誤的消息類型。在turtlesim documentation你可以看到你正在聽的vel_printer的話題並不型geometry_msgs的扭曲::作爲你寫的,但類型turtlesim/Pose的。這是你錯誤的原因。另外我建議你使用ConstPtr來接收消息。因此,對於龜姿態回調函數應該是:

void printVelocityReceived(const turtlesim::Pose::ConstPtr& msg){ ... do your magic ... }

2)的參數。在啓動文件中,您將參數max_ang_vel發佈到參數服務器。因爲您在標籤內部執行此操作,因此您將該標籤設爲該節點的專用標籤。爲了讀取節點的私有參數,您需要指定另一個節點句柄,即私有節點句柄。然後您可以使用node handle's functions訪問參數。的這個

實施例:

int main(int argc, char** argv) 
{ 
    ros::init(argc, argv, "test"); 
    ros::NodeHandle nh; // public node handle 
    ros::NodeHandle nh_privat("~"); // private node handle 

    // Get the parameter. 
    nh_privat.param("max_ang_vel", maxAngVel, 0.0); 
    // first argument: name of the parameter on the parameter server 
    // second argument: variable to save it to 
    // third argument: default value if the parameter is not set 
} 

和用於從控制檯改變maxAngVel,可以只使用標準的C++和讀出的輸入到控制檯,然後相應地改變變量。

+0

你好maetulj。感謝您的答覆。第二部分似乎現在工作,但第一部分給出了一個錯誤:a)在Pose ::後被顯示。我該如何解決這個問題? – user7845839

+0

hey @ user7845839你可能會寫出編譯器的完整錯誤嗎?因爲我檢查了turtlesim :: Pose應該有ConstPtr定義... – maetulj