无人驾驶仿真(六):编写基于ROS的C++测试代码,控制车辆运动

1. 为什么要基于ROS编写代码?

使用其他的机器人操作系统均可,本博客使用基础的ROS平台。

2. 需要做哪些准备?

windows:完整的prescan仿真模型,包括车辆,环境

linux:ROS1.0完整版本

3. ROS工程代码:

(1)创建工程项目:

mkdir -p ~/talker/src
cd talker/src
catkin_init_workspace
cd ..
catkin_make
cd src
catkin_create_package communication roscpp rospy std_msgs geometry_msgs

(2)编写talker代码:

#include 
#include 
#include 
#include 

int main(int argc, **argv){
    ros::init(argc, argv, "talker");
    ros::NodeHandle n;
    
    ros::Publisher chatter_pub = n.advertise("talker", 10);
    ros::Rate loop_rate(10);

    while(ros::ok()){
        geometry_msgs::PoseStamped pose_msg;
        
        pose_msg.pose.position.x=1;
        pose_msg.pose.position.y=2;
        pose_msg.pose.position.z=3;

        pose_msg.pose.orientation.x=3;
    
    chatter_pub.publish(pose_msg);
    ros::spinOnce();
    loop_rate.sleep;

}
}

(3)编辑CMakeLists.txt

cmake_minimum_requried(VERSION 2.8.3)
project(communication)

find_package(catkin REQUIRED COMPONENTS
    roscpp
    rospy
    std_msgs
    message_generation
    geometry_msgs
)

add_message_files(FILES PointStamped.msg)

generate_messages(DEPENDENCIES std_msgs)

catkin_package(CATKIN_DEPENDS roscpp rospy message_runtime message_generation geometry_msgs)

include_directories(
    include
    $(catkin_INCLUDE_DIRS)
)

add_executalbe(talker src/talker.cpp)
target_link_libraries(talker $(catkin_LIBRARIES))

(4)编辑packge.xml



    communication
    0.0.0
    The communication package
    
    ="[email protected]"

    TODO

    catkin
    roscpp
    rospy
    std_msgs
    message_generation
    message_runtime
    geometry_msgs

    roscpp
    rospy
    std_msgs
    message_generation
    message_runtime
    geometry_msgs

    roscpp
    rospy
    std_msgs
    message_generation
    message_runtime
    geometry_msgs
   
    
    

(5)配置环境:

cd ~/talker
catkin_make

source devel/setup.bash
export ROS_MASTER_URI=http://192.168.*.*:*** //rosmaster主机(Linux)ip和端口
epxort ROS_HOSTNAME=192.168.*.* //windows主机ip
rosrun communicaiton talker

 

你可能感兴趣的:(ROS,自动驾驶仿真)