机器人——ROS程序设计学习笔记(chapter7)_改个名字吧205的博客-程序员秘密

技术标签: Ubuntu  机器人  Linux  ROS  

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

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

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

1.几何模型

<?xml version ="1.0"?>
<robot name = "text1">

  <!--body-->
  <link name = "base_link">
    <visual>
      <geometry>
        <cylinder length = "0.6" radius = "0.2"/>  
        <!--圆柱体 长度0.6 半径0.2-->
      </geometry>
      <material name = "blue">
       <color rgba = "0 0 0.8 1"/>
      </material>
    </visual>
  </link>
<!--right leg-->
  <link name = "right_leg">
    <visual>
      <geometry>
        <box size = "0.6 .2 .1"/>
       <!--六面体长宽高分别为0.6 0.2 0.1-->
      </geometry>
      <material name = "white">
       <color rgba = "1 1 1 1"/>
      </material>
    </visual>  
  </link>

  <joint name = "base_to_right_leg" type = "fixed">
    <parent link = "base_link"/>
    <child link = "right_leg"/> 
  </joint>

</robot>

<geometry>中添加几何体信息

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

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

<!--right leg-->
  <link name = "right_leg">
    <visual>
      <geometry>
        <box size = "0.6 .2 .1"/>
      </geometry>
      <material name = "white">
       <color rgba = "1 1 1 1"/>
      </material>
      <origin rpy = "0 1.5705 0" xyz = "0 0 -0.3"/>
    </visual>  
  </link>
  <joint name = "base_to_right_leg" type = "fixed">
    <parent link = "base_link"/>
    <child link = "right_leg"/>
    <origin xyz = "0.22 0 0.25"> 
  </joint>

<!--left leg-->
  <link name = "left_leg">
    <visual>
      <geometry>
        <box size = "0.6 .2 .1"/>
      </geometry>
      <material name = "white">
       <color rgba = "1 1 1 1"/>
      </material>
      <origin rpy = "0 1.5705 0" xyz = "-0.22 0 -0.05"/>
    </visual>  
  </link>
  <joint name = "base_to_left_leg" type = "fixed">
    <parent link = "base_link"/>
    <child link = "left_leg"/> 
    <origin xyz = "-0.22 0 0.25">
  </joint>

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

3.添加剩余部位

调整一下,全部代码:

<?xml version ="1.0"?>
<robot name = "text1">

  <!--body-->
  <link name = "base_link">
    <visual>
      <geometry>
        <cylinder length = "0.6" radius = "0.2"/>
      </geometry>
      <material name = "blue">
       <color rgba = "0 0 0.8 1"/>
      </material>
    </visual>
  </link>

<!--right leg-->
  <link name = "right_leg">
    <visual>
      <geometry>
        <box size = "0.6 .2 .1"/>
      </geometry>
      <material name = "white">
       <color rgba = "1 1 1 1"/>
      </material>
      <origin rpy = "0 1.5705 0" xyz = "0 0 -0.3"/>
    </visual>  
  </link>
  <joint name = "base_to_right_leg" type = "fixed">
    <parent link = "base_link"/>
    <child link = "right_leg"/> 
    <origin xyz = "0.22 0 .25"/>
  </joint>

<!--left leg-->
  <link name = "left_leg">
    <visual>
      <geometry>
        <box size = "0.6 .2 .1"/>
      </geometry>
      <material name = "white">
       <color rgba = "1 1 1 1"/>
      </material>
      <origin rpy = "0 1.5705 0" xyz = "0 0 -0.3"/>
    </visual>  
  </link>
  <joint name = "base_to_left_leg" type = "fixed">
    <parent link = "base_link"/>
    <child link = "left_leg"/>
    <origin xyz = "-0.22 0 .25"/> 
  </joint>
<!--head-->
 <link name = "head">
  <visual>
   <geometry>
    <sphere radius = "0.2"/>
   </geometry>
   <material name  = "white"/>
  </visual>
 </link>
 <joint name="base_to_head" type="fixed">
   <parent link="base_link"/>
   <child link="head"/>
   <origin xyz="0 0 0.3"/>
  </joint>
