9 from nav_msgs.msg
import Odometry
12 import geometry_msgs.msg
13 from rospy_message_converter
import json_message_converter
20 from nus_msgs.msg
import StateWithCovarianceStamped
21 from common_msgs.msg
import target
22 from sensor_msgs.msg
import NavSatFix
24 from tf.transformations
import quaternion_from_euler, euler_from_quaternion
25 from geometry_msgs.msg
import Quaternion, Point, PoseStamped, PointStamped, Vector3Stamped, Polygon
26 from std_srvs.srv
import Trigger, TriggerRequest
32 file = open(filename,
'r')
33 fileText = file.read()
39 file = open(filename,
'w')
45 self.
UDPSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
46 listen_addr = (ip_addr, port)
55 sock = socket.socket(socket.AF_INET,
58 sock.sendto(text, (UDP_IP, UDP_PORT))
65 self.
vCmdPub = rospy.Publisher(
'/velTgtFromSwarm', Point, queue_size=10)
66 self.
pCmdPub = rospy.Publisher(
'/posTgtFromSwarm', Point, queue_size=10)
67 self.
approachPub = rospy.Publisher(
'/swarm/approach', std_msgs.msg.Bool, queue_size=1)
68 self.
onlinePub = rospy.Publisher(
'/swarm/online', std_msgs.msg.Bool, queue_size=1)
72 rospy.Subscriber(
"/mavros/position/local_state", StateWithCovarianceStamped, self.
uav_state_CB)
73 rospy.Subscriber(
"/mavros/global_position/global", NavSatFix, self.
gpsPosCb)
74 rospy.Subscriber(
"/task_manager/swarm", std_msgs.msg.Bool, self.
swarmCb)
75 rospy.Subscriber(
"/car_tracking", std_msgs.msg.Float32MultiArray, self.
carTrackingCb)
98 data = json_message_converter.convert_ros_message_to_json(data)
99 swarmDic = json.loads(data.strip())
100 print(
"Send swarm to CA: ", swarmDic[
"data"])
110 target_num = len(data.data)/4
111 print(
"Car Number: ", target_num)
112 data = json_message_converter.convert_ros_message_to_json(data)
115 carTrackDic = json.loads(data.strip())
116 jsonCarTrackDic[
'type'] =
"targettrack"
117 jsonCarTrackDic[
'result']=[]
118 for index
in range(target_num):
119 jsonCarTrackDic[
'result'].append({
"class_id": int(carTrackDic[
"data"][index]),
"confidence":carTrackDic[
"data"][index+1],
"u": carTrackDic[
"data"][index+2],
"v":carTrackDic[
"data"][index+3]})
121 jsonSendStr = json.dumps(jsonCarTrackDic)
127 data = json_message_converter.convert_ros_message_to_json(data)
128 uavStateDic = json.loads(data.strip())
129 q = [uavStateDic[
"pose"][
"pose"][
"orientation"][
"x"],
130 uavStateDic[
"pose"][
"pose"][
"orientation"][
"y"],
131 uavStateDic[
"pose"][
"pose"][
"orientation"][
"z"],
132 uavStateDic[
"pose"][
"pose"][
"orientation"][
"w"]]
133 euler = euler_from_quaternion(q)
134 self.
jsonDic[
'roll'] = euler[0]*57.3
135 self.
jsonDic[
'pitch'] = euler[1]*57.3
136 self.
jsonDic[
'yaw'] = euler[2]*57.3
140 self.
jsonDic[
'u'] = uavStateDic[
"velocity"][
"twist"][
"linear"][
"x"]
141 self.
jsonDic[
'v'] = uavStateDic[
"velocity"][
"twist"][
"linear"][
"y"]
142 self.
jsonDic[
'w'] = uavStateDic[
"velocity"][
"twist"][
"linear"][
"z"]
143 self.
jsonDic[
'time'] = int(round(time.time() * 1000))
151 data = json_message_converter.convert_ros_message_to_json(data)
152 uavStateDic = json.loads(data.strip())
154 self.
jsonDic[
'latitude'] = uavStateDic[
"latitude"]
155 self.
jsonDic[
'longitude'] = uavStateDic[
"longitude"]
156 self.
jsonDic[
'altitude'] = uavStateDic[
"altitude"]
157 self.
jsonDic[
'time'] = int(round(time.time() * 1000))
158 self.
jsonDic[
'type'] =
"UAVPosVel"
160 jsonSendStr = json.dumps(self.
jsonDic)
166 if 'time' in jsonMsg:
168 self.
preSep = int(jsonMsg[
"time"])
169 self.
curSeq = int(jsonMsg[
"time"])
174 cmdMsg = geometry_msgs.msg.Point()
175 if jsonMsg[
'type'] ==
'velocity':
176 cmdMsg.x = float(jsonMsg[
"u"])
177 cmdMsg.y = float(jsonMsg[
"v"])
178 cmdMsg.z = float(jsonMsg[
"w"])
181 if jsonMsg[
'type'] ==
'position':
182 cmdMsg.x = float(jsonMsg[
"latitude"])
183 cmdMsg.y = float(jsonMsg[
"longitude"])
184 cmdMsg.z = float(jsonMsg[
"altitude"])
187 if jsonMsg[
'type'] ==
'approach':
188 approachMsg = std_msgs.msg.Bool()
189 approachMsg.data = jsonMsg[
"flag"]
190 print(
"approach flag:", approachMsg.data)
194 if jsonMsg[
'type'] ==
'online':
195 onlineMsg = std_msgs.msg.Bool()
196 onlineMsg.data = jsonMsg[
"flag"]
198 if not onlineMsg.data:
206 self.
response.message =
"take off done"
211 rospy.init_node(
'SwarmSocket', log_level= rospy.DEBUG)
212 while not rospy.is_shutdown():
214 receivedJSONMessage = json.loads(data.strip())
223 swarm_socket.remoteInterfaceModule()
227 if __name__ ==
'__main__':