-
Notifications
You must be signed in to change notification settings - Fork 10
/
commands.py
executable file
·140 lines (118 loc) · 3.79 KB
/
commands.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import time
import definitions as vars
from pymavlink import mavutil
command_delays = {
'disconnect': 0.2,
'land' : 10,
'fallback': 3,
'prepare_for_attack': 1,
'attack': 1,
'target_search' : 0.5
}
def wait_for_execution(target, delay=0):
if delay == 0:
delay = command_delays.get(target)
time.sleep(delay)
def connect(system=255):
master = mavutil.mavlink_connection(vars.mavlink_url,
source_system=system)
vehicle = master.wait_heartbeat()
return master, vehicle
def disconnect(master):
wait_for_execution('disconnect')
master.close()
def send_message_to_gc(message):
master, vehicle = connect(1)
master.mav.statustext_send(
mavutil.mavlink.MAV_SEVERITY_NOTICE, message.encode())
disconnect(master)
def copter_init():
master, vehicle = connect(1)
master.mav.request_data_stream_send(
master.target_system,
master.target_component,
mavutil.mavlink.MAV_DATA_STREAM_ALL, 1, 1)
disconnect(master)
def land():
master, vehicle = connect()
print("Landing!")
master.mav.command_long_send(
master.target_system,
master.target_component,
mavutil.mavlink.MAV_CMD_NAV_LAND,
0,
0, 0, 0, 0, 0, 0, 0)
wait_for_execution('land')
disconnect(master)
def telemetry(target):
master, vehicle = connect(1)
msg = master.recv_match(type=target, blocking=False)
telemetry = {}
if msg is not None and msg.get_srcSystem() == 1 \
and msg.get_srcComponent() == 1:
telemetry = msg.to_dict()
disconnect(master)
return telemetry
def fallback():
master, vehicle = connect()
print("Fallback (10m back and left, 10m up)!")
msg = master.mav.set_position_target_local_ned_encode(
0, 0, 0, 9, 0b110111111000, -10, -10, -10, 0, 0, 0, 0, 0, 0, 0, 0
)
master.mav.send(msg)
wait_for_execution('fallback')
disconnect(master)
def prepare_for_attack(altitude):
master, vehicle = connect()
target_altitude = altitude - 5
print(f"Preparing for attack, target altitude {target_altitude}m")
msg = master.mav.set_position_target_local_ned_encode(
0, 0, 0, 9, 0b110111111000, 0, 0, target_altitude, 0, 0, 0, 0, 0, 0, 0, 0
)
master.mav.send(msg)
wait_for_execution('prepare_for_attack')
disconnect(master)
def attack():
master, vehicle = connect()
msg = master.mav.command_long_encode(
master.target_system,
master.target_component,
mavutil.mavlink.MAV_CMD_DO_SET_SERVO,
0,
6, # Param 1 (AUX channel number)
1100, # Param 2,
0, 0, 0, 0, 0)
master.mav.send(msg)
wait_for_execution('attack')
disconnect(master)
return True
def follow_target(n_coord, e_coord, d_coord, yaw_angle):
master, vehicle = connect()
msg = master.mav.set_position_target_local_ned_encode(
0, 0, 0, 9, 3576,
n_coord,
e_coord,
d_coord,
1, 0, 0, 0, 0, 0, yaw_angle, 0)
master.mav.send(msg)
# waiting for command to be executed
delay = int(n_coord / 7 + 1)
wait_for_execution(None, delay)
disconnect(master)
def target_search(target_lost_count, D_coord):
master, vehicle = connect()
yaw_angle = 0
d = 0
if target_lost_count > vars.target_lost_limit:
yaw_angle = 0.7854 # 15 deg
d = D_coord
msg = master.mav.set_position_target_local_ned_encode(
0, 0, 0, 9, 3576,
-2,
0,
d,
1, 0, 0, 0, 0, 0, yaw_angle, 0)
master.mav.send(msg)
wait_for_execution('target_search')
disconnect(master)
return d