之前,介绍了节点、主题、服务和行动等基本概念,以及rqt和rosbag2等工具。

采用了官方改版的二维环境,那么现在玩耍一下更为逼真的三维仿真环境吧。

  • 仿真软件Gazebo
  • 机器人TurtleBot3

TurtleBot3支持仿真开发环境,可以在仿真中用虚拟机器人编程开发。 有两种开发环境可以做到这一点,一种是使用带有 3D 可视化工具 RViz 的假节点,另一种是使用 3D 机器人模拟器 Gazebo。

  • 假节点适合用机器人模型和运动进行测试,但不支持传感器。
  • 如果需要执行 SLAM 或导航,Gazebo 将是一个可行的解决方案,因为它支持 IMU、LDS 和摄像头等传感器。

环境配置

# TURTLEBOT3_MODEL
export GAZEBO_MODEL_PATH=$GAZEBO_MODEL_PATH:/home/zhangrelay/RobSoft/turtlebot3/src/simulations/turtlebot3_gazebo/models
export TURTLEBOT3_MODEL=burger# ROS2
source /opt/ros/foxy/setup.bash#colcon
source /usr/share/colcon_cd/function/colcon_cd.sh

源码编译

可以使用deb直接安装:

  • sudo apt install ros-foxy-turtlebot3-gazebo

注意包要装全。

这里,采用源码编译如下:

  • colcon build

功能包列表如上所示。

仿真实践

1 启动环境

  • ros2 launch turtlebot3_gazebo empty_world.launch.py

蓝色射线为激光的可视化效果。

empty_world.launch代码如下:

import osfrom ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch.actions import ExecuteProcess
from launch.actions import IncludeLaunchDescription
from launch.launch_description_sources import PythonLaunchDescriptionSource
from launch.substitutions import LaunchConfigurationTURTLEBOT3_MODEL = os.environ['TURTLEBOT3_MODEL']def generate_launch_description():use_sim_time = LaunchConfiguration('use_sim_time', default='True')world_file_name = 'empty_worlds/' + TURTLEBOT3_MODEL + '.model'world = os.path.join(get_package_share_directory('turtlebot3_gazebo'),'worlds', world_file_name)launch_file_dir = os.path.join(get_package_share_directory('turtlebot3_gazebo'), 'launch')pkg_gazebo_ros = get_package_share_directory('gazebo_ros')return LaunchDescription([IncludeLaunchDescription(PythonLaunchDescriptionSource(os.path.join(pkg_gazebo_ros, 'launch', 'gzserver.launch.py')),launch_arguments={'world': world}.items(),),IncludeLaunchDescription(PythonLaunchDescriptionSource(os.path.join(pkg_gazebo_ros, 'launch', 'gzclient.launch.py')),),ExecuteProcess(cmd=['ros2', 'param', 'set', '/gazebo', 'use_sim_time', use_sim_time],output='screen'),IncludeLaunchDescription(PythonLaunchDescriptionSource([launch_file_dir, '/robot_state_publisher.launch.py']),launch_arguments={'use_sim_time': use_sim_time}.items(),),])

2 圆周运动

之前和之前二维环境圆周运动的指令非常类似哦。

  • ros2 topic pub --rate 2 /cmd_vel geometry_msgs/msg/Twist "{linear: {x: 1.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 0.8}}"

3 键盘遥控

使用如下命令启动键盘遥控:

  • ros2 run turtlebot3_teleop teleop_keyboard

键盘遥控代码如下:

