Skip to content

Commit 7281b58

Browse files
authored
Merge pull request #65 from openppg/v7-1
Enhanced Haptics, Refined Throttle/Cruise Control
2 parents e3e17a1 + 27a1dfa commit 7281b58

File tree

13 files changed

+396
-148
lines changed

13 files changed

+396
-148
lines changed

inc/sp140/buzzer.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,20 @@
55
#include "sp140/structs.h"
66

77
/**
8-
* Initialize the buzzer pin for output
8+
* Initialize the buzzer pin for output using LEDC
9+
* @return Returns true if initialization was successful, false otherwise
910
*/
10-
void initBuzz();
11+
bool initBuzz();
12+
13+
/**
14+
* Start playing a tone at the specified frequency
15+
*/
16+
void startTone(uint16_t frequency);
17+
18+
/**
19+
* Stop playing the tone
20+
*/
21+
void stopTone();
1122

1223
/**
1324
* Plays a melody using the piezo buzzer

inc/sp140/lvgl/lv_conf.h

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -168,15 +168,15 @@
168168
* Enable built-in fonts - only enable a minimal set
169169
*/
170170
#define LV_FONT_MONTSERRAT_8 0
171-
#define LV_FONT_MONTSERRAT_10 1 /* For small text like voltage */
171+
#define LV_FONT_MONTSERRAT_10 0
172172
#define LV_FONT_MONTSERRAT_12 1 /* For medium text, temperatures, and voltage labels */
173173
#define LV_FONT_MONTSERRAT_14 1 /* Used as default font */
174174
#define LV_FONT_MONTSERRAT_16 1 /* For battery percentage and power */
175175
#define LV_FONT_MONTSERRAT_18 0
176-
#define LV_FONT_MONTSERRAT_20 1 /* For status text */
176+
#define LV_FONT_MONTSERRAT_20 0
177177
#define LV_FONT_MONTSERRAT_22 0
178-
#define LV_FONT_MONTSERRAT_24 1 /* For altitude */
179-
#define LV_FONT_MONTSERRAT_26 1
178+
#define LV_FONT_MONTSERRAT_24 1
179+
#define LV_FONT_MONTSERRAT_26 0
180180
#define LV_FONT_MONTSERRAT_28 1
181181
#define LV_FONT_MONTSERRAT_30 0
182182
#define LV_FONT_MONTSERRAT_32 0
@@ -253,9 +253,9 @@ Documentation of the widgets: https://docs.lvgl.io/latest/en/html/widgets/index.
253253

254254
#define LV_USE_LINE 1
255255

256-
#define LV_USE_ROLLER 1 /* Don't need roller */
256+
#define LV_USE_ROLLER 0 /* Don't need roller */
257257

258-
#define LV_USE_SLIDER 1 /* Don't need slider */
258+
#define LV_USE_SLIDER 0 /* Don't need slider */
259259

260260
#define LV_USE_SPAN 1
261261
#if LV_USE_SPAN
@@ -267,10 +267,9 @@ Documentation of the widgets: https://docs.lvgl.io/latest/en/html/widgets/index.
267267

268268
#define LV_USE_TEXTAREA 1 /* Required by other components */
269269

270-
/* Add spinner widget */
271270
#define LV_USE_SPINNER 1
272271

273-
/* Add keyboard widget (set to 0 since we don't use it but dependency might exist) */
272+
/* keyboard widget (set to 0 since we don't use it) */
274273
#define LV_USE_KEYBOARD 0
275274

276275
/*==================
@@ -285,7 +284,7 @@ Documentation of the widgets: https://docs.lvgl.io/latest/en/html/widgets/index.
285284
#define LV_THEME_DEFAULT_DARK 0
286285

287286
/*1: Enable grow on press*/
288-
#define LV_THEME_DEFAULT_GROW 1
287+
#define LV_THEME_DEFAULT_GROW 0
289288

290289
/*Default transition time in [ms]*/
291290
#define LV_THEME_DEFAULT_TRANSITION_TIME 80

inc/sp140/shared-config.h

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,6 @@
44
#ifndef INC_SP140_SHARED_CONFIG_H_
55
#define INC_SP140_SHARED_CONFIG_H_
66

7-
// Batt setting now configurable by user. Read from device data
8-
#define BATT_MIN_V 60.0 // 24 * 2.5V per cell
9-
10-
// Calibration
11-
#define MAMP_OFFSET 200
12-
#define VOLT_OFFSET 1.5
13-
14-
157
#define CRUISE_GRACE 1.5 // 1.5 sec period to get off throttle
168
#define POT_ENGAGEMENT_LEVEL 0.05 * POT_MAX_VALUE // 5% or less // TODO calibrate for each device
179

@@ -24,8 +16,8 @@
2416
#define ESC_MIN_PWM 1035 // ESC min
2517
#define ESC_MAX_PWM 1950 // ESC max 1900
2618

27-
#define ENABLE_BUZ false // enable buzzer
28-
#define ENABLE_VIB_LOW_BAT false // vibrate if armed and battery voltage sags below min volts. Gets pilot's attention.
19+
#define ENABLE_BUZZ true // enable buzzer
20+
#define ENABLE_VIBE true // enable vibration motor
2921
#define POT_MIN_VALUE 0 // 12 bit ADC //TODO: use calibration and store in EEPROM
3022
#define POT_MAX_VALUE 4095 // 12 bit ADC //TODO: use calibration and store in EEPROM
3123

inc/sp140/vibration_pwm.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,47 @@ enum VibePattern {
1414
VIBE_WAVE
1515
};
1616

17+
// Vibration request structure for queue
18+
struct VibeRequest {
19+
uint16_t duration_ms;
20+
uint8_t intensity;
21+
};
22+
23+
// Task and queue handles
24+
extern TaskHandle_t vibeTaskHandle;
25+
extern QueueHandle_t vibeQueue;
26+
27+
/**
28+
* Initialize the vibration motor pin for output using LEDC
29+
* @return Returns true if initialization was successful, false otherwise
30+
*/
1731
bool initVibeMotor();
32+
33+
/**
34+
* Pulse the vibration motor with a single 400ms pulse
35+
*/
1836
void pulseVibeMotor();
37+
38+
/**
39+
* Run a custom vibration pattern using an array of intensities
40+
* @param pattern Array of intensity values (0-255)
41+
* @param patternSize Number of elements in the pattern array
42+
* @return Returns true if pattern was executed successfully, false otherwise
43+
*/
1944
bool runVibePattern(const unsigned int pattern[], int patternSize);
45+
46+
/**
47+
* Execute a predefined vibration pattern
48+
* @param pattern The VibePattern enum value to execute
49+
*/
2050
void executeVibePattern(VibePattern pattern);
51+
52+
/**
53+
* Execute a custom vibration pattern with specified intensities and durations
54+
* @param intensities Array of intensity values (0-255)
55+
* @param durations Array of duration values in milliseconds
56+
* @param steps Number of steps in the pattern
57+
*/
2158
void customVibePattern(const uint8_t intensities[], const uint16_t durations[], int steps);
2259

2360
#endif // INC_SP140_VIBRATION_PWM_H_

inc/version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#pragma once
33

44
#define VERSION_MAJOR 7
5-
#define VERSION_MINOR 0
5+
#define VERSION_MINOR 1
66

77
// Define a version string
88
#define VERSION_STRING STRINGIFY(VERSION_MAJOR) "." STRINGIFY(VERSION_MINOR)

platformio.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ debug_tool = esp-builtin
4242
lib_deps =
4343
Wire
4444
SPI
45-
45+
4646
[email protected] ; deprecated
4747
4848
adafruit/Adafruit [email protected]
4949
adafruit/Adafruit BMP3XX [email protected]
5050
adafruit/Adafruit ST7735 and ST7789 [email protected]
51-
adafruit/Adafruit NeoPixel@1.12.5
51+
adafruit/Adafruit NeoPixel@1.14.0
5252
adafruit/Adafruit [email protected]
5353
adafruit/Adafruit [email protected]
5454
https://github.com/rlogiacco/[email protected]

src/sp140/buzzer.cpp

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,60 @@
22

33
#include <Arduino.h>
44

5-
#include <algorithm>
6-
75
#include "sp140/shared-config.h"
86
#include "sp140/esp32s3-config.h"
97
#include <FreeRTOS.h>
108
#include <semphr.h>
119

1210
#define DEBUG_SERIAL USBSerial
1311

12+
// Buzzer PWM configuration - use different channel than vibration motor
13+
// PWM Channel allocation:
14+
// Channel 0: Vibration motor (vibration_pwm.cpp)
15+
// Channel 1: Buzzer (this file)
16+
// Channels 2-7: Available for future use
17+
const int BUZZER_PWM_CHANNEL = 1;
18+
const int BUZZER_PWM_RESOLUTION = 8;
19+
const int BUZZER_PWM_FREQUENCY = 1000; // Base frequency, will be changed for notes
20+
1421
// External global variables needed
1522
extern HardwareConfig board_config;
1623
extern QueueHandle_t melodyQueue;
1724

