机器人——ROS程序设计学习笔记(chapter7)

川长思鸟来 2022-05-17 00:34 327阅读 0赞

书籍名称:《Learning ROS for RoboticsProgramming - Second Edition》Chapter7

参考链接:https://www.cnblogs.com/jinee/p/5047021.html

  1. 一定要记住它使用的是右手坐标系:
    x正方向朝左, y正方向向内, z轴正方向朝上
  2. 构建树结构, 即写link和joint

1.几何模型

  1. <?xml version ="1.0"?>
  2. <robot name = "text1">
  3. <!--body-->
  4. <link name = "base_link">
  5. <visual>
  6. <geometry>
  7. <cylinder length = "0.6" radius = "0.2"/>
  8. <!--圆柱体 长度0.6 半径0.2-->
  9. </geometry>
  10. <material name = "blue">
  11. <color rgba = "0 0 0.8 1"/>
  12. </material>
  13. </visual>
  14. </link>
  15. <!--right leg-->
  16. <link name = "right_leg">
  17. <visual>
  18. <geometry>
  19. <box size = "0.6 .2 .1"/>
  20. <!--六面体长宽高分别为0.6 0.2 0.1-->
  21. </geometry>
  22. <material name = "white">
  23. <color rgba = "1 1 1 1"/>
  24. </material>
  25. </visual>
  26. </link>
  27. <joint name = "base_to_right_leg" type = "fixed">
  28. <parent link = "base_link"/>
  29. <child link = "right_leg"/>
  30. </joint>
  31. </robot>

70

中添加几何体信息

中添加材质 颜色(rgba格式)

2.调整位置,添加另一条腿

  1. <!--right leg-->
  2. <link name = "right_leg">
  3. <visual>
  4. <geometry>
  5. <box size = "0.6 .2 .1"/>
  6. </geometry>
  7. <material name = "white">
  8. <color rgba = "1 1 1 1"/>
  9. </material>
  10. <origin rpy = "0 1.5705 0" xyz = "0 0 -0.3"/>
  11. </visual>
  12. </link>
  13. <joint name = "base_to_right_leg" type = "fixed">
  14. <parent link = "base_link"/>
  15. <child link = "right_leg"/>
  16. <origin xyz = "0.22 0 0.25">
  17. </joint>
  18. <!--left leg-->
  19. <link name = "left_leg">
  20. <visual>
  21. <geometry>
  22. <box size = "0.6 .2 .1"/>
  23. </geometry>
  24. <material name = "white">
  25. <color rgba = "1 1 1 1"/>
  26. </material>
  27. <origin rpy = "0 1.5705 0" xyz = "-0.22 0 -0.05"/>
  28. </visual>
  29. </link>
  30. <joint name = "base_to_left_leg" type = "fixed">
  31. <parent link = "base_link"/>
  32. <child link = "left_leg"/>
  33. <origin xyz = "-0.22 0 0.25">
  34. </joint>

70 1

每个link的参考坐标系都在它的底部,并与关节的参考坐标系正交,为了添加尺寸,需要指定偏移从一个link到它的关节的子link, 这通过添加origin到每个节点解决。
origin表示的是关节相对于父关节的距离和旋转, xyz和rpy(1.5705:90°)

3.添加剩余部位

