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,7 +10,9 @@ int16_t GyY = 0;
int16_t GyZ = 0;
int16_t cels = 0;
void initAccel() {
void initAccel()
{
return;
Wire.begin();
Wire.beginTransmission(0x68); // I2C address of the MPU-6050
Wire.write(0x6B); // PWR_MGMT_1 register
@ -18,8 +20,9 @@ void initAccel() {
Wire.endTransmission(true);
}
void readAccel() {
void readAccel()
{
return;
Wire.beginTransmission(MPU_addr);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);

View File

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

File diff suppressed because it is too large Load Diff