ROS 自动驾驶多点巡航

2024-06-13 21:28
文章标签 自动 驾驶 ros 多点 巡航

本文主要是介绍ROS 自动驾驶多点巡航,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

ROS 自动驾驶多点巡航:

1、首先创建工作空间:

基于我们的artca_ws;

2、创建功能包:

进入src目录,输入命令:

catkin_create_pkg point_pkg std_msgs rospy roscpp

test_pkg 为功能包名,后面两个是依赖;
在这里插入图片描述

3、创建python文件

我们通过vscode打开src下功能包:
创建 point.py:
在这里插入图片描述
代码内容写入 :

#!/usr/bin/env python  
import rospy  
import actionlib  
import collections
from actionlib_msgs.msg import *  
from geometry_msgs.msg import Pose, PoseWithCovarianceStamped, Point, Quaternion, Twist  
from move_base_msgs.msg import MoveBaseAction, MoveBaseGoal  
from random import sample  
from math import pow, sqrt  class MultiNav():  def __init__(self):  rospy.init_node('MultiNav', anonymous=True)  rospy.on_shutdown(self.shutdown)  # How long in seconds should the robot pause at each location?  self.rest_time = rospy.get_param("~rest_time", 10)  # Are we running in the fake simulator?  self.fake_test = rospy.get_param("~fake_test", False)  # Goal state return values  goal_states = ['PENDING', 'ACTIVE', 'PREEMPTED','SUCCEEDED',  'ABORTED', 'REJECTED','PREEMPTING', 'RECALLING',   'RECALLED','LOST']  # Set up the goal locations. Poses are defined in the map frame.  # An easy way to find the pose coordinates is to point-and-click  # Nav Goals in RViz when running in the simulator.  # Pose coordinates are then displayed in the terminal  # that was used to launch RViz.  locations = collections.OrderedDict()  locations['point-1'] = Pose(Point(5.21, -2.07, 0.00), Quaternion(0.000, 0.000, -0.69, 0.72)) locations['point-2'] = Pose(Point(3.50, -5.78, 0.00), Quaternion(0.000, 0.000, 0.99, 0.021))#locations['point-3'] = Pose(Point(-6.95, 2.26, 0.00), Quaternion(0.000, 0.000, 0.000, 1.000))#locations['point-4'] = Pose(Point(-6.50, 2.04, 0.00), Quaternion(0.000, 0.000, 0.000, 1.000))# Publisher to manually control the robot (e.g. to stop it)  self.cmd_vel_pub = rospy.Publisher('cmd_vel', Twist, queue_size=5)  # Subscribe to the move_base action server  self.move_base = actionlib.SimpleActionClient("move_base", MoveBaseAction)  rospy.loginfo("Waiting for move_base action server...")  # Wait 60 seconds for the action server to become available  self.move_base.wait_for_server(rospy.Duration(10))  rospy.loginfo("Connected to move base server")  # A variable to hold the initial pose of the robot to be set by the user in RViz  initial_pose = PoseWithCovarianceStamped()  # Variables to keep track of success rate, running time, and distance traveled  n_locations = len(locations)  n_goals = 0  n_successes = 0  i = 0  distance_traveled = 0  start_time = rospy.Time.now()  running_time = 0  location = ""  last_location = ""  # Get the initial pose from the user  rospy.loginfo("Click on the map in RViz to set the intial pose...")  rospy.wait_for_message('initialpose', PoseWithCovarianceStamped)  self.last_location = Pose()  rospy.Subscriber('initialpose', PoseWithCovarianceStamped, self.update_initial_pose) keyinput = int(input("Input 0 to continue,or reget the initialpose!\n"))while keyinput != 0:rospy.loginfo("Click on the map in RViz to set the intial pose...")  rospy.wait_for_message('initialpose', PoseWithCovarianceStamped)  rospy.Subscriber('initialpose', PoseWithCovarianceStamped, self.update_initial_pose) rospy.loginfo("Press y to continue,or reget the initialpose!")keyinput = int(input("Input 0 to continue,or reget the initialpose!"))# Make sure we have the initial pose  while initial_pose.header.stamp == "":  rospy.sleep(1)  rospy.loginfo("Starting navigation test")  # Begin the main loop and run through a sequence of locations  for location in locations.keys():  rospy.loginfo("Updating current pose.")  distance = sqrt(pow(locations[location].position.x  - initial_pose.pose.pose.position.x, 2) +  pow(locations[location].position.y -  initial_pose.pose.pose.position.y, 2))  initial_pose.header.stamp = ""  # Store the last location for distance calculations  last_location = location  # Increment the counters  i += 1  n_goals += 1  # Set up the next goal location  self.goal = MoveBaseGoal()  self.goal.target_pose.pose = locations[location]  self.goal.target_pose.header.frame_id = 'map'  self.goal.target_pose.header.stamp = rospy.Time.now()  # Let the user know where the robot is going next  rospy.loginfo("Going to: " + str(location))  # Start the robot toward the next location  self.move_base.send_goal(self.goal)  # Allow 5 minutes to get there  finished_within_time = self.move_base.wait_for_result(rospy.Duration(300))  # Check for success or failure  if not finished_within_time:  self.move_base.cancel_goal()  rospy.loginfo("Timed out achieving goal")  else:  state = self.move_base.get_state()  if state == GoalStatus.SUCCEEDED:  rospy.loginfo("Goal succeeded!")  n_successes += 1  distance_traveled += distance  else:  rospy.loginfo("Goal failed with error code: " + str(goal_states[state]))  # How long have we been running?  running_time = rospy.Time.now() - start_time  running_time = running_time.secs / 60.0  # Print a summary success/failure, distance traveled and time elapsed  rospy.loginfo("Success so far: " + str(n_successes) + "/" +  str(n_goals) + " = " + str(100 * n_successes/n_goals) + "%")  rospy.loginfo("Running time: " + str(trunc(running_time, 1)) +  " min Distance: " + str(trunc(distance_traveled, 1)) + " m")  rospy.sleep(self.rest_time)  def update_initial_pose(self, initial_pose):  self.initial_pose = initial_pose  def shutdown(self):  rospy.loginfo("Stopping the robot...")  self.move_base.cancel_goal()  rospy.sleep(2)  self.cmd_vel_pub.publish(Twist())  rospy.sleep(1)  
def trunc(f, n):  # Truncates/pads a float f to n decimal places without rounding  slen = len('%.*f' % (n, f))  return float(str(f)[:slen])  if __name__ == '__main__':  try:  MultiNav()  rospy.spin()  except rospy.ROSInterruptException:  rospy.loginfo("AMCL navigation test finished.")  

