1 創建一個package
catkin_create_pkg myurdf joint_state_controller robot_state_publisher roscpp std_msgs tf
2 創建urdf文件夾
cd myurdf
mkdir urdf
3 創建urdf文件
gedit myfirstrobot.urdf
輸入以下內容,並保存:
<?xml version="1.0"?>
<robot name="myfirstrobot">
<!-- Base Link -->
<link name="base_link">
<visual>
<geometry>
<box size="0.1 0.1 2"/>
</geometry>
</visual>
</link>
</robot>
4 創建launch文件
cd myurdf
mkdir launch
gedit display.launch
輸入並保存以下內容:
<launch>
<param name="robot_description"
textfile="$(find myurdf)/urdf/myfirstrobot.urdf"/>
<node name="robot_state_publisher" pkg="robot_state_publisher" type="state_publisher"/>
<node name="rviz" pkg="rviz" type="rviz" respawn="false" output="screen"/>
</launch>
5 啓動launch文件
roslaunch myurdf display.launch
得到下圖結果:
6 增加一個連桿
更新後的urdf文件如下:
<?xml version="1.0"?>
<robot name="myfirstrobot">
<!-- Base Link -->
<link name="base_link">
<visual>
<geometry>
<box size="0.1 0.1 2"/>
</geometry>
</visual>
</link>
<joint name="joint1" type="continuous">
<parent link="base_link"/>
<child link="middle_link"/>
<origin rpy="0 0 0" xyz="0 0 1"/>
<axis xyz="0 1 0"/>
</joint>
<link name="middle_link">
<visual>
<geometry>
<box size="0.2 0.2 1"/>
</geometry>
</visual>
</link>
</robot>
得到下圖結果:
發現問題:
(1) Global Status: WARN狀態,顯示信息爲No tf data. Actual error: Fixed Frame [base_link] does not exist.
(2) 在TF顯示控件中,並沒優tf tree存在,由於此時機器人模型由兩個杆件組成,因此tf tree 應包含 base_link 和 middle_link.
問題原因:
urdf文件中,joint type=“continuous”,說名該joint爲旋轉型。對於旋轉型關節,必須給出robot_state_publisher 節點所需的sensor_msgs/JointState型topic :joint_states。可參見:robot_state_publisher
解決方案:
以任意方式發佈sensor_msgs/JointState型topic :joint_states。
7 以下是自定義節點發布joint_states
創建state_publisher.cpp
#include <string>
#include <ros/ros.h>
#include <sensor_msgs/JointState.h>
int main(int argc, char** argv) {
ros::init(argc, argv, "state_publisher");
ros::NodeHandle n;
ros::Publisher joint_pub = n.advertise<sensor_msgs::JointState>("joint_states", 1);
ros::Rate loop_rate(30);
const double degree = M_PI/180;
// robot state
double tilt = 0, tinc = degree, swivel=0, angle=0, height=0, hinc=0.005;
// message declarations
sensor_msgs::JointState joint_state;
while (ros::ok()) {
//update joint_state
joint_state.header.stamp = ros::Time::now();
joint_state.name.resize(3);
joint_state.position.resize(3);
joint_state.name[0] ="joint1";
joint_state.position[0] = swivel;
joint_pub.publish(joint_state);
// This will adjust as needed per iteration
loop_rate.sleep();
}
return 0;
};
正確配置CMakelists文件,在此不再贅述,完成後編譯。
8 配置launch文件
<launch>
<param name="robot_description"
textfile="$(find myurdf)/urdf/myfirstrobot.urdf"/>
<node name="robot_state_publisher" pkg="robot_state_publisher" type="state_publisher"/>
<node name="state_publisher" pkg="myurdf" type="state_publisher"/>
<node name="rviz" pkg="rviz" type="rviz" respawn="false" output="screen"/>
</launch>
8 再次啓動launch文件
roslaunch myurdf display.launch
得到如下結果:
發現:
(1) Global Status:狀態正常
(2) tf tree 中包含 base_link 和 middle_link
(3) 顯示窗口顯示base_link和middle_link frame
還可以通過其他方式處理,如通過joint_state_publisher節點,相關處理可參考wiki。
至此,問題解決。