当前位置:网站首页>Interesting practice of robot programming 14 robot 3D simulation (gazebo+turtlebot3)

Interesting practice of robot programming 14 robot 3D simulation (gazebo+turtlebot3)

2022-07-05 01:28:00 zhangrelay


Before , Introduces the node 、 The theme 、 Basic concepts of service and action , as well as rqt and rosbag2 Tools such as .

Using the official version of the two-dimensional environment , Now let's play with a more realistic 3D simulation environment .


  • Simulation software Gazebo
  • robot TurtleBot3

 Interesting practice of robot programming 14- 3D simulation of robot (Gazebo+TurtleBot3)_ Interesting practice

TurtleBot3 Support simulation development environment , It can be programmed and developed with virtual robot in simulation . There are two development environments that can do this , One is to use a device with 3D Visualization tools RViz False nodes of , The other is to use 3D Robot simulator Gazebo.


  • Pseudo nodes are suitable for testing with robot models and motions , But it doesn't support sensors .
  • If it is necessary to carry out SLAM Or navigation ,Gazebo It's going to be a viable solution , Because it supports IMU、LDS And cameras and other sensors .

Environment configuration

       
# 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
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

Source code compilation

have access to deb Direct installation :

  • sudo apt install ros-foxy-turtlebot3-gazebo

Pay attention to the whole bag .

here , Use source code to compile as follows :

  • colcon build

 Interesting practice of robot programming 14- 3D simulation of robot (Gazebo+TurtleBot3)_Gazebo_02

The list of feature packs is shown above .

Simulation practice

1 Startup environment

  • ros2 launch turtlebot3_gazebo empty_world.launch.py

 Interesting practice of robot programming 14- 3D simulation of robot (Gazebo+TurtleBot3)_TurtleBot3_03

The blue ray is the visualization of the laser .

empty_world.launch The code is as follows :

       
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(),
),
])
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.

2 Circular motion

Before and before the circular motion of two-dimensional environment instructions are very similar .

  • 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}}"

 Interesting practice of robot programming 14- 3D simulation of robot (Gazebo+TurtleBot3)_ Robot programming _04

3 Keyboard remote control

Use the following command to start the keyboard remote control :

  • ros2 run turtlebot3_teleop teleop_keyboard

 Interesting practice of robot programming 14- 3D simulation of robot (Gazebo+TurtleBot3)_Gazebo_05

The keyboard remote control code is as follows :

       
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()
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.
  • 194.
  • 195.
  • 196.
  • 197.
  • 198.
  • 199.
  • 200.

Easy to add Chinese, easy to use :

 Interesting practice of robot programming 14- 3D simulation of robot (Gazebo+TurtleBot3)_ Three dimensional simulation _06

It's very important to read the source code .

4 node

 Interesting practice of robot programming 14- 3D simulation of robot (Gazebo+TurtleBot3)_TurtleBot3_07

5 The theme

 Interesting practice of robot programming 14- 3D simulation of robot (Gazebo+TurtleBot3)_TurtleBot3_08

6 service

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 action

SLAM And navigation .

8 more

Please refer to the previous 13 The corresponding cases in this article , Practice in this 3D environment .

summary

From two-dimensional environment to three-dimensional environment , Simulation is more cool , But the principles and instructions are almost the same , Learn one move and take control !



原网站

版权声明
本文为[zhangrelay]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202141027396922.html