Skip to content

Commit b60565d

Browse files
committed
added examples of creating custom messages
1 parent d74b99b commit b60565d

File tree

7 files changed

+167
-0
lines changed

7 files changed

+167
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
cmake_minimum_required(VERSION 2.8.3)
2+
project(custom_msg_example1)
3+
4+
find_package(catkin REQUIRED COMPONENTS
5+
message_generation
6+
roscpp
7+
rospy
8+
std_msgs
9+
geometry_msgs
10+
)
11+
12+
13+
add_message_files(
14+
FILES
15+
NodeInfo.msg
16+
NodeData.msg
17+
NodesInfo.msg
18+
)
19+
20+
21+
generate_messages(
22+
DEPENDENCIES
23+
std_msgs
24+
geometry_msgs
25+
)
26+
27+
28+
catkin_package(
29+
CATKIN_DEPENDS
30+
message_runtime
31+
roscpp
32+
rospy
33+
std_msgs
34+
geometry_msgs
35+
)
36+
37+
include_directories(
38+
${catkin_INCLUDE_DIRS}
39+
)
40+
41+
add_executable(custom_msg_cpp_simple_node
42+
ros/src/custom_msg_cpp_simple_node.cpp)
43+
44+
target_link_libraries(custom_msg_cpp_simple_node
45+
${catkin_LIBRARIES}
46+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
geometry_msgs/Vector3 vector_position
2+
geometry_msgs/Point point_position
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
std_msgs/String node_name
2+
std_msgs/Bool node_status
3+
std_msgs/Int32 node_id
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
std_msgs/String[] node_name
2+
std_msgs/Bool[] node_status
3+
std_msgs/Int32[] node_id
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0"?>
2+
<package>
3+
<name>custom_msg_example1</name>
4+
<version>0.1.0</version>
5+
<description>The custom_msg_example1 package</description>
6+
7+
<maintainer email="[email protected]">Shehzad Ahmed</maintainer>
8+
9+
<license>GPLv3</license>
10+
11+
<buildtool_depend>catkin</buildtool_depend>
12+
<build_depend>message_generation</build_depend>
13+
<build_depend>roscpp</build_depend>
14+
<build_depend>rospy</build_depend>
15+
<build_depend>std_msgs</build_depend>
16+
<build_depend>geometry_msgs</build_depend>
17+
<run_depend>roscpp</run_depend>
18+
<run_depend>rospy</run_depend>
19+
<run_depend>std_msgs</run_depend>
20+
<run_depend>geometry_msgs</run_depend>
21+
<run_depend>message_runtime</run_depend>
22+
</package>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env python
2+
"""
3+
This script initilizes simple python node.
4+
"""
5+
#-*- encoding: utf-8 -*-
6+
__author__ = 'shehzad ahmed'
7+
import rospy
8+
from custom_msg_example1.msg import NodeData
9+
10+
def initlize_node():
11+
'''
12+
Initilize node and spin which simply keeps python
13+
from exiting until this node is stopped
14+
'''
15+
rospy.init_node('custom_msg_python_simple_node', anonymous=False)
16+
rospy.loginfo("custom_msg_python_simple_node is now running")
17+
18+
node_data = NodeData()
19+
node_data.vector_position.x = 0.01
20+
node_data.vector_position.y = 0.01
21+
node_data.vector_position.z = 0.01
22+
23+
node_data.point_position.x = 0.1
24+
node_data.point_position.y = 0.2
25+
node_data.point_position.z = 0.01
26+
27+
rospy.spin()
28+
29+
if __name__ == '__main__':
30+
initlize_node()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* custom_msg_simple_node.cpp
3+
*
4+
* Created on: September 08, 2014
5+
* Author: Shehzad Ahmed
6+
**/
7+
8+
#include <ros/ros.h>
9+
#include <custom_msg_example1/NodeInfo.h>
10+
#include <custom_msg_example1/NodesInfo.h>
11+
12+
13+
14+
int main(int argc, char *argv[])
15+
{
16+
/**
17+
* Initilization of ROS
18+
**/
19+
ros::init(argc, argv, "custom_msg_simple_node");
20+
21+
/**
22+
* Initilization of node by using node handle
23+
**/
24+
ros::NodeHandle nh("~");
25+
26+
/**
27+
* Printing Information message on the console.
28+
**/
29+
ROS_INFO("custom_msg_cpp_simple_node is now running");
30+
31+
/*
32+
* Filling a simple custom message
33+
*/
34+
custom_msg_example1::NodeInfo node_info_msg;
35+
36+
node_info_msg.node_name.data = "custom_msg_cpp_simple_node";
37+
node_info_msg.node_status.data = false;
38+
node_info_msg.node_id.data = 1;
39+
40+
/*
41+
* Filling a an array custom message
42+
*/
43+
int number_of_nodes = 5;
44+
custom_msg_example1::NodesInfo nodes_info_msg;
45+
nodes_info_msg.node_name.resize(number_of_nodes);
46+
nodes_info_msg.node_status.resize(number_of_nodes);
47+
nodes_info_msg.node_id.resize(number_of_nodes);
48+
49+
for(int i=0;i < number_of_nodes; i++) {
50+
nodes_info_msg.node_name.at(i).data = "custom_msg_cpp_simple_node ";
51+
nodes_info_msg.node_status.at(i).data = false;
52+
nodes_info_msg.node_id.at(i).data = i;
53+
}
54+
55+
/**
56+
* Running a thread to keep spinning untill the user kills the node.
57+
**/
58+
ros::spin();
59+
60+
return 0;
61+
}

0 commit comments

Comments
 (0)