教程代码

First step with gazebo and ros

• setup a ROS workspace

• create projects for your simulated robot

• create a Gazebo world

• create your own robot model

• connect your robot model to ROS

• use a teleoperation node to control your robot

• add a camera to your robot

• use Rviz to vizualize all the robot information

Setup a new workspace

mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/src
catkin_init_workspace
cd ..
catkin_make
source ~/catkin_ws/devel/setup.bash

Create projects for your simulated robot

cd ~/catkin_ws/src
catkin_create_pkg mybot_gazebo gazebo_ros
catkin_create_pkg mybot_description
catkin_create_pkg mybot_control

Creating your own World

roscd mybot_gazebo
mkdir launch worlds
cd worlds
vim mybot.world

A basic world file defines at least a name:

<?xml version="1.0"?>
<sdf version="1.4">
<world name="myworld">
</world>
</sdf>

At first we just want to add some basic objects, like a ground and a basic illumination source inside the world tag.

<include>
<uri>model://sun</uri>
</include> <include>
<uri>model://ground_plane</uri>
</include> <include>
<uri>model://turtlebot</uri>
</include>
roscd mybot_gazebo/launch
vim mybot_world.launch

mybot_world.launch

<launch>
<include file="$(find gazebo_ros)/launch/empty_world.launch">
<arg name="world_name" value="$(find mybot_gazebo)/worlds/mybot.world"/>
<arg name="gui" value="true"/>
</include>
</launch>

make a test

roslaunch mybot_gazebo mybot_world.launch

Creating your own Model

roscd mybot_description
mkdir urdf
cd urdf
gedit mybot.xacro
XACRO CONCEPTS

• xacro:include: Import the content from other file. We can divide the content in different xacros and merge them using xacro:include.

• property: Useful to define constant values. Use it later using ${property_name}

• xacro:macro: Macro with variable values. Later, we can use this macro from another xacro file, and we specify the required value for the variables. To use a macro, you have to include the file where the macro is, and call it using the macro's name and filling

the required values.

mybot.xacro

This file will be the main description of our robot. Let's put some xacro basic structure:

<?xml version="1.0"?>
<robot name="mybot" xmlns:xacro="http://www.ros.org/wiki/xacro">
<!-- Put here the robot description -->
</robot>

Let's define some physical properties for our robot, mainly the dimensions of the chassis, the caster wheel, the wheels and the camera:

<xacro:property name="PI" value="3.1415926535897931"/>

<xacro:property name="chassisHeight" value="0.1"/>
<xacro:property name="chassisLength" value="0.4"/>
<xacro:property name="chassisWidth" value="0.2"/>
<xacro:property name="chassisMass" value="50"/> <xacro:property name="casterRadius" value="0.05"/>
<xacro:property name="casterMass" value="5"/> <xacro:property name="wheelWidth" value="0.05"/>
<xacro:property name="wheelRadius" value="0.1"/>
<xacro:property name="wheelPos" value="0.2"/>
<xacro:property name="wheelMass" value="5"/> <xacro:property name="cameraSize" value="0.05"/>
<xacro:property name="cameraMass" value="0.1"/>

We will also include three files :

 <xacro:include filename="$(find mybot_description)/urdf/mybot.gazebo" />
<xacro:include filename="$(find mybot_description)/urdf/materials.xacro" />
<xacro:include filename="$(find mybot_description)/urdf/macros.xacro" />

These three correspond respectively to:

• all the gazebo-specific aspects of our robot

• definition of the materials used (mostly colors)

• definitions of some macros for easier description of the robot

Add it to mybot.xacro

<link name='chassis'>
<collision>
<origin xyz="0 0 ${wheelRadius}" rpy="0 0 0"/>
<geometry>
<box size="${chassisLength} ${chassisWidth} ${chassisHeight}"/>
</geometry>
</collision>
<visual>
<origin xyz="0 0 ${wheelRadius}" rpy="0 0 0"/>
<geometry>
<box size="${chassisLength} ${chassisWidth} ${chassisHeight}"/>
</geometry>
<material name="orange"/>
</visual>
<inertial>
<origin xyz="0 0 ${wheelRadius}" rpy="0 0 0"/>
<mass value="${chassisMass}"/>
<box_inertia m="${chassisMass}" x="${chassisLength}" y="${chassisWidth}" z="${chassisHeight}"/>
</inertial>
</link>

Add it to mybot.gazebo

<gazebo reference="chassis">
<material>Gazebo/Orange</material>
</gazebo>

