Commit 27f8adf0 authored by Gertulla, Janis Luca's avatar Gertulla, Janis Luca
Browse files

hotfixes 1

parent ccb178be
QT += core gui network charts
greaterThan(QT_MAJOR_VERSION, 5): QT += widgets
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++17
......
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.13.1, 2020-10-07T08:42:59. -->
<!-- Written by QtCreator 4.13.1, 2020-10-09T13:24:40. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
......
#include "client.h"
#include "window.h"
#include <algorithm>
QString parse_byte(unsigned char b)
{
QString ret;
for(int i = 0; i < 8; i++)
{
ret.append((b & (1 << (7 - i))) ? "1" : "0");
}
return ret;
}
void write_byte(uint8_t **buf, uint8_t x)
{
**buf = x;
(*buf)++;
}
void read_byte(uint8_t **buf, uint8_t *x)
{
*x = **buf;
(*buf)++;
}
void write_int16(uint8_t **buf, int16_t x)
{
**buf = (uint8_t) (x >> 8);
(*buf)++;
**buf = (uint8_t) (x & 0xFF);
(*buf)++;
}
void read_int16(uint8_t **buf, int16_t *x)
{
*x = 0;
*x |= **buf << 8;
(*buf)++;
*x |= **buf;
(*buf)++;
}
void write_bools(uint8_t **buf, bool b1, bool b2, bool b3, bool b4, bool b5, bool b6, bool b7, bool b8)
{
**buf = (b1 << 7) | (b2 << 6) | (b3 << 5) | (b4 << 4) | (b5 << 3) | (b6 << 2) | (b7 << 1) | (b8 << 0);
(*buf)++;
}
void read_bools(uint8_t **buf, bool *b1, bool *b2, bool *b3, bool *b4, bool *b5, bool *b6, bool *b7, bool *b8)
{
if(b1) *b1 = (**buf & 1 << 7) ? true : false;
if(b2) *b2 = (**buf & 1 << 6) ? true : false;
if(b3) *b3 = (**buf & 1 << 5) ? true : false;
if(b4) *b4 = (**buf & 1 << 4) ? true : false;
if(b5) *b5 = (**buf & 1 << 3) ? true : false;
if(b6) *b6 = (**buf & 1 << 2) ? true : false;
if(b7) *b7 = (**buf & 1 << 1) ? true : false;
if(b8) *b8 = (**buf & 1 << 0) ? true : false;
(*buf)++;
}
Client::Client() : QTcpSocket()
{
config = std::make_unique<Config>();
......@@ -53,17 +115,23 @@ void Client::handleData()
{
data.append(readAll());
do {
int msgHeader = data[0];
int msgLen = data[1];
uint8_t *l = (uint8_t*) data.data();
uint8_t **p = &l;
uint8_t msgHeader;
uint8_t msgLen;
read_byte(p, &msgHeader);
read_byte(p, &msgLen);
if(data.length() < msgLen + 2)
{
emit log("partial data: " + QString::number(data.length()) + "uint8_ts");
return;
}
else
{
if(msgHeader == MsgPing) // pong
{
if(msgLen != 1) emit log("invalid ping length!");
if(data[2] == 'p')
{
pingInterval->start();
......@@ -76,50 +144,37 @@ void Client::handleData()
else if(msgHeader == MsgState) // state change
{
PlcState newStat;
newStat.override = data[2] & (1 << 7);
newStat.linked = data[2] & (1 << 6);
newStat.pGratActive = data[2] & (1 << 5);
newStat.pMcpActive = data[2] & (1 << 4);
newStat.pVvActive = data[2] & (1 << 3);
newStat.tDetActive = data[2] & (1 << 2);
newStat.tDpsActive = data[2] & (1 << 1);
if(msgLen != 2) emit log("invalid state length!");
PlcState newStat;
newStat.vTdetOpen = data[3] & (1 << 7);
newStat.vDetOpen = data[3] & (1 << 6);
newStat.vFoilOpen = data[3] & (1 << 5);
newStat.vDpsOpen = data[3] & (1 << 4);
read_bools(p, &newStat.override, &newStat.linked,
&newStat.pGratActive, &newStat.pMcpActive, &newStat.pVvActive,
&newStat.tDetActive, &newStat.tDpsActive,
nullptr);
newStat.hvmcpHigh = data[3] & (1 << 3);
newStat.rbeamlineHigh = data[3] & (1 << 2);
read_bools(p, &newStat.vTdetOpen, &newStat.vDetOpen, &newStat.vFoilOpen, &newStat.vDpsOpen,
&newStat.hvmcpHigh, &newStat.rbeamlineHigh,
&newStat.pumpEnabled,
nullptr);
newStat.pumpEnabled = data[3] & (1 << 1);
emit newState(newStat);
}
else if(msgHeader == MsgMeasurement)
{
if(msgLen != 11) emit log("invalid measurement length!");
PlcMeasurement newMeas;
newMeas.pGrat = (((int16_t) data[2]) << 8) | (((int16_t) data[3]) & 0xFF);
newMeas.pMcp = (((int16_t) data[4]) << 8) | (((int16_t) data[5]) & 0xFF);
newMeas.pVv = (((int16_t) data[6]) << 8) | (((int16_t) data[7]) & 0xFF);
newMeas.tDet = (((int16_t) data[8]) << 8) | (((int16_t) data[9]) & 0xFF);
newMeas.tDps = (((int16_t) data[10]) << 8) | (((int16_t) data[11]) & 0xFF);
read_int16(p, &newMeas.pGrat);
read_int16(p, &newMeas.pMcp);
read_int16(p, &newMeas.pVv);
newMeas.vTdetHigh = data[12] & (1 << 7);
newMeas.vDetHigh = data[12] & (1 << 6);
newMeas.vFoilHigh = data[12] & (1 << 5);
newMeas.vDpsHigh = data[12] & (1 << 4);
read_int16(p, &newMeas.tDet);
read_int16(p, &newMeas.tDps);
newMeas.vTdetLow = data[12] & (1 << 3);
newMeas.vDetLow = data[12] & (1 << 2);
newMeas.vFoilLow = data[12] & (1 << 1);
newMeas.vDpsLow = data[12] & (1 << 0);
read_bools(p, &newMeas.vTdetHigh, &newMeas.vDetHigh, &newMeas.vFoilHigh, &newMeas.vDpsHigh,
&newMeas.vTdetLow, &newMeas.vDetLow, &newMeas.vFoilLow, &newMeas.vDpsLow);
emit newMeasurements(newMeas);
}
......@@ -135,29 +190,45 @@ void Client::handleData()
void Client::onSendState(PlcState &state)
{
uint8_t data[4];
data[0] = 0x01; // PlcState
data[1] = 0x02; // Length = 2;
uint8_t *l = data;
uint8_t **p = &l;
// data[0] = 0x01; // PlcState
// data[1] = 0x02; // Length = 2;
// data[2] |= (state.override & 1) << 7;
// data[2] |= (state.linked & 1) << 6;
// data[2] |= (state.pGratActive & 1) << 5;
// data[2] |= (state.pMcpActive & 1) << 4;
// data[2] |= (state.pVvActive & 1) << 3;
// data[2] |= (state.tDetActive & 1) << 2;
// data[2] |= (state.tDpsActive & 1) << 1;
data[2] |= (state.override & 1) << 7;
data[2] |= (state.linked & 1) << 6;
// data[3] |= (state.vTdetOpen & 1) << 7;
// data[3] |= (state.vDetOpen & 1) << 6;
// data[3] |= (state.vFoilOpen & 1) << 5;
// data[3] |= (state.vDpsOpen & 1) << 4;
data[2] |= (state.pGratActive & 1) << 5;
data[2] |= (state.pMcpActive & 1) << 4;
data[2] |= (state.pVvActive & 1) << 3;
// data[3] |= (state.hvmcpHigh & 1) << 3;
// data[3] |= (state.rbeamlineHigh & 1)<< 2;
data[2] |= (state.tDetActive & 1) << 2;
data[2] |= (state.tDpsActive & 1) << 1;
// data[3] |= (state.pumpEnabled & 1) << 1;
data[3] |= (state.vTdetOpen & 1) << 7;
data[3] |= (state.vDetOpen & 1) << 6;
data[3] |= (state.vFoilOpen & 1) << 5;
data[3] |= (state.vDpsOpen & 1) << 4;
write_byte(p, 1);
write_byte(p, 2);
data[3] |= (state.hvmcpHigh & 1) << 3;
data[3] |= (state.rbeamlineHigh & 1)<< 2;
write_bools(p, state.override, state.linked,
state.pGratActive, state.pMcpActive, state.pVvActive,
state.tDetActive, state.tDpsActive,
0);
data[3] |= (state.pumpEnabled & 1) << 1;
write_bools(p, state.vTdetOpen, state.vDetOpen, state.vFoilOpen, state.vDpsOpen,
state.hvmcpHigh, state.rbeamlineHigh,
state.pumpEnabled,
0);
write((const char *) data, 4);
}
......
......@@ -6,10 +6,6 @@
#include "lib.h"
class Client : public QTcpSocket
{
Q_OBJECT
......
......@@ -10,6 +10,8 @@
#include <RS232.h>
#endif
#define NC 255
// Arduino Mega 2560 Compatibility
#ifndef MDUINO_PLUS
......@@ -28,19 +30,19 @@ uint8_t I1_2 = A3;
uint8_t I1_3 = A4;
// Gauges in
uint8_t I1_4 = 0; //A0; <- unused
uint8_t I1_5 = 0; //A1; <- unused
uint8_t I1_4 = NC; //A0; <- unused
uint8_t I1_5 = NC; //A1; <- unused
uint8_t I2_2 = A0;//A2; <- change
uint8_t I2_3 = A1;
uint8_t I2_4 = A2;
// Relay outs
uint8_t R0_1 = 2;
uint8_t R0_2 = 3;
uint8_t R0_3 = 4;
uint8_t R0_4 = 5;
uint8_t R0_5 = 0;
uint8_t R0_6 = 0;
uint8_t R0_1 = 6;
uint8_t R0_2 = 7;
uint8_t R0_3 = 8;
uint8_t R0_4 = 9;
uint8_t R0_5 = 10;
uint8_t R0_6 = 11;
#endif
......@@ -54,7 +56,7 @@ IPAddress GATEWAY ( 169, 254, 170, 86 );
IPAddress SUBNET ( 255, 255, 0, 0 );
unsigned int ETH_DATA_PORT = 5000;
unsigned int ETH_SHELL_PORT = 21;
unsigned int ETH_SHELL_PORT = 23;
#define NUM_DATA_CLIENTS 4
#define NUM_SHELL_CLIENTS 4
......@@ -73,7 +75,7 @@ typedef struct ClientData {
typedef struct RunningState
{
bool override;
bool sysOverride;
bool linked;
bool pGratActive;
......@@ -138,7 +140,7 @@ const uint8_t pins_vTdet [ 4 ] =
[ I_OPEN ] = I0_0,
[ I_CLOSED ] = I0_1,
[ O_CONTROL_OPEN ] = R0_1,
[ O_CONTROL_CLOSE ] = 0
[ O_CONTROL_CLOSE ] = NC
};
const uint8_t pins_vdet [ 4 ] =
......@@ -146,14 +148,14 @@ const uint8_t pins_vdet [ 4 ] =
[ I_OPEN ] = I0_2,
[ I_CLOSED ] = I0_3,
[ O_CONTROL_OPEN ] = R0_2,
[ O_CONTROL_CLOSE ] = 0
[ O_CONTROL_CLOSE ] = NC
};
const uint8_t pins_vfoil [ 4 ] =
{
[ I_OPEN ] = I0_4,
[ I_CLOSED ] = I0_5,
[ O_CONTROL_OPEN ] = 0,
[ O_CONTROL_OPEN ] = NC,
[ O_CONTROL_CLOSE ] = R0_3
};
......@@ -162,7 +164,7 @@ const uint8_t pins_vdps [ 4 ] =
[ I_OPEN ] = I1_0,
[ I_CLOSED ] = I1_1,
[ O_CONTROL_OPEN ] = R0_4,
[ O_CONTROL_CLOSE ] = 0
[ O_CONTROL_CLOSE ] = NC
};
const uint8_t *pins_valves [ 4 ] =
......@@ -214,6 +216,54 @@ int t_det_cutoff = 818; // assuming 100% = 1023 / 10V
// Routines
void write_byte(byte **buf, byte x)
{
**buf = x;
(*buf)++;
}
void read_byte(byte **buf, byte *x)
{
*x = **buf;
(*buf)++;
}
void write_int(byte **buf, int x)
{
**buf = (byte) (x >> 8);
(*buf)++;
**buf = (byte) (x & 0xFF);
(*buf)++;
}
void read_int(byte **buf, int *x)
{
*x = 0;
*x |= **buf << 8;
(*buf)++;
*x |= **buf;
(*buf)++;
}
void write_bools(byte **buf, bool b1, bool b2, bool b3, bool b4, bool b5, bool b6, bool b7, bool b8)
{
**buf = (b1 << 7) | (b2 << 6) | (b3 << 5) | (b4 << 4) | (b5 << 3) | (b6 << 2) | (b7 << 1) | (b8 << 0);
(*buf)++;
}
void read_bools(byte **buf, bool *b1, bool *b2, bool *b3, bool *b4, bool *b5, bool *b6, bool *b7, bool *b8)
{
if(b1) *b1 = (**buf & 1 << 7) ? true : false;
if(b2) *b2 = (**buf & 1 << 6) ? true : false;
if(b3) *b3 = (**buf & 1 << 5) ? true : false;
if(b4) *b4 = (**buf & 1 << 4) ? true : false;
if(b5) *b5 = (**buf & 1 << 3) ? true : false;
if(b6) *b6 = (**buf & 1 << 2) ? true : false;
if(b7) *b7 = (**buf & 1 << 1) ? true : false;
if(b8) *b8 = (**buf & 1 << 0) ? true : false;
(*buf)++;
}
void measure_gauges()
{
for( int i = 0; i < 3; i++ )
......@@ -256,7 +306,7 @@ void handle_message(client_t &client)
{
running_state_t req_state;
req_state.override = client.data[ 2 ] & (1 << 7);
req_state.sysOverride = client.data[ 2 ] & (1 << 7);
req_state.linked = client.data[ 2 ] & (1 << 6);
req_state.pGratActive = client.data[ 2 ] & (1 << 5);
......@@ -281,42 +331,42 @@ void handle_message(client_t &client)
// change rules
if(req_state.override != new_state.override)
if(req_state.sysOverride != new_state.sysOverride)
{
new_state.override = req_state.override;
new_state.sysOverride = req_state.sysOverride;
}
if(req_state.linked != new_state.linked)
else if(req_state.linked != new_state.linked)
{
new_state.linked = req_state.linked;
}
if(req_state.pGratActive != new_state.pGratActive)
else if(req_state.pGratActive != new_state.pGratActive)
{
new_state.pGratActive = req_state.pGratActive;
}
if(req_state.pMcpActive != new_state.pMcpActive)
else if(req_state.pMcpActive != new_state.pMcpActive)
{
new_state.pMcpActive = req_state.pMcpActive;
}
if(req_state.pVvActive != new_state.pVvActive)
else if(req_state.pVvActive != new_state.pVvActive)
{
new_state.pVvActive = req_state.pVvActive;
}
if(req_state.tDetActive != new_state.tDetActive)
else if(req_state.tDetActive != new_state.tDetActive)
{
new_state.tDetActive = req_state.tDetActive;
}
if(req_state.tDpsActive != new_state.tDpsActive)
else if(req_state.tDpsActive != new_state.tDpsActive)
{
new_state.tDpsActive = req_state.tDpsActive;
}
if(req_state.vTdetOpen != new_state.vTdetOpen)
else if(req_state.vTdetOpen != new_state.vTdetOpen)
{
if(req_state.vTdetOpen == false)
{
......@@ -324,7 +374,7 @@ void handle_message(client_t &client)
}
else
{
if(new_state.override || !(new_state.tDetActive && (tp_states[TP_DET] < t_det_cutoff)))
if(new_state.sysOverride || !(new_state.tDetActive && (tp_states[TP_DET] < t_det_cutoff)))
{
new_state.vTdetOpen = true;
}
......@@ -335,7 +385,7 @@ void handle_message(client_t &client)
}
}
if(req_state.vDetOpen != current_state.vDetOpen)
else if(req_state.vDetOpen != current_state.vDetOpen)
{
if(req_state.vDetOpen == false)
{
......@@ -346,7 +396,7 @@ void handle_message(client_t &client)
}
else
{
if(new_state.override ||
if(new_state.sysOverride ||
!((new_state.tDetActive && (tp_states[TP_DET] < t_det_cutoff)
|| (new_state.pGratActive && (gauge_pressures[GRAT] > p_grat_cutoff))
|| (new_state.pMcpActive && (gauge_pressures[MCP] > p_mcp_cutoff)))))
......@@ -363,7 +413,7 @@ void handle_message(client_t &client)
}
}
if(req_state.vFoilOpen != current_state.vFoilOpen)
else if(req_state.vFoilOpen != current_state.vFoilOpen)
{
if(req_state.vFoilOpen == true)
{
......@@ -374,7 +424,7 @@ void handle_message(client_t &client)
}
else
{
if(new_state.override || !(new_state.pGratActive && (gauge_pressures[GRAT] > p_grat_cutoff)))
if(new_state.sysOverride || !(new_state.pGratActive && (gauge_pressures[GRAT] > p_grat_cutoff)))
{
new_state.vFoilOpen = false;
if(new_state.linked){
......@@ -388,7 +438,7 @@ void handle_message(client_t &client)
}
}
if(req_state.vDpsOpen != current_state.vDpsOpen)
else if(req_state.vDpsOpen != current_state.vDpsOpen)
{
if(req_state.vDpsOpen == false)
{
......@@ -396,7 +446,7 @@ void handle_message(client_t &client)
}
else
{
if(new_state.override || !(new_state.pGratActive && (gauge_pressures[GRAT] > p_grat_cutoff)))
if(new_state.sysOverride || !(new_state.pGratActive && (gauge_pressures[GRAT] > p_grat_cutoff)))
{
new_state.vDpsOpen = true;
}
......@@ -408,7 +458,7 @@ void handle_message(client_t &client)
}
}
if(req_state.pumpEnabled != current_state.pumpEnabled)
else if(req_state.pumpEnabled != current_state.pumpEnabled)
{
if(req_state.pumpEnabled == false)
{
......@@ -416,7 +466,7 @@ void handle_message(client_t &client)
}
else
{
if(new_state.override || !(new_state.pGratActive && (gauge_pressures[GRAT] > p_grat_cutoff)))
if(new_state.sysOverride || !(new_state.pGratActive && (gauge_pressures[GRAT] > p_grat_cutoff)))
{
new_state.pumpEnabled = true;
}
......@@ -436,45 +486,28 @@ void handle_message(client_t &client)
void write_measurements(byte *msg)
{
msg[0] = 0x02; // measurements
msg[1] = 0x0B; // 11 bytes
// Part A: Gauges (2 bytes each)
// WARNING: LITTLE ENDIAN!
msg[ 2 ] = gauge_pressures [ GRAT ] >> 8;
msg[ 3 ] = gauge_pressures [ GRAT ] & 0xFF;
msg[ 4 ] = gauge_pressures [ MCP ] >> 8;
msg[ 5 ] = gauge_pressures [ MCP ] & 0xFF;
msg[ 6 ] = gauge_pressures [ VV ] >> 8;
msg[ 7 ] = gauge_pressures [ VV ] & 0xFF;
byte *loc_msg = msg;
byte **p = &loc_msg;
write_byte(p, 2);
write_byte(p, 11);
write_int(p, gauge_pressures[GRAT]);
write_int(p, gauge_pressures[MCP]);
write_int(p, gauge_pressures[VV]);
write_int(p, tp_states[TP_DET]);
write_int(p, tp_states[TP_DPS]);
// Part B: Turbopumps (2 bytes each)
msg[ 8 ] = tp_states [ TP_DET ] >> 8;
msg[ 9 ] = tp_states [ TP_DET ] & 0xFF;
msg[ 10 ] = tp_states [ TP_DPS ] >> 8;
msg[ 11 ] = tp_states [ TP_DPS ] & 0xFF;
// Part C: Valve states (1 bit each)
byte valve_states =
( valve_state_measured_h [ VTDET ] << 7 ) |
( valve_state_measured_h [ VDET ] << 6 ) |
( valve_state_measured_h [ VFOIL ] << 5 ) |
( valve_state_measured_h [ VDPS ] << 4 ) |
( valve_state_measured_l [ VTDET ] << 3 ) |
( valve_state_measured_l [ VDET ] << 2 ) |
( valve_state_measured_l [ VFOIL ] << 1 ) |
( valve_state_measured_l [ VDPS ] << 0 );
write_bools(p, valve_state_measured_h[VTDET], valve_state_measured_h[VDET], valve_state_measured_h[VFOIL], valve_state_measured_h[VDPS],
valve_state_measured_l[VTDET], valve_state_measured_l[VDET], valve_state_measured_l[VFOIL], valve_state_measured_l[VDPS]);
msg[12] = valve_states;
}
void send_measurements(EthernetClient &client)
{
byte msg [ 13 ];
write_measurements(msg);
memset(msg, 0, 13);
write_measurements(msg);
client.write(msg, 13);
}
......@@ -495,29 +528,21 @@ void broadcast_measurements()
void write_state(byte *msg)
{
msg[ 0 ] = 0x01; // state
msg[ 1 ] = 0x02; // 2 bytes