ROS - tf（四）

ROS - tf（三）+https://developer.aliyun.com/article/1585199

7.2 编写代码

cd %TOP_DIR_YOUR_CATKIN_WS%/src
catkin_create_pkg robot_setup_tf roscpp tf geometry_msgs

sudo apt-get install ros-%YOUR_ROS_DISTRO%-navigation-tutorials

7.3 广播变换

#include <ros/ros.h>
int main(int argc, char** argv){
ros::init(argc, argv, "robot_tf_publisher");
ros::NodeHandle n;
ros::Rate r(100);
while(n.ok()){
tf::StampedTransform(
tf::Transform(tf::Quaternion(0, 0, 0, 1), tf::Vector3(0.1, 0.0, 0.2)),
r.sleep();
}
}

#include <tf/transform_broadcaster.h>

tf::TransformBroadcaster broadcaster;

broadcaster.sendTransform(
tf::StampedTransform(
tf::Transform(tf::Quaternion(0, 0, 0, 1), tf::Vector3(0.1, 0.0, 0.2)),
ros::Time::now(),"base_link", "base_laser"));

7.4 使用变换

#include <ros/ros.h>
#include <geometry_msgs/PointStamped.h>
#include <tf/transform_listener.h>
void transformPoint(const tf::TransformListener& listener){
//we'll create a point in the base_laser frame that we'd like to transform to the base_link frame
geometry_msgs::PointStamped laser_point;
//we'll just use the most recent transform available for our simple example
//just an arbitrary point in space
laser_point.point.x = 1.0;
laser_point.point.y = 0.2;
laser_point.point.z = 0.0;
try{
geometry_msgs::PointStamped base_point;
ROS_INFO("base_laser: (%.2f, %.2f. %.2f) -----> base_link: (%.2f, %.2f, %.2f) at time %.2f",
laser_point.point.x, laser_point.point.y, laser_point.point.z,
base_point.point.x, base_point.point.y, base_point.point.z, base_point.header.stamp.toSec());
}
catch(tf::TransformException& ex){
ROS_ERROR("Received an exception trying to transform a point from \"base_laser\" to \"base_link\": %s", ex.what());
}
}
int main(int argc, char** argv){
ros::init(argc, argv, "robot_tf_listener");
ros::NodeHandle n;
tf::TransformListener listener(ros::Duration(10));
//we'll transform a point once every second
ros::Timer timer = n.createTimer(ros::Duration(1.0), boost::bind(&transformPoint, boost::ref(listener)));
ros::spin();
}

#include <tf/transform_listener.h>

void transformPoint(const tf::TransformListener& listener){

//we'll create a point in the base_laser frame that we'd like to transform to the base_link frame
geometry_msgs::PointStamped laser_point;
//we'll just use the most recent transform available for our simple example
//just an arbitrary point in space
laser_point.point.x = 1.0;
laser_point.point.y = 0.2;
laser_point.point.z = 0.0;

try{
geometry_msgs::PointStamped base_point;
ROS_INFO("base_laser: (%.2f, %.2f. %.2f) -----> base_link: (%.2f, %.2f, %.2f) at time %.2f",
laser_point.point.x, laser_point.point.y, laser_point.point.z,
base_point.point.x, base_point.point.y, base_point.point.z, base_point.header.stamp.toSec());
}

catch(tf::TransformException& ex){
ROS_ERROR("Received an exception trying to transform a point from \"base_laser\" to \"base_link\": %s", ex.what());
}

7.5 构建代码

add_executable(tf_broadcaster src/tf_broadcaster.cpp)
target_link_libraries(tf_broadcaster ${catkin_LIBRARIES}) target_link_libraries(tf_listener${catkin_LIBRARIES})

cd %TOP_DIR_YOUR_CATKIN_WS%
catkin_make

7.6 运行代码

roscore

rosrun robot_setup_tf tf_broadcaster

rosrun robot_setup_tf tf_listener

[ INFO] 1248138528.200823000: base_laser: (1.00, 0.20. 0.00) -----> base_link: (1.10, 0.20, 0.20) at time 1248138528.19
[ INFO] 1248138529.200820000: base_laser: (1.00, 0.20. 0.00) -----> base_link: (1.10, 0.20, 0.20) at time 1248138529.19
[ INFO] 1248138530.200821000: base_laser: (1.00, 0.20. 0.00) -----> base_link: (1.10, 0.20, 0.20) at time 1248138530.19
[ INFO] 1248138531.200835000: base_laser: (1.00, 0.20. 0.00) -----> base_link: (1.10, 0.20, 0.20) at time 1248138531.19
[ INFO] 1248138532.200849000: base_laser: (1.00, 0.20. 0.00) -----> base_link: (1.10, 0.20, 0.20) at time 1248138532.19

八、在机器人上使用机器人状态发布器 （robot state publisher）

8.1作为 ROS 节点运行

8.1.1 机器人状态发布器（robot_state_publisher）

• 参数服务器上加载的 urdf xml 机器人描述。
• 以 sensor_msgs/JointState 形式发布关节位置的源。

8.1.1.1 订阅主题

joint_states (sensor_msgs/JointState)

8.1.1.2 参数

urdf xml 机器人描述。可通过 urdf_model::initParam 访问

tf_prefix (string)

publish_frequency (double)

8.1.2 启动文件示例

<launch>
<!-- Load the urdf into the parameter server. -->
<param name="my_robot_description" textfile="\$(find mypackage)/urdf/robotmodel.xml"/>

<node pkg="robot_state_publisher" type="robot_state_publisher" name="rob_st_pub" >
<remap from="robot_description" to="my_robot_description" />
<remap from="joint_states" to="different_joint_states" />
</node>
</launch>

ROS - tf（五）+https://developer.aliyun.com/article/1585203

|
25天前
|

07 ROS的TF坐标管理工具

39 0
|
1月前
|

ROS - tf（五）
ROS - tf（五）
46 0
|
1月前
|

ROS - tf（三）
ROS - tf
15 0
|
1月前
|
Python
ROS - tf（二）
ROS - tf
28 0
|
1月前
|

ROS - tf（一）
ROS - tf（一）
28 0
|

[ROS基础] --- TF坐标转换
[ROS基础] --- TF坐标转换
166 0
|
C++
167 0
|

ROS学习-tf介绍
ROS学习-tf介绍
238 0
|

ROS TF 将传感器数据转换为机器人坐标系下
ROS TF 将传感器数据转换为机器人坐标系下
585 0
|
XML C++ 数据格式
【古月21讲】ROS入门系列（4）——参数使用与编程方法、坐标管理系统、tf坐标系广播与监听的编程实现、launch启动文件的使用方法
【古月21讲】ROS入门系列（4）——参数使用与编程方法、坐标管理系统、tf坐标系广播与监听的编程实现、launch启动文件的使用方法
251 0