Add this in the “materials.xacro” :

<?xml version="1.0"?>
<robot>
<material name="black">
<color rgba="0.0 0.0 0.0 1.0"/>
</material> <material name="blue">
<color rgba="0.0 0.0 0.8 1.0"/>
</material> <material name="green">
<color rgba="0.0 0.8 0.0 1.0"/>
</material> <material name="grey">
<color rgba="0.2 0.2 0.2 1.0"/>
</material> <material name="orange">
<color rgba="${255/255} ${108/255} ${10/255} 1.0"/>
</material> <material name="brown">
<color rgba="${222/255} ${207/255} ${195/255} 1.0"/>
</material> <material name="red">
<color rgba="0.8 0.0 0.0 1.0"/>
</material> <material name="white">
<color rgba="1.0 1.0 1.0 1.0"/>
</material>
</robot>

Add this in the macros.xacro file, within the robot tag

<?xml version="1.0" ?>
<robot name="mybot" xmlns:xacro="http://www.ros.org/wiki/xacro"> <macro name="cylinder_inertia" params="m r h">
<inertia ixx="${m*(3*r*r+h*h)/12}" ixy = "0" ixz = "0"
iyy="${m*(3*r*r+h*h)/12}" iyz = "0"
izz="${m*r*r/2}"
/>
</macro> <macro name="box_inertia" params="m x y z">
<inertia ixx="${m*(y*y+z*z)/12}" ixy = "0" ixz = "0"
iyy="${m*(x*x+z*z)/12}" iyz = "0"
izz="${m*(x*x+z*z)/12}"
/>
</macro> <macro name="sphere_inertia" params="m r">
<inertia ixx="${2*m*r*r/5}" ixy = "0" ixz = "0"
iyy="${2*m*r*r/5}" iyz = "0"
izz="${2*m*r*r/5}"
/>
</macro> </robot>

Add this before the chassis link in the mybot.xacro file :

<link name="footprint" />

<joint name="base_joint" type="fixed">
<parent link="footprint"/>
<child link="chassis"/>
</joint>

mybot_world.launch by adding the following two tags in the launch tag:

<!-- urdf xml robot description loaded on the Parameter Server, converting the xacro into a proper urdf file-->
<param name="robot_description" command="$(find xacro)/xacro.py '$(find mybot_description)/urdf/mybot.xacro'" /> <!-- push robot_description to factory and spawn robot in gazebo -->
<node name="mybot_spawn" pkg="gazebo_ros" type="spawn_model" output="screen"
args="-urdf -param robot_description -model mybot" />

make a test

 roslaunch mybot_gazebo mybot_world.launch

mybot.xacro As a next step we add a caster wheel to the robot. This is the simplest wheel as we have no axis and no friction

<joint name="fixed" type="fixed">
<parent link="chassis"/>
<child link="caster_wheel"/>
</joint> <link name="caster_wheel">
<collision>
<origin xyz="${casterRadius-chassisLength/2} 0 ${casterRadius-chassisHeight+wheelRadius}" rpy="0 0 0"/>
<geometry>
<sphere radius="${casterRadius}"/>
</geometry>
</collision> <visual>
<origin xyz="${casterRadius-chassisLength/2} 0 ${casterRadius-chassisHeight+wheelRadius}" rpy="0 0 0"/>
<geometry>
<sphere radius="${casterRadius}"/>
</geometry>
<material name="red"/>
</visual> <inertial>
<origin xyz="${casterRadius-chassisLength/2} 0 ${casterRadius-chassisHeight+wheelRadius}" rpy="0 0 0"/>
<mass value="${casterMass}"/>
<sphere_inertia m="${casterMass}" r="${casterRadius}"/>
</inertial>
</link>

mybot.gazebo Add a gazebo tag in the gazebo file for this link :

<gazebo reference="caster_wheel">
<mu1>0.0</mu1>
<mu2>0.0</mu2>
<material>Gazebo/Red</material>
</gazebo>

As usual, we specify the color used in material. We also added mu1 and mu2, with value 0 to remove the friction.

macros.xacro .We could add the two links in the main file, but let's make one macro to make it simple.

<macro name="wheel" params="lr tY">

