-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
arm-disarm.py
206 lines (149 loc) · 5.37 KB
/
arm-disarm.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
"""
Source: https://mavlink.io/en/messages/common.html#SYS_STATUS
https://mavlink.io/en/messages/common.html#HEARTBEAT
https://mavlink.io/en/messages/common.html#MAV_CMD_COMPONENT_ARM_DISARM
https://mavlink.io/en/messages/common.html#COMMAND_ACK
onboard_control_sensors_health of SYS_STATUS message contains pre-arm status bit as MAV_SYS_STATUS_PREARM_CHECK
base_mode of HEARTBEAT message contain armed status bit as MAV_MODE_FLAG_SAFETY_ARMED
MAV_CMD_COMPONENT_ARM_DISARM can be sent with COMMAND_LONG to arm the vehicle
If COMMAND_ACK has MAV_CMD_COMPONENT_ARM_DISARM in command and MAV_RESULT_ACCEPTED in result, armed or disarmed
"""
import time
import pymavlink.mavutil as utility
import pymavlink.dialects.v20.all as dialect
# arm disarm definitions
VEHICLE_ARM = 1
VEHICLE_DISARM = 0
# connect to vehicle
vehicle = utility.mavlink_connection(device="udpin:127.0.0.1:14560")
# vehicle arm message
vehicle_arm_message = dialect.MAVLink_command_long_message(
target_system=vehicle.target_system,
target_component=vehicle.target_component,
command=dialect.MAV_CMD_COMPONENT_ARM_DISARM,
confirmation=0,
param1=VEHICLE_ARM,
param2=0,
param3=0,
param4=0,
param5=0,
param6=0,
param7=0
)
# vehicle disarm message
vehicle_disarm_message = dialect.MAVLink_command_long_message(
target_system=vehicle.target_system,
target_component=vehicle.target_component,
command=dialect.MAV_CMD_COMPONENT_ARM_DISARM,
confirmation=0,
param1=VEHICLE_DISARM,
param2=0,
param3=0,
param4=0,
param5=0,
param6=0,
param7=0
)
# wait for a heartbeat
vehicle.wait_heartbeat()
# inform user
print("Connected to system:", vehicle.target_system, ", component:", vehicle.target_component)
# check the pre-arm
while True:
# observe the SYS_STATUS messages
message = vehicle.recv_match(type=dialect.MAVLink_sys_status_message.msgname, blocking=True)
# convert to dictionary
message = message.to_dict()
# get sensor health
onboard_control_sensors_health = message["onboard_control_sensors_health"]
# get pre-arm healthy bit
prearm_status_bit = onboard_control_sensors_health & dialect.MAV_SYS_STATUS_PREARM_CHECK
prearm_status = prearm_status_bit == dialect.MAV_SYS_STATUS_PREARM_CHECK
# check prearm
if prearm_status:
# vehicle can be armable
print("Vehicle is armable")
# break the prearm check loop
break
# do below always
while True:
# arm the vehicle
print("Vehicle is arming...")
# send arm message
vehicle.mav.send(vehicle_arm_message)
# wait COMMAND_ACK message
message = vehicle.recv_match(type=dialect.MAVLink_command_ack_message.msgname, blocking=True)
# convert the message to dictionary
message = message.to_dict()
# check if the vehicle is armed
if message["result"] == dialect.MAV_RESULT_ACCEPTED and message["command"] == dialect.MAV_CMD_COMPONENT_ARM_DISARM:
# print that vehicle is armed
print("Vehicle is armed!")
else:
# print that vehicle is not armed
print("Vehicle is not armed!")
# wait some time
time.sleep(10)
# disarm the vehicle
print("Vehicle is disarming...")
# send disarm message
vehicle.mav.send(vehicle_disarm_message)
# check if the vehicle is disarmed
if message["result"] == dialect.MAV_RESULT_ACCEPTED and message["command"] == dialect.MAV_CMD_COMPONENT_ARM_DISARM:
# print that vehicle is disarmed
print("Vehicle is disarmed!")
else:
# print that vehicle is not disarmed
print("Vehicle is not disarmed!")
# wait some time
time.sleep(10)
"""
# do below always
while True:
# arm the vehicle
print("Vehicle is arming...")
# send arm message
vehicle.mav.send(vehicle_arm_message)
# wait until vehicle is armed
while True:
# catch a heartbeat message
message = vehicle.recv_match(type=dialect.MAVLink_heartbeat_message.msgname, blocking=True)
# convert message to dictionary
message = message.to_dict()
# get base mode
base_mode = message["base_mode"]
# get armed status
armed_bit = base_mode & dialect.MAV_MODE_FLAG_SAFETY_ARMED
arm_status = armed_bit == dialect.MAV_MODE_FLAG_SAFETY_ARMED
# check armed status
if arm_status:
# vehicle is armed, exit from infinite loop
break
# print arm status
print("Vehicle is armed!")
# wait some time
time.sleep(10)
# disarm the vehicle
print("Vehicle is disarming...")
# send disarm message
vehicle.mav.send(vehicle_disarm_message)
# wait until vehicle is disarmed
while True:
# catch a heartbeat message
message = vehicle.recv_match(type=dialect.MAVLink_heartbeat_message.msgname, blocking=True)
# convert message to dictionary
message = message.to_dict()
# get base mode
base_mode = message["base_mode"]
# get armed status
armed_bit = base_mode & dialect.MAV_MODE_FLAG_SAFETY_ARMED
arm_status = armed_bit == dialect.MAV_MODE_FLAG_SAFETY_ARMED
# check armed status
if not arm_status:
# vehicle is disarmed, exit from infinite loop
break
# print arm status
print("Vehicle is disarmed!")
# wait some time
time.sleep(10)
"""