From af0e105366ab821ede673ecc9696b31013e6b475 Mon Sep 17 00:00:00 2001 From: Lukas Bachschwell Date: Mon, 30 Apr 2018 20:46:31 +0200 Subject: [PATCH] Adding Quick selection, light, proper mode definitions and CRUISE MODE --- src/remote.cpp | 230 +++++++++++++++++++++++++++++++++++++++++-------- src/settings.h | 1 - 2 files changed, 195 insertions(+), 36 deletions(-) diff --git a/src/remote.cpp b/src/remote.cpp index 9bf0638..c29fac3 100644 --- a/src/remote.cpp +++ b/src/remote.cpp @@ -22,7 +22,24 @@ Preferences preferences; uint8_t boardData[6] = {0, 0, 0, 0, 0, 0}; -//#define DEBUG +#define M_NORMAL 0 // Limit is a sub of normal +#define M_SELECT 1 +#define M_SETTINGS 2 +#define M_CRUISE 3 +#define M_STEERING 4 + +//#define steeringInfluential + +uint8_t currentMode = M_NORMAL; +uint8_t selectedIndex = 2; + +bool lightActive = false; +bool shouldUpdateSettings = false; // Needed to update limitmode on core 1 instead of 0 + +bool crusing = false; +uint8_t crusingSpeed = 127; + +#define DEBUG #ifdef DEBUG #define DEBUG_PRINTLN(x) Serial.println(x) @@ -48,9 +65,6 @@ unsigned long lastSignalBlink; bool signalBlink = false; unsigned long lastDataRotation; -bool beginnerMode = false; // TODO: moved to setting -bool steeringMode = false; - // Defining variables for Hall Effect throttle. short hallMeasurement; @@ -60,8 +74,8 @@ uint8_t esc2 = 127; byte hallCenterMargin = 5; -const float minVoltage = 3.4; -const float maxVoltage = 4.1; +const float minVoltage = 2.9; // These values are heavily strange since the devider is not working nicely yet... +const float maxVoltage = 3.6; const float refVoltage = 3.3; // Resistors in Ohms const float deviderR1 = 1500; @@ -88,6 +102,15 @@ bool triggerActive(); #include "settings.h" +void setCrusing(uint8_t speed) { + if(speed < 127) { // no backward cruse! + crusing = false; + crusingSpeed = 127; + } else { + crusing = true; + if(speed > crusingSpeed) crusingSpeed = speed; + } +} // ESPNOW functions ############################## // Scan for boards in AP mode @@ -237,6 +260,7 @@ void sendData() { DEBUG_PRINT("Sending: "); DEBUG_PRINTLN(esc1); esp_err_t result = esp_now_send(peer_addr, data, sizeof(data)); DEBUG_PRINT("Send Status: "); + if(result != ESP_OK) setCrusing(0); if (result == ESP_OK) { DEBUG_PRINTLN("Success"); } else if (result == ESP_ERR_ESPNOW_NOT_INIT) { @@ -268,6 +292,7 @@ void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) { } else { connected = false; DEBUG_PRINTLN("Delivery Fail"); + setCrusing(0); } } @@ -315,7 +340,7 @@ void calculateThrottlePosition() { DEBUG_PRINTLN(hallMeasurement); int maxSpeed = 255; - if(settings[limitMode] == 1) maxSpeed = 190; + if(settings[limitMode] == 1) maxSpeed = 180; if (hallMeasurement >= HAL_CENTER) { throttle = c_map(hallMeasurement, HAL_CENTER, HAL_MAX, 127, maxSpeed); @@ -387,12 +412,42 @@ void checkClicks(void * parameter) { int timeSinceLastClick = millis()-lastClick; lastClick = millis(); + if(currentMode == M_SELECT) { + switch(selectedIndex) { + case 0: + currentMode = M_SETTINGS; + break; + case 1: + if(settings[limitMode] == 0) settings[limitMode] = 1; + else settings[limitMode] = 0; + shouldUpdateSettings = true; + currentMode = M_NORMAL; + break; + case 2: + lightActive = !lightActive; + currentMode = M_NORMAL; + break; + case 3: + currentMode = M_STEERING; + break; + case 4: + currentMode = M_CRUISE; + break; + } + } + if(timeSinceLastClick < clickDiff) clickCounter++; else clickCounter = 1; if(clickCounter == 3) { DEBUG_PRINTLN("YEAH TRIPPLE"); - if(changeSettings) changeSettings = false; - else steeringMode = !steeringMode; + + if(currentMode == M_NORMAL) { + currentMode = M_SELECT; + selectedIndex = 2; + } else { + currentMode = M_NORMAL; + } + vTaskDelay(pdMS_TO_TICKS(2000)); } @@ -430,7 +485,8 @@ void drawBatteryLevel() { void drawThrottle() { int x = 0; int y = 18; - + uint8_t displayThrottle = throttle; + if(crusing) displayThrottle = crusingSpeed; // Draw throttle u8g2.drawHLine(x, y, 52); u8g2.drawVLine(x, y, 10); @@ -438,14 +494,14 @@ void drawThrottle() { u8g2.drawHLine(x, y + 10, 5); u8g2.drawHLine(x + 52 - 4, y + 10, 5); - if (throttle >= 127) { - int width = map(throttle, 127, 255, 0, 49); + if (displayThrottle >= 127) { + int width = map(displayThrottle, 127, 255, 0, 49); for (int i = 0; i < width; i++) { u8g2.drawVLine(x + i + 2, y + 2, 7); } } else { - int width = map(throttle, 0, 126, 49, 0); + int width = map(displayThrottle, 0, 126, 49, 0); for (int i = 0; i < width; i++) { u8g2.drawVLine(x + 50 - i, y + 2, 7); } @@ -577,18 +633,95 @@ void drawPage() { u8g2.drawStr(x + 86 + 2, y + 13, displayBuffer); } + +void controlSelect() { + if (hallMeasurement >= (HAL_MAX - 250) && settingsLoopFlag == false) { //settings[maxHallValue] + // Up + if (selectedIndex != 0) { + selectedIndex--; + settingsLoopFlag = true; + } + } else if (hallMeasurement <= (HAL_MIN + 250) && settingsLoopFlag == false) { //settings[minHallValue] + // Down + if (selectedIndex < 4) { + selectedIndex++; + settingsLoopFlag = true; + } + } else if (inRange(hallMeasurement, HAL_CENTER - 50, HAL_CENTER + 50)) { // settings[centerHallValue] + settingsLoopFlag = false; + } +} + +String selectionItems[5] = { + "Se","Li","B","St","Cr" +}; + +void drawSelectionMenu() { + const uint8_t y = 35; + const uint8_t xStart = 10; + const uint8_t xSpace = 20; + + u8g2.setFontMode(0); + u8g2.setDrawColor(1); + + String title = "Select Action"; + title.toCharArray(displayBuffer, 20); + u8g2.setFont(u8g2_font_helvR10_tr ); + u8g2.drawStr(20, 12, displayBuffer); + + for(int i = 0; i < 5; i++) { + if(selectedIndex == i) { + u8g2.setFontMode(0); + u8g2.drawBox(xStart-2 + xSpace * i, y-12, 15, 15); + u8g2.setDrawColor(0); + } + + selectionItems[i].toCharArray(displayBuffer, 10); + u8g2.setFont(u8g2_font_profont12_tr); + u8g2.drawStr(xStart + xSpace * i, y, displayBuffer); + + u8g2.setFontMode(0); + u8g2.setDrawColor(1); + } +} + +void drawLight() { + if(lightActive) { + displayString = "Light"; + displayString.toCharArray(displayBuffer, 12); + u8g2.setFont(u8g2_font_profont10_tr); + u8g2.drawStr(100, 38, displayBuffer); + } +} + void drawMode() { - if(steeringMode) { + if(currentMode == M_STEERING) { displayString = (String)esc1 + " |S| " + (String)esc2; displayString.toCharArray(displayBuffer, 12); u8g2.setFont(u8g2_font_profont12_tr); u8g2.drawStr(25, 50, displayBuffer); - } - if(settings[limitMode] == 1) { + } else if(currentMode == M_NORMAL && settings[limitMode] == 1) { displayString = "LIM"; displayString.toCharArray(displayBuffer, 12); u8g2.setFont(u8g2_font_profont12_tr); u8g2.drawStr(105, 50, displayBuffer); + } else if(currentMode == M_CRUISE) { + if(crusing) { + displayString = "ACTIVE: " + (String) crusingSpeed; + displayString.toCharArray(displayBuffer, 12); + u8g2.setFont(u8g2_font_profont12_tr); + u8g2.drawStr(0, 50, displayBuffer); + } else { + displayString = "Inactive"; + displayString.toCharArray(displayBuffer, 12); + u8g2.setFont(u8g2_font_profont12_tr); + u8g2.drawStr(0, 50, displayBuffer); + } + + displayString = "CRU"; + displayString.toCharArray(displayBuffer, 12); + u8g2.setFont(u8g2_font_profont12_tr); + u8g2.drawStr(105, 50, displayBuffer); } } @@ -596,8 +729,9 @@ void updateMainDisplay() { u8g2.firstPage(); do { - - if (changeSettings == true) { + if(currentMode == M_SELECT) { + drawSelectionMenu(); + } else if (currentMode == M_SETTINGS) { drawSettingsMenu(); drawSettingNumber(); } else { @@ -605,6 +739,7 @@ void updateMainDisplay() { drawPage(); drawBatteryLevel(); drawSignal(); + drawLight(); drawMode(); } @@ -661,7 +796,7 @@ void setup() { drawStartScreen(); if (triggerActive()) { - changeSettings = true; + currentMode = M_SETTINGS; drawTitleScreen("Remote Settings"); } @@ -697,7 +832,7 @@ void setup() { void loop() { updateMainDisplay(); - readAccel(); + if(currentMode == M_STEERING) readAccel(); Serial.print("Accel Value Left Right( Y): "); Serial.print(map(AcY, -15000, 15000, -100, 100)); @@ -705,41 +840,61 @@ void loop() { Serial.println(map(GyY, -15000, 15000, -100, 100)); calculateThrottlePosition(); - if (changeSettings == true) { + + if (currentMode == M_SELECT) { + controlSelect(); + esc1 = 127; + esc2 = 127; + } else if (currentMode == M_SETTINGS) { // Use throttle and trigger to change settings controlSettingsMenu(); } else { // Use throttle and trigger to drive motors if (triggerActive()) { - if(!steeringMode) { - esc1 = (uint8_t) throttle; - esc2 = (uint8_t) throttle; - } else { + if(currentMode == M_STEERING) { DEBUG_PRINT("Value: "); - int map = c_map(AcX, 15000, -15000, -50, 50); + int map = c_map(AcX, 15000, -15000, -60, 60); DEBUG_PRINTLN(map); - esc1 = (uint8_t) constrain(throttle + map, 0, 200); - esc2 = (uint8_t) constrain(throttle - map, 0, 200); - +#ifdef steeringInfluential + esc1 = (uint8_t) constrain(throttle - map, 0, 200); + esc2 = (uint8_t) constrain(throttle + map, 0, 200); +#else + esc1 = (uint8_t) constrain(127 - map, 0, 200); + esc2 = (uint8_t) constrain(127 + map, 0, 200); + if(-10 < map && map < 10) { + esc1 = throttle; + esc2 = throttle; + } +#endif if(throttle == 127) { esc1 = 127; esc2 = 127; } DEBUG_PRINT("SteeringMode: ESC1: "); DEBUG_PRINT(esc1); DEBUG_PRINT(" ESC2: "); DEBUG_PRINTLN(esc2); + } else if(currentMode == M_CRUISE) { + + setCrusing(throttle); + + esc1 = (uint8_t) throttle; + esc2 = (uint8_t) throttle; + + if(crusing) { + esc1 = (uint8_t) crusingSpeed; + esc2 = (uint8_t) crusingSpeed; + } + + } else { + esc1 = (uint8_t) throttle; + esc2 = (uint8_t) throttle; } } else { // 127 is the middle position - no throttle and no brake/reverse esc1 = 127; esc2 = 127; - if(throttle < 30) { // enter settings mode - changeSettings = true; - } - // could use the other side for cruise control... - if(throttle > 230) { - } + if(crusing) setCrusing(0); } } @@ -759,4 +914,9 @@ void loop() { else { // No board found to process } + + if(shouldUpdateSettings) { + updateSettings(); + shouldUpdateSettings = false; + } } diff --git a/src/settings.h b/src/settings.h index abaf968..0166360 100644 --- a/src/settings.h +++ b/src/settings.h @@ -15,7 +15,6 @@ float ratioRpmSpeed; float ratioPulseDistance; // Defining variables for Settings menu -bool changeSettings = false; bool changeSelectedSetting = false; bool settingsLoopFlag = false;