ROS学习 ArbotiX+rviz仿真

将功能包下载到项目根目录

git clone https://github.com/vanadiumlabs/arbotix_ros.git
catkin_make
#修改所有者 赋予执行权限 zw 为所有者
sudo chown -R zw arbotix_ros/
sudo chmod -R +x arbotix_ros/

在第一节的模型基础上修改launch文件
https://blog.csdn.net/weixin_43928944/article/details/113244731

<launch>
	<arg name="model" default="$(find xacro)/xacro --inorder '$(find mbot_description)/urdf/xacro/mbot.xacro'" />
	<arg name="gui" default="true" />

	<param name="robot_description" command="$(arg model)" />

    
	<param name="use_gui" value="$(arg gui)"/>

	<node name="arbotix" pkg="arbotix_python" type="arbotix_driver" output="screen">
        <rosparam file="$(find mbot_description)/config/fake_mbot_arbotix.yaml" command="load" />
        <param name="sim" value="true"/>
    node>

    
	<node name="joint_state_publisher" pkg="joint_state_publisher" type="joint_state_publisher" />

	
	<node name="robot_state_publisher" pkg="robot_state_publisher" type="robot_state_publisher" />

launch>

创建一个ctl配置文件
fake_mbot_arbotix.yaml 位于$(find mbot_description)/config/fake_mbot_arbotix.yaml"

controllers: {
   base_controller: {
       type: diff_controller,#类型:差速控制器类型 
       base_frame_id: base_footprint, #坐标系
       base_width: 0.26, #轮子间距
       ticks_meter: 4100, #频率
       #pid控制
       Kp: 12, 
       Kd: 12, 
       Ki: 0, 
       Ko: 50, 
       accel_limit: 1.0 #加速度限制
    }
}
# 我这是18版本的
sudo apt-get install ros-melodic-ros-tutorials ros-melodic-geometry-tutorials ros-melodic-rviz ros-melodic-rosbash ros-melodic-rqt-tf-tree

如果

ERROR: cannot launch node of type [arbotix_python/arbotix_driver]: arbotix_python
ROS path [0]=/opt/ros/noetic/share/ros
ROS path [1]=/home/zjh/catkin_ws/src
ROS path [2]=/opt/ros/noetic/share

roscd arbotix_python

如果

[ERROR] [1612251786.438756396]: No link elements found in urdf file
[robot_state_publisher-4] process has died [pid 25563, exit code 1, cmd /opt/ros/noetic/lib/robot_state_publisher/robot_state_publisher __name:=robot_state_publisher __log:=/root/.ros/log/47d52e38-652a-11eb-9227-eb9a2e8153a4/robot_state_publisher-4.log].
log file: /root/.ros/log/47d52e38-652a-11eb-9227-eb9a2e8153a4/robot_state_publisher-4*.log

<robot name="arm" xmlns:xacro="http://www.ros.org/wiki/xacro">
    <xacro:include filename="$(find mbot_description)/urdf/xacro/mbot_base.xacro" />

    <mbot_base/>
robot>

版本问题,修改为


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

    <xacro:include filename="$(find mbot_description)/urdf/xacro/mbot_base.xacro" />

    <xacro:mbot_base/>

robot>

如果

[ WARN] [1612252981.227181315]: Joint state with name: "base_l_wheel_joint" was received but not found in URDF

原因是在robot描述文件URDF中关节定义出错,找到launch文件使用的URDF描述文件,并将left_wheel_joint修改成base_l_wheel_joint,将right_wheel_joint修改为base_r_wheel_joint
参考:https://blog.csdn.net/big_cai/article/details/112357009

No transform from [] to [] TF警告

rosnode list 查看该启动的都启动了
模型出不来,键盘控制小车没动,坐标跑了…

urdf 文件中定义 base_link 到 base_footprint 的 joint。
这个 base_footprint 就把它看成是你的起点,你也要定义他到世界中心的关系。

参考:https://blog.csdn.net/qq_43481884/article/details/105433575

键盘控制

可以新建个功能包

