回到tf系列,今天快速帶一下tutorial中的broadcaster和listener,基本上就是把前兩天的小烏龜demo中的code解析,因為關係到tf的原理,所以就來講解一下吧!
broadcaster的部分很簡單,就是實作一個發送tf的node,code如下:
#include <ros/ros.h>
#include <tf/transform_broadcaster.h>
#include <turtlesim/Pose.h>
std::string turtle_name;
void poseCallback(const turtlesim::PoseConstPtr& msg){
static tf::TransformBroadcaster br;
tf::Transform transform;
transform.setOrigin( tf::Vector3(msg->x, msg->y, 0.0) );
tf::Quaternion q;
q.setRPY(0, 0, msg->theta);
transform.setRotation(q);
br.sendTransform(tf::StampedTransform(transform, ros::Time::now(), "world", turtle_name));
}
int main(int argc, char** argv){
ros::init(argc, argv, "my_tf_broadcaster");
if (argc != 2){ROS_ERROR("need turtle name as argument"); return -1;};
turtle_name = argv[1];
ros::NodeHandle node;
ros::Subscriber sub = node.subscribe(turtle_name+"/pose", 10, &poseCallback);
ros::spin();
return 0;
};
解析一下code的部分,
main的部分很簡單,就是建立一個會subscribe烏龜的pose的node,因此可以著重在callback function:
1 static tf::TransformBroadcaster br;
2 tf::Transform transform;
3 transform.setOrigin( tf::Vector3(msg->x, msg->y, 0.0) );
4 tf::Quaternion q;
5 q.setRPY(0, 0, msg->theta);
6 transform.setRotation(q);
7 br.sendTransform(tf::StampedTransform(transform, ros::Time::now(), "world", turtle_name));
首先就是先呼叫tf::TransformBroadcaster物件,並且在2-6行把從烏龜那邊收到的msg的pose丟入tf::Transform物件內,做一個2d轉3d的動作,最後再把轉完的tf送出來。
最後執行的結果可以透過tf_echo看到:
$ rosrun tf tf_echo /world /turtle1
就可以看到tf顯示小烏龜的座標跟頭朝的方向。
接下來的listener也很簡單,就是實作第二隻小烏龜去跟著前一隻烏龜的部分:
#include <ros/ros.h>
#include <tf/transform_listener.h>
#include <geometry_msgs/Twist.h>
#include <turtlesim/Spawn.h>
int main(int argc, char** argv){
ros::init(argc, argv, "my_tf_listener");
ros::NodeHandle node;
ros::service::waitForService("spawn");
ros::ServiceClient add_turtle =
node.serviceClient<turtlesim::Spawn>("spawn");
turtlesim::Spawn srv;
add_turtle.call(srv);
ros::Publisher turtle_vel =
node.advertise<geometry_msgs::Twist>("turtle2/cmd_vel", 10);
tf::TransformListener listener;
ros::Rate rate(10.0);
while (node.ok()){
tf::StampedTransform transform;
try{
listener.lookupTransform("/turtle2", "/turtle1",
ros::Time(0), transform);
}
catch (tf::TransformException &ex) {
ROS_ERROR("%s",ex.what());
ros::Duration(1.0).sleep();
continue;
}
geometry_msgs::Twist vel_msg;
vel_msg.angular.z = 4.0 * atan2(transform.getOrigin().y(),
transform.getOrigin().x());
vel_msg.linear.x = 0.5 * sqrt(pow(transform.getOrigin().x(), 2) +
pow(transform.getOrigin().y(), 2));
turtle_vel.publish(vel_msg);
rate.sleep();
}
return 0;
};
一樣解析一下code的部分,第一行一樣是宣告tf::TransformListener的物件,在這個物件被宣告以後,就會開始接收tf的資料:
1 tf::TransformListener listener;
2 // 略
3 try{
4 listener.lookupTransform("/turtle2", "/turtle1",
5 ros::Time(0), transform);
6 }
7 // 略
8 geometry_msgs::Twist vel_msg;
9 vel_msg.angular.z = 4.0 * atan2(transform.getOrigin().y(),
10 transform.getOrigin().x());
11 vel_msg.linear.x = 0.5 * sqrt(pow(transform.getOrigin().x(), 2) +
12 pow(transform.getOrigin().y(), 2));
13 turtle_vel.publish(vel_msg);
3-6行就是在做tf最主要的工作,就是把turtle1的frame對應到turtle2上,並且拿到最新的時間收到的tf(time=0),並且把值丟到transform內。
8-12行就是用transform來計算二號烏龜接下來要走的速度與角速度,並將結果發送出去,這樣二號烏龜就會跟著一號烏龜走囉!
http://wiki.ros.org/tf/Tutorials/Writing%20a%20tf%20broadcaster%20%28C%2B%2B%29
http://wiki.ros.org/tf/Tutorials/Writing%20a%20tf%20listener%20%28C%2B%2B%29