欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-07-12 12:43:35
...

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 <ssteam>
#include <std_msgs/String.h>
#include <geometry_msgs/PoseStamped.h>
#include <ros/ros.h>

int main(int argc, **argv){
    ros::init(argc, argv, "talker");
    ros::NodeHandle n;
    
    ros::Publisher chatter_pub = n.advertise<geometry_msgs::PoseStamped>("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

<?xml version="1.0"?>
<package format="2">
    <name>communication</name>
    <version>0.0.0</vervion>
    <description>The communication package</description>
    
    <miantainer email>="[email protected]"<maintainer>

    <license>TODO</license>

    <buildtool_depend>catkin</buildtool_depend>
    <build_depend>roscpp</build_depend>
    <build_depend>rospy</build_depend>
    <build_depend>std_msgs</build_depend>
    <build_depend>message_generation</build_depend>
    <build_depend>message_runtime</build_depend>
    <build_depend>geometry_msgs</build_depend>

    <build_export_depend>roscpp</build_export_depend>
    <build_export_depend>rospy</build_export_depend>
    <build_export_depend>std_msgs</build_export_depend>
    <build_export_depend>message_generation</build_export_depend>
    <build_export_depend>message_runtime</build_export_depend>
    <build_export_depend>geometry_msgs</build_export_depend>

    <exec_depend>roscpp</exec_depend>
    <exec_depend>rospy</exec_depend>
    <exec_depend>std_msgs</exec_depend>
    <exec_depend>message_generation</exec_depend>
    <exec_depend>message_runtime</exec_depend>
    <exec_depend>geometry_msgs</exec_depend>
   
    <export>
    </export>
</package>

(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