<!--right foot-->
 <link name = "right_foot">
  <visual>
    <geometry>
     <box size = "0.1 .4 .1"/>
    </geometry>
    <material name = "yellow">
     <color rgba = "1 1 0 1"/>
    </material>
  </visual>
 </link>
 <joint name = "leg_to_right_foot" type = "fixed">
  <parent link = "right_leg"/>
  <child link = "right_foot"/>
  <origin xyz = "0 0 -0.65"/>
 </joint>
 <!--left foot--> 
 <link name = "left_foot">
  <visual>
    <geometry>
     <box size = "0.1 .4 .1"/>
    </geometry>
    <material name = "yellow">
     <color rgba = "1 1 0 1"/>
    </material>
  </visual>
 </link>
 <joint name = "leg_to_left_foot" type = "fixed">
  <parent link = "left_leg"/>
  <child link = "left_foot"/>
  <origin xyz = "0 0 -0.65"/>
 </joint>
<!--right foot front wheel-->
 <link name = "right_front_wheel">
  <visual>
   <geometry>
    <cylinder length= "0.08" radius = "0.05"/>
   </geometry>
   <material name = "black">
    <color rgba = "0 0 0 1"/>
   </material>
  </visual>
 </link>
 <joint name  = "right_foot_to_front_wheel" type = "fixed">
  <parent link ="right_foot"/>
  <child link = "right_front_wheel"/>
  <origin xyz = "0 0.14 -0.05" rpy = "0 1.5705 0"/>
 </joint>
<!--right foot back wheel-->
 <link name = "right_back_wheel">
  <visual>
   <geometry>
    <cylinder length= "0.08" radius = "0.05"/>
   </geometry>
   <material name = "black">
    <color rgba = "0 0 0 1"/>
   </material>
  </visual>
 </link>
 <joint name  = "right_foot_to_back_wheel" type = "fixed">
  <parent link ="right_foot"/>
  <child link = "right_back_wheel"/>
  <origin xyz = "0 -0.14 -0.05" rpy =  "0 1.5705 0"/>
 </joint>
<!--left foot front wheel-->
 <link name = "left_front_wheel">
  <visual>
   <geometry>
    <cylinder length= "0.08" radius = "0.05"/>
   </geometry>
   <material name = "black">
    <color rgba = "0 0 0 1"/>
   </material>
  </visual>
 </link>
 <joint name  = "left_foot_to_front_wheel" type = "fixed">
  <parent link ="left_foot"/>
  <child link = "left_front_wheel"/>
  <origin xyz = "0 0.14 -0.05" rpy =  "0 1.5705 0"/>
 </joint>
<!--left foot back wheel-->
 <link name = "left_back_wheel">
  <visual>
   <geometry>
    <cylinder length= "0.08" radius = "0.05"/>
   </geometry>
   <material name = "black">
    <color rgba = "0 0 0 1"/>
   </material>
  </visual>
 </link>
 <joint name  = "left_foot_to_back_wheel" type = "fixed">
  <parent link ="left_foot"/>
  <child link = "left_back_wheel"/>
  <origin xyz = "0 -0.14 -0.05" rpy =  "0 1.5705 0"/>
 </joint>
</robot>

4.添加碰撞collision

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

  <!--body-->
  <link name = "base_link">
    <visual>
      <geometry>
        <cylinder length = "0.6" radius = "0.2"/>
      </geometry>
      <material name = "blue">
       <color rgba = "0 0 0.8 1"/>
      </material>
    </visual>
    <collision>
     <geometry>
      <cylinder length = "0.6" radius = "0.2"/>
     </geometry>
    </collision>
  </link>

5.xacro

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

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

<?xml version ="1.0"?>
<robot name = "text1" xmlns:xacro="http://www.ros.org/wiki/xacro">

 <xacro:property name="wheel_length" value="0.08" />
 <xacro:property name="wheel_radius" value="0.05" />
 <xacro:macro name="default_inertial" params="mass">
  <inertial>
   <mass value="${mass}" />
   <inertia ixx="1.0" ixy="0.0" ixz="0.0"
            iyy="1.0" iyz="0.0"
            izz="1.0" />
   </inertial>
 </xacro:macro>
.
.
.
<!--left foot back wheel-->
 <link name = "left_back_wheel">
  <visual>
   <geometry>
    <cylinder length= "${wheel_length}" radius = "${wheel_radius}"/>
   </geometry>
   <material name = "black">
    <color rgba = "0 0 0 1"/>
   </material>
  </visual>
  <collision>
   <geometry>
    <cylinder length= "${wheel_length}" radius = "${wheel_radius}"/>
   </geometry>
  </collision>
  <xacro:default_inertial mass = "1"/>
 </link>
 <joint name  = "left_foot_to_back_wheel" type = "continuous">
  <parent link ="left_foot"/>
  <child link = "left_back_wheel"/>
  <axis xyz = "0 0 1"/>
  <origin xyz = "0 -0.14 -0.05" rpy =  "0 1.5705 0"/>
 </joint>