调整一下,全部代码:

  1. <?xml version ="1.0"?>
  2. <robot name = "text1">
  3. <!--body-->
  4. <link name = "base_link">
  5. <visual>
  6. <geometry>
  7. <cylinder length = "0.6" radius = "0.2"/>
  8. </geometry>
  9. <material name = "blue">
  10. <color rgba = "0 0 0.8 1"/>
  11. </material>
  12. </visual>
  13. </link>
  14. <!--right leg-->
  15. <link name = "right_leg">
  16. <visual>
  17. <geometry>
  18. <box size = "0.6 .2 .1"/>
  19. </geometry>
  20. <material name = "white">
  21. <color rgba = "1 1 1 1"/>
  22. </material>
  23. <origin rpy = "0 1.5705 0" xyz = "0 0 -0.3"/>
  24. </visual>
  25. </link>
  26. <joint name = "base_to_right_leg" type = "fixed">
  27. <parent link = "base_link"/>
  28. <child link = "right_leg"/>
  29. <origin xyz = "0.22 0 .25"/>
  30. </joint>
  31. <!--left leg-->
  32. <link name = "left_leg">
  33. <visual>
  34. <geometry>
  35. <box size = "0.6 .2 .1"/>
  36. </geometry>
  37. <material name = "white">
  38. <color rgba = "1 1 1 1"/>
  39. </material>
  40. <origin rpy = "0 1.5705 0" xyz = "0 0 -0.3"/>
  41. </visual>
  42. </link>
  43. <joint name = "base_to_left_leg" type = "fixed">
  44. <parent link = "base_link"/>
  45. <child link = "left_leg"/>
  46. <origin xyz = "-0.22 0 .25"/>
  47. </joint>
  48. <!--head-->
  49. <link name = "head">
  50. <visual>
  51. <geometry>
  52. <sphere radius = "0.2"/>
  53. </geometry>
  54. <material name = "white"/>
  55. </visual>
  56. </link>
  57. <joint name="base_to_head" type="fixed">
  58. <parent link="base_link"/>
  59. <child link="head"/>
  60. <origin xyz="0 0 0.3"/>
  61. </joint>
  62. <!--right foot-->
  63. <link name = "right_foot">
  64. <visual>
  65. <geometry>
  66. <box size = "0.1 .4 .1"/>
  67. </geometry>
  68. <material name = "yellow">
  69. <color rgba = "1 1 0 1"/>
  70. </material>
  71. </visual>
  72. </link>
  73. <joint name = "leg_to_right_foot" type = "fixed">
  74. <parent link = "right_leg"/>
  75. <child link = "right_foot"/>
  76. <origin xyz = "0 0 -0.65"/>
  77. </joint>
  78. <!--left foot-->
  79. <link name = "left_foot">
  80. <visual>
  81. <geometry>
  82. <box size = "0.1 .4 .1"/>
  83. </geometry>
  84. <material name = "yellow">
  85. <color rgba = "1 1 0 1"/>
  86. </material>
  87. </visual>
  88. </link>
  89. <joint name = "leg_to_left_foot" type = "fixed">
  90. <parent link = "left_leg"/>
  91. <child link = "left_foot"/>
  92. <origin xyz = "0 0 -0.65"/>
  93. </joint>
  94. <!--right foot front wheel-->
  95. <link name = "right_front_wheel">
  96. <visual>
  97. <geometry>
  98. <cylinder length= "0.08" radius = "0.05"/>
  99. </geometry>
  100. <material name = "black">
  101. <color rgba = "0 0 0 1"/>
  102. </material>
  103. </visual>
  104. </link>
  105. <joint name = "right_foot_to_front_wheel" type = "fixed">
  106. <parent link ="right_foot"/>
  107. <child link = "right_front_wheel"/>
  108. <origin xyz = "0 0.14 -0.05" rpy = "0 1.5705 0"/>
  109. </joint>
  110. <!--right foot back wheel-->
  111. <link name = "right_back_wheel">
  112. <visual>
  113. <geometry>
  114. <cylinder length= "0.08" radius = "0.05"/>
  115. </geometry>
  116. <material name = "black">
  117. <color rgba = "0 0 0 1"/>
  118. </material>
  119. </visual>
  120. </link>
  121. <joint name = "right_foot_to_back_wheel" type = "fixed">
  122. <parent link ="right_foot"/>
  123. <child link = "right_back_wheel"/>
  124. <origin xyz = "0 -0.14 -0.05" rpy = "0 1.5705 0"/>
  125. </joint>
  126. <!--left foot front wheel-->
  127. <link name = "left_front_wheel">
  128. <visual>
  129. <geometry>
  130. <cylinder length= "0.08" radius = "0.05"/>
  131. </geometry>
  132. <material name = "black">
  133. <color rgba = "0 0 0 1"/>
  134. </material>
  135. </visual>
  136. </link>
  137. <joint name = "left_foot_to_front_wheel" type = "fixed">
  138. <parent link ="left_foot"/>
  139. <child link = "left_front_wheel"/>
  140. <origin xyz = "0 0.14 -0.05" rpy = "0 1.5705 0"/>
  141. </joint>
  142. <!--left foot back wheel-->
  143. <link name = "left_back_wheel">
  144. <visual>
  145. <geometry>
  146. <cylinder length= "0.08" radius = "0.05"/>
  147. </geometry>
  148. <material name = "black">
  149. <color rgba = "0 0 0 1"/>
  150. </material>
  151. </visual>
  152. </link>
  153. <joint name = "left_foot_to_back_wheel" type = "fixed">
  154. <parent link ="left_foot"/>
  155. <child link = "left_back_wheel"/>
  156. <origin xyz = "0 -0.14 -0.05" rpy = "0 1.5705 0"/>
  157. </joint>
  158. </robot>

