diff options
author | Jakob Stendahl <jakob.stendahl@outlook.com> | 2021-02-10 11:04:13 +0100 |
---|---|---|
committer | Jakob Stendahl <jakob.stendahl@outlook.com> | 2021-02-24 11:31:52 +0100 |
commit | 8cc460085e75a2cda1bb03cc51786c776da6ebcd (patch) | |
tree | 4b92e382ee0325e9d74e0f6fd437194812f13ed6 | |
parent | 318d850719a86d58012689143c01dd0edb6263b5 (diff) | |
download | hoverbit-ble-8cc460085e75a2cda1bb03cc51786c776da6ebcd.tar.gz hoverbit-ble-8cc460085e75a2cda1bb03cc51786c776da6ebcd.zip |
:boom: Refactor HoverBitController to only have API relevant for HoverBit
-rw-r--r-- | source/HoverBitController.cpp | 33 | ||||
-rw-r--r-- | source/HoverBitController.h | 18 | ||||
-rw-r--r-- | source/main.cpp | 12 |
3 files changed, 30 insertions, 33 deletions
diff --git a/source/HoverBitController.cpp b/source/HoverBitController.cpp index 16c6bf1..9860b43 100644 --- a/source/HoverBitController.cpp +++ b/source/HoverBitController.cpp @@ -44,8 +44,7 @@ void HoverBitController::init(MicroBit* _uBit) { roll = 0; yaw = 0; throttle = 0; - failSafeC = 0; - receiveTime = (*uBit).systemTime(); + lastReceiveTime = (*uBit).systemTime(); /* I am not completly sure what this does, but it seems to me like this is putting the air:bit board in some kind of "bind-mode", on the spec-sheet @@ -62,14 +61,15 @@ void HoverBitController::init(MicroBit* _uBit) { * This is not implemented yet. */ bool HoverBitController::failSafe(void) { - unsigned long deltaReceiveTime = (*uBit.systemTime()) - receiveTime; + unsigned long deltaReceiveTime = (*uBit).systemTime() - lastReceiveTime; if (deltaReceiveTime > FSAFE_TLIM_THROTTLE) { Throttle(0); - Roll(0); - Servo1(0); + Rudder(0); + AirBit(0, arm, 0, throttle, roll, roll + 45, servo_1); } if (deltaReceiveTime > FSAFE_TLIM_ARM) { Arm(0); + AirBit(0, arm, 0, throttle, roll, roll + 45, servo_1); } return (deltaReceiveTime > FSAFE_TLIM_THROTTLE) || (deltaReceiveTime > FSAFE_TLIM_ARM); } @@ -77,7 +77,7 @@ bool HoverBitController::failSafe(void) { /** * This returns the current voltage of the battery. */ -unsigned int HoverBitController::getBatteryVoltage() { +unsigned int HoverBitController::GetBatteryVoltage() { float batteryFactor = 4.42; int batteryMilliVolt = 3700; return ((float)((&(*uBit).io.P0)->getAnalogValue()) * batteryFactor * 0.05) + ((float)batteryMilliVolt * 0.95); @@ -164,22 +164,15 @@ void HoverBitController::Throttle(int _throttle) { else { throttle = _throttle; } lastReceiveTime = (*uBit).systemTime(); } -int HoverBitController::Servo1() { - return servo_1; -} -void HoverBitController::Servo1(int _servo1) { - if (_servo1 > 180) { servo_1 = 180; } - else if (_servo1 < 0) { servo_1 = 0; } - else { servo_1 = _servo1; } - lastReceiveTime = (*uBit).systemTime(); -} -int HoverBitController::Roll() { +int HoverBitController::Rudder() { + // The AirBit uses the roll parameter to control the hoverbit's rudder. return roll; } -void HoverBitController::Roll(int _roll) { - if (_roll > 90) { roll = 90; } - else if (_roll < -90) { roll = -90; } - else { roll = _roll; } +void HoverBitController::Rudder(int _rudder) { + // The AirBit uses the roll parameter to control the hoverbit's rudder. + if (_rudder > 90) { roll = 90; } + else if (_rudder < -90) { roll = -90; } + else { roll = _rudder; } lastReceiveTime = (*uBit).systemTime(); } bool HoverBitController::Arm() { diff --git a/source/HoverBitController.h b/source/HoverBitController.h index 477e96e..0c2960a 100644 --- a/source/HoverBitController.h +++ b/source/HoverBitController.h @@ -31,6 +31,12 @@ DEALINGS IN THE SOFTWARE. #define FSAFE_TLIM_THROTTLE 1000 // When to cut the throttle #define FSAFE_TLIM_ARM 5000 // When to disarm +/** + * This class can be used to interface with a AirBit card for controlling a HOVER:BIT kit. + * + * A lot of the features of the airbit is ignored here and made easy to understand if all + * you want to do is use it for a hoverbit. + */ class HoverBitController { private: MicroBit* uBit; @@ -42,7 +48,7 @@ class HoverBitController { int pitch; int yaw; int throttle; - unsigned long receiveTime; + unsigned long lastReceiveTime; bool mainController; bool batteryEmpty; @@ -50,19 +56,17 @@ class HoverBitController { float batteryFactor; bool failSafe(void); + void AirBit(int Pitch,int Arm,int Roll,int Throttle,int Yaw,int Aux1,int Aux2); public: void init(MicroBit* _uBit); - unsigned int getBatteryVoltage(void); - void AirBit(int Pitch,int Arm,int Roll,int Throttle,int Yaw,int Aux1,int Aux2); + unsigned int GetBatteryVoltage(void); void HoverControl(); int Throttle(); void Throttle(int _throttle); - int Servo1(); - void Servo1(int _servo1); - int Roll(); - void Roll(int _roll); + int Rudder(); + void Rudder(int _rudder); bool Arm(); void Arm(bool _arm); bool BatteryEmpty(); diff --git a/source/main.cpp b/source/main.cpp index caf8ffe..eaf3f30 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -77,17 +77,17 @@ void onConnected(MicroBitEvent) { int value = atoi(msg.substring(startI, startI + valLength).toCharArray()); if (cCommand == 'R') { - controller.Roll(value); - accString = accString + ManagedString("R") + ManagedString(controller.Roll()); + controller.Rudder(value); + accString = accString + ManagedString("R") + ManagedString(controller.Rudder()); } else if (cCommand == 'T') { controller.Throttle(value); accString = accString + ManagedString("T") + ManagedString(controller.Throttle()); } else if (cCommand == 'A') { controller.Arm(value == 1); accString = accString + ManagedString("A") + ManagedString(controller.Arm()); - } else if (cCommand == 'S') { - controller.Servo1(value); - accString = accString + ManagedString("S") + ManagedString(controller.Servo1()); + } + } else { + // We ignore it :) } cCommand = cChar; @@ -261,7 +261,7 @@ int main() { uBit.audio.soundExpressions.play(ManagedString("hello")); while (1) { - batteryMilliVolt = controller.getBatteryVoltage(); + batteryMilliVolt = controller.GetBatteryVoltage(); if (uBit.logo.isPressed()) { if (!bCapLogoIsPressed) { |