</robot>

6.让机器人动起来

让机器人走圆

cpp如下:

#include <string>
#include <ros/ros.h>
#include <sensor_msgs/JointState.h>
#include <tf/transform_broadcaster.h>

int main(int argc, char** argv) {
    ros::init(argc, argv, "state_publisher_text");
    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,wheel_1_inc= -0.05,wheel_2_inc= -0.05,wheel_3_inc= -0.07,wheel_4_inc= -0.07;    //轮子向后转,机器人向前移动,所以这里是负
    double angle= 0,base_head = 0,wheel_1= 0,wheel_2= 0,wheel_3= 0,wheel_4= 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(9);
        joint_state.position.resize(9);
        joint_state.name[0] ="base_to_head";
        joint_state.position[0] = base_head;
        joint_state.name[1] ="base_to_right_leg";
        joint_state.position[1] = 0;
        joint_state.name[2] ="base_to_left_leg";
        joint_state.position[2] = 0;
	    joint_state.name[3] ="right_leg_to_right_foot";
        joint_state.position[3] = 0;
	    joint_state.name[4] ="left_leg_to_left_foot";
        joint_state.position[4] = 0;
	    joint_state.name[5] ="right_foot_to_front_wheel";
        joint_state.position[5] = wheel_1;
	    joint_state.name[6] ="right_foot_to_back_wheel";
        joint_state.position[6] = wheel_2;
	    joint_state.name[7] ="left_foot_to_front_wheel";
        joint_state.position[7] = wheel_3;
	    joint_state.name[8] ="left_foot_to_back_wheel";
        joint_state.position[8] = wheel_4;

        // update transform
        // (moving in a circle with radius)
        //改变二维平面的x,y的坐标,z轴垂直于xOy,故z的坐标是0
        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
	    base_head += inc;
	    if(base_head< -2.0||base_head> 2.0) base_head *= -1;
	    wheel_1 += wheel_1_inc;
	    if(wheel_1< -2.0||wheel_1> 2.0) wheel_1 *= -1;
	    wheel_2 += wheel_2_inc;
	    if(wheel_2< -2.0||wheel_2> 2.0) wheel_2 *= -1;
	    wheel_3 += wheel_3_inc;
	    if(wheel_3< -2.0||wheel_3> 2.0) wheel_3 *= -1;
	    wheel_4 += wheel_4_inc;
	    if(wheel_4< -2.0||wheel_4> 2.0) wheel_4 *= -1;

	    angle += degree/4;

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


    return 0;
}

Chapter7部分错误与解决方法:

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

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

解决:.urdf前添加路径

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

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

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

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

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

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

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

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

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_40878688/article/details/81390497

智能推荐

python网址正则表达式_Python 使用正则表达式匹配URL网址_weixin_39692847的博客-程序员秘密

