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

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

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

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

1.几何模型




  
  
    
      
          
        
      
      
       
      
    
  

  
    
      
        
       
      
      
       
      
      
  

  
    
     
  

机器人——ROS程序设计学习笔记(chapter7)_第1张图片

中添加几何体信息

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

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


  
    
      
        
      
      
       
      
      
      
  
  
    
    
     
  


  
    
      
        
      
      
       
      
      
      
  
  
    
     
    
  

机器人——ROS程序设计学习笔记(chapter7)_第2张图片

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

3.添加剩余部位

调整一下,全部代码:




  
  
    
      
        
      
      
       
      
    
  


  
    
      
        
      
      
       
      
      
      
  
  
    
     
    
  


  
    
      
        
      
      
       
      
      
      
  
  
    
    
     
  

 
  
   
    
   
   
  
 
 
   
   
   
  

 
  
    
     
    
    
     
    
  
 
 
  
  
  
 
  
 
  
    
     
    
    
     
    
  
 
 
  
  
  
 

 
  
   
    
   
   
    
   
  
 
 
  
  
  
 

 
  
   
    
   
   
    
   
  
 
 
  
  
  
 

 
  
   
    
   
   
    
   
  
 
 
  
  
  
 

 
  
   
    
   
   
    
   
  
 
 
  
  
  
 

机器人——ROS程序设计学习笔记(chapter7)_第3张图片

4.添加碰撞collision

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

  
  
    
      
        
      
      
       
      
    
    
     
      
     
    
  

5.xacro

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

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




 
 
 
  
   
   
   
 
.
.
.

 
  
   
    
   
   
    
   
  
  
   
    
   
  
  
 
 
  
  
  
  
 

6.让机器人动起来

让机器人走圆

cpp如下:

#include 
#include 
#include 
#include 

int main(int argc, char** argv) {
    ros::init(argc, argv, "state_publisher_text");
    ros::NodeHandle n;
    ros::Publisher joint_pub = n.advertise("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;
}

机器人——ROS程序设计学习笔记(chapter7)_第4张图片

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//ROS/catkin_ws/src/learning_urdf/urdf/robot1.urdf

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

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

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

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

你可能感兴趣的:(ROS)