Commit f3b10a78 authored by Wouter Klop's avatar Wouter Klop

Progress on par library

parent cc5ec1b8
<!DOCTYPE html>
<html>
<head>
<script>
var connection = new WebSocket('ws://'+location.hostname+':81/', ['arduino']);
connection.binaryType = 'arraybuffer';
connection.onopen = function () {
<!-- connection.send('Connect ' + new Date()); -->
};
connection.onerror = function (error) {
console.log('WebSocket Error ', error);
};
connection.onmessage = function (e) {
if (typeof(e.data)=='string') { // Text frame
console.log('Server: ', e.data);
// if (e.data[0]=='c') {
// cmd = e.data.substr(0,3);
// val = e.data.substr(3);
// } else if (e.data[0]=='w') {
// var str = e.data.substr(1);
// parseWifiCommand(str);
// } else {
// cmd = e.data.substr(0,1);
// val = e.data.substr(1);
// }
// document.getElementById(cmd).value = parseFloat(val);
} else { // Binary frame
var dv = new DataView(e.data);
var cmd1 = dv.getUint8(0);
var cmd2 = dv.getUint8(1);
var val = dv.getFloat32(2,true);
console.log(cmd1 + ' ' + cmd2 + ' ' + val);
}
};
function sendCommand(cmd1, cmd2, val) {
var buf = new ArrayBuffer(6);
var dv = new DataView(buf);
dv.setUint8(0,cmd1);
dv.setUint8(1,cmd2);
dv.setFloat32(2,val,true);
console.log(buf);
console.log(dv);
connection.send(buf);
}
</script>
</head>
<body>
<form>
<button type="button" onclick="sendCommand(0,253,1.245)">Send</button>
<button type="button" onclick="sendCommand(0,254,1.245)">Read</button>
<button type="button" onclick="sendCommand(0,255,0)">Write</button>
</form>
</body>
\ No newline at end of file
......@@ -47,6 +47,8 @@ parList::parList(par* _l) {
groupNo = groupCounter++;
numPar = par::cmdCounter;
par::cmdCounter = 0;
flagAddress = par::addressCounter++;
}
void parList::sendList(WebSocketsServer *wsServer) {
......@@ -63,6 +65,24 @@ void parList::sendList(WebSocketsServer *wsServer) {
}
}
void parList::write(void) {
for (uint8_t i=0; i<numPar; i++) {
l[i].write();
}
EEPROM.write(flagAddress, EEPROM_WRITTEN);
EEPROM.commit();
}
void parList::read(void) {
uint8_t flag = EEPROM.read(flagAddress);
Serial << flag << endl;
if (flag==EEPROM_WRITTEN) { // Only read if EEPROM has been written to
for (uint8_t i=0; i<numPar; i++) {
l[i].read();
}
}
}
par::par(uint8_t* _p) {
p_u8 = _p;
tag = t_u8;
......@@ -90,7 +110,7 @@ void par::read(void) {
*p_f = EEPROM.readFloat(address);
break;
}
Serial.println(EEPROM.read(address));
// Serial.println(EEPROM.read(address));
}
void par::write(void) {
......@@ -116,7 +136,6 @@ void par::write(void) {
break;
}
EEPROM.commit();
}
float par::getFloat(void) {
......
......@@ -22,6 +22,7 @@ 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 {
uint8_t arr[6];
struct {
uint8_t cmd1;
uint8_t cmd2;
......@@ -29,9 +30,8 @@ typedef union {
float val;
uint8_t valU8[4];
};
};
uint8_t arr[6];
} cmd;
} __attribute__((packed));
} cmd;
// typedef union {
// uint8_t u8[4];
......@@ -79,10 +79,14 @@ public:
parList(par* _l);
void sendList(WebSocketsServer *wsServer);
void parseMessage(uint8_t* c);
void read(void);
void write(void);
static uint8_t groupCounter;
uint8_t groupNo;
uint8_t numPar;
uint16_t flagAddress;
private:
par* l;
......
......@@ -101,8 +101,6 @@ float maxStepSpeed = 3000;
#define PID_POS 1
#define PID_SPEED 2
PID pid[3];
#define PID_ANGLE_MAX 20
PID pidAngle(cPD, dT, PID_ANGLE_MAX, -PID_ANGLE_MAX);
#define PID_POS_MAX 35
......@@ -135,7 +133,10 @@ float speedFilterConstant = 0.9;
const char host[] = "balancingrobot";
// ----- Parameter definitions -----
par pidPar[] = {{&pid[0].K}, {&pid[0].Ti}, {&pid[0].Td}, {&pid[0].N}, {&pid[0].R}, {&pid[0].minOutput}, {&pid[0].minOutput}};
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};
// Use default arguments for PID constructor
parList pidParList(pidPar);
......@@ -338,7 +339,6 @@ void setup() {
Serial.println("Ready");
pid[0].setParameters(1,2,3,4);
}
......@@ -840,13 +840,30 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length
// send data to all connected clients
// webSocket.broadcastTXT("message here");
break;
case WStype_BIN:
case WStype_BIN: {
Serial.printf("[%u] get binary length: %u\n", num, length);
hexdump(payload, length);
// send message to client
// webSocket.sendBIN(num, payload, length);
if (length==6) {
cmd c;
memcpy(c.arr, payload, 6);
Serial << c.cmd1 << "\t" << c.cmd2 << "\t" << c.val << "\t" << sizeof(cmd) << endl;
// if (c.cmd1<parList::groupNo) {
if (c.cmd2==253) {
pidParList.sendList(&wsServer);
}
if (c.cmd2==254) {
pidParList.read();
}
if (c.cmd2==255) {
pidParList.write();
}
// }
}
break;
}
case WStype_ERROR:
case WStype_FRAGMENT_TEXT_START:
case WStype_FRAGMENT_BIN_START:
......
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