diff --git a/src/test_topic/CMakeLists.txt b/src/test_topic/CMakeLists.txt index fdcb976..11f9219 100644 --- a/src/test_topic/CMakeLists.txt +++ b/src/test_topic/CMakeLists.txt @@ -9,8 +9,10 @@ project(test_topic) ## is used, also find other catkin packages find_package(catkin REQUIRED COMPONENTS roscpp + rospy std_msgs std_srvs + message_generation ) ## System dependencies are found with CMake's conventions @@ -47,11 +49,10 @@ find_package(catkin REQUIRED COMPONENTS ## * 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 -# ) +add_message_files( + FILES + Bytes.msg +) ## Generate services in the 'srv' folder # add_service_files( @@ -68,10 +69,10 @@ find_package(catkin REQUIRED COMPONENTS # ) ## Generate added messages and services with any dependencies listed here -# generate_messages( -# DEPENDENCIES -# std_msgs -# ) +generate_messages( + DEPENDENCIES + std_msgs +) ################################################ ## Declare ROS dynamic reconfigure parameters ## @@ -103,9 +104,9 @@ find_package(catkin REQUIRED COMPONENTS ## CATKIN_DEPENDS: catkin_packages dependent projects also need ## DEPENDS: system dependencies of this project that dependent projects also need catkin_package( -# INCLUDE_DIRS include + INCLUDE_DIRS include # LIBRARIES test_topic -# CATKIN_DEPENDS roscpp std_msgs + CATKIN_DEPENDS roscpp rospy message_runtime # DEPENDS system_lib ) diff --git a/src/test_topic/include/test_topic/minHandler.h b/src/test_topic/include/test_topic/minHandler.h index 3d11cca..884ee9a 100644 --- a/src/test_topic/include/test_topic/minHandler.h +++ b/src/test_topic/include/test_topic/minHandler.h @@ -13,15 +13,15 @@ */ #include -#include #include #include #include +#include "test_topic/Bytes.h" namespace xyfc_std { -using MsgType = std_msgs::String; +using MsgType = test_topic::Bytes; using ThreadFuncType = boost::function; /** diff --git a/src/test_topic/msg/Bytes.msg b/src/test_topic/msg/Bytes.msg new file mode 100644 index 0000000..236ddc5 --- /dev/null +++ b/src/test_topic/msg/Bytes.msg @@ -0,0 +1,4 @@ +uint8[] data +time tick +int64 length +int64 id diff --git a/src/test_topic/package.xml b/src/test_topic/package.xml index 63a5dbe..e00c5c7 100644 --- a/src/test_topic/package.xml +++ b/src/test_topic/package.xml @@ -50,14 +50,26 @@ catkin roscpp + rospy std_msgs std_srvs roscpp + rospy std_msgs roscpp + rospy std_msgs std_srvs + message_generation + message_runtime + + message_generation + message_runtime + + message_generation + message_runtime + diff --git a/src/test_topic/src/minSubscriber.cpp b/src/test_topic/src/minSubscriber.cpp index 551714a..f86b538 100644 --- a/src/test_topic/src/minSubscriber.cpp +++ b/src/test_topic/src/minSubscriber.cpp @@ -32,8 +32,9 @@ MinSubscriber::~MinSubscriber() void MinSubscriber::callback(const boost::shared_ptr &msg) { - ROS_INFO("I heard: [%s]", msg->data.c_str()); - _serial.send(msg->data.c_str(), strlen(msg->data.c_str())); + ROS_INFO("I heard: [%s]", &msg->data[0]); + + _serial.send((const char *)&msg->data[0], msg->length); } int main(int argc, char *argv[]) diff --git a/src/test_topic/src/testMinHandler.cpp b/src/test_topic/src/testMinHandler.cpp index 2ce8679..76063a5 100644 --- a/src/test_topic/src/testMinHandler.cpp +++ b/src/test_topic/src/testMinHandler.cpp @@ -17,14 +17,13 @@ protected: void testMinHandler::callback(const boost::shared_ptr &msg) { - std::stringstream ss; - - ss << "handled data: " << msg->data; - ROS_INFO("handle :%s", msg->data.c_str()); + ROS_INFO("handle :%s", &msg->data[0]); std::this_thread::sleep_for(std::chrono::seconds(2)); + std::string str = "handled :"; + str.append((const char *)&msg->data[0]); MsgType send; - send.data = ss.str(); + send.data.assign(str.begin(), str.end()); _pub.publish(send); } diff --git a/src/test_topic/src/timerPublisher.cpp b/src/test_topic/src/timerPublisher.cpp index 31c4f03..e0b6bd8 100644 --- a/src/test_topic/src/timerPublisher.cpp +++ b/src/test_topic/src/timerPublisher.cpp @@ -1,7 +1,7 @@ #include -#include +#include "test_topic/Bytes.h" -using MsgType = std_msgs::String; +using MsgType = test_topic::Bytes; class TimerPublisher: ros::NodeHandle { public: TimerPublisher(); @@ -29,13 +29,19 @@ void TimerPublisher::run() ros::Rate loop_rate(10); while (ros::ok()) { - std_msgs::String msg; + test_topic::Bytes msg; std::stringstream ss; ss << "hello world " << _count++ << "\n"; - msg.data = ss.str(); + auto str = ss.str(); - ROS_INFO("%s", msg.data.c_str()); + msg.data.assign(str.begin(), str.end()); + msg.data.push_back(0); + msg.id = 1, + msg.length = str.size() + 1; + msg.tick = ros::Time::now(); + + ROS_INFO("%s", str.c_str()); _sender.publish(msg); ros::spinOnce();