Commit 680570b1 authored by Wouter Klop's avatar Wouter Klop

Progress on parameter setting library

parent dda130ae
#include <par.h>
#include <EEPROM.h>
#include <Streaming.h>
/*
How to pass objects to be modified?
......@@ -37,44 +38,50 @@ Add a save command, to save all parameters (both start and end address are known
int parameter::addressCounter = 300;
uint8_t parameter::cmdCounter = 0;
int par::addressCounter = 300;
uint8_t par::cmdCounter = 0;
uint8_t parList::groupCounter = 0;
// parameter::parameter(uint8_t* _p, uint8_t _tag, uint8_t _cmd, int _address) {
// p = _p;
// tag = _tag;
// cmd = _cmd;
// address = _address;
// }
//
// parameter::parameter(uint8_t* _p, uint8_t _tag) {
// p = _p;
// tag = _tag;
//
// cmd = cmdCounter++;
// address = addressCounter;
// addressCounter += tagSize[tag];
// }
parList::parList(par* _l) {
l = _l;
groupNo = groupCounter++;
numPar = par::cmdCounter;
par::cmdCounter = 0;
}
void parList::sendList(WebSocketsServer wsServer) {
uint8_t buf[6];
cmd c;
c.cmd1 = groupNo;
parameter::parameter(uint8_t* _p) {
for (uint8_t i=0; i<numPar; i++) {
Serial << groupNo << "\t" << l[i].cmd << "\t" << l[i].getFloat() << endl;
c.cmd2 = l[i].cmd;
c.val = l[i].getFloat();
wsServer.sendBIN(0,c.arr,6);
}
}
par::par(uint8_t* _p) {
p_u8 = _p;
tag = t_u8;
assignAddress();
}
parameter::parameter(float* _p) {
par::par(float* _p) {
p_f = _p;
tag = t_f;
assignAddress();
}
void parameter::assignAddress(void) {
void par::assignAddress(void) {
cmd = cmdCounter++;
address = addressCounter;
addressCounter += tagSize[tag];
}
void parameter::read(void) {
void par::read(void) {
switch(tag) {
case t_u8:
*p_u8 = EEPROM.read(address);
......@@ -86,7 +93,7 @@ void parameter::read(void) {
Serial.println(EEPROM.read(address));
}
void parameter::write(void) {
void par::write(void) {
switch(tag) {
case t_u8:
EEPROM.write(address, *p_u8);
......@@ -112,22 +119,52 @@ void parameter::write(void) {
EEPROM.commit();
}
float par::getFloat(void) {
switch (tag) {
case t_u8:
return ((float) *p_u8);
break;
case t_f:
return ((float) *p_f);
break;
}
}
// Use binary values or text for messages?
// Send everything as float?
// Add 2nd class for making groups of commands.
// Properties: groupNo, number of items, array of parameters
// EEPROM bit/byte to indicate if settings have been stored
// (pointer to) function for updating
// Functions: read / write group from/to EEPROM,
// Specify group as array of adresses, then loop through them.
uint8_t parameter::makeMessageBin(uint8_t* p) {
// Command (2 bytes), value (1-4 bytes)
// How to handle different variable types? Send everything as float, or send variable type as well
uint8_t length = 0;
// Specify group as array of adresses, then loop through them.
p[0] = command;
}
void parameter::makeMessageText(char* c) {
// How to implement a save button? I.e. how to define an action that has no variable, but executes a function?
// Maybe set cmd 0 to save all variables, and cmd 1 to return all variables. Or, 254 and 255.
}
// When receiving a message, all "groups"/lists should receive it.
// Or, do the "group" handling in main. Also allows for implementing custom requests.
/* So, the proposed workflow is:
- Create multiple arrays of grouped parameter objects
- Pass them to corresponding list objects. Or, use non-class function.
- Have a command for sending all variable values
- For incoming message, first byte is command 1. Switch statement points to correct group of variables.
-
*/
// Maybe have a list of parameter groups? Allows to nicely index when command is received.
// uint8_t par::makeMessageBin(uint8_t* p) {
// // Command (2 bytes), value (1-4 bytes)
// // How to handle different variable types? Send everything as float, or send variable type as well
// uint8_t length = 0;
//
// p[0] = command;
// }
//
// void par::makeMessageText(char* c) {
//
// }
......@@ -3,6 +3,7 @@
#include<Arduino.h>
#include<PID.h>
#include<WebSocketsServer.h>
#define EEPROM_ADR_PID_WRITTEN 200
#define EEPROM_ADR_PID 201
......@@ -20,8 +21,25 @@
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};
typedef union {
struct {
uint8_t cmd1;
uint8_t cmd2;
union {
float val;
uint8_t valU8[4];
};
};
uint8_t arr[6];
} cmd;
// typedef union {
// uint8_t u8[4];
// float f;
// } floatToByte;
class parameter {
class par {
public:
// uint8_t* p;
union {
......@@ -43,36 +61,30 @@ public:
static uint8_t cmdCounter;
// parameter(uint8_t* _p, uint8_t _tag, uint8_t _cmd, int _address);
// parameter(uint8_t* _p, uint8_t _tag);
parameter(uint8_t* _p);
parameter(float* _p);
// par(uint8_t* _p, uint8_t _tag, uint8_t _cmd, int _address);
// par(uint8_t* _p, uint8_t _tag);
par(uint8_t* _p);
par(float* _p);
void read(void);
void write(void);
float getFloat(void);
private:
void assignAddress(void);
};
class parList {
public:
parList(par* _l);
void sendList(WebSocketsServer wsServer);
void parseMessage(uint8_t* c);
static uint8_t groupCounter;
uint8_t groupNo;
uint8_t numPar;
private:
par* l;
// typedef struct {
// // union {
// // uint8_t * u8;
// // uint16_t * u16;
// // uint32_t * u32;
// // int8_t * i8;
// // int16_t * i16;
// // int32_t * i32;
// // float * f;
// // double * d;
// // };
// uint8_t *p;
// uint8_t tag;
// uint8_t cmdNo;
// uint16_t address;
// // void getFun() {};
// } parameter;
};
#endif
......@@ -15,5 +15,5 @@ framework = arduino
monitor_port = COM3
monitor_speed = 115200
; upload_port = COM3
; upload_port = balancingrobot.local
upload_port = 192.168.178.33
upload_port = balancingrobot.local
; upload_port = 192.168.178.33
......@@ -134,6 +134,10 @@ float speedFilterConstant = 0.9;
// -- WiFi
const char host[] = "balancingrobot";
// ----- Parameter definitions -----
par pidPar[] = {{&pid[0].K}, {&pid[0].Ti}, {&pid[0].Td}, {&pid[0].N}};
parList pidParList(pidPar);
// ----- Interrupt functions -----
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
......@@ -315,19 +319,7 @@ void setup() {
pidPos.setParameters(1,0,1.2,20);
pidSpeed.setParameters(6,5,0,20);
// float a = 1.2, b = 3.341;
// uint8_t c = 123, d = 245;
float a = 2.1, b = 1.341;
uint8_t c = 223, d = 145;
parameter p[] = {{&pid[0].K}, {&pid[0].Ti}, {&pid[0].Td}, {&pid[0].N}};
Serial << a << "\t" << b << "\t" << c << "\t" << d << endl;
for (uint8_t i=0; i<4; i++) {
Serial << i << "\t"<< p[i].tag << "\t" << p[i].cmd << "\t" << p[i].address << endl;
// p[i].write();
p[i].read();
}
Serial << a << "\t" << b << "\t" << c << "\t" << d << endl;
// Run wirless related tasks on core 0
xTaskCreatePinnedToCore(
......@@ -340,6 +332,8 @@ void setup() {
0); /* Core where the task should run */
Serial.println("Ready");
pid[0].setParameters(1,2,3,4);
}
......@@ -633,8 +627,9 @@ void parseCommand(char* data, uint8_t length) {
switch (cmd2) {
case 'r':
Serial.println("Rebooting...");
ESP.restart();
// Serial.println("Rebooting...");
// ESP.restart();
pidParList.sendList(wsServer);
break;
case 'l': // Send wifi networks to WS client
sendWifiList();
......
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