subscriber-example.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 # ---------------------------------------------------------------------
4 # This example shows how to:
5 # - Subscribe to an MVSIM topic with a custom callback.
6 #
7 # Install python3-mvsim, or test with a local build with:
8 # export PYTHONPATH=$HOME/code/mvsim/build/:$PYTHONPATH
9 # ---------------------------------------------------------------------
10 
11 from mvsim_comms import pymvsim_comms
12 from mvsim_msgs import TimeStampedPose_pb2
13 from mvsim_msgs import ObservationLidar2D_pb2
14 import time
15 
16 
17 # Callback for subscribed topic:
18 def onPoseMessage(msgType, msg):
19  assert(msgType == "mvsim_msgs.TimeStampedPose")
20  p = TimeStampedPose_pb2.TimeStampedPose()
21  p.ParseFromString(bytes(msg))
22  print("[pose callback] received: pose=\n" + str(p))
23 
24 
25 def onLidar2DMessage(msgType, msg):
26  assert(msgType == "mvsim_msgs.ObservationLidar2D")
27  p = ObservationLidar2D_pb2.ObservationLidar2D()
28  p.ParseFromString(bytes(msg))
29  print("[lidar callback] received:\n ranges=\n" +
30  str(p.scanRanges) + "\n validRanges=" + str(p.validRanges))
31 
32 
33 if __name__ == "__main__":
34  client = pymvsim_comms.mvsim.Client()
35  client.setName("tutorial1")
36  print("Connecting to server...")
37  client.connect()
38  print("Connected successfully.")
39 
40  # Subscribe to "/r1/pose"
41  client.subscribeTopic("/r1/pose", onPoseMessage)
42 
43  # Subscribe to "/r1/laser1_scan"
44  client.subscribeTopic("/r1/laser1_scan", onLidar2DMessage)
45 
46  time.sleep(2.0)
OutIt print(OutIt out, const xml_node< Ch > &node, int flags=0)
def onPoseMessage(msgType, msg)
def onLidar2DMessage(msgType, msg)


mvsim
Author(s):
autogenerated on Tue Jul 4 2023 03:08:21