import os
import select
import sys
import rclpyfrom geometry_msgs.msg import Twist
from rclpy.qos import QoSProfileif os.name == 'nt':import msvcrt
else:import termiosimport ttyBURGER_MAX_LIN_VEL = 0.22
BURGER_MAX_ANG_VEL = 2.84WAFFLE_MAX_LIN_VEL = 0.26
WAFFLE_MAX_ANG_VEL = 1.82LIN_VEL_STEP_SIZE = 0.01
ANG_VEL_STEP_SIZE = 0.1TURTLEBOT3_MODEL = os.environ['TURTLEBOT3_MODEL']msg = """
Control Your TurtleBot3!
---------------------------
Moving around:wa    s    dxw/x : increase/decrease linear velocity (Burger : ~ 0.22, Waffle and Waffle Pi : ~ 0.26)
a/d : increase/decrease angular velocity (Burger : ~ 2.84, Waffle and Waffle Pi : ~ 1.82)space key, s : force stopCTRL-C to quit
"""e = """
Communications Failed
"""def get_key(settings):if os.name == 'nt':return msvcrt.getch().decode('utf-8')tty.setraw(sys.stdin.fileno())rlist, _, _ = select.select([sys.stdin], [], [], 0.1)if rlist:key = sys.stdin.read(1)else:key = ''termios.tcsetattr(sys.stdin, termios.TCSADRAIN, settings)return keydef print_vels(target_linear_velocity, target_angular_velocity):print('currently:\tlinear velocity {0}\t angular velocity {1} '.format(target_linear_velocity,target_angular_velocity))def make_simple_profile(output, input, slop):if input > output:output = min(input, output + slop)elif input < output:output = max(input, output - slop)else:output = inputreturn outputdef constrain(input_vel, low_bound, high_bound):if input_vel < low_bound:input_vel = low_boundelif input_vel > high_bound:input_vel = high_boundelse:input_vel = input_velreturn input_veldef check_linear_limit_velocity(velocity):if TURTLEBOT3_MODEL == 'burger':return constrain(velocity, -BURGER_MAX_LIN_VEL, BURGER_MAX_LIN_VEL)else:return constrain(velocity, -WAFFLE_MAX_LIN_VEL, WAFFLE_MAX_LIN_VEL)def check_angular_limit_velocity(velocity):if TURTLEBOT3_MODEL == 'burger':return constrain(velocity, -BURGER_MAX_ANG_VEL, BURGER_MAX_ANG_VEL)else:return constrain(velocity, -WAFFLE_MAX_ANG_VEL, WAFFLE_MAX_ANG_VEL)def main():settings = Noneif os.name != 'nt':settings = termios.tcgetattr(sys.stdin)rclpy.init()qos = QoSProfile(depth=10)node = rclpy.create_node('teleop_keyboard')pub = node.create_publisher(Twist, 'cmd_vel', qos)status = 0target_linear_velocity = 0.0target_angular_velocity = 0.0control_linear_velocity = 0.0control_angular_velocity = 0.0try:print(msg)while(1):key = get_key(settings)if key == 'w':target_linear_velocity =\check_linear_limit_velocity(target_linear_velocity + LIN_VEL_STEP_SIZE)status = status + 1print_vels(target_linear_velocity, target_angular_velocity)elif key == 'x':target_linear_velocity =\check_linear_limit_velocity(target_linear_velocity - LIN_VEL_STEP_SIZE)status = status + 1print_vels(target_linear_velocity, target_angular_velocity)elif key == 'a':target_angular_velocity =\check_angular_limit_velocity(target_angular_velocity + ANG_VEL_STEP_SIZE)status = status + 1print_vels(target_linear_velocity, target_angular_velocity)elif key == 'd':target_angular_velocity =\check_angular_limit_velocity(target_angular_velocity - ANG_VEL_STEP_SIZE)status = status + 1print_vels(target_linear_velocity, target_angular_velocity)elif key == ' ' or key == 's':target_linear_velocity = 0.0control_linear_velocity = 0.0target_angular_velocity = 0.0control_angular_velocity = 0.0print_vels(target_linear_velocity, target_angular_velocity)else:if (key == '\x03'):breakif status == 20:print(msg)status = 0twist = Twist()control_linear_velocity = make_simple_profile(control_linear_velocity,target_linear_velocity,(LIN_VEL_STEP_SIZE / 2.0))twist.linear.x = control_linear_velocitytwist.linear.y = 0.0twist.linear.z = 0.0control_angular_velocity = make_simple_profile(control_angular_velocity,target_angular_velocity,(ANG_VEL_STEP_SIZE / 2.0))twist.angular.x = 0.0twist.angular.y = 0.0twist.angular.z = control_angular_velocitypub.publish(twist)except Exception as e:print(e)finally:twist = Twist()twist.linear.x = 0.0twist.linear.y = 0.0twist.linear.z = 0.0twist.angular.x = 0.0twist.angular.y = 0.0twist.angular.z = 0.0pub.publish(twist)if os.name != 'nt':termios.tcsetattr(sys.stdin, termios.TCSADRAIN, settings)if __name__ == '__main__':main()

