这里的自定义msg和python的其实是一样的:
首先在src目录下
catkin_create_pkg car_interfaces rospy roscpp std_msgs message_runtime message_generation
然后新建一个msg文件夹,然后建立相应的msg文件,接着就可以修改编译所需的东西了
定义的msg就自己想怎么写就怎么写吧
首先是CMakeLists.txt:
cmake_minimum_required(VERSION 3.0.2)
project(car_interfaces)
## Compile as C++11, supported in ROS Kinetic and newer
# add_compile_options(-std=c++11)
## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS
message_generation
message_runtime
roscpp
rospy
std_msgs
)
add_message_files(
FILES
GlobalPathPlanningInterface.msg
GpsImuInterface.msg
)
generate_messages(
DEPENDENCIES
std_msgs
)
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES car_interfaces
CATKIN_DEPENDS message_generation message_runtime roscpp rospy std_msgs
# DEPENDS system_lib
)
include_directories(
# include
${catkin_INCLUDE_DIRS}
)
然后是package.xml:
<?xml version="1.0"?>
<package format="2">
<name>car_interfaces</name>
<version>0.0.0</version>
<description>The car_interfaces package</description>
<!-- One maintainer tag required, multiple allowed, one person per tag -->
<!-- Example: -->
<!-- <maintainer email="[email protected]">Jane Doe</maintainer> -->
<maintainer email="[email protected]">cyun</maintainer>
<!-- One license tag required, multiple allowed, one license per tag -->
<!-- Commonly used license strings: -->
<!-- BSD, MIT, Boost Software License, GPLv2, GPLv3, LGPLv2.1, LGPLv3 -->
<license>TODO</license>
<!-- Url tags are optional, but multiple are allowed, one per tag -->
<!-- Optional attribute type can be: website, bugtracker, or repository -->
<!-- Example: -->
<!-- <url type="website">http://wiki.ros.org/car_interfaces -->
<!-- Author tags are optional, multiple are allowed, one per tag -->
<!-- Authors do not have to be maintainers, but could be -->
<!-- Example: -->
<!-- <author email="[email protected]">Jane Doe</author> -->
<!-- The *depend tags are used to specify dependencies -->
<!-- Dependencies can be catkin packages or system dependencies -->
<!-- Examples: -->
<!-- Use depend as a shortcut for packages that are both build and exec dependencies -->
<!-- <depend>roscpp</depend> -->
<!-- Note that this is equivalent to the following: -->
<!-- <build_depend>roscpp</build_depend> -->
<!-- <exec_depend>roscpp</exec_depend> -->
<!-- Use build_depend for packages you need at compile time: -->
<!-- <build_depend>message_generation</build_depend> -->
<!-- Use build_export_depend for packages you need in order to build against this package: -->
<!-- <build_export_depend>message_generation</build_export_depend> -->
<!-- Use buildtool_depend for build tool packages: -->
<!-- <buildtool_depend>catkin</buildtool_depend> -->
<!-- Use exec_depend for packages you need at runtime: -->
<!-- <exec_depend>message_runtime</exec_depend> -->
<!-- Use test_depend for packages you need only for testing: -->
<!-- <test_depend>gtest</test_depend> -->
<!-- Use doc_depend for packages you need only for building documentation: -->
<!-- <doc_depend>doxygen</doc_depend> -->
<buildtool_depend>catkin</buildtool_depend>
<build_depend>message_generation</build_depend>
<build_depend>message_runtime</build_depend>
<build_depend>roscpp</build_depend>
<build_depend>rospy</build_depend>
<build_depend>std_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>
<exec_depend>message_runtime</exec_depend>
<exec_depend>message_generation</exec_depend>
<exec_depend>roscpp</exec_depend>
<exec_depend>rospy</exec_depend>
<exec_depend>std_msgs</exec_depend>
<!-- The export tag contains other, unspecified, tags -->
<export>
<!-- Other tools can request additional information be placed here -->
</export>
</package>
基本上就按照这个结构来写,然后正常编译就可以了
#include
#include
// #include
int main(int argc, char *argv[])
{ ros::init(argc, argv, "plan_node") ;
ros::NodeHandle nh;
ros::Publisher pub = nh.advertise<car_interfaces::GlobalPathPlanningInterface>("global",10);
// ros::Publisher pub2 = nh.advertise("gps",10);
ros::Rate loop_rate(10);
while (ros::ok())
{
ROS_INFO("SUCCESS");
car_interfaces::GlobalPathPlanningInterface msg1;
// car_interfaces::GpsImuInterface msg2;
msg1.timestamp = 1000;
msg1.process_time = 230;
// msg2.gps_time = 10000;
pub.publish(msg1);
// pub2.publish(msg2);
ros::spinOnce();
loop_rate.sleep();
}
return 0;
}
这个是发布的部分,注意思路,将接收的全部开成一个线程,将发布的话题每个都写成一个线程。
然后是发布的数据
#include
#include
#include
// 回调函数
void plan_message_callback(const car_interfaces::GlobalPathPlanningInterface::ConstPtr& msg)
{
double timestamp = msg->timestamp;
float process_time = msg->process_time;
ROS_INFO("Received plan");
}
int main(int argc, char* argv[])
{
ros::init(argc, argv, "plan_sub");
ros::NodeHandle nh;
ros::Publisher pub = nh.advertise<car_interfaces::GpsImuInterface>("pub2", 10);
ros::Subscriber sub = nh.subscribe("global", 10, plan_message_callback);
ros::Rate loop_rate(10);
while (ros::ok())
{
car_interfaces::GpsImuInterface msg;
msg.gps_time = 10000;
pub.publish(msg);
ros::spinOnce();
loop_rate.sleep();
}
return 0;
}
然后修改相应的CMameLists.txt:
cmake_minimum_required(VERSION 3.0.2)
project(planning)
## Compile as C++11, supported in ROS Kinetic and newer
# add_compile_options(-std=c++11)
## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS
car_interfaces
roscpp
rospy
std_msgs
)
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES planning
# CATKIN_DEPENDS car_interfaces roscpp rospy std_msgs
# DEPENDS system_lib
)
## Specify additional locations of header files
## Your package locations should be listed before other locations
include_directories(
# include
${catkin_INCLUDE_DIRS}
)
add_executable(${PROJECT_NAME}_node src/plan.cpp)
add_executable(plan_sub_node src/plan_sub.cpp)
add_dependencies(${PROJECT_NAME}_node car_interfaces_generate_messages_cpp)
# Specify libraries to link a library or executable target against
target_link_libraries(${PROJECT_NAME}_node
${catkin_LIBRARIES}
)
target_link_libraries(plan_sub_node
${catkin_LIBRARIES}
)
package.xml:
<?xml version="1.0"?>
<package format="2">
<name>planning</name>
<version>0.0.0</version>
<description>The planning package</description>
<!-- One maintainer tag required, multiple allowed, one person per tag -->
<!-- Example: -->
<!-- <maintainer email="[email protected]">Jane Doe</maintainer> -->
<maintainer email="[email protected]">cyun</maintainer>
<!-- One license tag required, multiple allowed, one license per tag -->
<!-- Commonly used license strings: -->
<!-- BSD, MIT, Boost Software License, GPLv2, GPLv3, LGPLv2.1, LGPLv3 -->
<license>TODO</license>
<!-- Url tags are optional, but multiple are allowed, one per tag -->
<!-- Optional attribute type can be: website, bugtracker, or repository -->
<!-- Example: -->
<!-- <url type="website">http://wiki.ros.org/planning -->
<!-- Author tags are optional, multiple are allowed, one per tag -->
<!-- Authors do not have to be maintainers, but could be -->
<!-- Example: -->
<!-- <author email="[email protected]">Jane Doe</author> -->
<!-- The *depend tags are used to specify dependencies -->
<!-- Dependencies can be catkin packages or system dependencies -->
<!-- Examples: -->
<!-- Use depend as a shortcut for packages that are both build and exec dependencies -->
<!-- <depend>roscpp</depend> -->
<!-- Note that this is equivalent to the following: -->
<!-- <build_depend>roscpp</build_depend> -->
<!-- <exec_depend>roscpp</exec_depend> -->
<!-- Use build_depend for packages you need at compile time: -->
<!-- <build_depend>message_generation</build_depend> -->
<!-- Use build_export_depend for packages you need in order to build against this package: -->
<!-- <build_export_depend>message_generation</build_export_depend> -->
<!-- Use buildtool_depend for build tool packages: -->
<!-- <buildtool_depend>catkin</buildtool_depend> -->
<!-- Use exec_depend for packages you need at runtime: -->
<!-- <exec_depend>message_runtime</exec_depend> -->
<!-- Use test_depend for packages you need only for testing: -->
<!-- <test_depend>gtest</test_depend> -->
<!-- Use doc_depend for packages you need only for building documentation: -->
<!-- <doc_depend>doxygen</doc_depend> -->
<buildtool_depend>catkin</buildtool_depend>
<build_depend>car_interfaces</build_depend>
<build_depend>roscpp</build_depend>
<build_depend>rospy</build_depend>
<build_depend>std_msgs</build_depend>
<build_export_depend>car_interfaces</build_export_depend>
<build_export_depend>roscpp</build_export_depend>
<build_export_depend>rospy</build_export_depend>
<build_export_depend>std_msgs</build_export_depend>
<exec_depend>car_interfaces</exec_depend>
<exec_depend>roscpp</exec_depend>
<exec_depend>rospy</exec_depend>
<exec_depend>std_msgs</exec_depend>
<!-- The export tag contains other, unspecified, tags -->
<export>
<!-- Other tools can request additional information be placed here -->
</export>
</package>
以上完成后就可以建立c++的ros通信了
后面要做的事:
1.把这个结构给完全理解并深入掌握
2.按照相应的规则重新写线程