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

Overhaul/bugfixes

parent 357fa2bd
This diff is collapsed.
This diff is collapsed.
......@@ -5,7 +5,7 @@ Client::Client() : QTcpSocket()
{
config = std::make_unique<Config>();
connect(this, &QAbstractSocket::readyRead, this, &Client::handleMessage);
connect(this, &QAbstractSocket::readyRead, this, &Client::handleData);
pingInterval = std::make_unique<QTimer>(this);
pingInterval->setSingleShot(true);
......@@ -24,7 +24,6 @@ Client::Client() : QTcpSocket()
connect(this, &QAbstractSocket::disconnected, [=]()
{
// make sure these do not trigger after any disconnect
pingInterval->stop();
pingTimeout->stop();
emit log("Client disconnect.");
......@@ -45,140 +44,122 @@ Client::~Client()
void Client::ping()
{
// emit log("Sending ping");
uint8_t pingData[2] = {0b00000010, 'p'};
write((const char *) pingData, 2);
flush();
uint8_t pingData[3] = {0x00, 0x01, 'p'};
write((const char *) pingData, 3);
pingTimeout->start();
}
void Client::handleMessage()
void Client::handleData()
{
data.append(readAll());
do {
int msgHeader = data[0];
int msgLen = data[1];
QByteArray msg = readAll();
while(msg.length() > 0)
{
auto msgHeader = msg[0] >> 4;
auto msgLen = msg[0] & 0xF;
auto repr = std::accumulate(msg.begin(), msg.end(), QString(""), [](QString a, unsigned int b)
if(data.length() < msgLen + 2)
{
return std::move(a) + QString("\\") + QString::number(b, 16) + QString(" ");
});
if(msgHeader == MsgPing) // pong
return;
}
else
{
if(msgLen == 2 && msg[1] == 'p')
{
// emit log("Received valid pong!: " + repr + "(" + msg + ")");
pingInterval->start();
}
else
if(msgHeader == MsgPing) // pong
{
emit log("Received invalid pong-like message!: " + repr + "(" + msg + ")");
if(data[2] == 'p')
{
pingInterval->start();
}
else
{
emit log(QString("Received invalid pong-like message!: ") + "(" + data.mid(2, msgLen) + ")");
}
}
}
else if(msgHeader == MsgState) // state change
{
if(msgLen == 4)
else if(msgHeader == MsgState) // state change
{
StateMessage newStat;
newStat.operable = msg[1] & (1 << 7);
newStat.override = msg[1] & (1 << 6);
newStat.error = msg[1] & (1 << 5);
newStat.linked = msg[1] & (1 << 4);
newStat.settingVTdet = msg[2] & (1 << 7);
newStat.settingVdet = msg[2] & (1 << 6);
newStat.settingVfoil = msg[2] & (1 << 5);
newStat.settingVdps = msg[2] & (1 << 4);
newStat.settingHVmcp = msg[2] & (1 << 3);
newStat.settingRbeamline = msg[2] & (1 << 2);
newStat.pumpEnabled = msg[2] & (1 << 1);
// emit log("Received new state!: " + repr);
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);
newStat.vTdetOpen = data[3] & (1 << 7);
newStat.vDetOpen = data[3] & (1 << 6);
newStat.vFoilOpen = data[3] & (1 << 5);
newStat.vDpsOpen = data[3] & (1 << 4);
newStat.hvmcpHigh = data[3] & (1 << 3);
newStat.rbeamlineHigh = data[3] & (1 << 2);
newStat.pumpEnabled = data[3] & (1 << 1);
emit newState(newStat);
}
else
{
emit log("Received state-like garbage!");
}
}
else if(msgHeader == MsgMeasurement)
{ // new measurements
if(msgLen == 12)
else if(msgHeader == MsgMeasurement)
{
// emit log("Received measurements");
char *data = msg.data();
MeasurementMessage newMeas;
PlcMeasurement newMeas;
newMeas.pGrat = (((int16_t) msg[1]) << 8) | (((int16_t) msg[2]) & 0xFF);
newMeas.pMCP = (((int16_t) msg[3]) << 8) | (((int16_t) msg[4]) & 0xFF);
newMeas.pVV = (((int16_t) msg[5]) << 8) | (((int16_t) msg[6]) & 0xFF);
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) msg[7]) << 8) | (((int16_t) msg[8]) & 0xFF);
newMeas.tDPS = (((int16_t) msg[9]) << 8) | (((int16_t) msg[10]) & 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);
newMeas.vTdetHigh = data[11] & (1 << 7);
newMeas.vdetHigh = data[11] & (1 << 6);
newMeas.vfoilHigh = data[11] & (1 << 5);
newMeas.vdpsHigh = data[11] & (1 << 4);
newMeas.vTdetHigh = data[12] & (1 << 7);
newMeas.vDetHigh = data[12] & (1 << 6);
newMeas.vFoilHigh = data[12] & (1 << 5);
newMeas.vDpsHigh = data[12] & (1 << 4);
newMeas.vTdetLow = data[11] & (1 << 3);
newMeas.vdetLow = data[11] & (1 << 2);
newMeas.vfoilLow = data[11] & (1 << 1);
newMeas.vdpsLow = data[11] & (1 << 0);
newMeas.vTdetLow = data[12] & (1 << 3);
newMeas.vDetLow = data[12] & (1 << 2);
newMeas.vFoilLow = data[12] & (1 << 1);
newMeas.vDpsLow = data[12] & (1 << 0);
emit newMeasurements(newMeas);
}
else
else if(msgHeader == MsgError)
{
emit log("Error: received measurement-like garbage!");
emit log("PLC Error: " + data.mid(2, msgLen));
}
}
else
{
emit log("Got garbage: " + repr);
}
msg.remove(0, msgLen);
}
data.remove(0, msgLen + 2);
} while(data.length() > 0);
}
void Client::onSendState(StateMessage &state)
void Client::onSendState(PlcState &state)
{
uint8_t data[4];
data[0] = 0x01; // PlcState
data[1] = 0x02; // Length = 2;
data[2] |= (state.override & 1) << 7;
data[2] |= (state.linked & 1) << 6;
uint8_t msg[4];
// emit log("Writing state...");
msg[0] = 0b00010100;
msg[1] = 0;
data[2] |= (state.pGratActive & 1) << 5;
data[2] |= (state.pMcpActive & 1) << 4;
data[2] |= (state.pVvActive & 1) << 3;
msg[1] |= state.operable << 7;
msg[1] |= state.override << 6;
msg[1] |= state.error << 5;
msg[1] |= state.linked << 4;
data[2] |= (state.tDetActive & 1) << 2;
data[2] |= (state.tDpsActive & 1) << 1;
msg[2] = 0;
msg[2] |= state.settingVTdet << 7;
msg[2] |= state.settingVdet << 6;
msg[2] |= state.settingVfoil << 5;
msg[2] |= state.settingVdps << 4;
data[3] |= (state.vTdetOpen & 1) << 7;
data[3] |= (state.vDetOpen & 1) << 6;
data[3] |= (state.vFoilOpen & 1) << 5;
data[3] |= (state.vDpsOpen & 1) << 4;
msg[2] |= state.settingHVmcp << 3;
msg[2] |= state.settingRbeamline << 2;
data[3] |= (state.hvmcpHigh & 1) << 3;
data[3] |= (state.rbeamlineHigh & 1)<< 2;
msg[2] |= state.pumpEnabled << 1;
data[3] |= (state.pumpEnabled & 1) << 1;
write((const char *) msg, 4);
flush();
write((const char *) data, 4);
}
void Client::onNewConfig(Config &newConfig)
......
......@@ -18,15 +18,15 @@ public:
~Client();
signals:
// void new_pong(PingMessage *p);
void newState(StateMessage &state);
void newMeasurements(MeasurementMessage &measurements);
// void new_pong(Ping *p);
void newState(PlcState &state);
void newMeasurements(PlcMeasurement &measurements);
void log(QString msg);
public slots:
void onSendState(StateMessage &newState);
void onSendState(PlcState &newState);
void onNewConfig(Config &newConfig);
......@@ -37,7 +37,10 @@ private:
std::unique_ptr<Config> config;
void handleMessage();
QByteArray data;
void handleData();
void ping();
};
......
#include "lib.h"
StateMessage::StateMessage()
PlcState::PlcState()
{
this->error = false;
this->linked = true;
this->operable = true;
this->override = false;
this->linked = true;
this->pGratActive = true;
this->pMcpActive = true;
this->pVvActive = true;
this->tDetActive = true;
this->tDpsActive = true;
this->vTdetOpen = false;
this->vDetOpen = false;
this->vFoilOpen = true;
this->vDpsOpen = false;
this->hvmcpHigh = true;
this->rbeamlineHigh = true;
this->pumpEnabled = false;
this->settingHVmcp = true;
this->settingRbeamline = true;
this->settingVTdet = false;
this->settingVdet = false;
this->settingVdps = false;
this->settingVfoil = true;
}
StateMessage::StateMessage(StateMessage &other)
PlcState::PlcState(PlcState &other)
{
this->error = other.error;
this->linked = other.linked;
this->operable = other.operable;
this->override = other.override;
this->pumpEnabled = other.pumpEnabled;
this->settingHVmcp = other.settingHVmcp;
this->settingRbeamline = other.settingRbeamline;
this->settingVTdet = other.settingVTdet;
this->settingVdet = other.settingVdet;
this->settingVdps = other.settingVdps;
this->settingVfoil = other.settingVfoil;
this->override = other.override;
this->linked = other.linked;
this->pGratActive = other.pGratActive;
this->pMcpActive = other.pMcpActive;
this->pVvActive = other.pVvActive;
this->tDetActive = other.tDetActive;
this->tDpsActive = other.tDpsActive;
this->vTdetOpen = other.vTdetOpen;
this->vDetOpen = other.vDetOpen;
this->vDpsOpen = other.vDpsOpen;
this->vFoilOpen = other.vFoilOpen;
this->hvmcpHigh = other.hvmcpHigh;
this->rbeamlineHigh = other.rbeamlineHigh;
this->pumpEnabled = other.pumpEnabled;
}
StateMessage::StateMessage(StateMessage *other) : StateMessage()
PlcState::PlcState(PlcState *other) : PlcState()
{
if(other != nullptr)
{
this->error = other->error;
this->linked = other->linked;
this->operable = other->operable;
this->override = other->override;
this->pumpEnabled = other->pumpEnabled;
this->settingHVmcp = other->settingHVmcp;
this->settingRbeamline = other->settingRbeamline;
this->settingVTdet = other->settingVTdet;
this->settingVdet = other->settingVdet;
this->settingVdps = other->settingVdps;
this->settingVfoil = other->settingVfoil;
this->override = other->override;
this->linked = other->linked;
this->pGratActive = other->pGratActive;
this->pMcpActive = other->pMcpActive;
this->pVvActive = other->pVvActive;
this->tDetActive = other->tDetActive;
this->tDpsActive = other->tDpsActive;
this->vTdetOpen = other->vTdetOpen;
this->vDetOpen = other->vDetOpen;
this->vDpsOpen = other->vDpsOpen;
this->vFoilOpen = other->vFoilOpen;
this->hvmcpHigh = other->hvmcpHigh;
this->rbeamlineHigh = other->rbeamlineHigh;
this->pumpEnabled = other->pumpEnabled;
}
}
void StateMessage::operator=(StateMessage &other)
void PlcState::operator=(PlcState &other)
{
this->error = other.error;
this->linked = other.linked;
this->operable = other.operable;
this->override = other.override;
this->pumpEnabled = other.pumpEnabled;
this->settingHVmcp = other.settingHVmcp;
this->settingRbeamline = other.settingRbeamline;
this->settingVTdet = other.settingVTdet;
this->settingVdet = other.settingVdet;
this->settingVdps = other.settingVdps;
this->settingVfoil = other.settingVfoil;
this->override = other.override;
this->linked = other.linked;
this->pGratActive = other.pGratActive;
this->pMcpActive = other.pMcpActive;
this->pVvActive = other.pVvActive;
this->tDetActive = other.tDetActive;
this->tDpsActive = other.tDpsActive;
this->vTdetOpen = other.vTdetOpen;
this->vDetOpen = other.vDetOpen;
this->vDpsOpen = other.vDpsOpen;
this->vFoilOpen = other.vFoilOpen;
this->hvmcpHigh = other.hvmcpHigh;
this->rbeamlineHigh = other.rbeamlineHigh;
this->pumpEnabled = other.pumpEnabled;
}
Config::Config() // default
{
this->networkIP = "192.168.171.12";
this->networkIP = "169.254.170.12";
this->networkPort = 5000;
this->displayModeDynamic = 0;
......@@ -74,7 +107,7 @@ Config::Config() // default
this->gaugeTypeGrat = 0; // PBR
this->gaugeTypeMcp = 0; // PBR
this->gaugeTypeVv = 2; // IKR
this->gaugeTypeVv = 3; // TPR
}
Config::Config(Config &other)
......@@ -121,36 +154,36 @@ void Config::operator=(Config &other)
}
void MeasurementMessage::operator=(MeasurementMessage &other)
void PlcMeasurement::operator=(PlcMeasurement &other)
{
this->pGrat = other.pGrat;
this->pMCP = other.pMCP;
this->pVV = other.pVV;
this->pMcp = other.pMcp;
this->pVv = other.pVv;
this->tDPS = other.tDPS;
this->tDps = other.tDps;
this->tDet = other.tDet;
this->vTdetHigh = other.vTdetHigh;
this->vTdetLow = other.vTdetLow;
this->vdetHigh = other.vdetHigh;
this->vdetLow = other.vdetLow;
this->vdpsHigh = other.vdpsHigh;
this->vdpsLow = other.vdpsLow;
this->vfoilHigh = other.vfoilHigh;
this->vfoilLow = other.vfoilLow;
this->vDetHigh = other.vDetHigh;
this->vDetLow = other.vDetLow;
this->vDpsHigh = other.vDpsHigh;
this->vDpsLow = other.vDpsLow;
this->vFoilHigh = other.vFoilHigh;
this->vFoilLow = other.vFoilLow;
}
qreal pressureFromReading(int16_t measure, int type){
qreal baseVal = measure / 1023.0 * 10.0;
qreal baseVal = (qreal) measure * 10.0 / 1023.0;
switch(type) {
case 0: // PBR
return qPow(10, (baseVal - 7.75) / 0.75);
return qPow(10, 1.333 * baseVal - 10.33);
case 1: // PKR
return qPow(10, 1.667 * baseVal - 11.33);
case 2: // IKR
return qPow(10, 1.25 * baseVal - 12.75);
return qPow(10, 1.250 * baseVal - 12.75);
case 3:
return qPow(10, 1.667 * baseVal - 11.33);
return qPow(10, baseVal - 5.5);
default:
return NAN; // something went terribly wrong in UI or Settings
}
......
......@@ -3,14 +3,16 @@
#include <QtCore>
#include <QLabel>
enum MsgType {
enum MsgType
{
MsgPing = 0,
MsgState = 1 << 0,
MsgMeasurement = 1 << 1,
MsgError = 1 << 2, // needed?
MsgError = 1 << 2
};
struct Config {
struct Config
{
QString networkIP;
int networkPort;
......@@ -27,52 +29,59 @@ struct Config {
void operator=(Config &other);
};
// what ino/client intend
struct StateMessage { // repr: 1 + 1(header) bytes
bool operable;
bool override;
bool error;
bool linked;
bool settingVTdet;
bool settingVdet;
bool settingVfoil;
bool settingVdps;
bool settingHVmcp;
bool settingRbeamline;
bool pumpEnabled;
StateMessage();
StateMessage(StateMessage &other);
StateMessage(StateMessage *other);
void operator=(StateMessage &other);
struct PlcState
{
bool override = false;
bool linked = true;
bool pGratActive = true;
bool pMcpActive = true;
bool pVvActive = true;
bool tDetActive = true;
bool tDpsActive = true;
bool vTdetOpen = false;
bool vDetOpen = false;
bool vFoilOpen = true;
bool vDpsOpen = false;
bool hvmcpHigh = true;
bool rbeamlineHigh = true;
bool pumpEnabled = false;
PlcState();
PlcState(PlcState &other);
PlcState(PlcState *other);
void operator=(PlcState &other);
};
// what the ino actually measures
struct MeasurementMessage { // repr: 11 + 1(header) bytes
struct PlcMeasurement { // repr: 11 + 1(header) bytes
int16_t pGrat;
int16_t pMCP;
int16_t pVV;
int16_t pMcp;
int16_t pVv;
int16_t tDet;
int16_t tDPS;
int16_t tDps;
bool vTdetHigh;
bool vdetHigh;
bool vfoilHigh;
bool vdpsHigh;
bool vDetHigh;
bool vFoilHigh;
bool vDpsHigh;
bool vTdetLow;
bool vdetLow;
bool vfoilLow;
bool vdpsLow;
bool vDetLow;
bool vFoilLow;
bool vDpsLow;
void operator=(MeasurementMessage &other);
void operator=(PlcMeasurement &other);
};
struct PingMessage {
struct Ping {
uint8_t p = 'p'; // redundancy/"checksum"
};
......
......@@ -8,8 +8,8 @@ MainTab::MainTab(QWidget *parent) : QWidget(parent), ui(std::make_unique<Ui::Mai
ui->setupUi(this);
config = std::make_unique<Config>();
meas = std::make_unique<MeasurementMessage>();
state = std::make_unique<StateMessage>();
meas = std::make_unique<PlcMeasurement>();