70 2

4.添加碰撞collision

碰撞只要将link包裹住就可以了

  1. <!--body-->
  2. <link name = "base_link">
  3. <visual>
  4. <geometry>
  5. <cylinder length = "0.6" radius = "0.2"/>
  6. </geometry>
  7. <material name = "blue">
  8. <color rgba = "0 0 0.8 1"/>
  9. </material>
  10. </visual>
  11. <collision>
  12. <geometry>
  13. <cylinder length = "0.6" radius = "0.2"/>
  14. </geometry>
  15. </collision>
  16. </link>

5.xacro

使用宏,简化代码,顺便添加inertial

这里使用了三个宏:代码如下(中间代码省略)

  1. <?xml version ="1.0"?>
  2. <robot name = "text1" xmlns:xacro="http://www.ros.org/wiki/xacro">
  3. <xacro:property name="wheel_length" value="0.08" />
  4. <xacro:property name="wheel_radius" value="0.05" />
  5. <xacro:macro name="default_inertial" params="mass">
  6. <inertial>
  7. <mass value="${mass}" />
  8. <inertia ixx="1.0" ixy="0.0" ixz="0.0"
  9. iyy="1.0" iyz="0.0"
  10. izz="1.0" />
  11. </inertial>
  12. </xacro:macro>
  13. .
  14. .
  15. .
  16. <!--left foot back wheel-->
  17. <link name = "left_back_wheel">
  18. <visual>
  19. <geometry>
  20. <cylinder length= "${wheel_length}" radius = "${wheel_radius}"/>
  21. </geometry>
  22. <material name = "black">
  23. <color rgba = "0 0 0 1"/>
  24. </material>
  25. </visual>
  26. <collision>
  27. <geometry>
  28. <cylinder length= "${wheel_length}" radius = "${wheel_radius}"/>
  29. </geometry>
  30. </collision>
  31. <xacro:default_inertial mass = "1"/>
  32. </link>
  33. <joint name = "left_foot_to_back_wheel" type = "continuous">
  34. <parent link ="left_foot"/>
  35. <child link = "left_back_wheel"/>
  36. <axis xyz = "0 0 1"/>
  37. <origin xyz = "0 -0.14 -0.05" rpy = "0 1.5705 0"/>
  38. </joint>
  39. </robot>

6.让机器人动起来

让机器人走圆

