|
| 1 | +#include "emergency.h" |
| 2 | + |
| 3 | +#include <pico/mutex.h> |
| 4 | + |
| 5 | +#include "pins.h" |
| 6 | + |
| 7 | +namespace emergency |
| 8 | +{ |
| 9 | + |
| 10 | +namespace |
| 11 | +{ |
| 12 | +constexpr unsigned long STOP_DEBOUNCE_MS = 20; |
| 13 | +constexpr unsigned long LIFT_PERIOD_MS = 100; |
| 14 | +constexpr unsigned long TILT_PERIOD_MS = 2500; |
| 15 | +constexpr unsigned long COMMAND_WINDOW_MS = 1000; |
| 16 | +constexpr uint8_t COMMAND_CONFIRMATIONS_REQUIRED = 3; |
| 17 | + |
| 18 | +auto_init_mutex(emergency_mutex); |
| 19 | + |
| 20 | +bool latch_active = true; |
| 21 | +bool stop_active = false; |
| 22 | +bool lift_active = false; |
| 23 | +bool tilt_active = false; |
| 24 | +bool software_requested = false; |
| 25 | +bool release_blocked = false; |
| 26 | +uint8_t lifted_wheels = 0; |
| 27 | + |
| 28 | +bool raw_stop_active = false; |
| 29 | +bool raw_lift_input_active = false; |
| 30 | + |
| 31 | +unsigned long stop_started_ms = 0; |
| 32 | +unsigned long lift_started_ms = 0; |
| 33 | +unsigned long tilt_started_ms = 0; |
| 34 | + |
| 35 | +bool command_pending = false; |
| 36 | +bool pending_command = false; |
| 37 | +uint8_t pending_command_count = 0; |
| 38 | +unsigned long pending_command_started_ms = 0; |
| 39 | + |
| 40 | +bool isActiveLowInput(uint8_t pin) |
| 41 | +{ |
| 42 | + return digitalRead(pin) == LOW; |
| 43 | +} |
| 44 | + |
| 45 | +bool elapsedSinceStart(bool condition, |
| 46 | + unsigned long& started_ms, |
| 47 | + unsigned long threshold_ms, |
| 48 | + unsigned long now) |
| 49 | +{ |
| 50 | + if (!condition) |
| 51 | + { |
| 52 | + started_ms = 0; |
| 53 | + return false; |
| 54 | + } |
| 55 | + |
| 56 | + if (started_ms == 0) |
| 57 | + { |
| 58 | + started_ms = now; |
| 59 | + return false; |
| 60 | + } |
| 61 | + |
| 62 | + return now - started_ms >= threshold_ms; |
| 63 | +} |
| 64 | + |
| 65 | +bool physicalInputPresent() |
| 66 | +{ |
| 67 | + return raw_stop_active || raw_lift_input_active; |
| 68 | +} |
| 69 | + |
| 70 | +void updateUnlocked() |
| 71 | +{ |
| 72 | + const unsigned long now = millis(); |
| 73 | + |
| 74 | + lifted_wheels = 0; |
| 75 | + if (isActiveLowInput(PIN_EMERGENCY_1)) |
| 76 | + { |
| 77 | + lifted_wheels++; |
| 78 | + } |
| 79 | + if (isActiveLowInput(PIN_EMERGENCY_2)) |
| 80 | + { |
| 81 | + lifted_wheels++; |
| 82 | + } |
| 83 | + |
| 84 | + raw_lift_input_active = lifted_wheels > 0; |
| 85 | + raw_stop_active = isActiveLowInput(PIN_EMERGENCY_3) || isActiveLowInput(PIN_EMERGENCY_4); |
| 86 | + |
| 87 | + stop_active = elapsedSinceStart(raw_stop_active, stop_started_ms, STOP_DEBOUNCE_MS, now); |
| 88 | + lift_active = elapsedSinceStart(lifted_wheels >= 2, lift_started_ms, LIFT_PERIOD_MS, now); |
| 89 | + tilt_active = elapsedSinceStart(raw_lift_input_active, tilt_started_ms, TILT_PERIOD_MS, now); |
| 90 | + |
| 91 | + if (stop_active || lift_active || tilt_active) |
| 92 | + { |
| 93 | + latch_active = true; |
| 94 | + } |
| 95 | + |
| 96 | + release_blocked = latch_active && physicalInputPresent(); |
| 97 | +} |
| 98 | + |
| 99 | +void requestLatch() |
| 100 | +{ |
| 101 | + software_requested = true; |
| 102 | + latch_active = true; |
| 103 | +} |
| 104 | + |
| 105 | +void requestRelease() |
| 106 | +{ |
| 107 | + updateUnlocked(); |
| 108 | + |
| 109 | + if (physicalInputPresent()) |
| 110 | + { |
| 111 | + release_blocked = true; |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + latch_active = false; |
| 116 | + software_requested = false; |
| 117 | + release_blocked = false; |
| 118 | +} |
| 119 | + |
| 120 | +} // namespace |
| 121 | + |
| 122 | +void init() |
| 123 | +{ |
| 124 | + pinMode(PIN_EMERGENCY_1, INPUT); |
| 125 | + pinMode(PIN_EMERGENCY_2, INPUT); |
| 126 | + pinMode(PIN_EMERGENCY_3, INPUT); |
| 127 | + pinMode(PIN_EMERGENCY_4, INPUT); |
| 128 | +} |
| 129 | + |
| 130 | +void update() |
| 131 | +{ |
| 132 | + mutex_enter_blocking(&emergency_mutex); |
| 133 | + updateUnlocked(); |
| 134 | + mutex_exit(&emergency_mutex); |
| 135 | +} |
| 136 | + |
| 137 | +void handleCommand(bool latch_requested) |
| 138 | +{ |
| 139 | + mutex_enter_blocking(&emergency_mutex); |
| 140 | + |
| 141 | + const unsigned long now = millis(); |
| 142 | + |
| 143 | + if (!command_pending || pending_command != latch_requested |
| 144 | + || now - pending_command_started_ms > COMMAND_WINDOW_MS) |
| 145 | + { |
| 146 | + command_pending = true; |
| 147 | + pending_command = latch_requested; |
| 148 | + pending_command_count = 1; |
| 149 | + pending_command_started_ms = now; |
| 150 | + mutex_exit(&emergency_mutex); |
| 151 | + return; |
| 152 | + } |
| 153 | + |
| 154 | + pending_command_count++; |
| 155 | + if (pending_command_count < COMMAND_CONFIRMATIONS_REQUIRED) |
| 156 | + { |
| 157 | + mutex_exit(&emergency_mutex); |
| 158 | + return; |
| 159 | + } |
| 160 | + |
| 161 | + command_pending = false; |
| 162 | + pending_command_count = 0; |
| 163 | + |
| 164 | + if (latch_requested) |
| 165 | + { |
| 166 | + requestLatch(); |
| 167 | + } |
| 168 | + else |
| 169 | + { |
| 170 | + requestRelease(); |
| 171 | + } |
| 172 | + |
| 173 | + mutex_exit(&emergency_mutex); |
| 174 | +} |
| 175 | + |
| 176 | +State getState() |
| 177 | +{ |
| 178 | + mutex_enter_blocking(&emergency_mutex); |
| 179 | + State state = {latch_active, |
| 180 | + stop_active, |
| 181 | + lift_active, |
| 182 | + tilt_active, |
| 183 | + software_requested, |
| 184 | + release_blocked, |
| 185 | + lifted_wheels}; |
| 186 | + mutex_exit(&emergency_mutex); |
| 187 | + return state; |
| 188 | +} |
| 189 | + |
| 190 | +} // namespace emergency |
0 commit comments