From 318d850719a86d58012689143c01dd0edb6263b5 Mon Sep 17 00:00:00 2001 From: Jakob Stendahl Date: Wed, 10 Feb 2021 10:56:36 +0100 Subject: :sprakles: Add failsafe code (addressing #5) --- source/HoverBitController.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'source/HoverBitController.h') diff --git a/source/HoverBitController.h b/source/HoverBitController.h index 4963cd1..477e96e 100644 --- a/source/HoverBitController.h +++ b/source/HoverBitController.h @@ -27,7 +27,9 @@ DEALINGS IN THE SOFTWARE. #include -#define BATTERY_LOW_LIMIT 3500 +#define BATTERY_LOW_LIMIT 3500 +#define FSAFE_TLIM_THROTTLE 1000 // When to cut the throttle +#define FSAFE_TLIM_ARM 5000 // When to disarm class HoverBitController { private: @@ -40,16 +42,17 @@ class HoverBitController { int pitch; int yaw; int throttle; - int failSafeC; + unsigned long receiveTime; bool mainController; bool batteryEmpty; int batteryMilliVolt; float batteryFactor; + bool failSafe(void); + public: void init(MicroBit* _uBit); - void failSafe(void); unsigned int getBatteryVoltage(void); void AirBit(int Pitch,int Arm,int Roll,int Throttle,int Yaw,int Aux1,int Aux2); void HoverControl(); -- cgit v1.2.3 From 8cc460085e75a2cda1bb03cc51786c776da6ebcd Mon Sep 17 00:00:00 2001 From: Jakob Stendahl Date: Wed, 10 Feb 2021 11:04:13 +0100 Subject: :boom: Refactor HoverBitController to only have API relevant for HoverBit --- source/HoverBitController.cpp | 33 +++++++++++++-------------------- source/HoverBitController.h | 18 +++++++++++------- source/main.cpp | 12 ++++++------ 3 files changed, 30 insertions(+), 33 deletions(-) (limited to 'source/HoverBitController.h') 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) { -- cgit v1.2.3 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 --- CMakeLists.txt | 3 +- inc/HoverBitController.h | 75 +++++++++++++++++++++++++++++++++++++++++++++ inc/Screen.h | 73 +++++++++++++++++++++++++++++++++++++++++++ source/HoverBitController.h | 75 --------------------------------------------- source/Screen.h | 73 ------------------------------------------- 5 files changed, 150 insertions(+), 149 deletions(-) create mode 100644 inc/HoverBitController.h create mode 100644 inc/Screen.h delete mode 100644 source/HoverBitController.h delete mode 100644 source/Screen.h (limited to 'source/HoverBitController.h') diff --git a/CMakeLists.txt b/CMakeLists.txt index ac32e4b..879568c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,6 +43,7 @@ sbeParseJson(codal codal_json) set(CODAL_APP_OUTPUT_DIR ".") set(CODAL_APP_SOURCE_DIR "source") +set(CODAL_APP_INCLUDE_DIR "inc") if("${codal.application}" STRGREATER "") set(CODAL_APP_SOURCE_DIR "${codal.application}") @@ -231,7 +232,7 @@ if("${device.libraries}" STRGREATER "") endif() #finally, find sources and includes of the application, and create a target. -RECURSIVE_FIND_DIR(INCLUDE_DIRS "./inc" "${PROJECT_SOURCE_DIR}/${CODAL_APP_SOURCE_DIR}" "*.h") +RECURSIVE_FIND_DIR(INCLUDE_DIRS "${PROJECT_SOURCE_DIR}/${CODAL_APP_INCLUDE_DIR}" "*.h") # *.c?? only catches .cpp, not .c, so let's be precise RECURSIVE_FIND_FILE(SOURCE_FILES "${PROJECT_SOURCE_DIR}/${CODAL_APP_SOURCE_DIR}" "*.cpp") 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_ diff --git a/inc/Screen.h b/inc/Screen.h new file mode 100644 index 0000000..b60ba2b --- /dev/null +++ b/inc/Screen.h @@ -0,0 +1,73 @@ +/* +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 SCREEN_H_ +#define SCREEN_H_ + +#include + +enum DisplayMainScreenMode { GRAPHS, BATTERY, OFF }; + +const char* const strBattDead = "\ + 000,255,255,255,000\n\ + 255,000,255,000,255\n\ + 255,255,255,255,255\n\ + 000,255,000,255,000\n\ + 000,255,000,255,000\n"; +const char* const strBattLow = "\ + 000,000,255,000,000\n\ + 000,255,255,255,000\n\ + 000,255,000,255,000\n\ + 000,255,000,255,000\n\ + 000,255,255,255,000\n"; +static const char* const strBattLevel[] = { + "\ + 000,000,255,000,000\n\ + 000,255,000,255,000\n\ + 000,255,000,255,000\n\ + 000,255,000,255,000\n\ + 000,255,255,255,000\n", + "\ + 000,000,255,000,000\n\ + 000,255,000,255,000\n\ + 000,255,000,255,000\n\ + 000,255,255,255,000\n\ + 000,255,255,255,000\n", + "\ + 000,000,255,000,000\n\ + 000,255,000,255,000\n\ + 000,255,255,255,000\n\ + 000,255,255,255,000\n\ + 000,255,255,255,000\n", + "\ + 000,000,255,000,000\n\ + 000,255,255,255,000\n\ + 000,255,255,255,000\n\ + 000,255,255,255,000\n\ + 000,255,255,255,000\n" +}; + +void plotYLine(MicroBit *uBit, int y1, int y2, int x); + +#endif // SCREEN_H_ diff --git a/source/HoverBitController.h b/source/HoverBitController.h deleted file mode 100644 index 0c2960a..0000000 --- a/source/HoverBitController.h +++ /dev/null @@ -1,75 +0,0 @@ -/* -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_ diff --git a/source/Screen.h b/source/Screen.h deleted file mode 100644 index b60ba2b..0000000 --- a/source/Screen.h +++ /dev/null @@ -1,73 +0,0 @@ -/* -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 SCREEN_H_ -#define SCREEN_H_ - -#include - -enum DisplayMainScreenMode { GRAPHS, BATTERY, OFF }; - -const char* const strBattDead = "\ - 000,255,255,255,000\n\ - 255,000,255,000,255\n\ - 255,255,255,255,255\n\ - 000,255,000,255,000\n\ - 000,255,000,255,000\n"; -const char* const strBattLow = "\ - 000,000,255,000,000\n\ - 000,255,255,255,000\n\ - 000,255,000,255,000\n\ - 000,255,000,255,000\n\ - 000,255,255,255,000\n"; -static const char* const strBattLevel[] = { - "\ - 000,000,255,000,000\n\ - 000,255,000,255,000\n\ - 000,255,000,255,000\n\ - 000,255,000,255,000\n\ - 000,255,255,255,000\n", - "\ - 000,000,255,000,000\n\ - 000,255,000,255,000\n\ - 000,255,000,255,000\n\ - 000,255,255,255,000\n\ - 000,255,255,255,000\n", - "\ - 000,000,255,000,000\n\ - 000,255,000,255,000\n\ - 000,255,255,255,000\n\ - 000,255,255,255,000\n\ - 000,255,255,255,000\n", - "\ - 000,000,255,000,000\n\ - 000,255,255,255,000\n\ - 000,255,255,255,000\n\ - 000,255,255,255,000\n\ - 000,255,255,255,000\n" -}; - -void plotYLine(MicroBit *uBit, int y1, int y2, int x); - -#endif // SCREEN_H_ -- cgit v1.2.3