Commit 9c76d5c4 authored by Wouter Klop's avatar Wouter Klop

Par library now supports function handles

parent 3bd3664b
......@@ -38,7 +38,7 @@ Add a save command, to save all parameters (both start and end address are known
int par::addressCounter = 300;
int par::addressCounter = 300; // Starting address
uint8_t par::cmdCounter = 0;
uint8_t parList::groupCounter = 0;
......@@ -83,9 +83,14 @@ void parList::read(void) {
}
void parList::set(uint8_t cmd, float f) {
l[cmd].setFloat(f);
if (l[cmd].tag == t_fun) {
l[cmd].p_fun();
} else {
l[cmd].setFloat(f);
}
}
// Parameter constructors for different variable types
par::par(uint8_t* _p) {
p_u8 = _p;
tag = t_u8;
......@@ -98,6 +103,12 @@ par::par(float* _p) {
assignAddress();
}
par::par(funPointer _p) {
p_fun = _p;
tag = t_fun;
assignAddress();
}
void par::assignAddress(void) {
cmd = cmdCounter++;
address = addressCounter;
......
......@@ -16,10 +16,10 @@
// void readPIDParameters(PID* pid);
// void writePIDParameters(PID* pid);
typedef void (*funPointer) ();
enum tag {t_u8,t_u16,t_u32,t_i8,t_i16,t_i32,t_f,t_d};
const uint8_t tagSize[] = {1,2,4,1,2,4,4,8};
enum tag {t_u8,t_u16,t_u32,t_i8,t_i16,t_i32,t_f,t_d,t_fun};
const uint8_t tagSize[] = {1,2,4,1,2,4,4,8,0};
typedef union {
uint8_t arr[6];
......@@ -51,6 +51,7 @@ public:
int32_t * p_i32;
float * p_f;
double * p_d;
funPointer p_fun;
};
int address;
......@@ -63,8 +64,9 @@ public:
// par(uint8_t* _p, uint8_t _tag, uint8_t _cmd, int _address);
// par(uint8_t* _p, uint8_t _tag);
par(uint8_t* _p);
par(uint8_t* _p); // Overload constructor for different variable types
par(float* _p);
par(funPointer _p);
void read(void);
void write(void);
......
......@@ -16,4 +16,4 @@ monitor_port = COM3
monitor_speed = 115200
; upload_port = COM3
; upload_port = balancingrobot.local
upload_port = 192.168.178.35
upload_port = 192.168.178.33
......@@ -133,13 +133,30 @@ float speedFilterConstant = 0.9;
const char host[] = "balancingrobot";
// ----- Parameter definitions -----
void updatePIDParameters() {
pidAngle.updateParameters();
pidSpeed.updateParameters();
pidPos.updateParameters();
}
par pidPar[] = {&pidAngle.K, &pidAngle.Ti, &pidAngle.Td, &pidAngle.N, &pidAngle.R, &pidAngle.minOutput, &pidAngle.maxOutput, &pidAngle.controllerType,
&pidPos.K, &pidPos.Ti, &pidPos.Td, &pidPos.N, &pidPos.R, &pidPos.minOutput, &pidPos.maxOutput, &pidPos.controllerType,
&pidSpeed.K, &pidSpeed.Ti, &pidSpeed.Td, &pidSpeed.N, &pidSpeed.R, &pidSpeed.minOutput, &pidSpeed.maxOutput, &pidSpeed.controllerType};
&pidSpeed.K, &pidSpeed.Ti, &pidSpeed.Td, &pidSpeed.N, &pidSpeed.R, &pidSpeed.minOutput, &pidSpeed.maxOutput, &pidSpeed.controllerType, &updatePIDParameters};
// Use default arguments for PID constructor
parList pidParList(pidPar);
// par motorPar[] = {&motorCurrent, &maxStepSpeed};
// par wifiPar[] = {&wifiMode, &wifiSSID, &wifiKey};
// par sensorPar[] = {&gyroOffset, &gyroGain, &angleOffset, &updateGyroOffset, &updateAngleOffset};
// par controlPar[] = {&remoteType, &controlMode};
// struct {
// struct {
// uint8_t mode;
// char ssid[30];
// char key[30];
// } wifi;
// } settings;
// ----- Interrupt functions -----
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
......@@ -154,6 +171,7 @@ void IRAM_ATTR motRightTimerFunction() {
portEXIT_CRITICAL_ISR(&timerMux);
}
void setMotorCurrent() {
dacWrite(motorCurrentPin, motorCurrent);
}
......@@ -258,30 +276,30 @@ void setup() {
Serial.println(WiFi.softAPIP());
}
ArduinoOTA.setHostname(host);
ArduinoOTA
.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH)
type = "sketch";
else // U_SPIFFS
type = "filesystem";
Serial.println("Start updating " + type);
})
.onEnd([]() {
Serial.println("\nEnd");
})
.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r\n", (progress / (total / 100)));
})
.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.setHostname(host);
ArduinoOTA
.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH)
type = "sketch";
else // U_SPIFFS
type = "filesystem";
Serial.println("Start updating " + type);
})
.onEnd([]() {
Serial.println("\nEnd");
})
.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r\n", (progress / (total / 100)));
})
.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
......@@ -327,7 +345,7 @@ void setup() {
pidParList.read();
// Run wirless related tasks on core 0
// Run wireless related tasks on core 0
xTaskCreatePinnedToCore(
wirelessTask, /* Function to implement the task */
"wirelessTask", /* Name of the task */
......@@ -638,9 +656,9 @@ void parseCommand(char* data, uint8_t length) {
switch (cmd2) {
case 'r':
// Serial.println("Rebooting...");
// ESP.restart();
pidParList.sendList(&wsServer);
Serial.println("Rebooting...");
ESP.restart();
// pidParList.sendList(&wsServer);
break;
case 'l': // Send wifi networks to WS client
sendWifiList();
......@@ -844,6 +862,7 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length
if (c.grp<parList::groupCounter) {
if (c.grp==0 && c.cmd<100) {
pidParList.set(c.cmd,c.val);
// pidPar[c.cmd].setFloat(c.val);
}
if (c.cmd==253) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment