Skip to content

Commit

Permalink
Merge pull request #45 from openppg/develop/free-rtos
Browse files Browse the repository at this point in the history
Rewrite threading to use FreeRTOS
  • Loading branch information
zjwhitehead authored Aug 18, 2023
2 parents 5bf5464 + 5b6b09b commit 3fb3be7
Show file tree
Hide file tree
Showing 17 changed files with 725 additions and 738 deletions.
61 changes: 31 additions & 30 deletions .github/workflows/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ jobs:
name: CPP Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Setup Python
uses: actions/setup-python@v2
uses: actions/setup-python@v3
with:
python-version: 3.x
- run: pip install cpplint
- run: cpplint --linelength 140 --filter=-runtime/int,-build/include_subdir --recursive ./inc/ ./lib/ ./src/
- run: cpplint --linelength 140 --filter=-legal/copyright,-runtime/int,-build/include_subdir --recursive ./inc/ ./lib/ ./src/
pio-build:
name: PlatformIO Build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Setup Python
uses: actions/setup-python@v2
uses: actions/setup-python@v3
with:
python-version: 3.x
- name: Install Platform IO
Expand All @@ -41,28 +41,29 @@ jobs:
with:
name: OpenPPG-CRP2040-SP140.uf2
path: .pio/build/OpenPPG-CRP2040-SP140/firmware.uf2
generate-uf2:
name: Save uf2 for ${{ matrix.device }}
needs: pio-build
runs-on: ubuntu-latest
strategy:
matrix:
device: [OpenPPG-CM0-SP140]
fail-fast: false
steps:
- name: Setup Python
uses: actions/setup-python@v2
- name: Checkout Microsoft uf2 repo
uses: actions/checkout@v2
with:
repository: microsoft/uf2
- name: Download built binary
uses: actions/download-artifact@v2
with:
name: bins
- name: Run uf2 conversion
run: python3 utils/uf2conv.py ${{ matrix.device }}/firmware.bin -c -o ${{ matrix.device }}.uf2
- uses: actions/upload-artifact@v3
with:
name: ${{ matrix.device }}.uf2
path: ${{ matrix.device }}.uf2
# Skip until M0 builds running by default
# generate-uf2:
# name: Save uf2 for ${{ matrix.device }}
# needs: pio-build
# runs-on: ubuntu-latest
# strategy:
# matrix:
# device: [OpenPPG-CM0-SP140]
# fail-fast: false
# steps:
# - name: Setup Python
# uses: actions/setup-python@v2
# - name: Checkout Microsoft uf2 repo
# uses: actions/checkout@v3
# with:
# repository: microsoft/uf2
# - name: Download built binary
# uses: actions/download-artifact@v2
# with:
# name: bins
# - name: Run uf2 conversion
# run: python3 utils/uf2conv.py ${{ matrix.device }}/firmware.bin -c -o ${{ matrix.device }}.uf2
# - uses: actions/upload-artifact@v3
# with:
# name: ${{ matrix.device }}.uf2
# path: ${{ matrix.device }}.uf2
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ X4 code has been migrated to a separate repo - https://github.com/openppg/x4-con
It may not be stable and is not recommended for flying.
See stable releases [here](https://github.com/openppg/eppg-controller/releases)

> Version 6.0 introduced [FreeRTOS](https://www.freertos.org/index.html) and is currently only working with RP2040 processors. For M0/SAMD21 processors please use [version/5 branch](https://github.com/openppg/eppg-controller/tree/version/5).
> For batch 3 (non-telemetry) controllers please see the [batch-3 branch](https://github.com/openppg/eppg-controller/tree/batch-3).
> For batch 2 (Arduino nano based) controllers please see the [batch-2 branch](https://github.com/openppg/eppg-controller/tree/batch-2).
Expand Down
17 changes: 17 additions & 0 deletions inc/sp140/altimeter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef INC_SP140_ALTIMETER_H_
#define INC_SP140_ALTIMETER_H_

#include <Arduino.h>

#include "sp140/structs.h"

// Set up the barometer
void setupAltimeter();

// Get the altitude (in meters)
float getAltitude(const STR_DEVICE_DATA_140_V1& deviceData);

// Set the ground altitude to the current altitude to compute AGL
void setGroundAltitude(const STR_DEVICE_DATA_140_V1& deviceData);

#endif // INC_SP140_ALTIMETER_H_
73 changes: 73 additions & 0 deletions inc/sp140/display.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#ifndef INC_SP140_DISPLAY_H_
#define INC_SP140_DISPLAY_H_

#include <Arduino.h>

#include "sp140/structs.h"
#include <Adafruit_ST7735.h>
#include "utilities.h"

// Library config
#define NO_ADAFRUIT_SSD1306_COLOR_COMPATIBILITY

#define BLACK ST77XX_BLACK
#define WHITE ST77XX_WHITE
#define GREEN ST77XX_GREEN
#define YELLOW ST77XX_YELLOW
#define RED ST77XX_RED
#define BLUE ST77XX_BLUE
#define ORANGE ST77XX_ORANGE
#define CYAN ST77XX_CYAN
#define PURPLE 0x780F
#define GRAY 0xDEFB

// Light mode (default)
// #define DEFAULT_TEXT_COLOR BLACK
// #define ERROR_TEXT_COLOR RED
// #define CHILL_TEXT_COLOR CYAN

// #define DEFAULT_BG_COLOR WHITE
// #define ARMED_BG_COLOR CYAN
// #define CRUISE_BG_COLOR YELLOW
// #define UI_ACCENT_COLOR BLACK

// Dark mode
// #define DEFAULT_TEXT_COLOR WHITE
// #define ERROR_TEXT_COLOR RED
// #define CHILL_TEXT_COLOR CYAN

// #define DEFAULT_BG_COLOR BLACK
// #define ARMED_BG_COLOR BLUE
// #define CRUISE_BG_COLOR ORANGE
// #define UI_ACCENT_COLOR GRAY

struct UIColors {
uint16_t default_text;
uint16_t error_text;
uint16_t chill_text;
uint16_t default_bg;
uint16_t armed_bg;
uint16_t cruise_bg;
uint16_t ui_accent;
};

extern float watts;
extern float wattHoursUsed;

// Set up the display and show splash screen
void setupDisplay(const STR_DEVICE_DATA_140_V1& deviceData);

void displayMeta(const STR_DEVICE_DATA_140_V1& deviceData, int duration = 2000);

// Clear screen and reset properties
void resetRotation(unsigned int orientation);

// Show data on screen
void updateDisplay(const STR_DEVICE_DATA_140_V1& deviceData,
const STR_ESC_TELEMETRY_140& escTelemetry,
float altitude, bool armed, bool cruising,
unsigned int armedStartMillis);

void setTheme(int theme); // 0,1

#endif // INC_SP140_DISPLAY_H_
33 changes: 4 additions & 29 deletions inc/sp140/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

byte escData[ESC_DATA_SIZE];
byte escDataV2[ESC_DATA_V2_SIZE];
unsigned long cruisedAtMilis = 0;
unsigned long cruisedAtMillis = 0;
unsigned long transmitted = 0;
unsigned long failed = 0;
bool cruising = false;
Expand All @@ -17,42 +17,17 @@ bool batteryFlag = true;
bool throttledFlag = true;
bool throttled = false;
unsigned long throttledAtMillis = 0;
unsigned int throttleSecs = 0;
float minutes = 0;
float prevMinutes = 0;
float seconds = 0;
float prevSeconds = 0;
float hours = 0; // logged flight hours
float wattsHoursUsed = 0;

float watts = 0;
float wattHoursUsed = 0;

// sensor states
bool bmpPresent = false;
bool vibePresent = false;

uint16_t _volts = 0;
uint16_t _temperatureC = 0;
int16_t _amps = 0;
uint32_t _eRPM = 0;
uint16_t _inPWM = 0;
uint16_t _outPWM = 0;

// ESC Telemetry
float prevVolts = 0;
float prevAmps = 0;
float watts = 0;
float prevKilowatts = 0;
float prevKwh = 0;

// ALTIMETER
float ambientTempC = 0;
float altitudeM = 0;
float lastAltM = 0;

Adafruit_BMP3XX bmp;
Servo esc; // Creating a servo class with name of esc

static STR_DEVICE_DATA_140_V1 deviceData;

uint16_t bottom_bg_color = DEFAULT_BG_COLOR;

#endif // INC_SP140_GLOBALS_H_
18 changes: 3 additions & 15 deletions inc/sp140/shared-config.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#define MAMP_OFFSET 200
#define VOLT_OFFSET 1.5

#define VERSION_MAJOR 5
#define VERSION_MINOR 9
#define VERSION_MAJOR 6
#define VERSION_MINOR 0

#define CRUISE_GRACE 1.5 // 1.5 sec period to get off throttle
#define POT_SAFE_LEVEL 0.05 * 4096 // 5% or less
Expand All @@ -24,26 +24,14 @@
#define ESC_MIN_PWM 1030 // ESC min is 1050
#define ESC_MAX_PWM 1990 // ESC max 1950

#define BLACK ST77XX_BLACK
#define WHITE ST77XX_WHITE
#define GREEN ST77XX_GREEN
#define YELLOW ST77XX_YELLOW
#define RED ST77XX_RED
#define BLUE ST77XX_BLUE
#define ORANGE ST77XX_ORANGE
#define PURPLE 0x780F

#define DEFAULT_BG_COLOR WHITE
#define ARMED_BG_COLOR ST77XX_CYAN
#define CRUISE_BG_COLOR YELLOW

#define DIGIT_ARRAY_SIZE 7
#define ESC_BAUD_RATE 115200
#define ESC_DATA_SIZE 20
#define ESC_DATA_V2_SIZE 22
#define READ_INTERVAL 0
#define ESC_TIMEOUT 15
#define ENABLE_BUZ true // enable buzzer
#define ENABLE_VIB_LOW_BAT false // vibrate if armed and battery voltage sags below min volts. Gets pilot's attention.
#define POT_MAX_VALUE 4095 // 12 bit ADC //TODO use calibration and store in EEPROM

#endif // INC_SP140_SHARED_CONFIG_H_
2 changes: 1 addition & 1 deletion inc/sp140/structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ typedef struct {
bool metric_alt; // false
uint8_t performance_mode; // 0,1,2
uint16_t batt_size; // 4000 (4kw) or 2000 (2kw)
uint8_t btn_mode; // for future use
uint8_t theme; // 0,1 for light/dark
uint8_t unused; // for future use
uint16_t crc; // error check
}STR_DEVICE_DATA_140_V1;
Expand Down
6 changes: 6 additions & 0 deletions inc/sp140/utilities.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef INC_SP140_UTILITIES_H_
#define INC_SP140_UTILITIES_H_

double mapd(double x, double in_min, double in_max, double out_min, double out_max);

#endif // INC_SP140_UTILITIES_H_
13 changes: 6 additions & 7 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
libr_dir = libraries
include_dir = inc
default_envs =
OpenPPG-CM0-SP140
;OpenPPG-CM0-SP140 ; not currently supported
OpenPPG-CRP2040-SP140

[extra]
Expand Down Expand Up @@ -46,7 +46,7 @@ lib_ignore =
${extra.lib_ignore}

[env:OpenPPG-CRP2040-SP140]
platform = https://github.com/openppg/platform-raspberrypi.git#190d06ec0ece2f38031389c8b5eccf2bd3d349e9
platform = https://github.com/openppg/platform-raspberrypi.git#d558b307a404efbfc4282ce9e61d16f338f1c172
board = sparkfun_promicrorp2040
framework = arduino
board_build.core = earlephilhower
Expand All @@ -58,17 +58,16 @@ board_build.filesystem_size = 14M ; 14 Mbyte for filesystem and 2 Mbyte for prog
src_folder = sp140
extra_scripts = pre:extra_script.py
lib_deps =
[email protected]
[email protected]
[email protected] ; deprecated
[email protected]
[email protected]
[email protected] ; deprecated
[email protected]
adafruit/Adafruit [email protected]
adafruit/Adafruit BMP3XX [email protected]
adafruit/Adafruit DRV2605 [email protected]
adafruit/Adafruit ST7735 and ST7789 Library@1.9.3
adafruit/Adafruit ST7735 and ST7789 Library@1.10.2
https://github.com/rlogiacco/CircularBuffer
Adafruit GFX [email protected].5
Adafruit GFX [email protected].7
lib_ignore =
Adafruit SleepyDog Library
${extra.lib_ignore}
32 changes: 32 additions & 0 deletions src/sp140/altimeter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "sp140/altimeter.h"
#include "sp140/structs.h"

#include <Adafruit_BMP3XX.h>

Adafruit_BMP3XX bmp;
bool bmpPresent = false;
float groundAltitude = 0;

float getAltitude(const STR_DEVICE_DATA_140_V1& deviceData) {
if (bmpPresent) {
const float altitude = bmp.readAltitude(deviceData.sea_pressure);
return altitude - groundAltitude;
}
return __FLT_MIN__;
}

// set the ground altitude to the current altitude
void setGroundAltitude(const STR_DEVICE_DATA_140_V1& deviceData) {
groundAltitude = bmp.readAltitude(deviceData.sea_pressure);
}

// Start the bmp388 sensor
void setupAltimeter() {
if (!bmp.begin_I2C()) return;
bmp.setOutputDataRate(BMP3_ODR_25_HZ);
bmp.setTemperatureOversampling(BMP3_OVERSAMPLING_2X);
bmp.setPressureOversampling(BMP3_OVERSAMPLING_4X);
bmp.setIIRFilterCoeff(BMP3_IIR_FILTER_COEFF_15);
bmp.readPressure(); // throw away first reading
bmpPresent = true;
}
Loading

0 comments on commit 3fb3be7

Please sign in to comment.