简单加入中文方便使用:

阅读源码非常重要。

4 节点

5 主题

6 服务

ros2 service list -t

  • /apply_joint_effort [gazebo_msgs/srv/ApplyJointEffort]
  • /apply_link_wrench [gazebo_msgs/srv/ApplyLinkWrench]
  • /clear_joint_efforts [gazebo_msgs/srv/JointRequest]
  • /clear_link_wrenches [gazebo_msgs/srv/LinkRequest]
  • /delete_entity [gazebo_msgs/srv/DeleteEntity]
  • /gazebo/describe_parameters [rcl_interfaces/srv/DescribeParameters]
  • /gazebo/get_parameter_types [rcl_interfaces/srv/GetParameterTypes]
  • /gazebo/get_parameters [rcl_interfaces/srv/GetParameters]
  • /gazebo/list_parameters [rcl_interfaces/srv/ListParameters]
  • /gazebo/set_parameters [rcl_interfaces/srv/SetParameters]
  • /gazebo/set_parameters_atomically [rcl_interfaces/srv/SetParametersAtomically]
  • /get_model_list [gazebo_msgs/srv/GetModelList]
  • /pause_physics [std_srvs/srv/Empty]
  • /reset_simulation [std_srvs/srv/Empty]
  • /reset_world [std_srvs/srv/Empty]
  • /robot_state_publisher/describe_parameters [rcl_interfaces/srv/DescribeParameters]
  • /robot_state_publisher/get_parameter_types [rcl_interfaces/srv/GetParameterTypes]
  • /robot_state_publisher/get_parameters [rcl_interfaces/srv/GetParameters]
  • /robot_state_publisher/list_parameters [rcl_interfaces/srv/ListParameters]
  • /robot_state_publisher/set_parameters [rcl_interfaces/srv/SetParameters]
  • /robot_state_publisher/set_parameters_atomically [rcl_interfaces/srv/SetParametersAtomically]
  • /rqt_gui_py_node_4338/describe_parameters [rcl_interfaces/srv/DescribeParameters]
  • /rqt_gui_py_node_4338/get_parameter_types [rcl_interfaces/srv/GetParameterTypes]
  • /rqt_gui_py_node_4338/get_parameters [rcl_interfaces/srv/GetParameters]
  • /rqt_gui_py_node_4338/list_parameters [rcl_interfaces/srv/ListParameters]
  • /rqt_gui_py_node_4338/set_parameters [rcl_interfaces/srv/SetParameters]
  • /rqt_gui_py_node_4338/set_parameters_atomically [rcl_interfaces/srv/SetParametersAtomically]
  • /spawn_entity [gazebo_msgs/srv/SpawnEntity]
  • /teleop_keyboard/describe_parameters [rcl_interfaces/srv/DescribeParameters]
  • /teleop_keyboard/get_parameter_types [rcl_interfaces/srv/GetParameterTypes]
  • /teleop_keyboard/get_parameters [rcl_interfaces/srv/GetParameters]
  • /teleop_keyboard/list_parameters [rcl_interfaces/srv/ListParameters]
  • /teleop_keyboard/set_parameters [rcl_interfaces/srv/SetParameters]
  • /teleop_keyboard/set_parameters_atomically [rcl_interfaces/srv/SetParametersAtomically]
  • /turtlebot3_diff_drive/describe_parameters [rcl_interfaces/srv/DescribeParameters]
  • /turtlebot3_diff_drive/get_parameter_types [rcl_interfaces/srv/GetParameterTypes]
  • /turtlebot3_diff_drive/get_parameters [rcl_interfaces/srv/GetParameters]
  • /turtlebot3_diff_drive/list_parameters [rcl_interfaces/srv/ListParameters]
  • /turtlebot3_diff_drive/set_parameters [rcl_interfaces/srv/SetParameters]
  • /turtlebot3_diff_drive/set_parameters_atomically [rcl_interfaces/srv/SetParametersAtomically]
  • /turtlebot3_imu/describe_parameters [rcl_interfaces/srv/DescribeParameters]
  • /turtlebot3_imu/get_parameter_types [rcl_interfaces/srv/GetParameterTypes]
  • /turtlebot3_imu/get_parameters [rcl_interfaces/srv/GetParameters]
  • /turtlebot3_imu/list_parameters [rcl_interfaces/srv/ListParameters]
  • /turtlebot3_imu/set_parameters [rcl_interfaces/srv/SetParameters]
  • /turtlebot3_imu/set_parameters_atomically [rcl_interfaces/srv/SetParametersAtomically]
  • /turtlebot3_joint_state/describe_parameters [rcl_interfaces/srv/DescribeParameters]
  • /turtlebot3_joint_state/get_parameter_types [rcl_interfaces/srv/GetParameterTypes]
  • /turtlebot3_joint_state/get_parameters [rcl_interfaces/srv/GetParameters]
  • /turtlebot3_joint_state/list_parameters [rcl_interfaces/srv/ListParameters]
  • /turtlebot3_joint_state/set_parameters [rcl_interfaces/srv/SetParameters]
  • /turtlebot3_joint_state/set_parameters_atomically [rcl_interfaces/srv/SetParametersAtomically]
  • /turtlebot3_laserscan/describe_parameters [rcl_interfaces/srv/DescribeParameters]
  • /turtlebot3_laserscan/get_parameter_types [rcl_interfaces/srv/GetParameterTypes]
  • /turtlebot3_laserscan/get_parameters [rcl_interfaces/srv/GetParameters]
  • /turtlebot3_laserscan/list_parameters [rcl_interfaces/srv/ListParameters]
  • /turtlebot3_laserscan/set_parameters [rcl_interfaces/srv/SetParameters]
  • /turtlebot3_laserscan/set_parameters_atomically [rcl_interfaces/srv/SetParametersAtomically]
  • /unpause_physics [std_srvs/srv/Empty]

