diff --git a/platformio.ini b/platformio.ini index e483d74..9770e24 100644 --- a/platformio.ini +++ b/platformio.ini @@ -38,6 +38,8 @@ monitor_speed = 115200 upload_speed = 921600 build_src_filter = -<*> + + + build_flags = -DFUEL -DIP_ADDRESS_END=62 -DFW_COMMIT=\"1234567\" -DEREG +monitor_echo = yes +monitor_filters = send_on_enter, colorize, time [env:LOX Tank EReg] platform = espressif32 @@ -47,6 +49,8 @@ monitor_speed = 115200 upload_speed = 921600 build_src_filter = -<*> + + + build_flags = -DLOX -DIP_ADDRESS_END=61 -DFW_COMMIT=\"1234567\" -DEREG +monitor_echo = yes +monitor_filters = send_on_enter, colorize, time diff --git a/src/EReg/Config.h b/src/EReg/Config.h index 9a6cbb5..c4a6afe 100644 --- a/src/EReg/Config.h +++ b/src/EReg/Config.h @@ -42,7 +42,7 @@ namespace Config { const unsigned long rampDuration = 500UL * 1000UL; // time in microseconds // Pressurization Parameters - const unsigned long pressurizationRampDuration = 30 * 1000UL * 1000UL; + const unsigned long pressurizationRampDuration = 60 * 1000UL * 1000UL; const float pressurizationCutoff = pressureSetpoint * 0.99; const unsigned long tankPidStart = 0; // time in microseconds const unsigned long tankPidFull = 1 * 1000UL * 1000UL; // time in microseconds diff --git a/src/EReg/Ducers.cpp b/src/EReg/Ducers.cpp index 25f1285..3c2c275 100644 --- a/src/EReg/Ducers.cpp +++ b/src/EReg/Ducers.cpp @@ -3,6 +3,30 @@ namespace Ducers { //set on call of HAL::readAllDucers(). + bool persistentCal = true; + + float data[4]; + float offset[4]; + float point1[4]; + float multiplier[4]; + + // //0 + // //float _upstreamPT1_offset = 0; + // //float _upstreamPT1_multiplier = 1; + + // //1 + // float _downstreamPT1_offset = 0; + // float _downstreamPT1_multiplier = 1; + + // //2 + // float _upstreamPT2_offset = 0; + // float _upstreamPT2_multiplier = 1; + + + // //3 + // float _downstreamPT2_offset = 0; + // float _downstreamPT2_multiplier = 1; + float _upstreamPT1; float _downstreamPT1; float _upstreamPT2; @@ -30,6 +54,103 @@ namespace Ducers { upstreamPT2Buff->insert(millis(), upstreamPT2); } + void cal1Channel(uint8_t channel, float inputvalue){ + float value; + + if(channel == 0){ + value = readRawPressurantPT1(); + } + else if(channel == 1){ + value = readRawTankPT1(); + } + else if(channel == 2){ + value = readRawPressurantPT2(); + } + else if(channel == 3){ + value = readRawTankPT2(); + } + + point1[channel] = inputvalue; + offset[channel] = inputvalue - value + offset[channel]; + Serial.println("calibrated channel offset" + String(channel) + " to " + String(offset[channel])); + if (persistentCal){ + EEPROM.begin(8*sizeof(float)); + EEPROM.put(channel*sizeof(float),offset[channel]); + EEPROM.end(); + } + } + + void cal2Channel(uint8_t channel, float inputvalue){ + float value; + + if(channel == 0){ + value = readRawPressurantPT1(); + } + else if(channel == 1){ + value = readRawTankPT1(); + } + else if(channel == 2){ + value = readRawPressurantPT2(); + } + else if(channel == 3){ + value = readRawTankPT2(); + } + + + multiplier[channel] = (inputvalue - point1[channel])/(value/multiplier[channel] - point1[channel]); + offset[channel] += inputvalue/multiplier[channel] - value; + Serial.println("calibrated channel multiplier" + String(channel) + " to " + String(multiplier[channel])); + if (persistentCal){ + EEPROM.begin(8*sizeof(float)); + EEPROM.put((channel)*sizeof(float),offset[channel]); + EEPROM.put((channel+4)*sizeof(float),multiplier[channel]); + EEPROM.end(); + } + } + + void onCal1Command(Comms::Packet packet, uint8_t ip){ + uint8_t channel = Comms::packetGetUint8(&packet, 0); + float value = Comms::packetGetFloat(&packet, 1); + cal1Channel(channel, value); + return; + } + + void onCal2Command(Comms::Packet packet, uint8_t ip){ + uint8_t channel = Comms::packetGetUint8(&packet, 0); + float value = Comms::packetGetFloat(&packet, 1); + cal2Channel(channel, value); + return; + } + + void sendCal(Comms::Packet packet, uint8_t ip){ + Comms::Packet response = {.id=102, .len =0}; + for (int i = 0; i < 4; i++){ + Comms::packetAddFloat(&response, offset[i]); + Comms::packetAddFloat(&response, multiplier[i]); + Serial.println(" " + String(offset[i]) + " " + String(multiplier[i])); + } + Comms::emitPacketToGS(&response); + return; + } + + void resetCal(Comms::Packet packet, uint8_t ip){ + uint8_t channel = Comms::packetGetUint8(&packet, 0); + + offset[channel] = 0; + multiplier[channel] = 1; + + Serial.println("reset channel " + String(channel)); + + if (persistentCal){ + EEPROM.begin(8*sizeof(float)); + EEPROM.put(channel*sizeof(float),offset[channel]); + EEPROM.put((channel+4)*sizeof(float),multiplier[channel]); + EEPROM.end(); + } + return; + } + + void initPTs() { upstreamPT1Buff = new Buffer(Config::PTFilterBufferSize); downstreamPT1Buff = new Buffer(Config::PTFilterBufferSize); @@ -39,9 +160,48 @@ namespace Ducers { downstreamPT1Buff->clear(); upstreamPT2Buff->clear(); downstreamPT2Buff->clear(); + + Comms::registerCallback(100, onCal1Command); + Comms::registerCallback(101, onCal2Command); + Comms::registerCallback(102, sendCal); + Comms::registerCallback(103, resetCal); + + + if (persistentCal){ + EEPROM.begin(8*sizeof(float)); + for (int i = 0; i < 4; i++){ + EEPROM.get(i*sizeof(float),offset[i]); + if (isnan(offset[i])){ + offset[i] = 0; + } + } + for (int i = 0; i < 4; i++){ + EEPROM.get((i+4)*sizeof(float),multiplier[i]); + if (isnan(multiplier[i]) || multiplier[i] < 0.01){ + multiplier[i] = 1; + } + } + EEPROM.end(); + } else { + for (int i = 0; i < 4; i++){ + offset[i] = 0; + } + for (int i = 0; i < 4; i++){ + multiplier[i] = 1; + } + } + + for (int i = 0; i < 4; i ++){ + Serial.println("channel " + String(i) + " offset value: " + String(offset[i])); + } + + for (int i = 0; i < 4; i ++){ + Serial.println("channel " + String(i) + " multiplier value: " + String(multiplier[i])); + } } + float interpolate1000(double rawValue) { return ((rawValue - 0.5) * 250) + 12.5; } @@ -50,45 +210,46 @@ namespace Ducers { return (rawValue * 1000 * 1.0042) + 5; //1.0042 from the voltage divider - 5647ohm and 5600ohm } + float readRawTankPT1() { + return multiplier[1] * (interpolate1000(_downstreamPT1) + offset[1]); + } + float readRawTankPT2() { + return multiplier[3] * (interpolate1000(_downstreamPT2) + offset[3]); + } + float readRawPressurantPT1() { + return multiplier[0] * (interpolate5000(_upstreamPT1) + offset[0]); + } + float readRawPressurantPT2() { + return multiplier[2] * (interpolate5000(_upstreamPT2) + offset[2]); + } + float readPressurantPT1() { - return max((float)1, interpolate5000(_upstreamPT1)); + return max((float)1, readRawPressurantPT1()); } float readPressurantPT2() { - return max((float)1, interpolate5000(_upstreamPT2)); + return max((float)1, readRawPressurantPT2()); } float readTankPT1() { - return max((float)1, interpolate1000(_downstreamPT1)); + return max((float)1, readRawTankPT1()); } float readTankPT2() { - return max((float)1, interpolate1000(_downstreamPT2)); - } - - float readRawTankPT1() { - return interpolate1000(_downstreamPT1); - } - float readRawTankPT2() { - return interpolate1000(_downstreamPT2); - } - float readRawPressurantPT1() { - return interpolate5000(_upstreamPT1); - } - float readRawPressurantPT2() { - return interpolate5000(_upstreamPT2); + return max((float)1, readRawTankPT2()); } + float readFilteredTankPT1() { - return (float) downstreamPT1Buff->getFiltered(); + return (float) multiplier[1] * (interpolate1000(downstreamPT1Buff->getFiltered()) + offset[1]); } float readFilteredTankPT2() { - return (float) downstreamPT2Buff->getFiltered(); + return (float) multiplier[3] * (interpolate1000(downstreamPT2Buff->getFiltered()) + offset[3]); } float readFilteredPressurantPT1() { - return (float) upstreamPT1Buff->getFiltered(); + return (float) multiplier[0] * (interpolate5000(upstreamPT1Buff->getFiltered()) + offset[0]); } float readFilteredPressurantPT2() { - return (float) upstreamPT2Buff->getFiltered(); + return (float) multiplier[2] * (interpolate5000(upstreamPT2Buff->getFiltered()) + offset[2]); } /** diff --git a/src/EReg/Ducers.h b/src/EReg/Ducers.h index 6c8f8b5..8022bbb 100644 --- a/src/EReg/Ducers.h +++ b/src/EReg/Ducers.h @@ -2,6 +2,7 @@ #include "HAL.h" #include "Util.h" #include "data_buff.h" +#include "EEPROM.h" namespace Ducers { diff --git a/src/EReg/ElectronicRegulator.cpp b/src/EReg/ElectronicRegulator.cpp index 5a77185..d8b531d 100644 --- a/src/EReg/ElectronicRegulator.cpp +++ b/src/EReg/ElectronicRegulator.cpp @@ -42,7 +42,7 @@ void flow(Comms::Packet packet, uint8_t ip) { Serial.printf("bad flow packet len %d\n", packet.len); return; } - uint32_t flowLength = packetGetUint32(&packet, 1); + uint32_t flowLength = packetGetUint32(&packet, 1) * 1000; if ((flowLength < 1 * 1000 * 1000) || (flowLength > 70 * 1000 * 1000)) { Serial.printf("bad flow duration %d\n", flowLength); return; diff --git a/src/EReg/Packets.cpp b/src/EReg/Packets.cpp index 47e880a..29e6ec5 100644 --- a/src/EReg/Packets.cpp +++ b/src/EReg/Packets.cpp @@ -21,13 +21,18 @@ namespace Packets { uint8_t ac1_ip = 11; uint8_t ac2_ip = 12; uint8_t ACTUATE_IP = 100; + uint32_t lastTelemetry = 0; void sendTelemetry( - float upstreamPressure1, - float upstreamPressure2, - float downstreamPressure1, - float downstreamPressure2, + float filteredUpstreamPressure1, + float filteredUpstreamPressure2, + float filteredDownstreamPressure1, + float filteredDownstreamPressure2, + float rawUpstreamPressure1, + float rawUpstreamPressure2, + float rawDownstreamPressure1, + float rawDownstreamPressure2, float encoderAngle, float angleSetpoint, float pressureSetpoint, @@ -37,11 +42,21 @@ namespace Packets { float pressureControlD ) { - Comms::Packet packet = {.id = TELEMETRY_ID}; - Comms::packetAddFloat(&packet, upstreamPressure1); - Comms::packetAddFloat(&packet, upstreamPressure2); - Comms::packetAddFloat(&packet, downstreamPressure1); - Comms::packetAddFloat(&packet, downstreamPressure2); + //pressure data + Comms::Packet packet = {.id = PT_TELEMETRY_ID, .len=0}; + Comms::packetAddFloat(&packet, filteredUpstreamPressure1); + Comms::packetAddFloat(&packet, filteredUpstreamPressure2); + Comms::packetAddFloat(&packet, filteredDownstreamPressure1); + Comms::packetAddFloat(&packet, filteredDownstreamPressure2); + Comms::packetAddFloat(&packet, rawUpstreamPressure1); + Comms::packetAddFloat(&packet, rawUpstreamPressure2); + Comms::packetAddFloat(&packet, rawDownstreamPressure1); + Comms::packetAddFloat(&packet, rawDownstreamPressure2); + Comms::emitPacket(&packet); + + //misc data + packet.id = MISC_TELEMETRY_ID; + packet.len = 0; Comms::packetAddFloat(&packet, encoderAngle); Comms::packetAddFloat(&packet, angleSetpoint); Comms::packetAddFloat(&packet, pressureSetpoint); @@ -50,9 +65,20 @@ namespace Packets { Comms::packetAddFloat(&packet, pressureControlI); Comms::packetAddFloat(&packet, pressureControlD); Comms::emitPacket(&packet); + sendTemperatures(); sendPhaseCurrents(); sendLimitSwitches(); + + if (millis() - lastTelemetry > 2000) { + lastTelemetry = millis(); + Serial.println("Upstream Pressure 1: filtered" + String(filteredUpstreamPressure1) + ", raw" + String(rawUpstreamPressure1)); + Serial.println("Upstream Pressure 2: filtered" + String(filteredUpstreamPressure2) + ", raw" + String(rawUpstreamPressure2)); + Serial.println("Downstream Pressure 1: filtered" + String(filteredDownstreamPressure1) + ", raw" + String(rawDownstreamPressure1)); + Serial.println("Downstream Pressure 2: filtered" + String(filteredDownstreamPressure2) + ", raw" + String(rawDownstreamPressure2)); + Serial.println("Encoder Angle: " + String(encoderAngle) + ", Setpoint: " + String(angleSetpoint) + ", Pressure Setpoint: " + String(pressureSetpoint) + ", Motor Power: " + String(motorPower)); + Serial.println("Pressure Control P: " + String(pressureControlP) + ", I: " + String(pressureControlI) + ", D: " + String(pressureControlD)); + } // Serial.printf("packet sent\n"); } diff --git a/src/EReg/Packets.h b/src/EReg/Packets.h index c038f17..bb7aed7 100644 --- a/src/EReg/Packets.h +++ b/src/EReg/Packets.h @@ -4,22 +4,27 @@ namespace Packets { - const uint8_t TELEMETRY_ID = 1; - const uint8_t CONFIG_ID = 2; - const uint8_t DIAGNOSTIC_ID = 3; - const uint8_t STATE_TRANSITION_FAIL_ID = 4; - const uint8_t FLOW_STATE = 5; - const uint8_t LIMIT_SWITCHES = 6; - const uint8_t PHASE_CURRENTS = 7; - const uint8_t TEMPS = 8; + const uint8_t PT_TELEMETRY_ID = 1; + const uint8_t MISC_TELEMETRY_ID = 2; + const uint8_t CONFIG_ID = 3; + const uint8_t DIAGNOSTIC_ID = 4; + const uint8_t STATE_TRANSITION_FAIL_ID = 5; + const uint8_t FLOW_STATE = 6; + const uint8_t LIMIT_SWITCHES = 7; + const uint8_t PHASE_CURRENTS = 8; + const uint8_t TEMPS = 9; const uint8_t ABORT_ID = 133; const uint8_t OVERCURRENT_ID = 10; void sendTelemetry( - float upstreamPressure1, - float upstreamPressure2, - float downstreamPressure1, - float downstreamPressure2, + float filteredUpstreamPressure1, + float filteredUpstreamPressure2, + float filteredDownstreamPressure1, + float filteredDownstreamPressure2, + float rawUpstreamPressure1, + float rawUpstreamPressure2, + float rawDownstreamPressure1, + float rawDownstreamPressure2, float encoderAngle, float angleSetpoint, float pressureSetpoint, diff --git a/src/EReg/States/DiagnosticState.cpp b/src/EReg/States/DiagnosticState.cpp index cb9724f..00d1ffd 100644 --- a/src/EReg/States/DiagnosticState.cpp +++ b/src/EReg/States/DiagnosticState.cpp @@ -106,6 +106,10 @@ namespace StateMachine { //send data to AC if (TimeUtil::timeInterval(lastPrint_, micros()) > Config::telemetryInterval) { Packets::sendTelemetry( + filteredUpstreamPT1, + filteredUpstreamPT2, + filteredDownstreamPT1, + filteredDownstreamPT2, rawUpstreamPT1, rawUpstreamPT2, rawDownstreamPT1, @@ -176,6 +180,10 @@ namespace StateMachine { // send data to AC if (TimeUtil::timeInterval(lastPrint_, micros()) > Config::telemetryInterval) { Packets::sendTelemetry( + filteredUpstreamPT1, + filteredUpstreamPT2, + filteredDownstreamPT1, + filteredDownstreamPT2, rawUpstreamPT1, rawUpstreamPT2, rawDownstreamPT1, @@ -230,6 +238,10 @@ namespace StateMachine { float downstreamPsi = Ducers::chooseDucerRead(downstreamPT1, downstreamPT2); Packets::sendTelemetry( + filteredUpstreamPT1, + filteredUpstreamPT2, + filteredDownstreamPT1, + filteredDownstreamPT2, rawUpstreamPT1, rawUpstreamPT2, rawDownstreamPT1, @@ -291,6 +303,10 @@ namespace StateMachine { float downstreamPsi = Ducers::chooseDucerRead(downstreamPT1, downstreamPT2); Packets::sendTelemetry( + filteredUpstreamPT1, + filteredUpstreamPT2, + filteredDownstreamPT1, + filteredDownstreamPT2, rawUpstreamPT1, rawUpstreamPT2, rawDownstreamPT1, diff --git a/src/EReg/States/FlowState.cpp b/src/EReg/States/FlowState.cpp index c91295f..b617f31 100644 --- a/src/EReg/States/FlowState.cpp +++ b/src/EReg/States/FlowState.cpp @@ -78,6 +78,10 @@ namespace StateMachine { //send data to AC if (TimeUtil::timeInterval(lastPrint_, micros()) > Config::telemetryInterval) { Packets::sendTelemetry( + filteredUpstreamPT1, + filteredUpstreamPT2, + filteredDownstreamPT1, + filteredDownstreamPT2, rawUpstreamPT1, rawUpstreamPT2, rawDownstreamPT1, diff --git a/src/EReg/States/IdleClosedState.cpp b/src/EReg/States/IdleClosedState.cpp index f9648e0..ed70b35 100644 --- a/src/EReg/States/IdleClosedState.cpp +++ b/src/EReg/States/IdleClosedState.cpp @@ -67,6 +67,10 @@ namespace StateMachine { filteredUpstreamPT2, filteredDownstreamPT1, filteredDownstreamPT2, + rawUpstreamPT1, + rawUpstreamPT2, + rawDownstreamPT1, + rawDownstreamPT2, motorAngle, 0, 0, diff --git a/src/EReg/States/PartiallyOpenState.cpp b/src/EReg/States/PartiallyOpenState.cpp index 08557b5..9115ea3 100644 --- a/src/EReg/States/PartiallyOpenState.cpp +++ b/src/EReg/States/PartiallyOpenState.cpp @@ -58,6 +58,10 @@ namespace StateMachine { //send data to AC if (TimeUtil::timeInterval(lastPrint_, micros()) > Config::telemetryInterval) { Packets::sendTelemetry( + filteredUpstreamPT1, + filteredUpstreamPT2, + filteredDownstreamPT1, + filteredDownstreamPT2, rawUpstreamPT1, rawUpstreamPT2, rawDownstreamPT1, diff --git a/src/EReg/States/PressurizeState.cpp b/src/EReg/States/PressurizeState.cpp index 7e43e2b..b52c95a 100644 --- a/src/EReg/States/PressurizeState.cpp +++ b/src/EReg/States/PressurizeState.cpp @@ -60,6 +60,10 @@ namespace StateMachine { //send data to AC if (TimeUtil::timeInterval(lastPrint_, micros()) > Config::telemetryInterval) { Packets::sendTelemetry( + filteredUpstreamPT1, + filteredUpstreamPT2, + filteredDownstreamPT1, + filteredDownstreamPT2, rawUpstreamPT1, rawUpstreamPT2, rawDownstreamPT1, diff --git a/src/EspComms.cpp b/src/EspComms.cpp index e345c26..6a11e98 100644 --- a/src/EspComms.cpp +++ b/src/EspComms.cpp @@ -109,22 +109,75 @@ namespace Comms { } if (Serial.available()) - { - //That was for reading full formed packets from the USB serial port - /* - int cnt = 0; - while (Serial.available() && cnt < sizeof(Packet)) { - packetBuffer[cnt] = Serial.read(); - cnt++; + //That was for reading full formed packets from the USB serial port + /* + int cnt = 0; + while (Serial.available() && cnt < sizeof(Packet)) + { + packetBuffer[cnt] = Serial.read(); + cnt++; + } + Packet *packet = (Packet *)&packetBuffer; + // DEBUG("Got unverified packet with ID "); + // DEBUG(packet->id); + // DEBUG('\n'); + evokeCallbackFunction(packet, 255); // 255 signifies a USB packet + */ + + //Instead I want to read commands in the form of "id data" + //And then make the packet and trigger the callback + + Serial.println("Got a command"); + uint8_t id = (uint8_t)Serial.parseInt(); + Serial.print("id" + String(id)); + if (id == -1) return; + Packet packet = {.id = id, .len = 0}; + while(Serial.available()){ + if (Serial.peek() == ' ') Serial.read(); + if (Serial.peek() == '\n') {Serial.read(); break;} + //determine datatype of next value + if (Serial.peek() == 'f'){ + Serial.read(); + float val = Serial.parseFloat(); + Serial.print(" float" + String(val)); + packetAddFloat(&packet, val); + } + else if (Serial.peek() == 'i'){ + Serial.read(); + int val = Serial.parseInt(); + Serial.print(" int" + String(val)); + packetAddUint32(&packet, val); + } + else if (Serial.peek() == 's'){ + Serial.read(); + int val = Serial.parseInt(); + Serial.print(" short" + String(val)); + packetAddUint16(&packet, val); + } + else if (Serial.peek() == 'b'){ + Serial.read(); + int val = Serial.parseInt(); + Serial.print(" byte" + String(val)); + packetAddUint8(&packet, val); + } else{ + Serial.read(); + } + } + Serial.println(); + // add timestamp to struct + uint32_t timestamp = millis(); + packet.timestamp[0] = timestamp & 0xFF; + packet.timestamp[1] = (timestamp >> 8) & 0xFF; + packet.timestamp[2] = (timestamp >> 16) & 0xFF; + packet.timestamp[3] = (timestamp >> 24) & 0xFF; + + // calculate and append checksum to struct + uint16_t checksum = computePacketChecksum(&packet); + packet.checksum[0] = checksum & 0xFF; + packet.checksum[1] = checksum >> 8; + evokeCallbackFunction(&packet, 255); // 255 signifies a USB packet } - Packet *packet = (Packet *)&packetBuffer; - // DEBUG("Got unverified packet with ID "); - // DEBUG(packet->id); - // DEBUG('\n'); - evokeCallbackFunction(packet, 255); // 255 signifies a USB packet - */ - } } void packetAddFloat(Packet *packet, float value) diff --git a/src/PTBoard/Ducers.cpp b/src/PTBoard/Ducers.cpp index 95c672c..71845ac 100644 --- a/src/PTBoard/Ducers.cpp +++ b/src/PTBoard/Ducers.cpp @@ -12,7 +12,8 @@ namespace Ducers { Comms::Packet ptPacket = {.id = 2}; float data[8]; float offset[8]; - bool persistentOffset = true; + float multiplier[8]; + bool persistentCal = true; // float pressurantPTValue = 0.0; @@ -45,19 +46,36 @@ namespace Ducers { void zeroChannel(uint8_t channel){ offset[channel] = -data[channel] + offset[channel]; Serial.println("zeroed channel " + String(channel) + " to " + String(offset[channel])); - if (persistentOffset){ - EEPROM.begin(8*sizeof(float)); + if (persistentCal){ + EEPROM.begin(16*sizeof(float)); EEPROM.put(channel*sizeof(float),offset[channel]); EEPROM.end(); } } + void calChannel(uint8_t channel, float value){ + multiplier[channel] *= value / data[channel]; + Serial.println("calibrated channel multiplier" + String(channel) + " to " + String(multiplier[channel])); + if (persistentCal){ + EEPROM.begin(16*sizeof(float)); + EEPROM.put((channel+8)*sizeof(float),multiplier[channel]); + EEPROM.end(); + } + } + void onZeroCommand(Comms::Packet packet, uint8_t ip){ uint8_t channel = Comms::packetGetUint8(&packet, 0); zeroChannel(channel); return; } + void onCalCommand(Comms::Packet packet, uint8_t ip){ + uint8_t channel = Comms::packetGetUint8(&packet, 0); + float value = Comms::packetGetFloat(&packet, 2); + calChannel(channel, value); + return; + } + void init() { // Comms::registerCallback(140, handleFastReadPacket); spi2 = new SPIClass(HSPI); @@ -68,21 +86,32 @@ namespace Ducers { adc1.enableOTFMode(); Comms::registerCallback(100, onZeroCommand); + Comms::registerCallback(101, onCalCommand); //load offset from flash or set to 0 - if (persistentOffset){ - EEPROM.begin(8*sizeof(float)); + if (persistentCal){ + EEPROM.begin(16*sizeof(float)); for (int i = 0; i < 8; i++){ EEPROM.get(i*sizeof(float),offset[i]); if (isnan(offset[i])){ offset[i] = 0; } } + for (int i = 0; i < 8; i++){ + EEPROM.get((i+8)*sizeof(float),multiplier[i]); + if (isnan(multiplier[i])){ + multiplier[i] = 1; + } + } EEPROM.end(); } else { for (int i = 0; i < 8; i++){ offset[i] = 0; } + for (int i = 0; i < 8; i++){ + multiplier[i] = 1; + } + } } @@ -90,7 +119,7 @@ namespace Ducers { float samplePT(uint8_t channel) { adc1.setChannel(channel); - data[channel] = interpolate1000(adc1.readChannelOTF(channel)) + offset[channel]; + data[channel] = multiplier[channel] * (interpolate1000(adc1.readChannelOTF(channel)) + offset[channel]); return data[channel]; } @@ -102,14 +131,14 @@ namespace Ducers { // read from all 8 PTs in sequence adc1.setChannel(0); // switch mux back to channel 0 - data[0] = interpolate1000(adc1.readChannelOTF(1)) + offset[0]; - data[1] = interpolate1000(adc1.readChannelOTF(2)) + offset[1]; - data[2] = interpolate1000(adc1.readChannelOTF(3)) + offset[2]; - data[3] = interpolate1000(adc1.readChannelOTF(4)) + offset[3]; - data[4] = interpolate1000(adc1.readChannelOTF(5)) + offset[4]; - data[5] = interpolate1000(adc1.readChannelOTF(6)) + offset[5]; - data[6] = interpolate1000(adc1.readChannelOTF(7)) + offset[6]; - data[7] = interpolate1000(adc1.readChannelOTF(0)) + offset[7]; + data[0] = multiplier[0] * (interpolate1000(adc1.readChannelOTF(1)) + offset[0]); + data[1] = multiplier[1] * (interpolate1000(adc1.readChannelOTF(2)) + offset[1]); + data[2] = multiplier[2] * (interpolate1000(adc1.readChannelOTF(3)) + offset[2]); + data[3] = multiplier[3] * (interpolate1000(adc1.readChannelOTF(4)) + offset[3]); + data[4] = multiplier[4] * (interpolate1000(adc1.readChannelOTF(5)) + offset[4]); + data[5] = multiplier[5] * (interpolate1000(adc1.readChannelOTF(6)) + offset[5]); + data[6] = multiplier[6] * (interpolate1000(adc1.readChannelOTF(7)) + offset[6]); + data[7] = multiplier[7] * (interpolate1000(adc1.readChannelOTF(0)) + offset[7]); DEBUG("Read all PTs\n"); DEBUG_FLUSH();