728x90
반응형
$catkin_create_pkg <"packageName"> <depends..> roscpp std_msgs message_generation
$cd <"packageName">
$mkdir msg
$cd msg
$vim <"fileName">.msg
# Make a file <"messageName">.msg
time stamp
int32 data
$cd ..
$mkdir src
$cd src
$vim <"publisherName">.cpp
#include "ros/ros.h"
#include "packageName/messageName.h"
int main(int argc, char** argv)
{
ros::init(argc, argv, "publisherNmae");
ros::NodeHandle nh;
ros::Publisher pub = nh.advertise<packageName::messagename>("topicName", "queueSize");
ros::Rate loop_rate(Hz);
packageName::messageName "variable(msg)";
int cnt = 0;
while(ros::ok())
{
msg.stamp = ros::Time::now();
msg.data = cnt;
ROS_INFO("Send msg: %d", msg.stamp.sec);
ROS_INFO("Send msg: %d", msg.stamp.nsec);
ROS_INFO("Send msg: %d", msg.data);
pub.publish(msg);
cnt++;
loop_rate.sleep();
}
return 0;
}
// publisherName.cpp
$vim <"subscriberName">.cpp
#include "ros/ros.h"
#include "packageName/messageName.h"
void msgCallback(const packageName::messageName::msg::ConstPtr& msg)
{
ROS_INFO("receive msg: %d", msg->stamp.sec);
ROS_INFO("receive msg: %d", msg->stamp.nsecs);
ROS_INFO("recieve msg: %d", msg->data);
}
int main(int argc, char** argv)
{
ros::init(argc, argv, "subscriberName");
ros::NodeHandle nh;
ros::Subscriber sub = nh.subscribe("topicName", queueSize, msgCallback);
ros::spin();
return 0;
}
// subscriberName.cpp
$cd ..
$vim CMakeLists.txt
cmake_minimum_required(VERSION 3.0.2)
project(msg_tutorial)
## 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
roscpp
rospy
std_msgs
)
## Generate messages in the 'msg' folder
add_message_files(
FILES
Mymsg.msg
)
## Generate added messages and services with any dependencies listed here
generate_messages(
DEPENDENCIES
std_msgs
)
################################################
## Declare ROS dynamic reconfigure parameters ##
################################################
## 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 msg_tutorial
# CATKIN_DEPENDS message_generation roscpp rospy std_msgs
# DEPENDS system_lib
)
###########
## Build ##
###########
## Specify additional locations of header files
## Your package locations should be listed before other locations
include_directories(
# include
${catkin_INCLUDE_DIRS}
)
## The recommended prefix ensures that target names across packages don't collide
add_executable(msg_publisher src/msg_publisher.cpp)
add_executable(msg_subscriber src/msg_subscriber.cpp)
## Add cmake target dependencies of the executable
## same as for the library above
add_dependencies(msg_publisher ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
add_dependencies(msg_subscriber ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
## Specify libraries to link a library or executable target against
target_link_libraries(msg_publisher
${catkin_LIBRARIES}
)
target_link_libraries(msg_subscriber
${catkin_LIBRARIES}
)
728x90
반응형
'ROS_1' 카테고리의 다른 글
ROS 1.0 summary_01 (0) | 2022.10.09 |
---|---|
Error: Package not found (0) | 2022.10.03 |
ros::init() (0) | 2022.09.30 |
Message (0) | 2022.09.29 |
ros exam (0) | 2022.09.28 |