7 行动

SLAM和导航时候再补充。

8 更多

可参考前13篇中对应案例,在此三维环境中进行实践哦。

总结

由二维环境到三维环境,仿真更炫酷,但是原理和指令几乎一样,学一招全掌控!

机器人编程趣味实践14-机器人三维仿真(Gazebo+TurtleBot3)相关推荐

  1. 机器人编程趣味实践20-版本课程(教学)

    如何选择一款合适的工具入门机器人编程,相信经过前面19节,会有一个模糊的概念. 比如需要一定的编程基础如: C++ Python 机器人学的相关知识,哇哦,这好像不是一个零基础就能轻松上手的内容,事实 ...

  2. 机器人编程趣味实践02-程序(Hello World)

    上一节,概述中(机器人编程趣味实践01-简要介绍)简要说明课程包含内容. 互联网 物联网 智联网(机器人) 开篇 这些设备平台的Hello World,具体有哪些差异呢??? 对于如上设备,它的Hel ...

  3. 机器人编程趣味实践19-武林秘籍(文档)

    机器人技术快速发展,教程等迭代速度非常快,周期在1-2年,新生期都是如此,进入成熟期会好很多,文档迭代周期会延长至3-5年.至于那些经典技术通常生命周期长达10年或更久. 在开启本文之前,推荐一篇: ...

  4. 机器人编程趣味实践06-程序(节点)

    分别输入如下命令: ros2 run examples_rclcpp_minimal_publisher publisher_lambda ros2 run turtlesim turtlesim_n ...

  5. 机器人编程趣味实践07-信息交互(主题)

    在网络上,沟通交流的方式主要有文字.图片和视频.对于专业人士,通常是公式和代码. 在表述一个数学或物理规律所用字节或占用空间由小到大: 公式 文字 图片 视频 理解难度由简单到困难,如下: 视频 图片 ...

  6. 机器人编程趣味实践18-他山之石(功能包)

    机器人操作系统功能包可以使用如下两种方式使用: sudo apt install xxx colcon build xxx 第一种是打包的安装文件,第二种是源代码编译. 如何自己编写功能包后续细说,本 ...

  7. python机器人编程教程入门_机器人操作系统(ROS)入门必备:机器人编程一学就会...

    原标题:机器人操作系统(ROS)入门必备:机器人编程一学就会 ROS经过十几年的发展,已经得到了极大的推广和应用,尤其是在学术界.卡耐基梅隆大学机器人研究所的大部分实验室都是基于ROS编程的,现在所在 ...

  8. python机器人编程教程入门_机器人操作系统(ROS)入门必备:机器人编程一学就会

    本书是针对机器人操作系统(ROS)初学者的入门教程,从基础的如何安装ROS,到ROS的框架介绍和C/C++.Python编程基础概念介绍,直至完整搭建一个机器人项目,每一个部分都有详细的操作过程和相应 ...

  9. 湘潭哪里学计算机编程,湘潭哪里学机器人编程?湘潭学机器人编程的学校有哪些?...

    原标题:湘潭哪里学机器人编程?湘潭学机器人编程的学校有哪些? 随着机器人编程教育在一线城市的大众化,许多家长也纷纷跟随着趋势送孩子去学习,如果你也想要自己的孩子学习机器人编程,那就一起来看看学习机器人 ...

