Browse Source

ESPNOW fixes, Icons, Lights and much more

master
Lukas Bachschwell 3 years ago
parent
commit
25baa3a5bb
  1. 4
      platformio.ini
  2. 43
      src/lights.h
  3. 2
      src/mac_config.h
  4. 10
      src/receiver.cpp
  5. 114
      src/remote.cpp
  6. 6
      src/rpm.h
  7. 31
      src/settings.h

4
platformio.ini

@ -16,7 +16,7 @@ build_flags = -DLOG_LOCAL_LEVEL=ESP_LOG_DEBUG
src_filter = +<remote.cpp>
monitor_baud = 115200
monitor_speed = 115200
upload_speed = 921600
@ -27,7 +27,7 @@ framework = arduino
src_filter = +<receiver.cpp>
monitor_baud = 115200
monitor_speed = 115200
; upload_speed = 921600
lib_deps =

43
src/lights.h

@ -3,6 +3,7 @@
#define NUM_LEDS 40
#define PIN_STRIP 17
bool shouldUpdateLights = false;
int lightMode = 0;
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUM_LEDS, PIN_STRIP, NEO_GRB + NEO_KHZ800);
@ -24,10 +25,33 @@ void lightWhite() {
portENABLE_INTERRUPTS();
}
void lightPolice() {
// Temporary mode to test
for(int i = 0; i < NUM_LEDS; i++) {
pixels.setPixelColor(i, pixels.Color(0,0,180));
}
portDISABLE_INTERRUPTS();
pixels.show();
portENABLE_INTERRUPTS();
}
void lightBlinkers(bool isLeft) {
// Temporary mode to test
for(int i = 0; i < NUM_LEDS; i++) {
if(i<10 && isLeft) pixels.setPixelColor(i, pixels.Color(180,180,0));
else if(i>30 && !isLeft) pixels.setPixelColor(i, pixels.Color(180,180,0));
else pixels.setPixelColor(i, pixels.Color(180,180,180));
}
portDISABLE_INTERRUPTS();
pixels.show();
portENABLE_INTERRUPTS();
}
void lightBackFront() {
// Implement me :-)
for(int i = 0; i < NUM_LEDS; i++) {
pixels.setPixelColor(i, pixels.Color(180,180,180));
if(i<5 || i>34) pixels.setPixelColor(i, pixels.Color(190,0,0));
else pixels.setPixelColor(i, pixels.Color(180,180,180));
}
portDISABLE_INTERRUPTS();
pixels.show();
@ -36,7 +60,20 @@ void lightBackFront() {
void updateLights() {
if(lightActive) {
lightWhite();
switch(lightMode) {
case 0:
lightBackFront();
break;
case 1:
lightBlinkers(true); // left
break;
case 2:
lightBlinkers(false); // right
break;
case 3:
lightPolice();
break;
}
}else {
lightOff();
}

2
src/mac_config.h

@ -4,4 +4,4 @@
uint8_t mac_receiver[] = {0xB4, 0xE6, 0x2D, 0x8C, 0x34, 0xAE};
// remote macStr
uint8_t mac_remote[] = {0x24, 0x0a, 0xc4, 0x82, 0x51, 0x70};
uint8_t mac_remote[] = {0xB4, 0xE6, 0x2D, 0x8C, 0x38, 0x79};

10
src/receiver.cpp

@ -62,15 +62,14 @@ bool lightActive = false;
int fanMode = FANS_AUTO;
#include "lights.h"
void setBoardOptions(uint8_t options) {
Serial.print("opt: ");
Serial.println(options, BIN);
if(lightActive != options & 1) {
lightActive = options & 1;
if(lightActive != (options >> 4) & 1) {
lightActive = (options >> 4) & 1;
shouldUpdateLights = true;
}
lightMode = options & 3;
fanMode = (options >> 2) & 3;
}
@ -312,6 +311,7 @@ void setup() {
Serial.println("ESPNow Init Failed");
ESP.restart();
}
esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
// Once ESPNow is successfully Init, we will register for recv CB to
// get recv packer info.

114
src/remote.cpp

@ -74,28 +74,26 @@ uint8_t esc2 = 127;
byte hallCenterMargin = 5;
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 minVoltage = 3.4;
const float maxVoltage = 4.2;
const float refVoltage = 3.3;
// Resistors in Ohms
const float deviderR1 = 1500;
const float deviderR2 = 22000;
const float deviderR1 = 22000;
const float deviderR2 = 11200;
// Global copy of board
esp_now_peer_info_t board;
#define CHANNEL 1
#define PRINTSCANRESULTS 0
#define DELETEBEFOREPAIR 0
#define HAL_MIN 1390
#define HAL_MAX 2230
#define HAL_CENTER 1880
#define TRIM_LOW 180
#define TRIM_HIGH 0
#define HAL_MIN 1390 // defaults
#define HAL_MAX 2230 // defaults
#define HAL_CENTER 1880 // defaults
//#define pairingMode
#define leverPin 36
#define triggerPin 17
#define batteryMeasurePin 38
#define batteryMeasurePin 39
bool triggerActive();
@ -113,6 +111,16 @@ void setCrusing(uint8_t speed) {
}
// ESPNOW functions ##############################
// Scan for boards in AP mode
// config AP
void configDeviceAP(bool hidden) {
bool result = WiFi.softAP("ESK8", "ESK8_Password+vD8z2YAvoDBW?Zx", CHANNEL, hidden);
if (!result) {
Serial.println("AP Config failed.");
} else {
Serial.println("AP Config Success. Broadcasting with AP: " + String("ESK8"));
}
}
#ifdef pairingMode
void ScanForBoard() {
@ -158,6 +166,7 @@ void ScanForBoard() {
board.channel = CHANNEL; // pick a channel
board.encrypt = 0; // no encryption
board.ifidx = ESP_IF_WIFI_STA;
boardFound = 1;
// we are planning to have only one board in this example;
@ -180,6 +189,7 @@ void ScanForBoard() {
void deletePeer() {
const esp_now_peer_info_t *peer = &board;
const uint8_t *peer_addr = board.peer_addr;
esp_err_t delStatus = esp_now_del_peer(peer_addr);
DEBUG_PRINT("board Delete Status: ");
@ -272,8 +282,11 @@ void sendData() {
DEBUG_PRINTLN("ESP_ERR_ESPNOW_NO_MEM");
} else if (result == ESP_ERR_ESPNOW_NOT_FOUND) {
DEBUG_PRINTLN("Peer not found.");
} else if (result == ESP_ERR_ESPNOW_IF) {
DEBUG_PRINTLN("Interface error.");
} else {
DEBUG_PRINTLN("Not sure what happened");
DEBUG_PRINT("Not sure what happened ");
DEBUG_PRINTLN(result);
}
}
@ -340,10 +353,10 @@ void calculateThrottlePosition() {
int maxSpeed = 255;
if(settings[limitMode] == 1) maxSpeed = 180;
if (hallMeasurement >= HAL_CENTER) {
throttle = c_map(hallMeasurement, HAL_CENTER, HAL_MAX, 127, maxSpeed);
if (hallMeasurement >= settings[centerHallValue]) {
throttle = c_map(hallMeasurement, settings[centerHallValue], settings[maxHallValue], 127, maxSpeed);
} else {
throttle = c_map(hallMeasurement, HAL_MIN, HAL_CENTER, 0, 127);
throttle = c_map(hallMeasurement, settings[minHallValue], settings[centerHallValue], 0, 127);
}
// removeing center noise
if (abs(throttle - 127) < hallCenterMargin) {
@ -647,25 +660,29 @@ void drawPage() {
}
void controlSelect() {
if (hallMeasurement >= (HAL_MAX - 250) && settingsLoopFlag == false) { //settings[maxHallValue]
if (hallMeasurement >= (settings[maxHallValue] - 250) && settingsLoopFlag == false) { //settings[maxHallValue]
// Up
if (selectedIndex != 0) {
selectedIndex--;
settingsLoopFlag = true;
}
} else if (hallMeasurement <= (HAL_MIN + 250) && settingsLoopFlag == false) { //settings[minHallValue]
} else if (hallMeasurement <= (settings[minHallValue] + 250) && settingsLoopFlag == false) {
// Down
if (selectedIndex < 4) {
selectedIndex++;
settingsLoopFlag = true;
}
} else if (inRange(hallMeasurement, HAL_CENTER - 50, HAL_CENTER + 50)) { // settings[centerHallValue]
} else if (inRange(hallMeasurement, settings[centerHallValue] - 50, settings[centerHallValue] + 50)) { // settings[centerHallValue]
settingsLoopFlag = false;
}
}
String selectionItems[5] = {
"Se","Li","B","St","Cr"
String selectionTitles[5] = {
"Settings","Limit","Light","Steering","Cruise"
};
uint16_t selectionGlyphs[5] = {
0x0081, 0x008d, 0x0103, 0x00f6, 0x0088
};
void drawSelectionMenu() {
@ -676,21 +693,25 @@ void drawSelectionMenu() {
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) {
String title = selectionTitles[i];
title.toCharArray(displayBuffer, 20);
u8g2.setFont(u8g2_font_helvR10_tr );
u8g2.drawStr(30, 12, displayBuffer);
u8g2.setFontMode(0);
u8g2.drawBox(xStart-2 + xSpace * i, y-12, 15, 15);
//u8g2.drawBox(xStart-2 + xSpace * i, y-12, 15, 15);
u8g2.drawBox(xStart + 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);
//selectionItems[i].toCharArray(displayBuffer, 10);
//u8g2.setFont(u8g2_font_profont12_tr);
//u8g2.drawStr(xStart + xSpace * i, y, displayBuffer);
u8g2.setFont(u8g2_font_open_iconic_all_2x_t);
u8g2.drawGlyph(xStart + xSpace * i, y, selectionGlyphs[i]);
u8g2.setFontMode(0);
u8g2.setDrawColor(1);
@ -776,7 +797,11 @@ void drawStartScreen() {
void setup() {
Serial.begin(115200);
//Set device in STA mode to begin with
WiFi.mode(WIFI_STA);
//WiFi.mode(WIFI_STA);
//Set device in AP mode to begin with
WiFi.mode(WIFI_AP_STA);
// configure device AP mode
configDeviceAP(true);
DEBUG_PRINTLN("ESPNowSkate");
loadSettings();
@ -787,8 +812,6 @@ void setup() {
delay(50);
digitalWrite(16, HIGH);
analogSetPinAttenuation(batteryMeasurePin, ADC_6db);
// setup other pins
pinMode(triggerPin, INPUT_PULLUP);
@ -813,8 +836,8 @@ void setup() {
}
// This is the mac address of the Master in Station Mode
DEBUG_PRINT("STA MAC: "); DEBUG_PRINTLN(WiFi.macAddress());
DEBUG_PRINT("STA MAC: "); DEBUG_PRINTLN(WiFi.macAddress());
if (esp_now_init() == ESP_OK) {
DEBUG_PRINTLN("ESPNow Init Success");
}
@ -839,18 +862,27 @@ void setup() {
}
board.channel = CHANNEL; // pick a channel
board.encrypt = 0; // no encryption
//board.ifidx = ESP_IF_WIFI_STA;
}
void loop() {
updateMainDisplay();
if(currentMode == M_STEERING) readAccel();
//if(currentMode == M_STEERING) readAccel();
readAccel();
DEBUG_PRINT("Accel Value Left Right (Y): ");
DEBUG_PRINT(map(AcY, -15000, 15000, -100, 100));
//Serial.print("Help(Y): ");
//Serial.println(map(GyY, -15000, 15000, -100, 100));
Serial.print("Accel Value Left Right( Y): ");
Serial.print(map(AcY, -15000, 15000, -100, 100));
Serial.print("Help(Y): ");
Serial.println(map(GyY, -15000, 15000, -100, 100));
DEBUG_PRINT(" (X): ");
DEBUG_PRINT(map(AcX, -15000, 15000, -100, 100));
DEBUG_PRINT(" (Z): ");
DEBUG_PRINTLN(map(AcZ, -15000, 15000, -100, 100));
// left rigt : Z forward Back : Y
calculateThrottlePosition();
if (currentMode == M_SELECT) {
@ -865,7 +897,7 @@ void loop() {
if (triggerActive()) {
if(currentMode == M_STEERING) {
DEBUG_PRINT("Value: ");
int map = c_map(AcX, 15000, -15000, -60, 60);
int map = c_map(AcZ, -15000, 15000, -60, 60);
DEBUG_PRINTLN(map);
#ifdef steeringInfluential
@ -878,6 +910,12 @@ void loop() {
esc1 = throttle;
esc2 = throttle;
}
// ignore reverse
if(throttle < 127) {
esc1 = throttle;
esc2 = throttle;
}
#endif
if(throttle == 127) {
esc1 = 127;

6
src/rpm.h

@ -15,8 +15,8 @@
#include "driver/pcnt.h"
TaskHandle_t rpmTaskHandle;
int rpmRight = 0;
int rpmLeft = 0;
float rpmRight = 0;
float rpmLeft = 0;
int lastState[2] = {1, 1};
int16_t counter[2] = {0, 0};
@ -123,7 +123,7 @@ void measureRpm(void * parameter) {
rpmRight = (counter[0]/pulsesPerRotation)*4;
rpmLeft = (counter[1]/pulsesPerRotation)*4;
kmh = (wheelCircumference/100) * (rpmRight+rpmLeft)/2 * 3600/1000;
Serial.print(kmh); Serial.print(" "); Serial.println(direction[0]);
//Serial.print(kmh); Serial.print(" "); Serial.println(direction[0]);
sendSpeed = abs(floor(kmh));
sendSpeedDecimals = (kmh - sendSpeed) * 100;
counter[true] = 0;

31
src/settings.h

@ -1,18 +1,19 @@
uint8_t currentSetting = 0;
#define SETTINGS_COUNT 7
#define SETTINGS_COUNT 8
#define pageDisplay 0
#define limitMode 1
#define fanOveride 2
#define wheelDiameter 3 // Speed factor
#define minHallValue 4
#define centerHallValue 5
#define maxHallValue 6
#define lightMode 4
#define minHallValue 5
#define centerHallValue 6
#define maxHallValue 7
int settings[SETTINGS_COUNT];
uint8_t boardOptions = 0b00000000;
// Map:(Light, Fan) XXXXFFLL
// Map:(LightActive, Fan, LightMode) XXXAFFLL
/*
float gearRatio;
float ratioRpmSpeed;
@ -31,6 +32,7 @@ const char *settingPages[SETTINGS_COUNT] = {
"LimitMode",
"FanOveride",
"Wheel diameter",
"Light Mode",
"Throttle Min",
"Throttle Center",
"Throttle Max"
@ -42,9 +44,10 @@ int settingRules[SETTINGS_COUNT][3] {
{0, 0, 1}, //limit default is no limit
{0, 0, 2}, //fan 0 no, 1 on, 2 off
{83, 0, 250}, // wheel mm
{HAL_MIN, 1000, 2000},
{HAL_CENTER, 1000, 2000},
{HAL_MAX, 1000, 2000}
{0, 0, 3}, // LightMode
{HAL_MIN, 1000, 3000},
{HAL_CENTER, 1000, 3000},
{HAL_MAX, 1000, 3000}
};
/*
const char *settingAliases[SETTINGS_COUNT] = {
@ -66,8 +69,10 @@ bool inRange(int val, int minimum, int maximum) {
void updateBoardOptions() {
boardOptions = 0;
boardOptions |= (uint8_t) settings[fanOveride] << 2;
boardOptions |= boardOptions | (uint8_t) lightActive;
Serial.print("board: "); Serial.println(boardOptions);
boardOptions |= boardOptions | (uint8_t) lightActive << 3;
boardOptions |= boardOptions | (uint8_t) settings[lightMode];
DEBUG_PRINT("board: "); DEBUG_PRINTLN(boardOptions);
}
void updateSettings() {
@ -188,7 +193,7 @@ void controlSettingsMenu() {
settingsChangeFlag = false;
}
if (hallMeasurement >= (HAL_MAX - 250) && settingsLoopFlag == false) { //settings[maxHallValue]
if (hallMeasurement >= (settings[maxHallValue] - 250) && settingsLoopFlag == false) {
// Up
if (changeSelectedSetting == true) {
int val = settings[currentSetting] - 1;
@ -204,7 +209,7 @@ void controlSettingsMenu() {
settingsLoopFlag = true;
}
}
} else if (hallMeasurement <= (HAL_MIN + 250) && settingsLoopFlag == false) { //settings[minHallValue]
} else if (hallMeasurement <= (settings[minHallValue] + 250) && settingsLoopFlag == false) {
// Down
if (changeSelectedSetting == true) {
int val = settings[currentSetting] + 1;
@ -219,7 +224,7 @@ void controlSettingsMenu() {
settingsLoopFlag = true;
}
}
} else if (inRange(hallMeasurement, HAL_CENTER - 50, HAL_CENTER + 50)) { // settings[centerHallValue]
} else if (inRange(hallMeasurement, settings[centerHallValue] - 50, settings[centerHallValue] + 50)) {
settingsLoopFlag = false;
}
}
Loading…
Cancel
Save