neato xv21 ros下使用

news/2025/2/12 0:42:41/

修改neato_node 下neato.py文件如下:

#!/usr/bin/env python

# ROS node for the Neato Robot Vacuum
# Copyright (c) 2010 University at Albany. All right reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of the University at Albany nor the names of its
#       contributors may be used to endorse or promote products derived
#       from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL VANADIUM LABS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""
ROS node for Neato XV-11 Robot Vacuum.
"""

__author__ = "ferguson@cs.albany.edu (Michael Ferguson)"

import roslib; roslib.load_manifest("neato_node")
import rospy
from math import sin,cos

from sensor_msgs.msg import LaserScan
from geometry_msgs.msg import Quaternion
from geometry_msgs.msg import Twist
from nav_msgs.msg import Odometry
from tf.broadcaster import TransformBroadcaster

from neato_driver.neato_driver import xv11, BASE_WIDTH, MAX_SPEED

class NeatoNode:

    def __init__(self):
        """ Start up connection to the Neato Robot. """
        rospy.init_node('neato')

        self.port = rospy.get_param('~port', "/dev/ttyACM0")
        rospy.loginfo("Using port: %s"%(self.port))

        self.robot = xv11(self.port)

        rospy.Subscriber("cmd_vel", Twist, self.cmdVelCb)
        #self.scanPub = rospy.Publisher('base_scan', LaserScan, queue_size=10)
        #self.odomPub = rospy.Publisher('odom', Odometry, queue_size=10)
        self.scanPub = rospy.Publisher('base_scan', LaserScan)
        self.odomPub = rospy.Publisher('odom', Odometry)
        self.odomBroadcaster = TransformBroadcaster()

        self.cmd_vel = [0,0]

    def spin(self):        
        encoders = [0,0]

        self.x = 0                  # position in xy plane
        self.y = 0
        self.th = 0
        then = rospy.Time.now()

        # things that don't ever change
        scan_link = rospy.get_param('~frame_id','base_laser_link')
        scan = LaserScan(header=rospy.Header(frame_id=scan_link))
        scan.angle_min = 0
        scan.angle_max = 6.26
        scan.angle_increment = 0.017437326
        scan.range_min = 0.020
        scan.range_max = 5.0
        odom = Odometry(header=rospy.Header(frame_id="odom"), child_frame_id='base_link')
    
        # main loop of driver
        r = rospy.Rate(5)
        self.robot.requestScan()
        while not rospy.is_shutdown():
            # prepare laser scan
            scan.header.stamp = rospy.Time.now()    
            #self.robot.requestScan()
            scan.ranges = self.robot.getScanRanges()

            # get motor encoder values
            left, right = self.robot.getMotors()

            # send updated movement commands
            self.robot.setMotors(self.cmd_vel[0], self.cmd_vel[1], max(abs(self.cmd_vel[0]),abs(self.cmd_vel[1])))
            
            # ask for the next scan while we finish processing stuff
            self.robot.requestScan()
            
            # now update position information
            dt = (scan.header.stamp - then).to_sec()
            then = scan.header.stamp

            d_left = (left - encoders[0])/1000.0
            d_right = (right - encoders[1])/1000.0
            encoders = [left, right]
            
            dx = (d_left+d_right)/2
            dth = (d_right-d_left)/(BASE_WIDTH/1000.0)

            x = cos(dth)*dx
            y = -sin(dth)*dx
            self.x += cos(self.th)*x - sin(self.th)*y
            self.y += sin(self.th)*x + cos(self.th)*y
            self.th += dth

            # prepare tf from base_link to odom
            quaternion = Quaternion()
            quaternion.z = sin(self.th/2.0)
            quaternion.w = cos(self.th/2.0)

            # prepare odometry
            odom.header.stamp = rospy.Time.now()
            odom.pose.pose.position.x = self.x
            odom.pose.pose.position.y = self.y
            odom.pose.pose.position.z = 0
            odom.pose.pose.orientation = quaternion
            odom.twist.twist.linear.x = dx/dt
            odom.twist.twist.angular.z = dth/dt

            # publish everything
            self.odomBroadcaster.sendTransform( (self.x, self.y, 0), (quaternion.x, quaternion.y, quaternion.z, quaternion.w),
                then, "base_link", "odom" )
            self.scanPub.publish(scan)
            self.odomPub.publish(odom)

            # wait, then do it again
            r.sleep()

        # shut down
        self.robot.setLDS("off")
        self.robot.setTestMode("off")

    def cmdVelCb(self,req):
        x = req.linear.x * 1000
        th = req.angular.z * (BASE_WIDTH/2)
        k = max(abs(x-th),abs(x+th))
        # sending commands higher than max speed will fail
        if k > MAX_SPEED:
            x = x*MAX_SPEED/k; th = th*MAX_SPEED/k
        self.cmd_vel = [ int(x-th) , int(x+th) ]

if __name__ == "__main__":    
    robot = NeatoNode()
    robot.spin()



修改完后通过roslaunch neato_node bringup.launch实现neato的驱动


通过rostopic pub -r 10 /cmd_vel geometry_msgs/Twis '{linear:  {x: 0.0, y: 0.0, z: 0.0}, angular: {x: 0.0,y: 0.0,z: 0.0}}'可以测试neato xv-21运动能力。



http://www.ppmy.cn/news/558793.html

相关文章

激光雷达neato_laser与电脑连接记录

激光雷达neato_laser与电脑连接记录 第一步,建立工作空间 在虚拟机打开一个新的终端 mkdir -p ~/shizhan01_ws/src (shizhan01_ws是工作空间名字,这个名字不建议取catkin_ws,因为网上很多例程都是用这个名字作为命名空间&#…

贝壳扫地机器人_6千的扫地机器人是否值得买?有戴森手持的家,使用NEATO D7的体验及选购建议...

家里有个戴森V6,平时老公用得比较多,可能是因为他戴眼镜看地上我的掉发看得比较清楚吧,反正我总是不觉得家里脏。之前日常使用频率大概是一周1-2次。 主要是孩子床底、沙发、电视柜底下用手持清扫需要弯腰,就用扫地机器人了。 ——…

在ROS中使用Neato XV-11 激光雷达

本文转自雷达大叔:http://make4e.com/pages/g000007 在ROS系统中使用XV-11 Lidar 进行SLAM,需要做如下软硬件的准备: 硬件: xv-11 激光雷达一个 (店里有卖 http://y0.cn/lidar) USB 转换串口转接线一个 (店里也有卖&…

ValueError: Program neato not found in path.

说明: python程序里有用到graphviz里的neato 已经在pycharm里install graphviz库了 还是报错:ValueError: Program neato not found in path. 解决: 错误提示在path找不到neato程序,那我们就添加路径让它找到neato程序。 1.安装…

Neato XV-11-laser 激光雷达测试和 hector-mapping

采用XV-11驱动的雷达进行测试和绘图 串口配置> 波特率&#xff1a;115200 N 8 1 雷达帧格式> <start> <index> <speed_l> <speed_h> [Data 0] [Data 1] [Data 2] [Data 3] <checksum_l> <checksum_h> <start>…

Graphviz安装配置及入门(windows安装亲测)

Graphviz 是一个开源工具&#xff0c;可以运行在类似于 UNIX 的大多数平台和 Microsoft Windows 之上。适用于大多数平台的二进制文件可以在 Graphviz 主页 上找到。AIX 二进制文件可以在 perzl.org 上找到。 Graphviz 应用程序中有多种工具可以生成各种类型的图表&#xff08…

ROS中使用Neato XV-11激光雷达

说明&#xff1a; 1、Neato XV-11介绍 2、在ROS中使用 Neato XV-11 图示&#xff1a; 数据参数&#xff1a; 串口通讯&#xff1a;速率 115200 8N1 雷达每完整旋转一周会发送90个数据包 每个数据包包含4个测量点的信息 每个数据包的长度固定是22字节 数据包格式&#…

项目实战——旅行社导航页

html代码&#xff1a; <!DOCTYPE html> <html lang"zh-cn"> <head><meta charset"UTF-8"><title>项目实战</title><link rel"stylesheet" href"css/basic.css"><link rel"stylesh…