6 communication middleware between ros and non-ros modules
8 Designed for kerrigan object originally
14 from nav_msgs.msg
import Odometry
17 import geometry_msgs.msg
18 from rospy_message_converter
import json_message_converter
25 from nus_msgs.msg
import StateWithCovarianceStamped
26 import common_msgs.msg
27 from sensor_msgs.msg
import NavSatFix
28 import tf.transformations
30 from tf.transformations
import quaternion_from_euler, euler_from_quaternion
31 from geometry_msgs.msg
import Quaternion, Point, TwistStamped, PoseStamped, PointStamped, Vector3Stamped, Polygon
32 from std_srvs.srv
import Trigger, TriggerRequest
40 file = open(filename,
'r')
41 fileText = file.read()
49 file = open(filename,
'w')
54 """ Set up a UDP server"""
55 self.
UDPSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
56 listen_addr = (ip_addr, port)
61 """send text to remote machine """
66 sock = socket.socket(socket.AF_INET,
69 sock.sendto(text, (UDP_IP, UDP_PORT))
76 self.
vCmdPub = rospy.Publisher(
'/velTgtFromSwarm', common_msgs.msg.target, queue_size=1)
77 self.
pCmdPub = rospy.Publisher(
'/posTgtFromSwarm', common_msgs.msg.target, queue_size=1)
78 self.
approachPub = rospy.Publisher(
'/swarm/approach', std_msgs.msg.Bool, queue_size=1)
79 self.
onlinePub = rospy.Publisher(
'/swarm/online', std_msgs.msg.Bool, queue_size=1)
83 rospy.Subscriber(
"/mavros/local_position/velocity_local", TwistStamped, self.
VelCb)
84 rospy.Subscriber(
"/task_manager/position_nwu", PoseStamped, self.
PoseCb)
85 rospy.Subscriber(
"/mavros/global_position/global", NavSatFix, self.
gpsPosCb)
86 rospy.Subscriber(
"/task_manager/swarm", std_msgs.msg.Bool, self.
swarmCb)
87 rospy.Subscriber(
"/detector/result", std_msgs.msg.Float32MultiArray, self.
senseCb)
96 self.
file = open(
"/media/nvidia/SD/catkin_ws/data/detection_result",
"w")
97 self.
file.write(
'x_body, y_body, z_body, x_global, y_global, z_global, roll, pitch, yaw\n')
98 self.
jsonDic = {
"sense":
False,
"type":
"SenseAvoid" }
105 """ send other uav's info with detection result """
108 jsonSendStr = json.dumps(self.
jsonDic)
112 sending_thread.start()
122 """get swarm control info from /task_manager/swarm """
124 data = json_message_converter.convert_ros_message_to_json(data)
125 swarmDic = json.loads(data.strip())
126 print(
"Send swarm to CA: ", swarmDic[
"data"])
135 """get detection result from /detector/result"""
138 target_num = len(data.data)/4
139 data = json_message_converter.convert_ros_message_to_json(data)
141 droneDic = json.loads(data.strip())
143 for index
in range(target_num):
147 self.
jsonDic[
'sense'] = bool(droneDic[
"data"][index])
149 if np.isinf(droneDic[
"data"][index+1])
or np.isinf(droneDic[
"data"][index+2])
or np.isinf(droneDic[
"data"][index+3]):
151 other_body = [droneDic[
"data"][index+3], -droneDic[
"data"][index+1], -droneDic[
"data"][index+2],0]
154 other_global = self.
qv_mult(self.
q, other_body)
157 self.
jsonDic[
'other_body_x'] = other_body[0]
158 self.
jsonDic[
'other_body_y'] = other_body[1]
159 self.
jsonDic[
'other_body_z'] = other_body[2]
161 self.
jsonDic[
'other_x'] = other_global[0]
162 self.
jsonDic[
'other_y'] = other_global[1]
163 self.
jsonDic[
'other_z'] = other_global[2]
164 self.
jsonDic[
'h_pixel_dist'] = droneDic[
"data"][index+4]
167 self.
file.write(
'%s, %s, %s, %s, %s, %s, %s, %s, %s\n' %(other_body[0], other_body[1], other_body[2],other_global[0], other_global[1], other_global[2],self.
jsonDic[
'roll'], self.
jsonDic[
'pitch'] ,self.
jsonDic[
'yaw'] ))
172 """ get position from /task_manager/position_nwu"""
173 data = json_message_converter.convert_ros_message_to_json(data)
174 uavStateDic = json.loads(data.strip())
175 self.
q = [uavStateDic[
"pose"][
"orientation"][
"x"],
176 uavStateDic[
"pose"][
"orientation"][
"y"],
177 uavStateDic[
"pose"][
"orientation"][
"z"],
178 uavStateDic[
"pose"][
"orientation"][
"w"]]
179 euler = euler_from_quaternion(self.
q)
180 self.
jsonDic[
'roll'] = euler[0]
181 self.
jsonDic[
'pitch'] = euler[1]
187 """get velocity from /mavros/local_position/velocity_local"""
188 data = json_message_converter.convert_ros_message_to_json(data)
189 uavStateDic = json.loads(data.strip())
191 self.
jsonDic[
'u'] = uavStateDic[
"twist"][
"linear"][
"y"]
192 self.
jsonDic[
'v'] = -uavStateDic[
"twist"][
"linear"][
"x"]
193 self.
jsonDic[
'w'] = uavStateDic[
"twist"][
"linear"][
"z"]
194 self.
jsonDic[
'time'] = int(round(time.time() * 1000))
199 """transform vector v1 through q1"""
203 return tf.transformations.quaternion_multiply(
204 tf.transformations.quaternion_multiply(q1, v1),
205 tf.transformations.quaternion_conjugate(q1)
209 """get global pose from /mavros/global_position/global"""
210 data = json_message_converter.convert_ros_message_to_json(data)
211 uavStateDic = json.loads(data.strip())
213 self.
jsonDic[
'latitude'] = uavStateDic[
"latitude"]
214 self.
jsonDic[
'longitude'] = uavStateDic[
"longitude"]
215 self.
jsonDic[
'altitude'] = uavStateDic[
"altitude"]
216 self.
jsonDic[
'time'] = int(round(time.time() * 1000))
220 """sent all the reqired info to non-ros CA module"""
221 if 'time' in jsonMsg:
223 self.
preSep = int(jsonMsg[
"time"])
224 self.
curSeq = int(jsonMsg[
"time"])
230 cmdMsg = common_msgs.msg.target()
232 if jsonMsg[
'type'] ==
'velocity':
233 cmdMsg.controller =
True
234 cmdMsg.tgt.x = float(jsonMsg[
"u"])
235 cmdMsg.tgt.y = float(jsonMsg[
"v"])
236 cmdMsg.tgt.z = float(jsonMsg[
"w"])
237 cmdMsg.yaw_tgt = float(jsonMsg[
"yaw"])
240 if jsonMsg[
'type'] ==
'position':
241 cmdMsg.controller =
False
242 cmdMsg.tgt.x = float(jsonMsg[
"latitude"])
243 cmdMsg.tgt.y = float(jsonMsg[
"longitude"])
244 cmdMsg.tgt.z = float(jsonMsg[
"altitude"])
245 cmdMsg.yaw_tgt = float(jsonMsg[
"yaw"])
248 if jsonMsg[
'type'] ==
'approach':
249 approachMsg = std_msgs.msg.Bool()
250 approachMsg.data = jsonMsg[
"flag"]
251 print(
"approach flag:", approachMsg.data)
255 if jsonMsg[
'type'] ==
'online':
256 onlineMsg = std_msgs.msg.Bool()
257 onlineMsg.data = jsonMsg[
"flag"]
259 if not onlineMsg.data:
266 """get notified of taking off status through /takeoffdone"""
268 self.
response.message =
"take off done"
273 """Info receving and sending"""
274 rospy.init_node(
'SwarmSocket', log_level= rospy.DEBUG)
275 while not rospy.is_shutdown():
277 receivedJSONMessage = json.loads(data.strip())
286 swarm_socket.remoteInterfaceModule()
288 if __name__ ==
'__main__':