ROS学习:智能车室外光电组仿真

一、模型建立

首先先准备我们的开源仿真包mit-racecar,博主已经把资源上传到CSDN上了。需要请自取。
https://download.csdn.net/download/weixin_44606638/12470588
接下来将racecar_gazebo放入工作区间
ROS学习:智能车室外光电组仿真_第1张图片编译工作区间

catkin_make

ROS学习:智能车室外光电组仿真_第2张图片
设置环境变量

echo "source ~/catkin_ws/devel/setup.bash" >> ~/.bashrc
source ~/.bashrc

在这里插入图片描述
这下准备就完成了,可以启动launc文件了。

roslaunch racecar_gazebo racecar.launch

ROS学习:智能车室外光电组仿真_第3张图片
注意,如果出现raise ResourceNotFound(name, ros_paths=self._ros_paths)
ResourceNotFound: racecar
说明你没有racecar的模型
在这里插入图片描述
还是老样子,博主博主已经把资源上传到CSDN上了。需要请自取。
https://download.csdn.net/download/weixin_44606638/12470596

如果你执行成功,就会启动gazebo,就可以看见小车模型了。
ROS学习:智能车室外光电组仿真_第4张图片

二、准备赛道

目前比赛规则已经渐渐明朗,参考官方,制作标准比赛仿真环境,学习阿克曼小车控制,ROS基础和相关智能车算法(后续会补充)。开启Building Editor,依据下图绘制环境:
ROS学习:智能车室外光电组仿真_第5张图片这时,我们在gazebo中按Ctrl+B,就会弹出绘图画面。
ROS学习:智能车室外光电组仿真_第6张图片选择Wall,在白色区域画出你需要的赛道模型。
ROS学习:智能车室外光电组仿真_第7张图片保存并应用:
ROS学习:智能车室外光电组仿真_第8张图片效果展示:
ROS学习:智能车室外光电组仿真_第9张图片当然,如果你还要添加障碍物之类的,可以在Insert中自行选择添加。这里我就不多添加了。
ROS学习:智能车室外光电组仿真_第10张图片

三、控制小车

新建文件racecar.py
ROS学习:智能车室外光电组仿真_第11张图片
Python代码:

#!/usr/bin/env python
 
"""
template_node.py
LCHS RACECAR 2016-17
Written by Braden Oh
This program serves as a template for a working node.
It contains a useful implementation of the drive function,
an outline for the main function, and contains all commands
necessary to initialize a node.  It also contains comments
outlining specific documentation.
"""
 
#----------------------------------------------------------
# IMPORTS
#----------------------------------------------------------
import rospy											# Import ROS libraries
from sensor_msgs.msg import LaserScan					# Import required msg types
from ackermann_msgs.msg import AckermannDriveStamped	
# Other imports here...
 
#----------------------------------------------------------
# CONSTANTS
#----------------------------------------------------------
DATA_IN = '/scan'									# Read data from this topic
DATA_OUT = 'vesc/ackermann_cmd_mux/input/navigation'	# Publish data to this topic
NODE_NAME = 'sampleController'						# Name of the node
SPEED = 1.0											# Default speed in m/s
# Other constants here...
 
#----------------------------------------------------------
# Functions
#----------------------------------------------------------
def drive(speed, angle, myPublisher):
        msg = AckermannDriveStamped()           # Initializes msg object
        msg.drive.speed = speed                 # Sets msg speed to entered speed
        msg.drive.acceleration = 0              # Sets msg acceleration to 0
        msg.drive.jerk = 1                      # Sets msg jerk to 1
        msg.drive.steering_angle = angle        # Sets msg steering angle to entered angle
        msg.drive.steering_angle_velocity = 1   # Sets msg angle velocity to 1
        myPublisher.publish(msg)              	# Publishes the message
        
# def myFunction(parameter_1, parameter_n):
	# Other functions here
 
#============================
# MAIN
#============================
def main(msg):
 
	# Initialize publisher object
	myPublisher = rospy.Publisher(DATA_OUT,AckermannDriveStamped,queue_size=10)
 
	# Useful calculations here...
	
	# Use the publisher
	# i.e. to drive straight ahead, call the drive function:
	# drive(SPEED, 0, myPublisher)  
 
#============================
 
rospy.init_node(NODE_NAME)					# Initialize node with preset name
rospy.Subscriber(DATA_IN,LaserScan,main)	# Create subscriber object
											#  - Reads data from preset topic
											#  - Data of type LaserScan
											#  - Upon new data, execute main
rospy.spin()	# Continue running until user stops this node
 
 

赋予可执行文件:

chmod 777 *

执行脚本:

python racecar.py

结果显示:
ROS学习:智能车室外光电组仿真_第12张图片

你可能感兴趣的:(ROS学习:智能车室外光电组仿真)