4、编译:

nano@nano-desktop:~/artcar_ws/src$ cd ..
nano@nano-desktop:~/artcar_ws$ catkin build 

在这里插入图片描述

5、案例实操;

启动小车并进入到相应环境:

(1)打开终端,启动底盘环境,输入如下命令:

$ roslaunch artcar_nav artcar_bringup.launch

(2)启动导航程序:

$ roslaunch artcar_nav artcar_move_base.launch

(3)启动RVIZ:

(4)获取点位:

 rostopic echo /move_base_sile/goal 

获取点位

roscar@roscar-virtual-machine:~/artcar_simulation/src$ rostopic echo /move_base_simple/goal 
WARNING: no messages received and simulated time is active.
Is /clock being published?
header: seq: 0stamp: secs: 405nsecs: 141000000frame_id: "odom"
pose: position: x: 5.21420097351y: -2.07076597214z: 0.0orientation: x: 0.0y: 0.0z: -0.69109139328w: 0.722767380375
---
header: seq: 1stamp: secs: 422nsecs:  52000000frame_id: "odom"
pose: position: x: 3.50902605057y: -5.78046607971z: 0.0orientation: x: 0.0y: 0.0z: 0.999777096296w: 0.0211129752124
---

(5)修改point.py文件中点位数据的位置:

在这里插入图片描述

(6 ) 然后开启终端执行:

nano@nano-desktop:~/artcar_ws/src/point_pkg/src$ ./point.py 

在这里插入图片描述

此时确定位置是否准确,准确的话,在此终端中输入:0
小车开始多点运行。

