URDF、Xacro编写及介绍及其在RVIZ中显示与控制

文章目录

  • URDF文件:
    • 文件内容:
    • 检查urdf语法:
    • urdf图形化界面命令:
    • launch文件:
  • Xacro文件:
    • Xacro文件解释:(参考书本)
  • 用代码移动机器人:
    • robot_learning_test包的依赖:
    • state_publisher.cpp
    • 修改cmakelists.txt文件:
    • 编译运行:
    • 查看话题消息的发布:
    • 代码解释:
  • URDF文件转Xacro文件、在rviz和gazebo中同时启动
  • 自己编写的urdf文件及资料包:

URDF文件:

移动小车,包括四个轮子+一个手臂+一个夹具

文件内容:

描述机器人几何关系的两个主要方面:links and joints。

<robot name="origins">
	<link name="base_link">
   		<visual>
     			<geometry>
       				<box size="0.2 .3 .1"/>
     			</geometry>
			<origin rpy="0 0 0" xyz="0 0 0.05"/>
			<material name="white">
				<color rgba="1 1 1 1"/>
			</material>
   		</visual>
 	</link>

link:几何定义,材料定义,原点定义

link定义完成后需要定义joint

 	<joint name="base_to_wheel1" type="fixed">
   		<parent link="base_link"/>
   		<child link="wheel_1"/>
   		<origin xyz="0 0 0"/>
 	</joint>

joint:关节连接类型(fixed , revolute , continuous , floating , or planar)、父、子关系、
当关节类型选为revolute:需要添加limit:
例如:

<joint name="arm_1_to_arm_base" type="revolute">
		<parent link="arm_base"/>
		<child link="arm_1"/>
		<axis xyz="1 0 0"/>
		<origin xyz="0 0 0.15"/>
		<limit effort ="1000.0" lower="-1.0" upper="1.0" velocity="0.5"/>
	</joint>

effort (maximum force supported by the joint);
lower to assign the lower limit of a joint (radian per revolute joints, meters for prismatic joints);
upper for the upper limit;
velocity for enforcing the maximum joint velocity;

检查urdf语法:

URDF、Xacro编写及介绍及其在RVIZ中显示与控制_第1张图片

urdf图形化界面命令:

URDF、Xacro编写及介绍及其在RVIZ中显示与控制_第2张图片
URDF、Xacro编写及介绍及其在RVIZ中显示与控制_第3张图片

launch文件:

<launch>
<arg name="model" />
<arg name="gui" default="true" />
<param name="robot_description" textfile="$(arg model)" />
<param name="use_gui" value="$(arg gui)" />
<node name="joint_state_publisher" pkg="joint_state_publisher" type="joint_state_publisher" />
<node name="robot_state_publisher" pkg="robot_state_publisher" type="state_publisher" />
</launch>

运行命令

roslaunch robot_learning_test robot1.launch model:="`rospack find robot_learning_test`/robot_description/urdf/robot1.urdf"

也可以将urdf文件路径写到launch文件里:

<arg name="model" default="/home/ma/catkin_ws1/src/robot_learning_test/robot_description/urdf/robot1.urdf" />

修改完的运行命令为:

roslaunch robot_learning_test robot1.launch

URDF、Xacro编写及介绍及其在RVIZ中显示与控制_第4张图片
URDF、Xacro编写及介绍及其在RVIZ中显示与控制_第5张图片

Xacro文件:

比URDF的机器人描述文件更好

Xacro文件解释:(参考书本)

URDF、Xacro编写及介绍及其在RVIZ中显示与控制_第6张图片
URDF、Xacro编写及介绍及其在RVIZ中显示与控制_第7张图片

URDF、Xacro编写及介绍及其在RVIZ中显示与控制_第8张图片
URDF、Xacro编写及介绍及其在RVIZ中显示与控制_第9张图片

用代码移动机器人:

robot_learning_test包的依赖:

URDF、Xacro编写及介绍及其在RVIZ中显示与控制_第10张图片

state_publisher.cpp

在/home/ma/catkin_ws1/src/robot_learning_test/src下
新建state_publisher.cpp

#include 
#include 
#include 
#include 

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);
    tf::TransformBroadcaster broadcaster;
    ros::Rate loop_rate(30);

    const double degree = M_PI/180;

    // robot state
    double inc= 0.005, base_arm_inc= 0.005, arm1_armbase_inc= 0.005, arm2_arm1_inc= 0.005;
    double angle= 0 ,base_arm = 0, arm1_armbase = 0, arm2_arm1 = 0;
    // message declarations
    geometry_msgs::TransformStamped odom_trans;
    sensor_msgs::JointState joint_state;
    odom_trans.header.frame_id = "odom";
    odom_trans.child_frame_id = "base_link";

    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] ="base_to_arm_base";
        joint_state.position[0] = base_arm;
        joint_state.name[1] ="arm_1_to_arm_base";
        joint_state.position[1] = arm1_armbase;
        joint_state.name[2] ="arm_2_to_arm_1";
        joint_state.position[2] = arm2_arm1;
	
        // update transform
        // (moving in a circle with radius)
        odom_trans.header.stamp = ros::Time::now();
        //odom_trans.transform.translation.x = cos(angle);
      //  odom_trans.transform.translation.y = sin(angle);
      //  odom_trans.transform.translation.z = 0.0;
        odom_trans.transform.rotation = tf::createQuaternionMsgFromYaw(angle);

        //send the joint state and transform
        joint_pub.publish(joint_state);
        broadcaster.sendTransform(odom_trans);
	// Create new robot state
        arm2_arm1 += arm2_arm1_inc;
        if (arm2_arm1<-1.5 || arm2_arm1>1.5) arm2_arm1_inc *= -1;
		  arm1_armbase += arm1_armbase_inc;
        if (arm1_armbase>1.2 || arm1_armbase<-1.0) arm1_armbase_inc *= -1;
        base_arm += base_arm_inc;
        if (base_arm>1. || base_arm<-1.0) base_arm_inc *= -1;
		
		  angle += degree/8;

        // This will adjust as needed per iteration
        loop_rate.sleep();
    }


    return 0;
}

修改cmakelists.txt文件:

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

编译运行:

catkin_make
roslaunch robot_learning_test robot1.launch
rviz
rosrun robot_learning_test state_publisher

URDF、Xacro编写及介绍及其在RVIZ中显示与控制_第11张图片

查看话题消息的发布:

 rostopic list 
 rostopic echo /joint_states 
 

URDF、Xacro编写及介绍及其在RVIZ中显示与控制_第12张图片
URDF、Xacro编写及介绍及其在RVIZ中显示与控制_第13张图片

代码解释:

URDF、Xacro编写及介绍及其在RVIZ中显示与控制_第14张图片
URDF、Xacro编写及介绍及其在RVIZ中显示与控制_第15张图片
URDF、Xacro编写及介绍及其在RVIZ中显示与控制_第16张图片
待添加
20190425
20190426
20190503

URDF文件转Xacro文件、在rviz和gazebo中同时启动

教程:
https://blog.csdn.net/answerMack/article/details/89791263

自己编写的urdf文件及资料包:

链接:https://download.csdn.net/download/answermack/11157874
URDF、Xacro编写及介绍及其在RVIZ中显示与控制_第17张图片

你可能感兴趣的:(Ubuntu学习)