working display

Signed-off-by: Lukas Bachschwell <lukas@lbsfilm.at>
This commit is contained in:
Lukas Bachschwell 2024-05-11 17:16:12 +02:00
parent 76b2639d82
commit 5646c3d33b
Signed by: lbsadmin
GPG Key ID: CCC6AA87CC8DF425
3 changed files with 798 additions and 437 deletions

View File

@ -10,18 +10,21 @@ int16_t GyY = 0;
int16_t GyZ = 0; int16_t GyZ = 0;
int16_t cels = 0; int16_t cels = 0;
void initAccel() { void initAccel()
{
return;
Wire.begin(); Wire.begin();
Wire.beginTransmission(0x68); // I2C address of the MPU-6050 Wire.beginTransmission(0x68); // I2C address of the MPU-6050
Wire.write(0x6B); // PWR_MGMT_1 register Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050) Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true); Wire.endTransmission(true);
} }
void readAccel()
void readAccel() { {
return;
Wire.beginTransmission(MPU_addr); Wire.beginTransmission(MPU_addr);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H) Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false); Wire.endTransmission(false);
Wire.requestFrom(MPU_addr, 14); // request a total of 14 registers LB : removing stop, no support in tiny wire Wire.requestFrom(MPU_addr, 14); // request a total of 14 registers LB : removing stop, no support in tiny wire
@ -32,5 +35,5 @@ void readAccel() {
GyX = Wire.read() << 8 | Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L) GyX = Wire.read() << 8 | Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
GyY = Wire.read() << 8 | Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L) GyY = Wire.read() << 8 | Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
GyZ = Wire.read() << 8 | Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L) GyZ = Wire.read() << 8 | Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
cels = Tmp / 340.00 + 36.53; //equation for temperature in degrees C from datasheet cels = Tmp / 340.00 + 36.53; // equation for temperature in degrees C from datasheet
} }

View File

