From 31c0ec6191af1a9d8f491876942a404d4f85468a Mon Sep 17 00:00:00 2001 From: Jakob Stendahl Date: Wed, 24 Feb 2021 11:45:14 +0100 Subject: :truck: Move header files to inc folder --- inc/HoverBitController.h | 75 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 inc/HoverBitController.h (limited to 'inc/HoverBitController.h') diff --git a/inc/HoverBitController.h b/inc/HoverBitController.h new file mode 100644 index 0000000..0c2960a --- /dev/null +++ b/inc/HoverBitController.h @@ -0,0 +1,75 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 British Broadcasting Corporation. +This software is provided by Lancaster University by arrangement with the BBC. + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. +*/ +#ifndef HOVERBITCONTROLLER_H_ +#define HOVERBITCONTROLLER_H_ + +#include + +#define BATTERY_LOW_LIMIT 3500 +#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; + + int buzzer; + int servo_1; + int arm; + int roll; + int pitch; + int yaw; + int throttle; + unsigned long lastReceiveTime; + + bool mainController; + bool batteryEmpty; + int batteryMilliVolt; + 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 HoverControl(); + + int Throttle(); + void Throttle(int _throttle); + int Rudder(); + void Rudder(int _rudder); + bool Arm(); + void Arm(bool _arm); + bool BatteryEmpty(); +}; + +#endif // HOVERBITCONTROLLER_H_ -- cgit v1.2.3 From 7989505bf43c6dd0ea846f075337261f52e7bd5f Mon Sep 17 00:00:00 2001 From: Jakob Stendahl Date: Wed, 24 Feb 2021 13:37:24 +0100 Subject: :art: Use extern MicroBit instead of sending pointer to classes --- inc/HoverBitController.h | 6 +++--- source/HoverBitController.cpp | 21 ++++++++++----------- source/main.cpp | 2 +- 3 files changed, 14 insertions(+), 15 deletions(-) (limited to 'inc/HoverBitController.h') diff --git a/inc/HoverBitController.h b/inc/HoverBitController.h index 0c2960a..80ad173 100644 --- a/inc/HoverBitController.h +++ b/inc/HoverBitController.h @@ -31,6 +31,8 @@ DEALINGS IN THE SOFTWARE. #define FSAFE_TLIM_THROTTLE 1000 // When to cut the throttle #define FSAFE_TLIM_ARM 5000 // When to disarm +extern MicroBit uBit; + /** * This class can be used to interface with a AirBit card for controlling a HOVER:BIT kit. * @@ -39,8 +41,6 @@ DEALINGS IN THE SOFTWARE. */ class HoverBitController { private: - MicroBit* uBit; - int buzzer; int servo_1; int arm; @@ -59,7 +59,7 @@ class HoverBitController { void AirBit(int Pitch,int Arm,int Roll,int Throttle,int Yaw,int Aux1,int Aux2); public: - void init(MicroBit* _uBit); + void init(); unsigned int GetBatteryVoltage(void); void HoverControl(); diff --git a/source/HoverBitController.cpp b/source/HoverBitController.cpp index fc4c51b..2e908c3 100644 --- a/source/HoverBitController.cpp +++ b/source/HoverBitController.cpp @@ -31,8 +31,7 @@ DEALINGS IN THE SOFTWARE. * * @param _uBit the MicroBit instance */ -void HoverBitController::init(MicroBit* _uBit) { - uBit = _uBit; +void HoverBitController::init() { mainController = false; batteryEmpty = false; batteryMilliVolt = 3700; @@ -44,16 +43,16 @@ void HoverBitController::init(MicroBit* _uBit) { roll = 0; yaw = 0; throttle = 0; - lastReceiveTime = 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 there isn't any documentation for what 20 pulses means tho... */ - uBit->sleep(100); + uBit.sleep(100); int o; for (o = 0; o < 20; o++) { AirBit(-90, 0, 90, 0, 90, 0, 0); - uBit->sleep(20); + uBit.sleep(20); } } @@ -61,7 +60,7 @@ void HoverBitController::init(MicroBit* _uBit) { * This is not implemented yet. */ bool HoverBitController::failSafe(void) { - unsigned long deltaReceiveTime = uBit->systemTime() - lastReceiveTime; + unsigned long deltaReceiveTime = uBit.systemTime() - lastReceiveTime; if (deltaReceiveTime > FSAFE_TLIM_THROTTLE) { Throttle(0); Rudder(0); @@ -80,7 +79,7 @@ bool HoverBitController::failSafe(void) { unsigned int HoverBitController::GetBatteryVoltage() { float batteryFactor = 4.42; int batteryMilliVolt = 3700; - return ((float)((&uBit->io.P0)->getAnalogValue()) * batteryFactor * 0.05) + ((float)batteryMilliVolt * 0.95); + return ((float)((&uBit.io.P0)->getAnalogValue()) * batteryFactor * 0.05) + ((float)batteryMilliVolt * 0.95); } /** @@ -143,7 +142,7 @@ void HoverBitController::AirBit(int Pitch,int Arm,int Roll,int Throttle,int Yaw, buf[13] = aux1S & 255; buf[14] = (6 << 2) | ((aux2S >> 8) & 3); buf[15] = aux2S & 255; - uBit->serial.send(buf, 16, SYNC_SPINWAIT); + uBit.serial.send(buf, 16, SYNC_SPINWAIT); } /** @@ -162,7 +161,7 @@ void HoverBitController::Throttle(int _throttle) { if (_throttle > 99) { throttle = 100; } else if (_throttle < 0) { throttle = 0; } else { throttle = _throttle; } - lastReceiveTime = uBit->systemTime(); + lastReceiveTime = uBit.systemTime(); } int HoverBitController::Rudder() { // The AirBit uses the roll parameter to control the hoverbit's rudder. @@ -173,14 +172,14 @@ void HoverBitController::Rudder(int _rudder) { if (_rudder > 90) { roll = 90; } else if (_rudder < -90) { roll = -90; } else { roll = _rudder; } - lastReceiveTime = uBit->systemTime(); + lastReceiveTime = uBit.systemTime(); } bool HoverBitController::Arm() { return (arm == 1); } void HoverBitController::Arm(bool _arm) { arm = (int)_arm; - lastReceiveTime = uBit->systemTime(); + lastReceiveTime = uBit.systemTime(); } bool HoverBitController::BatteryEmpty() { return batteryEmpty; diff --git a/source/main.cpp b/source/main.cpp index 8d5e837..de11801 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -258,7 +258,7 @@ int main() { /* Initialize hover:bit controller module * the init procedure have to be run within 100ms after air:bit power up */ - controller.init(&uBit); + controller.init(); // Setup listeners uBit.messageBus.listen(MICROBIT_ID_BLE, MICROBIT_BLE_EVT_CONNECTED, onConnected); -- cgit v1.2.3 From c4554532612d139de31723267cb1c3192ff6d4a8 Mon Sep 17 00:00:00 2001 From: Jakob Stendahl Date: Sun, 28 Feb 2021 11:50:53 +0100 Subject: :sparkles: Add emptyBattery code --- inc/HoverBitController.h | 3 ++- source/HoverBitController.cpp | 21 +++++++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) (limited to 'inc/HoverBitController.h') diff --git a/inc/HoverBitController.h b/inc/HoverBitController.h index 80ad173..f2c10b7 100644 --- a/inc/HoverBitController.h +++ b/inc/HoverBitController.h @@ -51,11 +51,12 @@ class HoverBitController { unsigned long lastReceiveTime; bool mainController; - bool batteryEmpty; + bool bBatteryEmpty; int batteryMilliVolt; float batteryFactor; bool failSafe(void); + void checkBattery(); void AirBit(int Pitch,int Arm,int Roll,int Throttle,int Yaw,int Aux1,int Aux2); public: diff --git a/source/HoverBitController.cpp b/source/HoverBitController.cpp index df9893e..1ecc21b 100644 --- a/source/HoverBitController.cpp +++ b/source/HoverBitController.cpp @@ -32,7 +32,7 @@ DEALINGS IN THE SOFTWARE. */ void HoverBitController::init() { mainController = false; - batteryEmpty = false; + bBatteryEmpty = false; batteryMilliVolt = 3700; batteryFactor = 4.42; @@ -81,6 +81,18 @@ unsigned int HoverBitController::GetBatteryVoltage() { return ((float)((&uBit.io.P0)->getAnalogValue()) * batteryFactor * 0.05) + ((float)batteryMilliVolt * 0.95); } +/** + * Check wether battery level is too low. + */ +void HoverBitController::checkBattery() { + if (GetBatteryVoltage() < BATTERY_LOW_LIMIT - 60) { + bBatteryEmpty = true; + Throttle(0); + Arm(0); + Rudder(0); + } +} + /** * Method for sending commands to the AirBit-card, * this code is translated from the ts-code in MakeKit's original hex-file. @@ -148,6 +160,10 @@ void HoverBitController::AirBit(int Pitch,int Arm,int Roll,int Throttle,int Yaw, * Method that sends commands with the current values for all parameters. */ void HoverBitController::HoverControl() { + checkBattery(); + if (BatteryEmpty()) { + Arm(0); + } if (!failSafe()) { AirBit(0, arm, 0, throttle, roll, roll + 45, servo_1); } @@ -181,5 +197,6 @@ void HoverBitController::Arm(bool _arm) { lastReceiveTime = uBit.systemTime(); } bool HoverBitController::BatteryEmpty() { - return batteryEmpty; + checkBattery(); + return bBatteryEmpty; } -- cgit v1.2.3