<link name="${lr}_wheel">
<collision>
<origin xyz="0 0 0" rpy="0 ${PI/2} ${PI/2}" />
<geometry>
<cylinder length="${wheelWidth}" radius="${wheelRadius}"/>
</geometry>
</collision> <visual>
<origin xyz="0 0 0" rpy="0 ${PI/2} ${PI/2}" />
<geometry>
<cylinder length="${wheelWidth}" radius="${wheelRadius}"/>
</geometry>
<material name="black"/>
</visual> <inertial>
<origin xyz="0 0 0" rpy="0 ${PI/2} ${PI/2}" />
<mass value="${wheelMass}"/>
<cylinder_inertia m="${wheelMass}" r="${wheelRadius}" h="${wheelWidth}"/>
</inertial>
</link> <gazebo reference="${lr}_wheel">
<mu1 value="1.0"/>
<mu2 value="1.0"/>
<kp value="10000000.0" />
<kd value="1.0" />
<fdir1 value="1 0 0"/>
<material>Gazebo/Black</material>
</gazebo> <joint name="${lr}_wheel_hinge" type="continuous">
<parent link="chassis"/>
<child link="${lr}_wheel"/>
<origin xyz="${-wheelPos+chassisLength/2} ${tY*wheelWidth/2+tY*chassisWidth/2} ${wheelRadius}" rpy="0 0 0" />
<axis xyz="0 1 0" rpy="0 0 0" />
<limit effort="100" velocity="100"/>
<joint_properties damping="0.0" friction="0.0"/>
</joint> <transmission name="${lr}_trans">
<type>transmission_interface/SimpleTransmission</type>
<joint name="${lr}_wheel_hinge">
<hardwareInterface>EffortJointInterface</hardwareInterface>
</joint>
<actuator name="${lr}Motor">
<hardwareInterface>EffortJointInterface</hardwareInterface>
<mechanicalReduction>10</mechanicalReduction>
</actuator>
</transmission> </macro>

mybot.xacro

<wheel lr="left" tY="1"/>
<wheel lr="right" tY="-1"/>

Connect your robot to ROS

Alright, our robot is all nice and has this new car smell, but we can't do anything with it yet as it has no connection with

ROS In order to add this connection we need to add gazebeo plugins to our model. There are different kinds of plugins:

  • World: Dynamic changes to the world, e.g. Physics, like illumination or gravity, inserting models
  • Model: Manipulation of models (robots), e.g. move the robots
  • Sensor: Feedback from virtual sensor, like camera, laser scanner
  • System: Plugins that are loaded by the GUI, like saving images

First of all we'll use a plugin to provide access to the joints of the wheels. The transmission tags in our URDF will be used by this plugin the define how to link the joints to controllers. To activate the plugin, add the following to mybot.gazebo:

<gazebo>
<plugin name="gazebo_ros_control" filename="libgazebo_ros_control.so">
<robotNamespace>/mybot</robotNamespace>
</plugin>
</gazebo>

With this plugin, we will be able to control the joints, however we need to provide some extra configuration and explicitely

start controllers for the joints. In order to do so, we'll use the package mybot_control that we have defined before. Let's first create the configuration file:

roscd mybot_control
mkdir config
cd config
vim mybot_control.yaml

This file will define three controllers: one for each wheel, connections to the joint by the transmission tag, one for

publishing the joint states. It also defined the PID gains to use for this controller:

mybot:
# Publish all joint states -----------------------------------
joint_state_controller:
type: joint_state_controller/JointStateController
publish_rate: 50 # Effort Controllers ---------------------------------------
leftWheel_effort_controller:
type: effort_controllers/JointEffortController
joint: left_wheel_hinge
pid: {p: 100.0, i: 0.1, d: 10.0}
rightWheel_effort_controller:
type: effort_controllers/JointEffortController
joint: right_wheel_hinge
pid: {p: 100.0, i: 0.1, d: 10.0}

Now we need to create a launch file to start the controllers. For this let's do:

roscd mybot_control
mkdir launch
cd launch
vim mybot_control.launch

In this file we'll put two things. First we'll load the configuration and the controllers, and we'll also start a node that will

provide 3D transforms (tf) of our robot. This is not mandatory but that makes the simulation more complete

<launch>

  <!-- Load joint controller configurations from YAML file to parameter server -->
<rosparam file="$(find mybot_control)/config/mybot_control.yaml" command="load"/> <!-- load the controllers -->
<node name="controller_spawner"
pkg="controller_manager"
type="spawner" respawn="false"
output="screen" ns="/mybot"
args="joint_state_controller
rightWheel_effort_controller
leftWheel_effort_controller"/> <!-- convert joint states to TF transforms for rviz, etc -->
<node name="robot_state_publisher" pkg="robot_state_publisher" type="robot_state_publisher" respawn="false" output="screen">
<param name="robot_description" command="$(find xacro)/xacro.py '$(find mybot_description)/urdf/mybot.xacro'" />
<remap from="/joint_states" to="/mybot/joint_states" />
</node> </launch>