这篇关于ROS 自动驾驶多点巡航的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1058464

相关文章

Go Mongox轻松实现MongoDB的时间字段自动填充

《GoMongox轻松实现MongoDB的时间字段自动填充》这篇文章主要为大家详细介绍了Go语言如何使用mongox库,在插入和更新数据时自动填充时间字段,从而提升开发效率并减少重复代码,需要的可以... 目录前言时间字段填充规则Mongox 的安装使用 Mongox 进行插入操作使用 Mongox 进行更

C语言中自动与强制转换全解析

《C语言中自动与强制转换全解析》在编写C程序时,类型转换是确保数据正确性和一致性的关键环节,无论是隐式转换还是显式转换,都各有特点和应用场景,本文将详细探讨C语言中的类型转换机制,帮助您更好地理解并在... 目录类型转换的重要性自动类型转换(隐式转换)强制类型转换(显式转换)常见错误与注意事项总结与建议类型

IDEA如何让控制台自动换行

《IDEA如何让控制台自动换行》本文介绍了如何在IDEA中设置控制台自动换行,具体步骤为:File-Settings-Editor-General-Console,然后勾选Usesoftwrapsin... 目录IDEA如何让控制台自http://www.chinasem.cn动换行操作流http://www

vscode保存代码时自动eslint格式化图文教程

《vscode保存代码时自动eslint格式化图文教程》:本文主要介绍vscode保存代码时自动eslint格式化的相关资料,包括打开设置文件并复制特定内容,文中通过代码介绍的非常详细,需要的朋友... 目录1、点击设置2、选择远程--->点击右上角打开设置3、会弹出settings.json文件,将以下内

Python脚本实现自动删除C盘临时文件夹

《Python脚本实现自动删除C盘临时文件夹》在日常使用电脑的过程中,临时文件夹往往会积累大量的无用数据,占用宝贵的磁盘空间,下面我们就来看看Python如何通过脚本实现自动删除C盘临时文件夹吧... 目录一、准备工作二、python脚本编写三、脚本解析四、运行脚本五、案例演示六、注意事项七、总结在日常使用

SpringBoot项目启动后自动加载系统配置的多种实现方式

《SpringBoot项目启动后自动加载系统配置的多种实现方式》:本文主要介绍SpringBoot项目启动后自动加载系统配置的多种实现方式,并通过代码示例讲解的非常详细,对大家的学习或工作有一定的... 目录1. 使用 CommandLineRunner实现方式:2. 使用 ApplicationRunne

Springboot的ThreadPoolTaskScheduler线程池轻松搞定15分钟不操作自动取消订单

《Springboot的ThreadPoolTaskScheduler线程池轻松搞定15分钟不操作自动取消订单》:本文主要介绍Springboot的ThreadPoolTaskScheduler线... 目录ThreadPoolTaskScheduler线程池实现15分钟不操作自动取消订单概要1,创建订单后

python实现自动登录12306自动抢票功能

《python实现自动登录12306自动抢票功能》随着互联网技术的发展,越来越多的人选择通过网络平台购票,特别是在中国,12306作为官方火车票预订平台,承担了巨大的访问量,对于热门线路或者节假日出行... 目录一、遇到的问题?二、改进三、进阶–展望总结一、遇到的问题?1.url-正确的表头:就是首先ur

Spring使用@Retryable实现自动重试机制

《Spring使用@Retryable实现自动重试机制》在微服务架构中,服务之间的调用可能会因为一些暂时性的错误而失败,例如网络波动、数据库连接超时或第三方服务不可用等,在本文中,我们将介绍如何在Sp... 目录引言1. 什么是 @Retryable?2. 如何在 Spring 中使用 @Retryable

使用 Python 和 LabelMe 实现图片验证码的自动标注功能

《使用Python和LabelMe实现图片验证码的自动标注功能》文章介绍了如何使用Python和LabelMe自动标注图片验证码,主要步骤包括图像预处理、OCR识别和生成标注文件,通过结合Pa... 目录使用 python 和 LabelMe 实现图片验证码的自动标注环境准备必备工具安装依赖实现自动标注核心