25+
static bool buzzerInitialized = false;
26+
27+
/**
28+
* Initialize the buzzer pin for output using LEDC
29+
* @return Returns true if initialization was successful, false otherwise
30+
*/
31+
bool initBuzz() {
32+
// Setup LEDC channel for buzzer
33+
ledcSetup(BUZZER_PWM_CHANNEL, BUZZER_PWM_FREQUENCY, BUZZER_PWM_RESOLUTION);
34+
ledcAttachPin(board_config.buzzer_pin, BUZZER_PWM_CHANNEL);
35+
buzzerInitialized = true;
36+
return true;
37+
}
38+
1839
/**
19-
* Initialize the buzzer pin for output
40+
* Start playing a tone at the specified frequency
2041
*/
21-
void initBuzz() {
22-
pinMode(board_config.buzzer_pin, OUTPUT);
42+
void startTone(uint16_t frequency) {
43+
if (!buzzerInitialized || !ENABLE_BUZZ) return;
44+
45+
// Change the frequency for this channel
46+
ledcChangeFrequency(BUZZER_PWM_CHANNEL, frequency, BUZZER_PWM_RESOLUTION);
47+
// Set 50% duty cycle (square wave)
48+
ledcWrite(BUZZER_PWM_CHANNEL, 128);
49+
}
50+
51+
/**
52+
* Stop playing the tone
53+
*/
54+
void stopTone() {
55+
if (!buzzerInitialized) return;
56+
57+
// Set duty cycle to 0 to stop the tone
58+
ledcWrite(BUZZER_PWM_CHANNEL, 0);
2359
}
2460

2561
/**
@@ -30,7 +66,7 @@ void initBuzz() {
3066
* @return Returns true if the melody was queued successfully, false otherwise
3167
*/
3268
bool playMelody(uint16_t melody[], int siz) {
33-
if (!ENABLE_BUZ) return false;
69+
if (!ENABLE_BUZZ) return false;
3470

3571
// Create a static buffer for the melody
3672
static uint16_t melodyBuffer[32]; // Adjust size as needed
@@ -43,7 +79,7 @@ bool playMelody(uint16_t melody[], int siz) {
4379
MelodyRequest request = {
4480
.notes = melodyBuffer,
4581
.size = (uint8_t)std::min(siz, 32),
46-
.duration = 125 // Default duration
82+
.duration = 100 // Default duration
4783
};
4884

4985
// Send to queue with timeout
@@ -59,6 +95,6 @@ bool playMelody(uint16_t melody[], int siz) {
5995
* Plays a melody to indicate arm failure
6096
*/
6197
void handleArmFailMelody() {
62-
uint16_t arm_fail_melody[] = { 820, 640 };
63-
playMelody(arm_fail_melody, 2);
98+
uint16_t arm_fail_melody[] = { 1760 };
99+
playMelody(arm_fail_melody, 1);
64100
}

src/sp140/esc.cpp

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ STR_ESC_TELEMETRY_140 escTelemetryData = {
2121
.escState = TelemetryState::NOT_CONNECTED
2222
};
2323

24+
/**
25+
* Initialize the ESC communication system
26+
* Sets up the CAN bus (TWAI) interface and configures the ESC with default settings
27+
* Must be called before any other ESC functions
28+
*/
2429
void initESC() {
2530
escTwaiInitialized = setupTWAI();
2631
if (!escTwaiInitialized) {
@@ -39,6 +44,12 @@ void initESC() {
3944
delay(20); // Wait for ESC to process the command
4045
}
4146

47+
/**
48+
* Set the ESC throttle value
49+
* @param throttlePWM Throttle value in microseconds (1000-2000)
50+
* 1000 = minimum throttle, 2000 = maximum throttle
51+
* Note: The ESC requires messages at least every 300ms or it will reset
52+
*/
4253
void setESCThrottle(int throttlePWM) {
4354
// Input validation
4455
if (throttlePWM < 1000 || throttlePWM > 2000) {
@@ -50,6 +61,12 @@ void setESCThrottle(int throttlePWM) {
5061
esc.setThrottleSettings2(scaledThrottle);
5162
}
5263

64+
/**
65+
* Read telemetry data from the ESC
66+
* Updates the global escTelemetryData structure with current values
67+
* Should be called regularly (every 20-50ms) to maintain connection
68+
* Also monitors connection state and sets NOT_CONNECTED if timeout occurs
69+
*/
5370
void readESCTelemetry() {
5471
// Only proceed if TWAI is initialized
5572
if (!escTwaiInitialized) { return; } // NOLINT(whitespace/newline)
@@ -120,7 +137,11 @@ void readESCTelemetry() {
120137
adapter.processTxRxOnce(); // Process CAN messages
121138
}
122139

123-
// CAN specific setup
140+
/**
141+
* Setup the ESP32 TWAI (Two-Wire Automotive Interface) for CAN communication
142+
* Configures the CAN bus at 1Mbps for communication with the ESC
143+
* @return true if setup was successful, false otherwise
144+
*/
124145
bool setupTWAI() {
125146
// Check if already installed by checking status
126147
twai_status_info_t status_info;
@@ -181,6 +202,10 @@ bool setupTWAI() {
181202
return true;
182203
}
183204

205+
/**
206+
* Debug function to dump ESC throttle response data to serial
207+
* @param res Pointer to the throttle response structure from ESC
208+
*/
184209
void dumpThrottleResponse(const sine_esc_SetThrottleSettings2Response *res) {
185210
USBSerial.println("Got SetThrottleSettings2 response");
186211

@@ -227,6 +252,10 @@ void dumpThrottleResponse(const sine_esc_SetThrottleSettings2Response *res) {
227252
USBSerial.println(res->time_10ms);
228253
}
229254

255+
/**
256+
* Debug function to dump all available ESC messages to serial
257+
* Displays hardware info, throttle response, and rotation speed settings if available
258+
*/
230259
void dumpESCMessages(void) {
231260
const SineEscModel &model = esc.getModel();
232261

@@ -251,14 +280,34 @@ void dumpESCMessages(void) {
251280
}
252281
}
253282

283+
/**
284+
* Map a double value from one range to another
285+
* @param x Input value to map
286+
* @param in_min Minimum of input range
287+
* @param in_max Maximum of input range
288+
* @param out_min Minimum of output range
289+
* @param out_max Maximum of output range
290+
* @return Mapped value in output range
291+
*/
254292
double mapDouble(double x, double in_min, double in_max, double out_min, double out_max) {
255293
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
256294
}
257295

296+
/**
297+
* Get the highest temperature reading from all ESC sensors
298+
* @param telemetry ESC telemetry data structure
299+
* @return The highest temperature value among motor, MOSFET, and capacitor temps
300+
*/
258301
float getHighestTemp(const STR_ESC_TELEMETRY_140& telemetry) {
259302
return max(telemetry.motor_temp, max(telemetry.mos_temp, telemetry.cap_temp));
260303
}
261304

305+
/**
306+
* Check temperature state for a specific component
307+
* @param temp Temperature value to check
308+
* @param component Component type (ESC_MOS, ESC_MCU, ESC_CAP, or MOTOR)
309+
* @return Temperature state (NORMAL, WARNING, CRITICAL, or INVALID)
310+
*/
262311
TempState checkTempState(float temp, TempComponent component) {
263312
// Check for invalid temperature readings
264313
if (temp < -50 || temp > 200) {

src/sp140/esp32s3-config.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
#include "../../inc/sp140/esp32s3-config.h"
44

5+
#define LED_BUILTIN 21 // m5 stamp
56

6-
#define LED_BUILTIN 21 // m5 stack
77
// V1 configuration
88
HardwareConfig s3_config = {
99
.button_top = 1,

src/sp140/extra-data.ino

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,10 @@ void setupBLE() {
447447
BLECharacteristic *pFirmwareVersion = pConfigService->createCharacteristic(
448448
BLEUUID(FW_VERSION_UUID),
449449
BLECharacteristic::PROPERTY_READ);
450-
pFirmwareVersion->setValue(VERSION_STRING);
450+
451+
// Use 2-byte format: [major, minor] instead of string
452+
uint8_t versionBytes[2] = {VERSION_MAJOR, VERSION_MINOR};
453+
pFirmwareVersion->setValue(versionBytes, sizeof(versionBytes));
451454

452455
BLECharacteristic *pHardwareRevision = pConfigService->createCharacteristic(
453456
BLEUUID(HW_REVISION_UUID),

0 commit comments

Comments
 (0)