主要参考:
智能车竞赛——室外光电创意组仿真之脱缰的野马
这个教程基本可以实现,在自己操作之前最好先把这个大佬的教程捋一遍!!!
最后附上大佬的bug总汇!在最下面!还有自己遇到的bug!
mkdir -p ~/hg_racecar_ws/src
cd ~/racecar_ws/src
catkin_init_workplace
mkdir hg_racecar_control
mkdir hg_racecar_description
mkdir hg_racecar_gazebo
将下载下来的比赛以及赛道模型文件打开
打开racecar_description文件夹,将其中的所有的文件全部copy到我们自己创建的hg_racecar_description文件夹中;
将smart_plane整个文件夹全部copy到hg_racecar_ws/src文件夹下;
打开hg_racecar_control文件夹,创建script文件夹,将control_plugin.py文件copy到script文件夹里面去;
打开hg_racecar_gazebo文件夹,创建worlds文件夹,将racetrack.world文件copy到worlds文件夹里面去;
至此,下载下来的比赛以及赛道模型文件中的全部内容移植完毕!
打开roboware stdio,将创建好的工作空间拖到其中,由于我们功能包的文件名叫做hg_...,因此我们需要修改文件中的内容
打开roboware stdio中左侧面板的搜索功能
将racecar_description字段全部替换为hg_racecar_description;
将racecar_gazebo字段全部替换为hg_racecar_gazebo;
cd ~/hg_racecar_ws/src/hg_racecar_gazebo
gedit CMakeLists.txt
将一下内容复制到里面,然后保存退出
cmake_minimum_required(VERSION 2.8.3)
project(hg_racecar_gazebo)
## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
set(OpenCV_DIR /opt/ros/kinetic/share/OpenCV-3.3.1-dev/)
find_package(catkin REQUIRED COMPONENTS
gazebo_ros
geometry_msgs
OpenCV
roscpp
cv_bridge
image_transport
)
## System dependencies are found with CMake's conventions
# find_package(Boost REQUIRED COMPONENTS system)
## Uncomment this if the package has a setup.py. This macro ensures
## modules and global scripts declared therein get installed
## See http://ros.org/doc/api/catkin/html/user_guide/setup_dot_py.html
# catkin_python_setup()
################################################
## Declare ROS messages, services and actions ##
################################################
## To declare and build messages, services or actions from within this
## package, follow these steps:
## * Let MSG_DEP_SET be the set of packages whose message types you use in
## your messages/services/actions (e.g. std_msgs, actionlib_msgs, ...).
## * In the file package.xml:
## * add a build_depend tag for "message_generation"
## * add a build_depend and a run_depend tag for each package in MSG_DEP_SET
## * If MSG_DEP_SET isn't empty the following dependency has been pulled in
## but can be declared for certainty nonetheless:
## * add a run_depend tag for "message_runtime"
## * In this file (CMakeLists.txt):
## * add "message_generation" and every package in MSG_DEP_SET to
## find_package(catkin REQUIRED COMPONENTS ...)
## * add "message_runtime" and every package in MSG_DEP_SET to
## catkin_package(CATKIN_DEPENDS ...)
## * uncomment the add_*_files sections below as needed
## and list every .msg/.srv/.action file to be processed
## * uncomment the generate_messages entry below
## * add every package in MSG_DEP_SET to generate_messages(DEPENDENCIES ...)
## Generate messages in the 'msg' folder
# add_message_files(
# FILES
# Message1.msg
# Message2.msg
# )
## Generate services in the 'srv' folder
# add_service_files(
# FILES
# Service1.srv
# Service2.srv
# )
## Generate actions in the 'action' folder
# add_action_files(
# FILES
# Action1.action
# Action2.action
# )
## Generate added messages and services with any dependencies listed here
# generate_messages(
# DEPENDENCIES
# std_msgs # Or other packages containing msgs
# )
################################################
## Declare ROS dynamic reconfigure parameters ##
################################################
## To declare and build dynamic reconfigure parameters within this
## package, follow these steps:
## * In the file package.xml:
## * add a build_depend and a run_depend tag for "dynamic_reconfigure"
## * In this file (CMakeLists.txt):
## * add "dynamic_reconfigure" to
## find_package(catkin REQUIRED COMPONENTS ...)
## * uncomment the "generate_dynamic_reconfigure_options" section below
## and list every .cfg file to be processed
## Generate dynamic reconfigure parameters in the 'cfg' folder
# generate_dynamic_reconfigure_options(
# cfg/DynReconf1.cfg
# cfg/DynReconf2.cfg
# )
###################################
## catkin specific configuration ##
###################################
## The catkin_package macro generates cmake config files for your package
## Declare things to be passed to dependent projects
## INCLUDE_DIRS: uncomment this if you package contains header files
## LIBRARIES: libraries you create in this project that dependent projects also need
## CATKIN_DEPENDS: catkin_packages dependent projects also need
## DEPENDS: system dependencies of this project that dependent projects also need
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES hg_racecar_gazebo
# CATKIN_DEPENDS gazebo_ros
# DEPENDS system_lib
)
###########
## Build ##
###########
## Specify additional locations of header files
## Your package locations should be listed before other locations
# include_directories(include)
include_directories(
${catkin_INCLUDE_DIRS}
${OpenCV_INCLUDE_DIRS}
)
## Declare a C++ library
# add_library(hg_racecar_gazebo
# src/${PROJECT_NAME}/hg_racecar_gazebo.cpp
# )
## Add cmake target dependencies of the library
## as an example, code may need to be generated before libraries
## either from message generation or dynamic reconfigure
# add_dependencies(hg_racecar_gazebo ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
## Declare a C++ executable
add_executable(findLine src/findLine.cpp)
## Add cmake target dependencies of the executable
## same as for the library above
# add_dependencies(hg_racecar_gazebo_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
## Specify libraries to link a library or executable target against
target_link_libraries(findLine
${catkin_LIBRARIES}
${OpenCV_LiBRARIES}
)
add_dependencies(findLine findLine_tutorials_generate_messages_cpp)
#############
## Install ##
#############
# all install targets should use catkin DESTINATION variables
# See http://ros.org/doc/api/catkin/html/adv_user_guide/variables.html
## Mark executable scripts (Python etc.) for installation
## in contrast to setup.py, you can choose the destination
# install(PROGRAMS
# scripts/my_python_script
# DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
# )
## Mark executables and/or libraries for installation
# install(TARGETS hg_racecar_gazebo hg_racecar_gazebo_node
# ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
# LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
# RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
# )
## Mark cpp header files for installation
# install(DIRECTORY include/${PROJECT_NAME}/
# DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
# FILES_MATCHING PATTERN "*.h"
# PATTERN ".svn" EXCLUDE
# )
## Mark other files for installation (e.g. launch and bag files, etc.)
# install(FILES
# # myfile1
# # myfile2
# DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
# )
#############
## Testing ##
#############
## Add gtest based cpp test target and link libraries
# catkin_add_gtest(${PROJECT_NAME}-test test/test_hg_racecar_gazebo.cpp)
# if(TARGET ${PROJECT_NAME}-test)
# target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME})
# endif()
## Add folders to be run by python nosetests
# catkin_add_nosetests(test)
gedit package.xml
复制下面内容到文件中,然后保存退出
hg_racecar_gazebo
0.0.0
The hg_racecar_gazebo package
ubuntu
TODO
catkin
gazebo_ros
gazebo_ros
controller_manager
ros_controllers
ros_control
gazebo_ros_control
geometry_msgs
roscpp
image_transport
cv_bridge
trajectory_msgs
geometry_msgs
roscpp
image_transport
cv_bridge
trajectory_msgs
mkdir src
cd src
gedit findLine.cpp
复制下面的内容然后到文件中,保存退出
#include
#include
#include
#include "std_msgs/Float64.h"
#include "sensor_msgs/Image.h"
#include "geometry_msgs/Twist.h"
#include "ackermann_msgs/AckermannDrive.h"
#include "ackermann_msgs/AckermannDriveStamped.h"
#include
#include
#include
#include "cv_bridge/cv_bridge.h"
#include
#include
#include
#include
using namespace cv;
class Follower{
public:
ros::NodeHandle node;
static ros::Publisher cmdpub;
static ros::Subscriber imageSub;
static void image_callback(const sensor_msgs::ImageConstPtr& msg);
static void speed_contrl(float speed_car,float angluar_car);
Follower();
};
ros::Publisher Follower::cmdpub;
ros::Subscriber Follower::imageSub;
int main(int argc, char* argv[])
{
ros::init(argc,argv,"follower_cpp");
Follower f;//调用构造函数
while(ros::ok())
{
ros::Rate loop_rate(0.2);
//这里是程序的循环处,可以写你的代码
ros::spinOnce();
}
}
void Follower::image_callback(const sensor_msgs::ImageConstPtr& msg)
{
cv_bridge::CvImagePtr cv_ptr;
try
{
cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8);
}
catch(cv_bridge::Exception& e)
{
ROS_ERROR("cv_bridge exception: %s", e.what());
return;
}
Mat hsv = cv_ptr->image.clone();
Mat mask = cv_ptr->image.clone();
cvtColor(cv_ptr->image, hsv, COLOR_BGR2HSV);
double low_H = 0;
double low_S = 0;
double low_V = 100;
double high_H = 180;
double high_S = 30;
double high_V = 255;
inRange(hsv, Scalar(low_H, low_S, low_V), Scalar(high_H, high_S, high_V), mask);
speed_contrl(0.5,0.5);
imshow("mask",mask);
waitKey(3);
}
void Follower::speed_contrl(float speed_car,float angluar_car)
{
ackermann_msgs::AckermannDriveStamped ack;
ack.drive.speed = speed_car;
ack.drive.steering_angle = angluar_car;
cmdpub.publish(ack);
}
Follower::Follower()
{
cmdpub = node.advertise("/vesc/low_level/ackermann_cmd_mux/input/teleop", 10, true);
imageSub = node.subscribe("/camera/zed/rgb/image_rect_color",10,image_callback);
}
cd ~/hg_racecar_ws/src
catkin_make
如果编译成功则会输出:
gedit ~/.bashrc
然后将下面这句话添加到最后一行,保存退出:
source ~/hg_racecar_ws/devel/setup.bash
roslaunch hg_racecar_description racecar_rviz.launch
或者只是运行gazebo
roslaunch hg_racecar_description racecar.launch
如果输出成功那么就会显示如下:
注意,添加source...到 ~/.bashrc文件的时候一定要添加到最后一行,不然可能找不到功能包!!!
'ascii' codec can't decode byte 0xe6 in position 258: ordinal not in ra The traceback for the exception was written to the log file 如果运行launch出现这个错误,多半就是文件中含有中文字母!!! 还有可能就是 如果找地图的模型的时候,要是没有添加CMakeLists.txt和package.xml文件,也会报这样的错误,这个时候需要在放置地图的文件夹中添加CMakeLists.txt和package.xml文件,同时主要文件内容的功能包的名字的修改!!!
如果遇到这个cmd 139 Segmentation fault的问题就是gazebo的版本不合适,比如你用的是gazebo9,但是其实换成gazebo7就解决问题了
但是!!!要是遇到类似的问题,根据一个大佬说,你可以这么考虑:
对于情况三而言,平时避免,就是不要直接插掉软件,可以通过ctrl+c的方法来kill,要是不行的话,直接 运行下面的命令:
killall gzserver killall gzclient
!!!
要是少了什么依赖,直接少什么加什么,或者直接百度都有!!!
其他问题可以在大佬的bug总汇中查到:(这个实在这个大佬的教程的基础上进行的,最好把大佬的运行一遍熟悉熟悉)
# 智能车仿真 代码使用参考教程:[教程地址](https://www.guyuehome.com/6463) # bug汇总 ## 报错controllers相关 ```bash sudo apt-get install ros-kinetic-controller-manager sudo apt-get install ros-kinetic-gazebo-ros-control sudo apt-get install ros-kinetic-effort-controllers sudo apt-get install ros-kinetic-joint-state-controller ``` ## 报错driver_base相关 ```bash sudo apt-get install ros-kinetic-driver-base ``` ## 报错rtabmap相关 ```bash sudo apt-get install ros-kinetic-rtabmap-ros ``` ## 报错ackermann_msgs相关 ```bash sudo apt-get install ros-kinetic-ackermann-msgs ``` ## 报错findline.cpp找不到opencv头文件 执行:`locate OpenCVConfig.cmake`得到你的opencv的路径 执行:`gedit ~/racecar_ws/src/racecar_gazebo/CMakeLists.txt` 修改第7行的路径成你的路径:set(OpenCV_DIR /opt/ros/kinetic/share/OpenCV-3.3.1-dev/) ## 报错Failed to create the teb_local_planner/TebLocalPlannerROS planner ```bash sudo apt-get install ros-kinetic-teb-local-planner ``` ## 没有laser相关话题,无法建图 **这个纯属个人失误,写四轮摄像头组的博客的时候觉得激光雷达的蓝色太碍事了就把它关了......我单纯的以为只是关闭了激光显示,原来是把数据都关了!抱歉~** ~/racecar_ws/src/racecar_description/urdf/racecar.gazebo在这个文件中61行改成false即可,代码我已修复,直接下载没有问题的