@ -1,11 +1,10 @@
// ESPNOWSkate Receiver by Lukas Bachschwell this device SLAVE =D // ESPNOWSkate Receiver by Lukas Bachschwell this device SLAVE =D
#include <Servo.h>
#include "Arduino.h" #include "Arduino.h"
#include "SSD1306.h"
#include <esp_now.h> #include <esp_now.h>
#include <WiFi.h> #include <WiFi.h>
#include <OneWire.h> // #include <OneWire.h>
#include <DallasTemperature.h> // #include <DallasTemperature.h>
#include "valuehelpers.h"
#include "mac_config.h" #include "mac_config.h"
@ -13,9 +12,8 @@
#define esc2pin 13 #define esc2pin 13
#define voltageEnable 12 #define voltageEnable 12
#define voltagePin 36 #define voltagePin 36
#define failsafeValue 127
#define ONE_WIRE_BUS 14 #define ONE_WIRE_BUS 14
#define fanRelais 16 #define fanRelais 18 // Changed from originally 16 to help hover usart
#define DELETEBEFOREPAIR 0 #define DELETEBEFOREPAIR 0
@ -24,13 +22,26 @@ const float deviderR1 = 1275; // needs to be calibrated carefully using a multim
const float deviderR2 = 22000; const float deviderR2 = 22000;
const float refVoltage = 3.3; const float refVoltage = 3.3;
#define VARIANT_HOVER 1
// #define ENABLE_DISPLAY 1
#ifdef VARIANT_HOVER
#define failsafeValue 0
#include "hoverusart.h"
#else
#define failsafeValue 127
#include <Servo.h>
Servo esc1; Servo esc1;
Servo esc2; Servo esc2;
SSD1306 display(0x3c, 5, 4); #endif
#ifdef ENABLE_DISPLAY
#include "SSD1306.h"
SSD1306 display(0x3c, 5, 4);
#endif
OneWire oneWire(ONE_WIRE_BUS); OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire); // one instance for all sensrs // DallasTemperature sensors(&oneWire); // one instance for all sensrs
esp_now_peer_info_t remote; esp_now_peer_info_t remote;
@ -47,7 +58,7 @@ uint8_t sendSpeedDecimals = 0;
#include "rpm.h" #include "rpm.h"
//#define pairingMode // #define pairingMode
#define CONNECTION_TIMEOUT 300 #define CONNECTION_TIMEOUT 300
#define CHANNEL 1 #define CHANNEL 1
long lastPacket = 0; long lastPacket = 0;
@ -62,11 +73,13 @@ bool lightActive = false;
int fanMode = FANS_AUTO; int fanMode = FANS_AUTO;
#include "lights.h" #include "lights.h"
void setBoardOptions(uint8_t options) { void setBoardOptions(uint8_t options)
if(options != oldOptions) { {
if (options != oldOptions)
{
oldOptions = options; oldOptions = options;
shouldUpdateLights = true; shouldUpdateLights = true;
Serial.println("true2"); // Serial.println("true2");
} }
lightMode = options & 3; lightMode = options & 3;
@ -76,93 +89,137 @@ void setBoardOptions(uint8_t options) {
// ESPNOW Functions ############################ // ESPNOW Functions ############################
// config AP // config AP
void configDeviceAP(bool hidden) { void configDeviceAP(bool hidden)
{
bool result = WiFi.softAP("ESK8", "ESK8_Password+vD8z2YAvoDBW?Zx", CHANNEL, hidden); bool result = WiFi.softAP("ESK8", "ESK8_Password+vD8z2YAvoDBW?Zx", CHANNEL, hidden);
if (!result) { if (!result)
{
Serial.println("AP Config failed."); Serial.println("AP Config failed.");
} else { }
else
{
Serial.println("AP Config Success. Broadcasting with AP: " + String("ESK8")); Serial.println("AP Config Success. Broadcasting with AP: " + String("ESK8"));
} }
} }
void writeServos(uint8_t firstServo, uint8_t secondServo) { void writeServos(uint16_t firstServo, uint16_t secondServo)
{
#ifdef VARIANT_HOVER
Send(firstServo, secondServo);
#else
esc1.write(firstServo); esc1.write(firstServo);
esc2.write(secondServo); esc2.write(secondServo);
#endif
} }
// callback when data is recv from remote // callback when data is recv from remote
void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len) { void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len)
{
char macStr[18]; char macStr[18];
snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x", snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x", mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); Serial.print("Last Packet Recv from: ");
Serial.print("Last Packet Recv from: "); Serial.println(macStr); Serial.println(macStr);
uint8_t recData[3]; uint8_t recData[5];
memcpy(recData, data, data_len); memcpy(recData, data, data_len);
Serial.print("Last Packet Recv Data: "); Serial.println(recData[0]); Serial.print(" "); Serial.print(recData[1]); Serial.print(" len:"); Serial.println(data_len); Serial.print("Last Packet Recv Data: ");
Serial.println(make16(recData[0], recData[1]));
Serial.print(" ");
Serial.print(make16(recData[2], recData[3]));
Serial.print(" len:");
Serial.println(data_len);
// Answer with response // Answer with response
const uint8_t respData[] = { sendVoltage, sendVoltageDecimals, sendTemperature, sendTemperatureDecimals, sendSpeed, sendSpeedDecimals }; const uint8_t respData[] = {sendVoltage, sendVoltageDecimals, sendTemperature, sendTemperatureDecimals, sendSpeed, sendSpeedDecimals};
Serial.print("Sending RESPONSE.... "); Serial.print("Sending RESPONSE.... ");
esp_err_t result = esp_now_send(mac_addr, respData, sizeof(respData)); esp_err_t result = esp_now_send(mac_addr, respData, sizeof(respData));
if (result == ESP_OK) { if (result == ESP_OK)
{
Serial.println("Success"); Serial.println("Success");
} else if (result == ESP_ERR_ESPNOW_NOT_INIT) { }
else if (result == ESP_ERR_ESPNOW_NOT_INIT)
{
// How did we get so far!! // How did we get so far!!
Serial.println("ESPNOW not Init."); Serial.println("ESPNOW not Init.");
} else if (result == ESP_ERR_ESPNOW_ARG) { }
else if (result == ESP_ERR_ESPNOW_ARG)
{
Serial.println("Invalid Argument"); Serial.println("Invalid Argument");
} else if (result == ESP_ERR_ESPNOW_INTERNAL) { }
else if (result == ESP_ERR_ESPNOW_INTERNAL)
{
Serial.println("Internal Error"); Serial.println("Internal Error");
} else if (result == ESP_ERR_ESPNOW_NO_MEM) { }
else if (result == ESP_ERR_ESPNOW_NO_MEM)
{
Serial.println("ESP_ERR_ESPNOW_NO_MEM"); Serial.println("ESP_ERR_ESPNOW_NO_MEM");
} else if (result == ESP_ERR_ESPNOW_NOT_FOUND) { }
else if (result == ESP_ERR_ESPNOW_NOT_FOUND)
{
Serial.println("Peer not found."); Serial.println("Peer not found.");
} else if (result == ESP_ERR_ESPNOW_IF) { }
else if (result == ESP_ERR_ESPNOW_IF)
{
Serial.println("ESP_ERR_ESPNOW_IF"); Serial.println("ESP_ERR_ESPNOW_IF");
} else { }
else
{
Serial.println("Not sure what happened"); Serial.println("Not sure what happened");
} }
lastPacket = millis(); lastPacket = millis();
isConnected = true; isConnected = true;
// Could check mac here for some security // TODO: Could check mac here for some minimal security
writeServos(recData[0], recData[1]); writeServos(make16(recData[0], recData[1]), make16(recData[2], recData[3]));
setBoardOptions(recData[2]);
Serial.print("recieved: "); Serial.print("recieved Options: ");
Serial.println(recData[2], BIN); setBoardOptions(recData[4]);
Serial.println(recData[4], BIN);
#ifdef ENABLE_DISPLAY
display.clear(); display.clear();
char buf[25]; char buf[25];
sprintf(buf, "1: %i | 2: %i", recData[0], recData[1]); sprintf(buf, "1: %i | 2: %i", make16(recData[0], recData[1]), make16(recData[2], recData[3]));
display.drawString(2, 0, buf); display.drawString(2, 0, buf);
display.display(); display.display();
#endif
} }
void deletePeer() { void deletePeer()
{
const esp_now_peer_info_t *peer = &remote; const esp_now_peer_info_t *peer = &remote;
const uint8_t *peer_addr = remote.peer_addr; const uint8_t *peer_addr = remote.peer_addr;
esp_err_t delStatus = esp_now_del_peer(peer_addr); esp_err_t delStatus = esp_now_del_peer(peer_addr);
Serial.print("Slave Delete Status: "); Serial.print("Slave Delete Status: ");
if (delStatus == ESP_OK) { if (delStatus == ESP_OK)
{
// Delete success // Delete success
Serial.println("Success"); Serial.println("Success");
} else if (delStatus == ESP_ERR_ESPNOW_NOT_INIT) { }
else if (delStatus == ESP_ERR_ESPNOW_NOT_INIT)
{
// How did we get so far!! // How did we get so far!!
Serial.println("ESPNOW Not Init"); Serial.println("ESPNOW Not Init");
} else if (delStatus == ESP_ERR_ESPNOW_ARG) { }
else if (delStatus == ESP_ERR_ESPNOW_ARG)
{
Serial.println("Invalid Argument"); Serial.println("Invalid Argument");
} else if (delStatus == ESP_ERR_ESPNOW_NOT_FOUND) { }
else if (delStatus == ESP_ERR_ESPNOW_NOT_FOUND)
{
Serial.println("Peer not found."); Serial.println("Peer not found.");
} else { }
else
{
Serial.println("Not sure what happened"); Serial.println("Not sure what happened");
} }
} }
bool manageRemote() { bool manageRemote()
if (remote.channel == CHANNEL) { {
if (DELETEBEFOREPAIR) { if (remote.channel == CHANNEL)
{
if (DELETEBEFOREPAIR)
{
deletePeer(); deletePeer();
} }
@ -171,39 +228,57 @@ bool manageRemote() {
const uint8_t *peer_addr = remote.peer_addr; const uint8_t *peer_addr = remote.peer_addr;
// check if the peer exists // check if the peer exists
bool exists = esp_now_is_peer_exist(peer_addr); bool exists = esp_now_is_peer_exist(peer_addr);
if ( exists) { if (exists)
{
// Slave already paired. // Slave already paired.
Serial.println("Already Paired"); Serial.println("Already Paired");
return true; return true;
} else { }
else
{
// Slave not paired, attempt pair // Slave not paired, attempt pair
esp_err_t addStatus = esp_now_add_peer(peer); esp_err_t addStatus = esp_now_add_peer(peer);
if (addStatus == ESP_OK) { if (addStatus == ESP_OK)
{
// Pair success // Pair success
Serial.println("Pair success"); Serial.println("Pair success");
return true; return true;
} else if (addStatus == ESP_ERR_ESPNOW_NOT_INIT) { }
else if (addStatus == ESP_ERR_ESPNOW_NOT_INIT)
{
// How did we get so far!! // How did we get so far!!
Serial.println("ESPNOW Not Init"); Serial.println("ESPNOW Not Init");
return false; return false;
} else if (addStatus == ESP_ERR_ESPNOW_ARG) { }
else if (addStatus == ESP_ERR_ESPNOW_ARG)
{
Serial.println("Invalid Argument"); Serial.println("Invalid Argument");
return false; return false;
} else if (addStatus == ESP_ERR_ESPNOW_FULL) { }
else if (addStatus == ESP_ERR_ESPNOW_FULL)
{
Serial.println("Peer list full"); Serial.println("Peer list full");
return false; return false;
} else if (addStatus == ESP_ERR_ESPNOW_NO_MEM) { }
else if (addStatus == ESP_ERR_ESPNOW_NO_MEM)
{
Serial.println("Out of memory"); Serial.println("Out of memory");
return false; return false;
} else if (addStatus == ESP_ERR_ESPNOW_EXIST) { }
else if (addStatus == ESP_ERR_ESPNOW_EXIST)
{
Serial.println("Peer Exists"); Serial.println("Peer Exists");
return true; return true;
} else { }
else
{
Serial.println("Not sure what happened"); Serial.println("Not sure what happened");
return false; return false;
} }
} }
} else { }
else
{
// No slave found to process // No slave found to process
Serial.println("No Slave found to process"); Serial.println("No Slave found to process");
return false; return false;
@ -212,16 +287,19 @@ bool manageRemote() {
// end ESPNOW functions // end ESPNOW functions
void checkTemperature()
void checkTemperature() { {
sensors.requestTemperatures(); // Send the command to get temperatures // sensors.requestTemperatures(); // Send the command to get temperatures
temperature = sensors.getTempCByIndex(0); // temperature = sensors.getTempCByIndex(0);
//Serial.print("Temp: "); // Serial.print("Temp: ");
//Serial.println(temperature); // Serial.println(temperature);
switch(fanMode) { switch (fanMode)
{
case FANS_AUTO: case FANS_AUTO:
if(temperature < 35) digitalWrite(fanRelais, LOW); if (temperature < 35)
if(temperature > 40) digitalWrite(fanRelais, HIGH); digitalWrite(fanRelais, LOW);
if (temperature > 40)
digitalWrite(fanRelais, HIGH);
break; break;
case FANS_ON: case FANS_ON:
digitalWrite(fanRelais, HIGH); digitalWrite(fanRelais, HIGH);
@ -235,81 +313,91 @@ void checkTemperature() {
sendTemperatureDecimals = (temperature - sendTemperature) * 100; sendTemperatureDecimals = (temperature - sendTemperature) * 100;
} }
void checkVoltage() { void checkVoltage()
{
digitalWrite(voltageEnable, HIGH); digitalWrite(voltageEnable, HIGH);
//Serial.print("Voltage: "); // Serial.print("Voltage: ");
int value = analogRead(voltagePin); int value = analogRead(voltagePin);
//Serial.println(value); // Serial.println(value);
float batteryVoltage = 0.0; float batteryVoltage = 0.0;
int total = 0; int total = 0;
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++)
{
total += analogRead(voltagePin); total += analogRead(voltagePin);
} }
batteryVoltage = (refVoltage / 4095.0) * ((float)total / 10.0); batteryVoltage = (refVoltage / 4095.0) * ((float)total / 10.0);
// Now we have the actual Voltage, lets calculate the value befor the devider // Now we have the actual Voltage, lets calculate the value befor the devider
batteryVoltage = batteryVoltage / ( deviderR1 / (deviderR1 + deviderR2)); batteryVoltage = batteryVoltage / (deviderR1 / (deviderR1 + deviderR2));
sendVoltage = abs(floor(batteryVoltage)); sendVoltage = abs(floor(batteryVoltage));
sendVoltageDecimals = (batteryVoltage - sendVoltage) * 100; sendVoltageDecimals = (batteryVoltage - sendVoltage) * 100;
//Serial.print("Voltage: "); // Serial.print("Voltage: ");
//Serial.println(batteryVoltage); // Serial.println(batteryVoltage);
digitalWrite(voltageEnable, LOW); // change to low digitalWrite(voltageEnable, LOW); // change to low
} }
void setup() { void setup()
{
Serial.begin(115200); Serial.begin(115200);
Serial.println("ESPNowSkate Receiver"); Serial.println("ESPNowSkate Receiver");
#ifdef ENABLE_DISPLAY
display.init(); display.init();
display.flipScreenVertically(); display.flipScreenVertically();
display.setFont(ArialMT_Plain_16); display.setFont(ArialMT_Plain_16);
#endif
pinMode(voltageEnable, OUTPUT); pinMode(voltageEnable, OUTPUT);
pinMode(fanRelais, OUTPUT); pinMode(fanRelais, OUTPUT);
initRPMPins(); #ifdef VARIANT_HOVER
initHoverSerial();
#else
// Init escs, min and max value similar as Traxxas TQI 1100, 1900 // Init escs, min and max value similar as Traxxas TQI 1100, 1900
// chanel, minAngel, maxAngel, minPulseWidth, maxPulseWidth // chanel, minAngel, maxAngel, minPulseWidth, maxPulseWidth
esc1.attach(esc1pin, 0, 0, 255, 1100, 1900); esc1.attach(esc1pin, 0, 0, 255, 1100, 1900);
esc2.attach(esc2pin, 1, 0, 255, 1100, 1900); esc2.attach(esc2pin, 1, 0, 255, 1100, 1900);
sensors.begin(); sensors.begin();
initRPMPins();
#endif
setupLights(); // setupLights();
lightOff(); // lightOff();
xTaskCreatePinnedToCore( // xTaskCreatePinnedToCore(
measureRpm, // measureRpm,
"rpm task", // "rpm task",
1000, // 1000,
NULL, // NULL,
1, // 1,
&rpmTaskHandle, // &rpmTaskHandle,
0); // 0);
// Set device in AP mode to begin with
Serial.println("INIT Wifi");
//Set device in AP mode to begin with
WiFi.mode(WIFI_AP); WiFi.mode(WIFI_AP);
// configure device AP mode Serial.println("INIT Pair");
#ifdef pairingMode // configure device AP mode
#ifdef pairingMode
configDeviceAP(false); configDeviceAP(false);
#else #else
configDeviceAP(true); configDeviceAP(true);
#endif #endif
Serial.print("AP MAC: "); Serial.println(WiFi.softAPmacAddress()); Serial.print("AP MAC: ");
Serial.println(WiFi.softAPmacAddress());
// Init ESPNow // Init ESPNow
if (esp_now_init() == ESP_OK) { if (esp_now_init() == ESP_OK)
{
Serial.println("ESPNow Init Success"); Serial.println("ESPNow Init Success");
} }
else { else
{
Serial.println("ESPNow Init Failed"); Serial.println("ESPNow Init Failed");
ESP.restart(); ESP.restart();
} }
@ -318,31 +406,40 @@ void setup() {
// get recv packer info. // get recv packer info.
esp_now_register_recv_cb(OnDataRecv); esp_now_register_recv_cb(OnDataRecv);
for (int i = 0; i < 6; ++i ) { for (int i = 0; i < 6; ++i)
remote.peer_addr[i] = (uint8_t) mac_remote[i]; {
remote.peer_addr[i] = (uint8_t)mac_remote[i];
} }
remote.channel = CHANNEL; // pick a channel remote.channel = CHANNEL; // pick a channel
remote.encrypt = 0; // no encryption remote.encrypt = 0; // no encryption
remote.ifidx = ESP_IF_WIFI_AP; remote.ifidx = WIFI_IF_AP;
manageRemote(); manageRemote();
} }
void loop()
void loop() { {
if(millis() - lastPacket > CONNECTION_TIMEOUT ) { if (millis() - lastPacket > CONNECTION_TIMEOUT)
{
Serial.println("Con timeout!!");
isConnected = false; isConnected = false;
//int failsafeValue = map(analogRead(fallbackpin), 0, 4095, 0, 180); // int failsafeValue = map(analogRead(fallbackpin), 0, 4095, 0, 180);
// Taking 127 because it should be the center value anyway // Taking 127 because it should be the center value anyway
writeServos(failsafeValue, failsafeValue); writeServos(failsafeValue, failsafeValue);
#ifdef ENABLE_DISPLAY
display.clear(); display.clear();
char buf[25]; char buf[25];
sprintf(buf, "FAIL: %i", failsafeValue); sprintf(buf, "FAIL: %i", failsafeValue);
display.drawString(2, 0, buf); display.drawString(2, 0, buf);
display.display(); display.display();
#endif
} }
checkTemperature(); // checkTemperature();
checkVoltage(); // checkVoltage();
if(shouldUpdateLights) updateLights(); #ifdef VARIANT_HOVER
Receive();
#endif
// if (shouldUpdateLights)
// updateLights();
} }

File diff suppressed because it is too large Load Diff