Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

ROS flydra service #1

Merged
merged 2 commits into from about 2 years ago

2 participants

Floris van Breugel Andrew Straw
Floris van Breugel

Hi Andrew,

I found a need for having a service that sends back the latest flydra_super_packet. It made sense to put that in the ros_flydra project, so I thought you might like to pull it into the main branch.

  • Floris
Andrew Straw astraw merged commit 5d6ac91 into from
Andrew Straw astraw closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
1  CMakeLists.txt
@@ -7,3 +7,4 @@ set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
7 7 set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
8 8
9 9 rosbuild_genmsg()
  10 +rosbuild_gensrv()
24 nodes/flydra_service.py
... ... @@ -0,0 +1,24 @@
  1 +#!/usr/bin/env python
  2 +import roslib; roslib.load_manifest('ros_flydra')
  3 +import rospy
  4 +from ros_flydra.msg import *
  5 +from ros_flydra.srv import *
  6 +
  7 +class Flydra_Service:
  8 + def __init__(self):
  9 + self.super_packet = None
  10 + self.service = rospy.Service("flydra_super_packet_service", super_packet_service, self.service_callback)
  11 + self.subscriber = rospy.Subscriber("flydra_mainbrain_super_packets", flydra_mainbrain_super_packet, self.listen_callback)
  12 + rospy.init_node('listener', anonymous=True)
  13 + rospy.spin()
  14 +
  15 + def listen_callback(self, super_packet):
  16 + #print 'super packet, ', len(super_packet.packets), ' packets'
  17 + self.super_packet = super_packet
  18 +
  19 + def service_callback(self, request):
  20 + # request is null, just a placeholder
  21 + return super_packet_serviceResponse(self.super_packet)
  22 +
  23 +if __name__ == '__main__':
  24 + flydra_service = Flydra_Service()
24 nodes/flydra_service.py~
... ... @@ -0,0 +1,24 @@
  1 +#!/usr/bin/env python
  2 +import roslib; roslib.load_manifest('ros_flydra')
  3 +import rospy
  4 +from ros_flydra.msg import *
  5 +from ros_flydra.srv import *
  6 +
  7 +class Flydra_Service:
  8 + def __init__(self):
  9 + self.super_packet = None
  10 + self.service = rospy.Service("flydra_super_packet_service", super_packet_service, self.service_callback)
  11 + self.subscriber = rospy.Subscriber("flydra_mainbrain_super_packets", flydra_mainbrain_super_packet, self.listen_callback)
  12 + rospy.init_node('listener', anonymous=True)
  13 + rospy.spin()
  14 +
  15 + def listen_callback(self, super_packet):
  16 + #print 'super packet, ', len(super_packet.packets), ' packets'
  17 + self.super_packet = super_packet
  18 +
  19 + def service_callback(self, request):
  20 + # request is null, just a placeholder
  21 + return super_packet_serviceResponse(self.super_packet)
  22 +
  23 +if __name__ == '__main__':
  24 + flydra_service = Flydra_Service()
35 nodes/flydra_service_usage_example.py
... ... @@ -0,0 +1,35 @@
  1 +#!/usr/bin/env python
  2 +import roslib; roslib.load_manifest('ros_flydra')
  3 +import rospy
  4 +from ros_flydra.msg import *
  5 +from ros_flydra.srv import *
  6 +import time
  7 +
  8 +class Flydra_Service_Listener:
  9 + def __init__(self):
  10 + rospy.wait_for_service("flydra_super_packet_service")
  11 + self.get_latest_flydra_data = rospy.ServiceProxy("flydra_super_packet_service", super_packet_service)
  12 +
  13 + def print_data_every_second(self):
  14 + time_prev = time.time()
  15 + while 1:
  16 + if time.time()-time_prev >= 1:
  17 + superpacket = self.get_latest_flydra_data().packets
  18 +
  19 + for packet in superpacket.packets:
  20 + print
  21 + print '*'*80
  22 + for obj in packet.objects:
  23 + position = [obj.position.x, obj.position.y, obj.position.z]
  24 + velocity = [obj.velocity.x, obj.velocity.y, obj.velocity.z]
  25 + print
  26 + print 'obj id: ', obj.obj_id
  27 + print position
  28 + print velocity
  29 +
  30 + time_prev = time.time()
  31 +
  32 +if __name__ == '__main__':
  33 + flydra_service_listener = Flydra_Service_Listener()
  34 + flydra_service_listener.print_data_every_second()
  35 +
24 nodes/flydra_service_usage_example.py~
... ... @@ -0,0 +1,24 @@
  1 +#!/usr/bin/env python
  2 +import roslib; roslib.load_manifest('ros_flydra')
  3 +import rospy
  4 +from ros_flydra.msg import *
  5 +from ros_flydra.srv import *
  6 +import time
  7 +
  8 +class Flydra_Service_Listener:
  9 + def __init__(self):
  10 + rospy.wait_for_service("flydra_super_packet_service")
  11 + self.get_latest_flydra_data = rospy.ServiceProxy("flydra_super_packet_service", super_packet_service)
  12 +
  13 + def print_data_every_second(self):
  14 + time_prev = time.time()
  15 + while 1:
  16 + if time.time()-time_prev >= 1:
  17 + super_packet = self.get_latest_flydra_data()
  18 + print super_packet
  19 + time_prev = time.time()
  20 +
  21 +if __name__ == '__main__':
  22 + flydra_service_listener = Flydra_Service_Listener()
  23 + flydra_service_listener.print_data_every_second()
  24 +
2  srv/super_packet_service.srv
... ... @@ -0,0 +1,2 @@
  1 +---
  2 +flydra_mainbrain_super_packet packets
0  srv/super_packet_service.srv~
No changes.

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.