View on GitHub

Rosworkshop

HOME

ROS messages

Nodes communicate with each other by publishing messages to topics. A message is a simple data structure, comprising typed fields. Standard primitive types (integer, floating point, boolean, etc.) are supported, as are arrays of primitive types. Messages can include arbitrarily nested structures and arrays (much like C structs).

msgs are just simple text files with a field type and field name per line. The field types you can use are:

Let us create message file

As disscussed above message files are made in ‘msg’ folder located in side the package inside which has extention ‘xxx.msg’

A typical ‘xxx.msg’ file look like below

float32 data1
float64 data2

So for this tutorial we are going to create a message file named as ‘my_msg.msg’

‘my_msg.msg’

int32 count
float64 add
float64 sub
message_generation message_runtime
* then open ***'CMakeLists.txt'*** located in rosbasics Folder add lines and savefile and close
* >Ctrl+F and search **"find_package"** and add the line

```python
 find_package(catkin REQUIRED COMPONENTS
   roscpp
   rospy
   std_msgs
   message_generation ##add this line
)
 catkin_package(
  # INCLUDE_DIRS include
  #  LIBRARIES rosbasics
  CATKIN_DEPENDS roscpp rospy std_msgs message_runtime #remove comment
  # also add message_runtime
  #  DEPENDS system_lib
 )
 add_message_files(
   FILES
   my_msg.msg #add this line
#   Message1.msg
#   Message2.msg
 )

After this steps save and close ‘CMakeLists.txt’ and open terminal

 cd cd ROS_Worksop/
 catkin_make

Make sure previous steps have no errors to check message created

 rosmsg show rosbasics/my_msg

which outputs

int32 count
float64 add
float64 sub

# Now that we have created ROS message we create an publisher A publsiher which publish in our ROSmsg structure .In ‘src’ create ‘my_msgpub.py’

‘my_msgpub.py’

 #!/usr/bin/python

 import rospy
 from rosbasics.msg import my_msg
 def main():
  rospy.init_node("my_msgpub",anonymous=False)
  pub =rospy.Publisher("/mymsg",my_msg,queue_size=0)
  count=0
  data=my_msg()
  r=rospy.Rate(hz=10)
  while not rospy.is_shutdown():
    x,y=map(float,raw_input("input x,y :\t").split())
    data.add=x+y
    data.sub=x-y
    data.count=count
    count+=1
    pub.publish(data)
    r.sleep()

 if __name__ == '__main__':
     try:
         main()
     except rospy.ROSInterruptException:
         pass

## Important Points with Creating ROSmsgs

 int32 count
 float64[] fibonocci
 float64[] ap

Back to:Chapter 4
Next to:Chapter 6