<launch>
  <node name="mbot_teleop" pkg="mbot_teleop" type="mbot_teleop.py" output="screen">
    <param name="scale_linear" value="0.1" type="double"/>
    <param name="scale_angular" value="0.4" type="double"/>
  node>
launch>
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import rospy
from geometry_msgs.msg import Twist
import sys, select, termios, tty

msg = """
Control mbot!
---------------------------
Moving around:
   u    i    o
   j    k    l
   m    ,    .

q/z : increase/decrease max speeds by 10%
w/x : increase/decrease only linear speed by 10%
e/c : increase/decrease only angular speed by 10%
space key, k : force stop
anything else : stop smoothly

CTRL-C to quit
"""

moveBindings = {
        'i':(1,0),
        'o':(1,-1),
        'j':(0,1),
        'l':(0,-1),
        'u':(1,1),
        ',':(-1,0),
        '.':(-1,1),
        'm':(-1,-1),
           }

speedBindings={
        'q':(1.1,1.1),
        'z':(.9,.9),
        'w':(1.1,1),
        'x':(.9,1),
        'e':(1,1.1),
        'c':(1,.9),
          }

def getKey():
    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

speed = .2
turn = 1

def vels(speed,turn):
    return "currently:\tspeed %s\tturn %s " % (speed,turn)

if __name__=="__main__":
    settings = termios.tcgetattr(sys.stdin)
    
    rospy.init_node('mbot_teleop')
    #topic,数据类型Twist(线速度,角速度),队列大小
    pub = rospy.Publisher('/cmd_vel', Twist, queue_size=5)

    x = 0
    th = 0
    status = 0
    count = 0
    acc = 0.1
    target_speed = 0
    target_turn = 0
    control_speed = 0
    control_turn = 0
    try:
        print(msg)
        print(vels(speed,turn))
        while(1):
            key = getKey()
            # 运动控制方向键(1:正方向,-1负方向)
            if key in moveBindings.keys():
                x = moveBindings[key][0]
                th = moveBindings[key][1]
                count = 0
            # 速度修改键
            elif key in speedBindings.keys():
                speed = speed * speedBindings[key][0]  # 线速度增加0.1倍
                turn = turn * speedBindings[key][1]    # 角速度增加0.1倍
                count = 0

                print(vels(speed,turn))
                if (status == 14):
                    print(msg)
                status = (status + 1) % 15
            # 停止键
            elif key == ' ' or key == 'k' :
                x = 0
                th = 0
                control_speed = 0
                control_turn = 0
            else:
                count = count + 1
                if count > 4:
                    x = 0
                    th = 0
                if (key == '\x03'):
                    break

            # 目标速度=速度值*方向值
            target_speed = speed * x
            target_turn = turn * th

            # 速度限位,防止速度增减过快
            if target_speed > control_speed:
                control_speed = min( target_speed, control_speed + 0.02 )
            elif target_speed < control_speed:
                control_speed = max( target_speed, control_speed - 0.02 )
            else:
                control_speed = target_speed

            if target_turn > control_turn:
                control_turn = min( target_turn, control_turn + 0.1 )
            elif target_turn < control_turn:
                control_turn = max( target_turn, control_turn - 0.1 )
            else:
                control_turn = target_turn

            # 创建并发布twist消息
            twist = Twist()
            twist.linear.x = control_speed; 
            twist.linear.y = 0; 
            twist.linear.z = 0
            twist.angular.x = 0; 
            twist.angular.y = 0; 
            twist.angular.z = control_turn
            pub.publish(twist)

    except:
        print(e)

    finally:
        twist = Twist()
        twist.linear.x = 0; twist.linear.y = 0; twist.linear.z = 0
        twist.angular.x = 0; twist.angular.y = 0; twist.angular.z = 0
        pub.publish(twist)

    termios.tcsetattr(sys.stdin, termios.TCSADRAIN, settings)

只要将 Twist 发送给/cmd_vel就能通过键盘控制小车运动了。
ROS学习 ArbotiX+rviz仿真_第1张图片

你可能感兴趣的:(ROS,python,linux)