|
6 | 6 | */ |
7 | 7 |
|
8 | 8 | #include "FCAlign.h" |
| 9 | +#include <string.h> |
| 10 | +#include "Endianess.h" |
| 11 | +#include "SensorStore.h" |
| 12 | + |
| 13 | +extern uint8_t PriorityFC; |
9 | 14 |
|
10 | 15 | namespace App |
11 | 16 | { |
12 | 17 |
|
| 18 | +static_assert (sizeof(FCAlign) <= FCAdapter::Workspace, "FCAlign will not fit!"); |
| 19 | + |
| 20 | +void FCAlign::Init() |
| 21 | +{ |
| 22 | + |
| 23 | + static_assert (sizeof(FCAlign::Paket) == 35, "Align PaketSize does not match!"); |
| 24 | + |
| 25 | + m_usart.Init(GPIO_PUPD_PULLDOWN); |
| 26 | + m_usart.SetBaudrate(115200); |
| 27 | + m_usart.SetStopbits(USART_STOPBITS_1); |
| 28 | + m_usart.SetDatabits(8); |
| 29 | + m_usart.SetFlowControl(USART_FLOWCONTROL_NONE); |
| 30 | + m_usart.SetParity(USART_PARITY_NONE); |
| 31 | + m_usart.SetMode(USART_MODE_RX); |
| 32 | + m_usart.DisableOverrunDetection(); |
| 33 | + |
| 34 | + HAL::InterruptRegistry.Enable(m_usart.NVIC_IRQn, PriorityFC, this); |
| 35 | + |
| 36 | + m_usart.EnableRxInterrupt(); |
| 37 | + m_usart.Enable(); |
| 38 | +} |
| 39 | + |
| 40 | +void FCAlign::Run() |
| 41 | +{ |
| 42 | + Paket* tmpPaket; |
| 43 | + volatile uint16_t checksum = 0; |
| 44 | + |
| 45 | + while (IsAlive()) |
| 46 | + { |
| 47 | + if (gotMsg.wait(WaitForDataTimeout)) |
| 48 | + { |
| 49 | + tmpPaket = (Paket*) &m_receiveBuffer[m_processIndex]; |
| 50 | + |
| 51 | + if (tmpPaket->StartByte != StartSign || tmpPaket->Type != PaketTypeOSD) |
| 52 | + { |
| 53 | + //Find next Start sign |
| 54 | + for (; m_processIndex < m_receiveIndex; m_processIndex++) |
| 55 | + { |
| 56 | + tmpPaket = (Paket*) &m_receiveBuffer[m_processIndex]; |
| 57 | + if (tmpPaket->StartByte == StartSign && tmpPaket->Type == PaketTypeOSD) |
| 58 | + break; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + if (tmpPaket->StartByte == StartSign && tmpPaket->Type == PaketTypeOSD) |
| 63 | + { |
| 64 | + if ((m_receiveIndex - m_processIndex) >= (tmpPaket->PaketSize + 4)) |
| 65 | + { |
| 66 | + checksum = calculateChecksum((const uint8_t*) tmpPaket, tmpPaket->PaketSize + 3); |
| 67 | + if (checksum == tmpPaket->Checksum) |
| 68 | + { |
| 69 | + Process(*tmpPaket); |
| 70 | + m_processIndex += tmpPaket->PaketSize + 4; |
| 71 | + } |
| 72 | + else |
| 73 | + m_processIndex += 1; |
| 74 | + |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + if ((m_receiveIndex + TmpReceiveBufferSize) > ReceiveBufferSize) |
| 79 | + { |
| 80 | + m_receiveIndex = m_receiveIndex - m_processIndex; |
| 81 | + memmove(m_receiveBuffer, &m_receiveBuffer[m_processIndex], m_receiveIndex); |
| 82 | + m_processIndex = 0; |
| 83 | + memset(&m_receiveBuffer[m_receiveIndex], 0, ReceiveBufferSize - m_receiveIndex); |
| 84 | + } |
| 85 | + |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + DeInit(); |
| 90 | +} |
| 91 | + |
| 92 | +void FCAlign::Process(Paket& paket) |
| 93 | +{ |
| 94 | + SetHeartbeat(); |
| 95 | + |
| 96 | + SensorData.SetFCType(FCType::Align); |
| 97 | + |
| 98 | + SensorData.SetSensorPresent(Sensors::SGPS, 1); |
| 99 | + SensorData.SetSensorPresent(Sensors::GForce, 1); |
| 100 | + SensorData.SetSensorPresent(Sensors::Vario, 1); |
| 101 | + SensorData.SetSensorPresent(Sensors::Voltage, 1); |
| 102 | + |
| 103 | + SensorData.SetRoll(paket.Roll / 10.f); |
| 104 | + SensorData.SetPitch(paket.Pitch / 10.f); |
| 105 | + |
| 106 | + SensorData.SetPositionCurrent(paket.Latitude / 10000000.f, paket.Longitude / 10000000.f); |
| 107 | + SensorData.SetAltitude(paket.Altitude / 10.f); |
| 108 | + SensorData.SetVerticalSpeed(paket.VerticalVelocity / 10.f); |
| 109 | + SensorData.SetSpeed(paket.Speed / 10.f); |
| 110 | + SensorData.SetBattery(paket.Voltage * 100); |
| 111 | + |
| 112 | + if (paket.Mode == AlignModeManual) |
| 113 | + SensorData.SetFlightMode(FlightMode::Manual); |
| 114 | + else if (paket.Mode == AlignModeAttitude) |
| 115 | + SensorData.SetFlightMode(FlightMode::Attitude); |
| 116 | + else if (paket.Mode == AlignModeGPSVelocity || paket.Mode == AlignModeIOC || paket.Mode == AlignModeGPSAngular) |
| 117 | + SensorData.SetFlightMode(FlightMode::GPS); |
| 118 | + else if (paket.Mode == AlignModeFailsafeVelocity || paket.Mode == AlignModeFailsafeAngular) |
| 119 | + SensorData.SetFlightMode(FlightMode::Failsafe); |
| 120 | + |
| 121 | + SensorData.SetSatellites(paket.Satellites); |
| 122 | + SensorData.SetFixType( |
| 123 | + paket.Satellites == 0 ? GPSFixType::FixNo : paket.Satellites < 4 ? GPSFixType::Fix2D : GPSFixType::Fix3D); |
| 124 | +} |
| 125 | + |
| 126 | +uint8_t FCAlign::calculateChecksum(const uint8_t* data, uint8_t size) |
| 127 | +{ |
| 128 | + uint8_t sum = 0x00; |
| 129 | + |
| 130 | + for (uint8_t j = 0; j < size; j++) |
| 131 | + sum += data[j]; |
| 132 | + |
| 133 | + return sum; |
| 134 | +} |
| 135 | + |
| 136 | +void FCAlign::DeInit(void) |
| 137 | +{ |
| 138 | + m_usart.DisableRxInterrupt(); |
| 139 | + m_usart.Disable(); |
| 140 | + |
| 141 | + HAL::InterruptRegistry.Disable(m_usart.NVIC_IRQn); |
| 142 | + |
| 143 | + m_usart.DeInit(); |
| 144 | +} |
| 145 | + |
| 146 | +void FCAlign::ISR(void) |
| 147 | +{ |
| 148 | + if (m_usart.GetInterruptSource(USART_ISR_RXNE)) |
| 149 | + { |
| 150 | + m_tmpReceiveBuffer[m_tmpReceiveIndex++] = m_usart.Receive(); |
| 151 | + if (m_tmpReceiveIndex == TmpReceiveBufferSize) |
| 152 | + { |
| 153 | + memcpy(&m_receiveBuffer[m_receiveIndex], m_tmpReceiveBuffer, TmpReceiveBufferSize); |
| 154 | + m_receiveIndex += TmpReceiveBufferSize; |
| 155 | + m_tmpReceiveIndex = 0; |
| 156 | + gotMsg.signal_isr(); |
| 157 | + } |
| 158 | + } |
| 159 | +} |
| 160 | + |
13 | 161 | } /* namespace App */ |
0 commit comments