使用键盘控制gazebo小车模型运动

博客地址:https://www.cnblogs.com/zylyehuo/

gazebo小车模型创建详见另一篇博客

博客地址:gazebo小车模型(附带仿真环境) - zylyehuo - 博客园
参考链接

Autolabor-ROS机器人入门课程《ROS理论与实践》

ROS源码安装teleop_twist_keyboard

成果图

step1: 打开vscode进入之前创建的 mycar_ws 工作空间

step2:按如图结构创建文件夹及文件

step3: 代码部分

src/mycar/urdf/gazebo/move.xacro

<robot name="my_car_move" xmlns:xacro="http://wiki.ros.org/xacro">

    <!-- Transmission implementation: used to connect the controller and joints -->
    <xacro:macro name="joint_trans" params="joint_name">
        <!-- Transmission is important to link the joints and the controller -->
        <transmission name="${joint_name}_trans">
            <type>transmission_interface/SimpleTransmission</type>
            <joint name="${joint_name}">
                <hardwareInterface>hardware_interface/VelocityJointInterface</hardwareInterface>
            </joint>
            <actuator name="${joint_name}_motor">
                <hardwareInterface>hardware_interface/VelocityJointInterface</hardwareInterface>
                <mechanicalReduction>1</mechanicalReduction>
            </actuator>
        </transmission>
    </xacro:macro>

    <!-- Each drive wheel needs to be equipped with a transmission device -->
    <xacro:joint_trans joint_name="left_wheel2base_link" />
    <xacro:joint_trans joint_name="right_wheel2base_link" />

    <!-- controller -->
    <gazebo>
        <plugin name="differential_drive_controller" filename="libgazebo_ros_diff_drive.so">
            <rosDebugLevel>Debug</rosDebugLevel>
            <publishWheelTF>true</publishWheelTF>
            <robotNamespace>/</robotNamespace>
            <publishTf>1</publishTf>
            <publishWheelJointState>true</publishWheelJointState>
            <alwaysOn>true</alwaysOn>
            <updateRate>100.0</updateRate>
            <legacyMode>true</legacyMode>
            <leftJoint>left_wheel2base_link</leftJoint> <!-- left wheel -->
            <rightJoint>right_wheel2base_link</rightJoint> <!-- right wheel -->
            <wheelSeparation>${base_radius * 2}</wheelSeparation> <!-- Wheel spacing -->
            <wheelDiameter>${wheel_radius * 2}</wheelDiameter> <!-- Wheel diameter -->
            <broadcastTF>1</broadcastTF>
            <wheelTorque>30</wheelTorque>
            <wheelAcceleration>1.8</wheelAcceleration>
            <commandTopic>cmd_vel</commandTopic> <!-- Motion Control Topics -->
            <odometryFrame>odom</odometryFrame> 
            <odometryTopic>odom</odometryTopic> <!-- Odometer topic -->
            <robotBaseFrame>base_footprint</robotBaseFrame> <!-- Root coordinate system -->
        </plugin>
    </gazebo>

</robot>

src/mycar/urdf/xacro/car.urdf.xacro

<robot name="car" xmlns:xacro="http://wiki.ros.org/xacro">

    <xacro:include filename="inertial_matrix.xacro" />
    
    <xacro:include filename="car_base.urdf.xacro" />
    <xacro:include filename="car_camera.urdf.xacro" />
    <xacro:include filename="car_laser.urdf.xacro" />

    <!-- 以下为新添加的部分  -->  
    <!-- move control  -->
     <xacro:include filename="../gazebo/move.xacro" />


</robot>

step4: 运行 gazebo

(1)在终端执行 roscore 指令

roscore

(2)在 vscode 中 编译启动 launch文件

source ./devel/setup.bash

roslaunch mycar environment.launch

(3)gazebo 打开后,再打开一个新终端

rosrun teleop_twist_keyboard teleop_twist_keyboard.py _speed:=0.3 _turn:=0.5

问题汇总

1、[rospack] Error: package 'teleop_twist_keyboard' not found

解决方法

参考地址:ROS源码安装teleop_twist_keyboard

2、运行 gazebo 报错

解决方法

(1)检查 src/mycar/urdf/xacro/car.urdf.xacro 中路径是否正确

(2)检查 src/mycar/urdf/gazebo/move.xacro 中 joint_name 等结点名称是否与之前定义的一致

posted @ 2023-05-12 19:25  zylyehuo  阅读(481)  评论(0编辑  收藏  举报