|
4 | 4 | //
|
5 | 5 |
|
6 | 6 | #include "SystemManager.hpp"
|
7 |
| -#include "sbus_receiver.hpp" |
8 |
| -#include "sbus_defines.h" |
9 |
| -#include "rcreceiver_datatypes.h" |
| 7 | + |
10 | 8 | #include "drivers_config.hpp"
|
11 | 9 | #include "independent_watchdog.h"
|
| 10 | +#include "rcreceiver_datatypes.h" |
| 11 | +#include "sbus_defines.h" |
| 12 | +#include "sbus_receiver.hpp" |
12 | 13 | #include "tim.h"
|
13 | 14 |
|
14 |
| -#define TIMEOUT_CYCLES 250000 // 25k = 1 sec fro testing 10/14/2023 => 250k = 10 sec |
| 15 | +#define TIMEOUT_CYCLES 250000 // 25k = 1 sec fro testing 10/14/2023 => 250k = 10 sec |
15 | 16 |
|
16 | 17 | static uint32_t DisconnectionCount = 0;
|
17 | 18 | float prevthrottle;
|
18 | 19 | float prevyaw;
|
19 | 20 | float prevroll;
|
20 | 21 | float prevpitch;
|
21 | 22 |
|
22 |
| -SystemManager::SystemManager(): |
23 |
| - rcController_(sbus_pointer), |
24 |
| - throttleMotorChannel_(&htim2, TIM_CHANNEL_1), |
25 |
| - yawMotorChannel_(&htim2, TIM_CHANNEL_2), |
26 |
| - rollMotorChannel_(&htim2, TIM_CHANNEL_3), |
27 |
| - pitchMotorChannel_(&htim2, TIM_CHANNEL_4), |
28 |
| - invertedRollMotorChannel_(&htim3, TIM_CHANNEL_1), |
29 |
| - watchdog_(&hiwdg) |
30 |
| -{} |
| 23 | +SystemManager::SystemManager() |
| 24 | + : rcController_(sbus_pointer), |
| 25 | + throttleMotorChannel_(&htim2, TIM_CHANNEL_1), |
| 26 | + yawMotorChannel_(&htim2, TIM_CHANNEL_2), |
| 27 | + rollMotorChannel_(&htim2, TIM_CHANNEL_3), |
| 28 | + pitchMotorChannel_(&htim2, TIM_CHANNEL_4), |
| 29 | + invertedRollMotorChannel_(&htim3, TIM_CHANNEL_1), |
| 30 | + watchdog_(&hiwdg) { |
| 31 | + // VARIABLES FOR TELEMETRY MANAGER TO HAVE AS REFERENCES THEY OBV SHOULD BE PUT SOMEWHERE ELSE, |
| 32 | + // BUT I FEEL LIKE SM PM WOULD KNOW WHERE. MAYBE IN THE HPP FILE? IDK HOW YOU ARE PLANNING ON |
| 33 | + // GATHERING THE DATA. I JUST PUT THEM HERE FOR NOW |
| 34 | + int32_t lat = 0; |
| 35 | + int32_t lon = 0; |
| 36 | + int32_t alt = 0; |
| 37 | + int32_t relative_alt = 0; |
| 38 | + int16_t vx = 0; |
| 39 | + int16_t vy = 0; |
| 40 | + int16_t vz = 0; |
| 41 | + uint16_t hdg = 0; |
| 42 | + int32_t time_boot_ms = 0; |
| 43 | + MAV_STATE state = MAV_STATE::MAV_STATE_STANDBY; |
| 44 | + MAV_MODE_FLAG mode = MAV_MODE_FLAG::MAV_MODE_FLAG_MANUAL_INPUT_ENABLED; |
| 45 | + float roll = 0; |
| 46 | + float pitch = 0; |
| 47 | + float yaw = 0; |
| 48 | + float rollspeed = 0; |
| 49 | + float pitchspeed = 0; |
| 50 | + float yawspeed = 0; |
| 51 | + |
| 52 | + this->telemetryManager = |
| 53 | + new TelemetryManager(lat, lon, alt, relative_alt, vx, vy, vz, hdg, time_boot_ms, state, |
| 54 | + mode, roll, pitch, yaw, rollspeed, pitchspeed, yawspeed); |
| 55 | + this->telemetryManager->init(); |
| 56 | + // IDK WHERE SM PLANS TO DO THIS, BUT telemetryManager.update() NEEDS TO BE CALLED AT A SEMI |
| 57 | + // REGULAR INTERVAL AS IT DEALS WITH MESSAGE DECODING AND LOW PRIORITY DATA TRANSMISSION |
| 58 | +} |
31 | 59 |
|
32 | 60 | SystemManager::~SystemManager() {}
|
33 | 61 |
|
34 | 62 | void SystemManager::flyManually() {
|
35 |
| - for(;;){ |
| 63 | + for (;;) { |
36 | 64 | this->rcInputs_ = rcController_->GetRCControl();
|
37 | 65 |
|
38 |
| - //TO-DO: need to implement it using is_Data_New; |
39 |
| - // boolean is true if data has not changed since the last cycle |
40 |
| - bool is_unchanged{ |
41 |
| - rcInputs_.throttle == prevthrottle && |
42 |
| - rcInputs_.yaw == prevyaw && |
43 |
| - rcInputs_.roll == prevroll && |
44 |
| - rcInputs_.pitch == prevpitch |
45 |
| - }; |
| 66 | + // TO-DO: need to implement it using is_Data_New; |
| 67 | + // boolean is true if data has not changed since the last cycle |
| 68 | + bool is_unchanged{rcInputs_.throttle == prevthrottle && rcInputs_.yaw == prevyaw && |
| 69 | + rcInputs_.roll == prevroll && rcInputs_.pitch == prevpitch}; |
46 | 70 |
|
47 | 71 | if (is_unchanged) {
|
48 |
| - DisconnectionCount += 1; // if its not changed we increment the timeout counter |
49 |
| - if (DisconnectionCount > TIMEOUT_CYCLES) { // if timeout has occured |
50 |
| - DisconnectionCount = TIMEOUT_CYCLES+1; // avoid overflow but keep value above threshold |
51 |
| - this->rcInputs_.arm = 0; // failsafe |
52 |
| - } |
| 72 | + DisconnectionCount += 1; // if its not changed we increment the timeout counter |
| 73 | + if (DisconnectionCount > TIMEOUT_CYCLES) { // if timeout has occured |
| 74 | + DisconnectionCount = |
| 75 | + TIMEOUT_CYCLES + 1; // avoid overflow but keep value above threshold |
| 76 | + this->rcInputs_.arm = 0; // failsafe |
| 77 | + } |
53 | 78 | } else {
|
54 |
| - DisconnectionCount = 0; //if the data has changed we want to reset out counter |
| 79 | + DisconnectionCount = 0; // if the data has changed we want to reset out counter |
55 | 80 | }
|
56 | 81 |
|
57 |
| - watchdog_.refreshWatchdog(); // always hit the dog |
| 82 | + watchdog_.refreshWatchdog(); // always hit the dog |
58 | 83 |
|
59 |
| - if(this->rcInputs_.arm >= (SBUS_MAX/2)) { |
60 |
| - this->throttleMotorChannel_.set(rcInputs_.throttle); |
61 |
| - this->yawMotorChannel_.set(rcInputs_.yaw); |
62 |
| - this->rollMotorChannel_.set(SBUS_MAX - rcInputs_.roll); |
63 |
| - this->pitchMotorChannel_.set(SBUS_MAX - rcInputs_.pitch); |
64 |
| - this->invertedRollMotorChannel_.set(SBUS_MAX - rcInputs_.roll); |
| 84 | + if (this->rcInputs_.arm >= (SBUS_MAX / 2)) { |
| 85 | + this->throttleMotorChannel_.set(rcInputs_.throttle); |
| 86 | + this->yawMotorChannel_.set(rcInputs_.yaw); |
| 87 | + this->rollMotorChannel_.set(SBUS_MAX - rcInputs_.roll); |
| 88 | + this->pitchMotorChannel_.set(SBUS_MAX - rcInputs_.pitch); |
| 89 | + this->invertedRollMotorChannel_.set(SBUS_MAX - rcInputs_.roll); |
65 | 90 |
|
66 |
| - prevthrottle = rcInputs_.throttle; |
67 |
| - prevyaw = rcInputs_.yaw; |
68 |
| - prevroll = rcInputs_.roll; |
69 |
| - prevpitch = rcInputs_.pitch; |
| 91 | + prevthrottle = rcInputs_.throttle; |
| 92 | + prevyaw = rcInputs_.yaw; |
| 93 | + prevroll = rcInputs_.roll; |
| 94 | + prevpitch = rcInputs_.pitch; |
70 | 95 |
|
71 |
| - |
72 |
| - } |
73 |
| - else{ |
| 96 | + } else { |
74 | 97 | this->throttleMotorChannel_.set(0);
|
75 |
| - this->yawMotorChannel_.set(SBUS_MAX/2); |
76 |
| - this->rollMotorChannel_.set(SBUS_MAX/2); |
77 |
| - this->pitchMotorChannel_.set(SBUS_MAX/2); |
78 |
| - this->invertedRollMotorChannel_.set(SBUS_MAX/2); |
| 98 | + this->yawMotorChannel_.set(SBUS_MAX / 2); |
| 99 | + this->rollMotorChannel_.set(SBUS_MAX / 2); |
| 100 | + this->pitchMotorChannel_.set(SBUS_MAX / 2); |
| 101 | + this->invertedRollMotorChannel_.set(SBUS_MAX / 2); |
79 | 102 | }
|
80 | 103 | }
|
81 | 104 | }
|
0 commit comments