amr_interop_bridge.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 import websocket
5 try:
6  import thread
7 except ImportError:
8  import _thread as thread
9 import time
10 import json
11 import hashlib
12 import uuid
13 from datetime import datetime, timedelta
14 import random
15 import math
16 import time
17 import pytz
18 
19 import rospy
20 from std_msgs.msg import String
21 from std_msgs.msg import Float64
22 from geometry_msgs.msg import Point
23 from geometry_msgs.msg import Quaternion
24 from amr_interop_msgs.msg import Location
25 from amr_interop_msgs.msg import Velocity
26 from amr_interop_msgs.msg import ErrorCodes
27 from amr_interop_msgs.msg import PredictedLocation
28 from amr_interop_msgs.msg import PredictedLocations
29 
30 
31 def on_message(ws, message):
32  rospy.loginfo(message)
33 
34 def on_error(ws, error):
35  rospy.loginfo(error)
36 
37 def on_close(ws, close_status_code, close_msg):
38  rospy.loginfo("### closed ###")
39 
40 def on_open(ws):
41  def run(*args):
42  identity["timestamp"] = datetime.now(pytz.utc).strftime("%Y-%m-%dT%H:%M:%S%z")
43  ws.send(json.dumps(identity))
44  identity.clear()
45 
46  while True:
47  status["timestamp"] = datetime.now(pytz.utc).strftime("%Y-%m-%dT%H:%M:%S%z")
48  ws.send(json.dumps(status))
49  status.clear()
50  time.sleep(2)
51  ws.close()
52  thread.start_new_thread(run, ())
53 
54 def uuid_callback(msg):
55  identity["uuid"] = msg.data
56  status["uuid"] = msg.data
57 
59  identity["manufacturerName"] = msg.data
60 
62  identity["robotModel"] = msg.data
63 
65  identity["robotSerialNumber"] = msg.data
66 
68  if "baseRobotEnvelope" not in identity:
69  identity["baseRobotEnvelope"] = {}
70 
71  identity["baseRobotEnvelope"]["x"] = msg.x
72  identity["baseRobotEnvelope"]["y"] = msg.y
73  if not math.isnan(msg.z):
74  identity["baseRobotEnvelope"]["z"] = msg.z
75 
77  identity["maxSpeed"] = msg.data
78 
80  identity["maxRunTime"] = msg.data
81 
83  identity["emergencyContactInformation"] = msg.data
84 
86  identity["chargerType"] = msg.data
87 
89  identity["supportVendorName"] = msg.data
90 
92  identity["supportVendorContactInformation"] = msg.data
93 
95  identity["productDocumentation"] = msg.data
96 
98  identity["thumbnailImage"] = msg.data
99 
101  identity["cargoType"] = msg.data
102 
104  if "cargoMaxVolume" not in identity:
105  identity["cargoMaxVolume"] = {}
106 
107  identity["cargoMaxVolume"]["x"] = msg.x
108  identity["cargoMaxVolume"]["y"] = msg.y
109  if not math.isnan(msg.z):
110  identity["cargoMaxVolume"]["z"] = msg.z
111 
113  identity["cargoMaxWeight"] = msg.data
114 
116  status["operationalState"] = msg.data
117 
119  if "location" not in identity:
120  status["location"] = {}
121 
122  status["location"]["x"] = msg.x
123  status["location"]["y"] = msg.y
124  if math.isnan(msg.z):
125  status["location"].pop("z", None)
126  else:
127  status["location"]["z"] = msg.z
128  status["location"]["angle"] = {}
129  status["location"]["angle"]["x"] = msg.angle.x
130  status["location"]["angle"]["y"] = msg.angle.y
131  status["location"]["angle"]["z"] = msg.angle.z
132  status["location"]["angle"]["w"] = msg.angle.w
133 
134  status["location"]["planarDatum"] = msg.planar_datum
135 
137  if "velocity" not in identity:
138  status["velocity"] = {}
139 
140  status["velocity"]["linear"] = msg.linear
141 
142  if math.isnan(msg.angular.x) or \
143  math.isnan(msg.angular.y) or \
144  math.isnan(msg.angular.z) or \
145  math.isnan(msg.angular.w):
146  status["velocity"].pop("angular", None)
147  else:
148  status["velocity"]["angular"] = {}
149  status["velocity"]["angular"]["x"] = msg.angular.x
150  status["velocity"]["angular"]["y"] = msg.angular.y
151  status["velocity"]["angular"]["z"] = msg.angular.z
152  status["velocity"]["angular"]["w"] = msg.angular.w
153 
155  status["batteryPercentage"] = msg.data
156 
158  status["remainingRunTime"] = msg.data
159 
161  status["loadPercentageStillAvailable"] = msg.data
162 
164  if len(msg.data) > 0:
165  status["errorCodes"] = msg.data
166 
167 def path_callback(msg):
168  if len(msg.data) > 0:
169  status["path"] = []
170 
171  for i in range(len(msg.data)):
172  path = {}
173  path["timestamp"] = msg.data[i].timestamp
174  path["x"] = msg.data[i].x
175  path["y"] = msg.data[i].y
176  if not math.isnan(msg.data[i].z):
177  path["z"] = msg.data[i].z
178  path["angle"] = {}
179  path["angle"]["x"] = msg.data[i].angle.x
180  path["angle"]["y"] = msg.data[i].angle.y
181  path["angle"]["z"] = msg.data[i].angle.z
182  path["angle"]["w"] = msg.data[i].angle.w
183  path["planarDatumUUID"] = msg.data[i].planar_datum_uuid
184 
185  status["path"].append(path)
186 
188  if len(msg.data) > 0:
189  status["destinations"] = []
190 
191  for i in range(len(msg.data)):
192  path = {}
193  path["timestamp"] = msg.data[i].timestamp
194  path["x"] = msg.data[i].x
195  path["y"] = msg.data[i].y
196  if not math.isnan(msg.data[i].z):
197  path["z"] = msg.data[i].z
198  path["angle"] = {}
199  path["angle"]["x"] = msg.data[i].angle.x
200  path["angle"]["y"] = msg.data[i].angle.y
201  path["angle"]["z"] = msg.data[i].angle.z
202  path["angle"]["w"] = msg.data[i].angle.w
203  path["planarDatumUUID"] = msg.data[i].planar_datum_uuid
204 
205  status["destinations"].append(path)
206 
207 if __name__ == "__main__":
208  rospy.init_node("amr_interop_bridge")
209 
210  url = rospy.get_param("~url", "ws://localhost/")
211 
212  identity = {}
213  status = {}
214 
215  rospy.Subscriber("uuid", String, uuid_callback)
216  rospy.Subscriber("manufacture_name", String, manufacture_name_callback)
217  rospy.Subscriber("robot_model", String, robot_model_callback)
218  rospy.Subscriber("robot_serial_number", String, robot_serial_number_callback)
219  rospy.Subscriber("base_robot_envelope", Point, base_robot_envelope_callback)
220  rospy.Subscriber("max_speed", Float64, max_speed_callback)
221  rospy.Subscriber("max_run_time", Float64, max_run_time_callback)
222  rospy.Subscriber("emergency_contact_information", String, emergency_contact_information_callback)
223  rospy.Subscriber("charger_type", String, charger_type_callback)
224  rospy.Subscriber("support_vendor_name", String, support_vendor_name_callback)
225  rospy.Subscriber("support_vendor_contact_information", String, support_vendor_contact_information_callback)
226  rospy.Subscriber("product_documentation", String, product_documentation_callback)
227  rospy.Subscriber("thumbnail_image", String, thumbnail_image_callback)
228  rospy.Subscriber("cargo_type", String, cargo_type_callback)
229  rospy.Subscriber("cargo_max_volume", Point, cargo_max_volume_callback)
230  rospy.Subscriber("cargo_max_weight", String, cargo_max_weight_callback)
231 
232  rospy.Subscriber("operational_state", String, operational_state_callback)
233  rospy.Subscriber("location", Location, location_callback)
234  rospy.Subscriber("velocity", Velocity, velocity_callback)
235  rospy.Subscriber("battery_percentage", Float64, battery_percentage_callback)
236  rospy.Subscriber("remaining_run_time", Float64, remaining_run_time_callback)
237  rospy.Subscriber("load_percentage_still_avaiable", Float64, load_percentage_still_avaiable_callback)
238  rospy.Subscriber("error_codes", ErrorCodes, error_codes_callback)
239  rospy.Subscriber("path", PredictedLocations, path_callback)
240  rospy.Subscriber("destinations", PredictedLocations, destinations_callback)
241 
242  time.sleep(5) # wait subscribe
243 
244  ws = websocket.WebSocketApp(url,
245  on_open=on_open,
246  on_message=on_message,
247  on_error=on_error,
248  on_close=on_close)
249 
250  ws.run_forever()
amr_interop_bridge.support_vendor_contact_information_callback
def support_vendor_contact_information_callback(msg)
Definition: amr_interop_bridge.py:91
amr_interop_bridge.product_documentation_callback
def product_documentation_callback(msg)
Definition: amr_interop_bridge.py:94
amr_interop_bridge.thumbnail_image_callback
def thumbnail_image_callback(msg)
Definition: amr_interop_bridge.py:97
amr_interop_bridge.error_codes_callback
def error_codes_callback(msg)
Definition: amr_interop_bridge.py:163
amr_interop_bridge.cargo_type_callback
def cargo_type_callback(msg)
Definition: amr_interop_bridge.py:100
amr_interop_bridge.destinations_callback
def destinations_callback(msg)
Definition: amr_interop_bridge.py:187
amr_interop_bridge.remaining_run_time_callback
def remaining_run_time_callback(msg)
Definition: amr_interop_bridge.py:157
amr_interop_bridge.load_percentage_still_avaiable_callback
def load_percentage_still_avaiable_callback(msg)
Definition: amr_interop_bridge.py:160
amr_interop_bridge.robot_serial_number_callback
def robot_serial_number_callback(msg)
Definition: amr_interop_bridge.py:64
amr_interop_bridge.manufacture_name_callback
def manufacture_name_callback(msg)
Definition: amr_interop_bridge.py:58
amr_interop_bridge.max_run_time_callback
def max_run_time_callback(msg)
Definition: amr_interop_bridge.py:79
amr_interop_bridge.max_speed_callback
def max_speed_callback(msg)
Definition: amr_interop_bridge.py:76
amr_interop_bridge.charger_type_callback
def charger_type_callback(msg)
Definition: amr_interop_bridge.py:85
amr_interop_bridge.robot_model_callback
def robot_model_callback(msg)
Definition: amr_interop_bridge.py:61
amr_interop_bridge.on_error
def on_error(ws, error)
Definition: amr_interop_bridge.py:34
amr_interop_bridge.emergency_contact_information_callback
def emergency_contact_information_callback(msg)
Definition: amr_interop_bridge.py:82
amr_interop_bridge.support_vendor_name_callback
def support_vendor_name_callback(msg)
Definition: amr_interop_bridge.py:88
amr_interop_bridge.velocity_callback
def velocity_callback(msg)
Definition: amr_interop_bridge.py:136
amr_interop_bridge.base_robot_envelope_callback
def base_robot_envelope_callback(msg)
Definition: amr_interop_bridge.py:67
amr_interop_bridge.uuid_callback
def uuid_callback(msg)
Definition: amr_interop_bridge.py:54
amr_interop_bridge.operational_state_callback
def operational_state_callback(msg)
Definition: amr_interop_bridge.py:115
amr_interop_bridge.path_callback
def path_callback(msg)
Definition: amr_interop_bridge.py:167
amr_interop_bridge.on_message
def on_message(ws, message)
Definition: amr_interop_bridge.py:31
amr_interop_bridge.battery_percentage_callback
def battery_percentage_callback(msg)
Definition: amr_interop_bridge.py:154
amr_interop_bridge.location_callback
def location_callback(msg)
Definition: amr_interop_bridge.py:118
amr_interop_bridge.on_open
def on_open(ws)
Definition: amr_interop_bridge.py:40
amr_interop_bridge.cargo_max_weight_callback
def cargo_max_weight_callback(msg)
Definition: amr_interop_bridge.py:112
amr_interop_bridge.on_close
def on_close(ws, close_status_code, close_msg)
Definition: amr_interop_bridge.py:37
amr_interop_bridge.cargo_max_volume_callback
def cargo_max_volume_callback(msg)
Definition: amr_interop_bridge.py:103


amr_interop_bridge
Author(s):
autogenerated on Tue Mar 1 2022 23:45:33