cpp如下:

  1. #include <string>
  2. #include <ros/ros.h>
  3. #include <sensor_msgs/JointState.h>
  4. #include <tf/transform_broadcaster.h>
  5. int main(int argc, char** argv) {
  6. ros::init(argc, argv, "state_publisher_text");
  7. ros::NodeHandle n;
  8. ros::Publisher joint_pub = n.advertise<sensor_msgs::JointState>("joint_states", 1);
  9. tf::TransformBroadcaster broadcaster;
  10. ros::Rate loop_rate(30);
  11. const double degree = M_PI/180;
  12. // robot state
  13. double inc= 0.005,wheel_1_inc= -0.05,wheel_2_inc= -0.05,wheel_3_inc= -0.07,wheel_4_inc= -0.07; //轮子向后转,机器人向前移动,所以这里是负
  14. double angle= 0,base_head = 0,wheel_1= 0,wheel_2= 0,wheel_3= 0,wheel_4= 0;
  15. // message declarations
  16. geometry_msgs::TransformStamped odom_trans;
  17. sensor_msgs::JointState joint_state;
  18. odom_trans.header.frame_id = "odom";
  19. odom_trans.child_frame_id = "base_link";
  20. while (ros::ok()) {
  21. //update joint_state
  22. joint_state.header.stamp = ros::Time::now();
  23. joint_state.name.resize(9);
  24. joint_state.position.resize(9);
  25. joint_state.name[0] ="base_to_head";
  26. joint_state.position[0] = base_head;
  27. joint_state.name[1] ="base_to_right_leg";
  28. joint_state.position[1] = 0;
  29. joint_state.name[2] ="base_to_left_leg";
  30. joint_state.position[2] = 0;
  31. joint_state.name[3] ="right_leg_to_right_foot";
  32. joint_state.position[3] = 0;
  33. joint_state.name[4] ="left_leg_to_left_foot";
  34. joint_state.position[4] = 0;
  35. joint_state.name[5] ="right_foot_to_front_wheel";
  36. joint_state.position[5] = wheel_1;
  37. joint_state.name[6] ="right_foot_to_back_wheel";
  38. joint_state.position[6] = wheel_2;
  39. joint_state.name[7] ="left_foot_to_front_wheel";
  40. joint_state.position[7] = wheel_3;
  41. joint_state.name[8] ="left_foot_to_back_wheel";
  42. joint_state.position[8] = wheel_4;
  43. // update transform
  44. // (moving in a circle with radius)
  45. //改变二维平面的x,y的坐标,z轴垂直于xOy,故z的坐标是0
  46. odom_trans.header.stamp = ros::Time::now();
  47. odom_trans.transform.translation.x = cos(angle);
  48. odom_trans.transform.translation.y = sin(angle);
  49. odom_trans.transform.translation.z = 0.0;
  50. odom_trans.transform.rotation = tf::createQuaternionMsgFromYaw(angle);
  51. //send the joint state and transform
  52. joint_pub.publish(joint_state);
  53. broadcaster.sendTransform(odom_trans);
  54. // Create new robot state
  55. base_head += inc;
  56. if(base_head< -2.0||base_head> 2.0) base_head *= -1;
  57. wheel_1 += wheel_1_inc;
  58. if(wheel_1< -2.0||wheel_1> 2.0) wheel_1 *= -1;
  59. wheel_2 += wheel_2_inc;
  60. if(wheel_2< -2.0||wheel_2> 2.0) wheel_2 *= -1;
  61. wheel_3 += wheel_3_inc;
  62. if(wheel_3< -2.0||wheel_3> 2.0) wheel_3 *= -1;
  63. wheel_4 += wheel_4_inc;
  64. if(wheel_4< -2.0||wheel_4> 2.0) wheel_4 *= -1;
  65. angle += degree/4;
  66. // This will adjust as needed per iteration
  67. loop_rate.sleep();
  68. }
  69. return 0;
  70. }

70 3

Chapter7部分错误与解决方法:

P275:
问题:执行以下命令时,出现错误提示:

  1. $ check_urdf robot1.urdf
  2. Error: Error document empty.
  3. at line 72 in /build/buildd/urdfdom 0.2.10+dfsg/urdf_parser/src/model.cpp
  4. ERROR: Model Parsing the xml failed

解决:.urdf前添加路径

  1. check_urdf /home/<user_name>/ROS/catkin_ws/src/learning_urdf/urdf/robot1.urdf

P276:
问题:执行$ roslaunch …命令后没有3D模型
解决:<?xml version=”1.0”?>代码段,在倒数第二行添加,注意结尾是 urdf.rviz,否则会报错。

  1. <node name="rviz" pkg="rviz" type="rviz" args="-d $(find urdf_tutorial)/urdf.rviz" />

P277:
问题:在urdf中添加package:

  1. <mesh filename = "package://pr2_description/meshes/gripper_v0/l_finger.dae"/>

编译时一直报错,提示没有找到资源
解决:我将含有.dae的文件夹(meshes)放在”robot1_description“(功能包)中,相对路径改为

  1. <mesh filename = "package://robot1_description/meshes/gripper_v0/l_finger.dae"/>

加载成功。另外urdf中不能含有中文

发表评论

表情:
评论列表 (有 0 条评论,327人围观)

还没有评论,来说两句吧...

相关阅读

    相关 ROS基础学习笔记1

    ROS开发基础部分: 基本文件介绍 1.CMakeLists.txt 规定了catkin编译的规则 例如:源文件,依赖项,目标文件,会总动生成命令模板