最新文章

  1. BZOJ 1040 ZJOI2008 骑士 树形DP
  2. Netty整合SpringMVC,实现高效的HTTP服务请求
  3. SPRING STS Virgo OSGI 开发一 : bundle 项目的创建
  4. IOS开发之小实例--UIImagePickerController
  5. React开发(177):opentab没有menu会报错
  6. 中科大 计算机网络1 课程主要内容大概介绍
  7. Go语言的异常处理之errors,panic, recover
  8. 【设计模式笔记】代理模式
  9. PC版Android系统声卡驱动,android下调试声卡驱动之概述
  10. Php 生成随机字符串函数集成
  11. 自动匹配模板 一分钟搞定财务报表
  12. Google的Picasa网络相册很弓虽!!!
  13. Blekko推出类Flipboard社交新闻网站ROCKZi
  14. 多多情报通:拼多多推广账户金额可以通用吗?里面钱可以提出来吗?
  15. 有没有ai绘画教程?什么软件能实现ai绘画?
  16. idea启动报符号缺失,无法启动与构建
  17. LTspice 电路仿真入门
  18. 八问程序员-----总有一个适合你
  19. Kafka 发送消息
  20. 前端--导致页面白屏的原因

热门文章

  1. 菌群数据预处理-microbiome包
  2. dhcp服务器显示dns服务器更新挂起,如何动态更新DNS记录
  3. iOS一键生成所有图标
  4. 朗伯辐射强度模型MATLAB,基于室内非定向的可见光通信系统的朗伯光源阶数优化...
  5. ArrayList源码分析
  6. 蛊惑者马云发家史(曾推毛氏运动唐僧团队)三
  7. 浅谈css3的3D动画效果并制作一个简单的旋转照片墙
  8. 基于投影学习的负采样改进型上位词关系提取 (翻译学习使用)
  9. 把时间当作朋友 读后感
  10. 图像自动去暗角算法(vegnetting