原博文2020-04-21 16:58 −1 import re2 str = "http://www.baidu.com/"3 regular = re.compile(r'[a-zA-Z]+://[^\s]*[.com|.cn]')4 re.findall(regular, str) ['http:...01211相关推荐2019-12-18 21:26 −1,...

namenode格式化问题_stonyOL的博客-程序员秘密

如果是 格式化代码运行多次bin/hdfs namenode –format我自己试了各种解决方法,但是namenode启动还是失败,最后只能重新写。格式化namenode的代码只能运行一次!!!!

Java 接口、集成与多态_sytloveyxj的博客-程序员秘密

1、类的继承基本思想:基于某个父类的扩展,制定出一个新的子类,子类可以继承父类原有的属性和方法,也可以增加原来父类所不具备的属性和方法,或者直接重写父类中的某些方法。在Java中使用exdends关键字来表示两个类的继承关系。父类:class Test{public Test(){                       //构造方法//Somesentence}

zhang快速并行细化算法_菜鸟&攻城狮的博客-程序员秘密_zhang算法

相关介绍 http://www.doc88.com/p-7724593025971.html比较好的介绍 https://www.cnblogs.com/xianglan/archive/2011/01/01/1923779.htmlhttps://www.cnblogs.com/Summerio/p/8284602.htmlhttps://blog.csdn.net/weixin...

uniapp 小程序分享海报保存图片踩坑_Thekingyu的博客-程序员秘密

前言:第一次写的时候,思路。1:获取权限&gt;绘制canvas&gt;保存canvas返回路径,canvasToTempFilePath&gt;图片暂存downloadFile&gt;保存图片saveImageToPhotosAlbum但是在开发小程序开发工具中在图片暂存出现问题,因为开发工具内downloadFile正常,到了手机微信报错,因为只能下载http文件,二手机返回的是“wx...

Echarts城市人口流动监控图_董师傅的博客-程序员秘密

echarts图标可视化数据为上海某天不同时段的人口热力情况原始热力分布如图需要绘制7个同时段的热力情况,并随时间轴变动而变动先对数据进行封包,封包为一个文件夹只需要修改其中的html和set里的文件即可html代码如下:&amp;amp;amp;lt;!doctype html&amp;amp;amp;gt;&amp;amp;amp;lt;html&amp;amp;amp;gt; &amp;amp;amp;lt;head&amp;amp;amp;gt;

随便推点

《第一行代码》第六章 SQLite存储Test_飘过的小熊的博客-程序员秘密

参考:《第一行代码》 代码搬运工。还是按照书上的步骤来吧,先是进行创建数据库训练。安卓内置一款轻量级的关系型数据库SQLite。为了方便我们管理数据库,专门提供了SQLiteOpenHelper这个抽象类,在使用时我们只能借助其子类。它有两个构造方法,我们通常只是使用参数较少的一个,较少的方法有四个参数,分别是,context,数据库名,XXX目前用不到的参数(一般传入null),数据库版本。

【Halcon】霍夫直线检测hough_lines_Taily老段的博客-程序员秘密_halcon直线度检测

#include "HalconCpp.h"using namespace Halcon;#ifndef NO_EXPORT_MAIN// Main procedure void action(){ using namespace Halcon; // Local iconic variables Hobject Image, ImageReduced,

《精品毕设》 java springboot+mybatis二手物品网站系统完整源码+论文_java_dingli的博客-程序员秘密

项目背景:随着互联网的爆炸式发展,自中国加入WTO以来,电子商务在中国也得到了迅速的发展,现在互联网上的各种电子商务网站都在迅速增长。开发一个适合自己的二手商品交易网站,希望能为您提供便捷的服务。作为一个网购爱好者,我很同情他们。我穿的衣服,我用的工具,我用的书,生活用品和各种商品都会在一段时间后被淘汰。一些用户干脆把它们扔掉,这不仅是一种浪费,也污染了环境。还有一些用户想要出售自己的产品,但却很难找到买家,最终只能将产品卖给高端商品。因此,这种材料不能得到充分利用。为了解决这种资源的浪费,所以才开发.

拜托,面试官不要在问我Redis分布式锁原理了_码上代码的博客-程序员秘密_请利 redis 实现 个通 分布式锁,并说明您设计 案的优点以及还有哪些

Redisson 实现 Redis 分布式锁的底层原理(1)加锁机制(2)锁互斥机制(3)watch dog ⾃动延期机制(4)可重⼊加锁机制(5)锁释放机制(6)此种⽅案 Redis 分布式锁的缺陷

Android事件分发及拦截机制_阿飞__的博客-程序员秘密

一、MotionEvent        这是Android为触摸事件封装的一个类。只要是重写触摸相关方法,参数一般都含有MotionEvent,可见其重要性。其中包括的一些常用的重要属性和方法:MotionEvent.ActionDown:手指按下MotionEvent.ActionMove:滑动MotionEvent.ActionUp:手指抬起getX():获取相对于控件左上角的...

STM32F446移植USB驱动,实现外部FLASH模拟U盘_笔尖清风的博客-程序员秘密_stm32f446 usb库

采用USB方式,可以带来更多的可玩性,但也更加复杂,之前在STM32F103上实现了USB库的移植,由于F103的USB功能简单,移植比较顺利,具体的方法见本人的博客:STM32F1移植USB库实现外部FLASH模拟U盘功能。相较于 STMF1,STM32F4 的功能大为增强,引入了 OTG 功能,最近入手了一块 NUCLEO-64 开发板,板载芯片是 STM32F446RET6,尝试在上面移植...

推荐文章

热门文章

相关标签