发布于2021-06-07 21:12 阅读(914) 评论(0) 点赞(5) 收藏(1)
之前,介绍了节点、主题、服务和行动等基本概念,以及rqt和rosbag2等工具。
采用了官方改版的二维环境,那么现在玩耍一下更为逼真的三维仿真环境吧。
TurtleBot3支持仿真开发环境,可以在仿真中用虚拟机器人编程开发。 有两种开发环境可以做到这一点,一种是使用带有 3D 可视化工具 RViz 的假节点,另一种是使用 3D 机器人模拟器 Gazebo。
- # 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直接安装:
注意包要装全。
这里,采用源码编译如下:
功能包列表如上所示。
蓝色射线为激光的可视化效果。
empty_world.launch代码如下:
- import os
-
- from 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 LaunchConfiguration
-
- TURTLEBOT3_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(),
- ),
- ])
之前和之前二维环境圆周运动的指令非常类似哦。
使用如下命令启动键盘遥控:
键盘遥控代码如下:
- import os
- import select
- import sys
- import rclpy
-
- from geometry_msgs.msg import Twist
- from rclpy.qos import QoSProfile
-
- if os.name == 'nt':
- import msvcrt
- else:
- import termios
- import tty
-
- BURGER_MAX_LIN_VEL = 0.22
- BURGER_MAX_ANG_VEL = 2.84
-
- WAFFLE_MAX_LIN_VEL = 0.26
- WAFFLE_MAX_ANG_VEL = 1.82
-
- LIN_VEL_STEP_SIZE = 0.01
- ANG_VEL_STEP_SIZE = 0.1
-
- TURTLEBOT3_MODEL = os.environ['TURTLEBOT3_MODEL']
-
- msg = """
- Control Your TurtleBot3!
- ---------------------------
- Moving around:
- w
- a s d
- x
- w/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 stop
- CTRL-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 key
-
-
- def 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 = input
-
- return output
-
-
- def constrain(input_vel, low_bound, high_bound):
- if input_vel < low_bound:
- input_vel = low_bound
- elif input_vel > high_bound:
- input_vel = high_bound
- else:
- input_vel = input_vel
-
- return input_vel
-
-
- def 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 = None
- if 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 = 0
- target_linear_velocity = 0.0
- target_angular_velocity = 0.0
- control_linear_velocity = 0.0
- control_angular_velocity = 0.0
-
- try:
- 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 + 1
- print_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 + 1
- print_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 + 1
- print_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 + 1
- print_vels(target_linear_velocity, target_angular_velocity)
- elif key == ' ' or key == 's':
- target_linear_velocity = 0.0
- control_linear_velocity = 0.0
- target_angular_velocity = 0.0
- control_angular_velocity = 0.0
- print_vels(target_linear_velocity, target_angular_velocity)
- else:
- if (key == '\x03'):
- break
-
- if status == 20:
- print(msg)
- status = 0
-
- twist = Twist()
-
- control_linear_velocity = make_simple_profile(
- control_linear_velocity,
- target_linear_velocity,
- (LIN_VEL_STEP_SIZE / 2.0))
-
- twist.linear.x = control_linear_velocity
- twist.linear.y = 0.0
- twist.linear.z = 0.0
-
- control_angular_velocity = make_simple_profile(
- control_angular_velocity,
- target_angular_velocity,
- (ANG_VEL_STEP_SIZE / 2.0))
-
- twist.angular.x = 0.0
- twist.angular.y = 0.0
- twist.angular.z = control_angular_velocity
-
- pub.publish(twist)
-
- except Exception as e:
- print(e)
-
- finally:
- twist = Twist()
- twist.linear.x = 0.0
- twist.linear.y = 0.0
- twist.linear.z = 0.0
-
- twist.angular.x = 0.0
- twist.angular.y = 0.0
- twist.angular.z = 0.0
-
- pub.publish(twist)
-
- if os.name != 'nt':
- termios.tcsetattr(sys.stdin, termios.TCSADRAIN, settings)
-
-
- if __name__ == '__main__':
- main()
简单加入中文方便使用:
阅读源码非常重要。
ros2 service list -t
SLAM和导航时候再补充。
可参考前13篇中对应案例,在此三维环境中进行实践哦。
由二维环境到三维环境,仿真更炫酷,但是原理和指令几乎一样,学一招全掌控!
作者:hhbnn
链接:http://www.phpheidong.com/blog/article/89780/426ccd12b34a163a06dc/
来源:php黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 php黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-4
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!