This repository was archived by the owner on Dec 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathE1_arm.lua
More file actions
196 lines (177 loc) · 6.22 KB
/
Copy pathE1_arm.lua
File metadata and controls
196 lines (177 loc) · 6.22 KB
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
-- custom arming routine for Align helicopters - version 1.2
-- constants
local FAN_RELAY = 1
local BATT_INSTANCE = 0
local AUX_FUNCTION_MOTOR_INTERLOCK = 32
local AUX_HIGH = 2
local AUX_LOW = 0
local STATES = { WAIT_INTERLOCK_LOW = 0,
WAIT_INTERLOCK_HIGH = 1,
WAIT_THROTTLE_LOW = 2,
WAIT_THROTTLE_LOW_TIMEOUT = 3,
ARM = 4,
ARMED_ON = 5,
ARMED_OFF = 6,
ARMED_ON_SPOOLUP = 7,
ERROR_VIBRATIONS = 8
}
-- global variables
local state = STATES.WAIT_INTERLOCK_LOW
local time_ms = uint32_t(0)
local count = 0
-- parameters
local PARAM_TABLE_KEY = 46
assert(param:add_table(PARAM_TABLE_KEY, "VIBE_", 1), "could not add param table")
assert(param:add_param(PARAM_TABLE_KEY, 1, "TKOFF_MAX", 60), "could not add G3P_DEBUG param")
local VIBE_TKOFF_MAX = Parameter("VIBE_TKOFF_MAX")
function fan_control()
-- fan always on when armed
if state == STATES.ARMED_ON then
if relay:get(FAN_RELAY) == 0 then
relay:on(FAN_RELAY)
end
return
end
-- turn on fan if battery temperature is high
local batt_temp_fan_on = param:get("BATT_SERIAL_NUM")
local batt_temp = battery:get_temperature(BATT_INSTANCE)
-- check nil or out of range variables
if batt_temp_fan_on == nil then
batt_temp_fan_on = 50
end
if batt_temp == nil then
-- fan always ON if no motor temperature available
if relay:get(FAN_RELAY) == 0 then
relay:on(FAN_RELAY)
end
return
end
-- fan logic
if batt_temp > batt_temp_fan_on then
if relay:get(FAN_RELAY) == 0 then
relay:on(FAN_RELAY)
end
elseif batt_temp < batt_temp_fan_on - 5 then
if relay:get(FAN_RELAY) == 1 then
relay:off(FAN_RELAY)
end
end
end
function arm_control()
if state == STATES.WAIT_INTERLOCK_LOW then
if rc:get_pwm(3) < 950 then
-- rc not ready
return update, 100
end
-- need motor interlock low after boot
if rc:get_aux_cached(AUX_FUNCTION_MOTOR_INTERLOCK) == AUX_LOW then
state = STATES.WAIT_INTERLOCK_HIGH
end
elseif state == STATES.WAIT_INTERLOCK_HIGH then
-- need motor interlock high before arming routine and throttle not low
if rc:get_aux_cached(AUX_FUNCTION_MOTOR_INTERLOCK) == AUX_HIGH and rc:get_pwm(3) > 1150 then
time_ms = millis()
state = STATES.WAIT_THROTTLE_LOW
end
elseif state == STATES.WAIT_THROTTLE_LOW then
if rc:get_aux_cached(AUX_FUNCTION_MOTOR_INTERLOCK) == AUX_LOW then
-- user turned off motor
state = STATES.WAIT_INTERLOCK_HIGH
end
if rc:get_pwm(3) > 950 and rc:get_pwm(3) < 1150 then
state = STATES.WAIT_THROTTLE_LOW_TIMEOUT
time_ms = millis()
end
if millis() - time_ms > 10000 then
-- timeout after 10 seconds
state = STATES.WAIT_INTERLOCK_LOW
end
elseif state == STATES.WAIT_THROTTLE_LOW_TIMEOUT then
if rc:get_aux_cached(AUX_FUNCTION_MOTOR_INTERLOCK) == AUX_LOW then
-- user turned off motor
state = STATES.WAIT_INTERLOCK_HIGH
end
if rc:get_pwm(3) > 1150 then
state = STATES.WAIT_THROTTLE_LOW
end
if millis() - time_ms > 2000 then
rc:run_aux_function(AUX_FUNCTION_MOTOR_INTERLOCK, AUX_LOW)
state = STATES.ARM
end
elseif state == STATES.ARM then
if arming:arm() then
-- motor interlock ON
rc:run_aux_function(AUX_FUNCTION_MOTOR_INTERLOCK, AUX_HIGH)
state = STATES.ARMED_ON_SPOOLUP
else
-- arm fail, go back to wait interlock low
state = STATES.WAIT_INTERLOCK_LOW
end
elseif state == STATES.ARMED_ON_SPOOLUP then
if not arming:is_armed() then
state = STATES.WAIT_INTERLOCK_LOW
end
if rc:get_aux_cached(AUX_FUNCTION_MOTOR_INTERLOCK) == AUX_LOW then
-- user turned off motor
time_ms = millis()
state = STATES.ARMED_OFF
end
-- wait for spoolup
if motors:get_spool_state() == 3 then
state = STATES.ARMED_ON
end
-- check vibrations
if ahrs:get_vibration():x() > VIBE_TKOFF_MAX:get() or ahrs:get_vibration():y() > VIBE_TKOFF_MAX:get() or ahrs:get_vibration():z() > VIBE_TKOFF_MAX:get() then
-- turn off motor
rc:run_aux_function(AUX_FUNCTION_MOTOR_INTERLOCK, AUX_LOW)
state = STATES.ERROR_VIBRATIONS
end
elseif state == STATES.ARMED_ON then
if not arming:is_armed() then
state = STATES.WAIT_INTERLOCK_LOW
end
if rc:get_aux_cached(AUX_FUNCTION_MOTOR_INTERLOCK) == AUX_LOW then
-- user turned off motor
time_ms = millis()
state = STATES.ARMED_OFF
end
elseif state == STATES.ARMED_OFF then
if not arming:is_armed() then
state = STATES.WAIT_INTERLOCK_LOW
end
if rc:get_aux_cached(AUX_FUNCTION_MOTOR_INTERLOCK) == AUX_HIGH then
-- user turned on motor
state = STATES.ARMED_ON
end
-- wait for spooldown and disarm
if motors:get_spool_state() < 2 then
if arming:disarm() then
state = STATES.WAIT_INTERLOCK_LOW
end
end
elseif state == STATES.ERROR_VIBRATIONS then
-- no way to get out this state, to prevent user flying again
if arming:is_armed() then
-- force motor off
rc:run_aux_function(AUX_FUNCTION_MOTOR_INTERLOCK, AUX_LOW)
-- wait for spooldown and disarm
if motors:get_spool_state() < 2 then
arming:disarm()
end
end
end
-- send arming state to GCS
gcs:send_named_float("arm", state)
end
function update()
-- call arm_control at 10 Hz and fan_control at 1 Hz
count = count + 1
arm_control()
if count > 9 then
count = 0
fan_control()
end
return update, 100
end
gcs:send_text('6', "E1_arm.lua is running")
return update, 2000