make a test

roslaunch mybot_gazebo mybot_world.launch
roslaunch mybot_control mybot.launch
rostopic list

We could launch our model on gazebo and then launch the controller, but to save some time (and terminals), we'll start the controllers automatically by adding a line to the mybot_world.launch in the mybot_gazebo package :

<!-- ros_control mybot launch file -->
<include file="$(find mybot_control)/launch/mybot_control.launch" />
rostopic pub -1 /mybot/leftWheel_effort_controller/command std_msgs/Float64 "data: 1.5"
rostopic pub -1 /mybot/rightWheel_effort_controller/command std_msgs/Float64 "data: 1.0"
rostopic echo /mybot/joint_states

Teleoperation of your robot

Adding a camera

	<joint name="camera_joint" type="fixed">
<axis xyz="0 1 0" />
<origin xyz="0 0 0.2" rpy="0 0 0"/>
<parent link="footprint"/>
<child link="camera"/>
</joint> <link name="camera">
<collision>
<origin xyz="0 0 0" rpy="0 0 0"/>
<geometry>
<box size="${cameraSize} ${cameraSize} ${cameraSize}"/>
</geometry>
</collision> <visual>
<origin xyz="0 0 0" rpy="0 0 0"/>
<geometry>
<box size="${cameraSize} ${cameraSize} ${cameraSize}"/>
</geometry>
<material name="blue"/>
</visual> <inertial>
<mass value="${cameraMass}" />
<origin xyz="0 0 0" rpy="0 0 0"/>
<box_inertia m="${cameraMass}" x="${cameraSize}" y="${cameraSize}" z="${cameraSize}" />
</inertial>
</link>

Add the plugin to gazebo file

<gazebo reference="camera">
<material>Gazebo/Blue</material>
<sensor type="camera" name="camera1">
<update_rate>30.0</update_rate>
<camera name="head">
<horizontal_fov>1.3962634</horizontal_fov>
<image>
<width>800</width>
<height>800</height>
<format>R8G8B8</format>
</image>
<clip>
<near>0.02</near>
<far>300</far>
</clip>
</camera>
<plugin name="camera_controller" filename="libgazebo_ros_camera.so">
<alwaysOn>true</alwaysOn>
<updateRate>0.0</updateRate>
<cameraName>mybot/camera1</cameraName>
<imageTopicName>image_raw</imageTopicName>
<cameraInfoTopicName>camera_info</cameraInfoTopicName>
<frameName>camera_link</frameName>
<hackBaseline>0.07</hackBaseline>
<distortionK1>0.0</distortionK1>
<distortionK2>0.0</distortionK2>
<distortionK3>0.0</distortionK3>
<distortionT1>0.0</distortionT1>
<distortionT2>0.0</distortionT2>
</plugin>
</sensor>
</gazebo>
rosrun image_view image_view image:=/mybot/camera1/image_raw

Visualisation with RViz

rosrun rviz rviz

整个代码框架如下:

Gazebo Ros入门的更多相关文章

  1. SLAM+语音机器人DIY系列:(二)ROS入门——1.ROS是什么

    摘要 ROS机器人操作系统在机器人应用领域很流行,依托代码开源和模块间协作等特性,给机器人开发者带来了很大的方便.我们的机器人“miiboo”中的大部分程序也采用ROS进行开发,所以本文就重点对ROS ...

  2. ROS入门学习

    ROS学习笔记 ROS入门网站; ROS入门书籍 ROS主要包含包括功能包.节点.话题.消息类型和服务; ROS功能包/软件包(Packages) ROS软件包是一组用于实现特定功能的相关文件的集合, ...

  3. ROS_Kinetic_03 ROS入门向导

    ROS_Kinetic_03 ROS入门向导 每个人都有不同的学习习惯和爱好并针对不同的应用进行ROS相关设计与开发, 没有固定不变的学习模式,但以下的内容是通常都会用到的. 1. ROS基础教程 1 ...

  4. SLAM+语音机器人DIY系列:(二)ROS入门——2.ROS系统整体架构

    摘要 ROS机器人操作系统在机器人应用领域很流行,依托代码开源和模块间协作等特性,给机器人开发者带来了很大的方便.我们的机器人“miiboo”中的大部分程序也采用ROS进行开发,所以本文就重点对ROS ...

  5. SLAM+语音机器人DIY系列:(二)ROS入门——3.在ubuntu16.04中安装ROS kinetic

    摘要 ROS机器人操作系统在机器人应用领域很流行,依托代码开源和模块间协作等特性,给机器人开发者带来了很大的方便.我们的机器人“miiboo”中的大部分程序也采用ROS进行开发,所以本文就重点对ROS ...

  6. SLAM+语音机器人DIY系列:(二)ROS入门——4.如何编写ROS的第一个程序hello_world

    摘要 ROS机器人操作系统在机器人应用领域很流行,依托代码开源和模块间协作等特性,给机器人开发者带来了很大的方便.我们的机器人“miiboo”中的大部分程序也采用ROS进行开发,所以本文就重点对ROS ...

  7. SLAM+语音机器人DIY系列:(二)ROS入门——5.编写简单的消息发布器和订阅器

    摘要 ROS机器人操作系统在机器人应用领域很流行,依托代码开源和模块间协作等特性,给机器人开发者带来了很大的方便.我们的机器人“miiboo”中的大部分程序也采用ROS进行开发,所以本文就重点对ROS ...

  8. SLAM+语音机器人DIY系列:(二)ROS入门——6.编写简单的service和client

    摘要 ROS机器人操作系统在机器人应用领域很流行,依托代码开源和模块间协作等特性,给机器人开发者带来了很大的方便.我们的机器人“miiboo”中的大部分程序也采用ROS进行开发,所以本文就重点对ROS ...

  9. SLAM+语音机器人DIY系列:(二)ROS入门——7.理解tf的原理

    摘要 ROS机器人操作系统在机器人应用领域很流行,依托代码开源和模块间协作等特性,给机器人开发者带来了很大的方便.我们的机器人“miiboo”中的大部分程序也采用ROS进行开发,所以本文就重点对ROS ...

随机推荐

  1. 小白日记32:kali渗透测试之Web渗透-扫描工具-QWASP_ZAP

    扫描工具-QWASP_ZAP 十大安全工具之一,集成性工具,功能完善,而且强大.既可做主动扫描,也可做截断代理.开源免费跨平台,简单易用,体验相对混乱,但在主动扫描方面,相对占优.[kali集成] # ...

  2. 谈KVC、KVO(重点观察者模式)机制编程

    一不小心,小明在<跟着贝尔去冒险>这个真人秀节目中看到了“一日警察,一世警察”的Laughing哥,整个节目除了贝尔吃牛睾丸都不用刀叉的不雅餐饮文化外,还是镜头少普通话跟小明一样烂的Lau ...

  3. BootStrap2学习日记6---代码

    &lt:表示“<” &gt:表示“>” 在BootStrap中标记代码的标签使用<code>,标记代码块使用<pre>(里面的代码特殊标签必须转义) ...

  4. poj3080解题报告(暴力、最大公共子串)

    POJ 3080,题目链接http://poj.org/problem?id=3080 题意: 就是求m个长度为60的字符串的最长连续公共子串,2<=m<=10 规定: 1.最长公共串长度 ...

  5. Android(java)学习笔记113:Android编写代码调用Vibrator震动功能(Bug:按下按钮button始终没有震动)

    1.之前我编写的代码是如下: package com.himi.vibrate; import android.app.Activity; import android.app.Service; im ...

  6. APK文件安装模拟器和ADB命令的使用

    1.安装APK文件到模拟器 Android手机使用的执行文件为APK格式,类似于Windows平台的exe文件.在Android模拟器中安装APK文件有多种方法,如果你是开发人员,可以通过Eclips ...

  7. CentOS/Linux安装VNCserver

    VNC全称是Virtual Network Computing,属于远程控制类软件.其优点是支持跨操作系统的远程图形化控制.在日常工作中,服务器常常是存在机房,不可能每次需要图形界面操作就跑到机房,因 ...

  8. 【AR】增强现实安卓编程 - Vuforia SDK 的安装和使用 (Android Studio)

    Vuforia是个强大的AR平台.使用Vuforia API 可以实现物体识别,图片追踪,柱型追踪,多对象追踪,自定义目标追踪,云识别,文字识别,帧标识和虚拟按钮等功能. 它支持Android, iO ...

  9. LeetCode 260

    Single Number III Given an array of numbers nums, in which exactly two elements appear only once and ...

  10. JDBC的批量批量插入

    本文部分转载于:http://blog.itpub.net/29254281/viewspace-1151785/ http://www.cnblogs.com/chenjianjx/archive/ ...