|
| 1 | +// |
| 2 | +// Created by clemens on 26.07.24. |
| 3 | +// |
| 4 | + |
| 5 | +#include "emergency_service.hpp" |
| 6 | + |
| 7 | +#include "../../globals.hpp" |
| 8 | + |
| 9 | +bool EmergencyService::Configure() { |
| 10 | + // No config needed |
| 11 | + return true; |
| 12 | +} |
| 13 | +void EmergencyService::OnStart() { |
| 14 | + emergency_reason = "Boot"; |
| 15 | + // set the emergency and notify services |
| 16 | + chMtxLock(&mower_status_mutex); |
| 17 | + mower_status |= MOWER_FLAG_EMERGENCY_LATCH; |
| 18 | + chMtxUnlock(&mower_status_mutex); |
| 19 | + chEvtBroadcastFlags(&mower_events, MOWER_EVT_EMERGENCY_CHANGED); |
| 20 | +} |
| 21 | + |
| 22 | +void EmergencyService::OnStop() { |
| 23 | + emergency_reason = "Stopped"; |
| 24 | + // set the emergency and notify services |
| 25 | + chMtxLock(&mower_status_mutex); |
| 26 | + mower_status |= MOWER_FLAG_EMERGENCY_LATCH; |
| 27 | + chMtxUnlock(&mower_status_mutex); |
| 28 | + chEvtBroadcastFlags(&mower_events, MOWER_EVT_EMERGENCY_CHANGED); |
| 29 | +} |
| 30 | +void EmergencyService::OnCreate() {} |
| 31 | + |
| 32 | +void EmergencyService::tick() { |
| 33 | + // Get the current emergency state |
| 34 | + chMtxLock(&mower_status_mutex); |
| 35 | + uint32_t status_copy = mower_status; |
| 36 | + chMtxUnlock(&mower_status_mutex); |
| 37 | + bool emergency_latch = (status_copy & MOWER_FLAG_EMERGENCY_LATCH) != 0; |
| 38 | + bool emergency_active = (status_copy & MOWER_FLAG_EMERGENCY_ACTIVE) != 0; |
| 39 | + // Check timeout, but only overwrite if no emergency is currently active |
| 40 | + // reasoning is that we want to keep the original reason and not overwrite |
| 41 | + // with "timeout" |
| 42 | + if (!emergency_latch && |
| 43 | + chVTTimeElapsedSinceX(last_clear_emergency_message_) > TIME_S2I(1)) { |
| 44 | + emergency_reason = "Timeout"; |
| 45 | + // set the emergency and notify services |
| 46 | + chMtxLock(&mower_status_mutex); |
| 47 | + mower_status |= MOWER_FLAG_EMERGENCY_LATCH; |
| 48 | + chMtxUnlock(&mower_status_mutex); |
| 49 | + chEvtBroadcastFlags(&mower_events, MOWER_EVT_EMERGENCY_CHANGED); |
| 50 | + // The last flags did not have emergency yet, so need to set it here as well |
| 51 | + emergency_latch = true; |
| 52 | + } |
| 53 | + |
| 54 | + StartTransaction(); |
| 55 | + SendEmergencyActive(emergency_active); |
| 56 | + |
| 57 | + SendEmergencyLatch(emergency_latch); |
| 58 | + SendEmergencyReason(emergency_reason.c_str(), emergency_reason.length()); |
| 59 | + CommitTransaction(); |
| 60 | +} |
| 61 | + |
| 62 | +bool EmergencyService::OnSetEmergencyChanged(const uint8_t& new_value) { |
| 63 | + if (new_value) { |
| 64 | + emergency_reason = "High Level Emergency"; |
| 65 | + // set the emergency and notify services |
| 66 | + chMtxLock(&mower_status_mutex); |
| 67 | + mower_status |= MOWER_FLAG_EMERGENCY_LATCH; |
| 68 | + chMtxUnlock(&mower_status_mutex); |
| 69 | + chEvtBroadcastFlags(&mower_events, MOWER_EVT_EMERGENCY_CHANGED); |
| 70 | + } else { |
| 71 | + // Get the current emergency state |
| 72 | + chMtxLock(&mower_status_mutex); |
| 73 | + uint32_t status_copy = mower_status; |
| 74 | + chMtxUnlock(&mower_status_mutex); |
| 75 | + bool emergency_active = (status_copy & MOWER_FLAG_EMERGENCY_ACTIVE) != 0; |
| 76 | + |
| 77 | + // want to reset emergency, but only do it, if no emergency exists right now |
| 78 | + if (!emergency_active) { |
| 79 | + // clear the emergency and notify services |
| 80 | + chMtxLock(&mower_status_mutex); |
| 81 | + mower_status &= ~MOWER_FLAG_EMERGENCY_LATCH; |
| 82 | + chMtxUnlock(&mower_status_mutex); |
| 83 | + chEvtBroadcastFlags(&mower_events, MOWER_EVT_EMERGENCY_CHANGED); |
| 84 | + emergency_reason = "None"; |
| 85 | + last_clear_emergency_message_ = chVTGetSystemTime(); |
| 86 | + } |
| 87 | + } |
| 88 | + return true; |
| 89 | +} |
0 commit comments