diff --git a/icbm.ino b/icbm.ino index 4b1580e5f3de40d1586a04229b7900ef9ee8cf0e..c43b3cf30726d99a9c137b62a6b96d70b63a1de3 100644 --- a/icbm.ino +++ b/icbm.ino @@ -18,6 +18,8 @@ void setup() { start = millis(); poll_port_expander(); + PINS.stage = ""; + PINS.ramping = false; } enum { diff --git a/mpod.cpp b/mpod.cpp index ff68d800014a299bcabc7be7f7862232ba6ca827..86030b73e0f2e4ab59f7c7ec25c4d00c5e58c31c 100644 --- a/mpod.cpp +++ b/mpod.cpp @@ -23,6 +23,10 @@ SNMP::Message *MPOD::read(uint16_t channel) { snmp_cmd += channel; message->add(snmp_cmd.c_str()); + snmp_cmd = OID::NAMES[OID::OUTPUTVOLTAGE]; + snmp_cmd += channel; + message->add(snmp_cmd.c_str()); + snmp_cmd = OID::NAMES[OID::OUTPUTMEASUREMENTSENSEVOLTAGE]; snmp_cmd += channel; message->add(snmp_cmd.c_str()); @@ -31,11 +35,15 @@ SNMP::Message *MPOD::read(uint16_t channel) { snmp_cmd += channel; message->add(snmp_cmd.c_str()); + snmp_cmd = OID::NAMES[OID::OUTPUTCURRENT]; + snmp_cmd += channel; + message->add(snmp_cmd.c_str()); + return message; } -// Create an SNMP SETREQUEST message to switch on or off the MPOD -SNMP::Message *MPOD::output(const uint16_t channel, const bool on) { +// Create an SNMP SETREQUEST message to switch on or off the specified channel +SNMP::Message *MPOD::setChannelState(const uint16_t channel, const bool on) { SNMP::Message *message = new SNMP::Message(SNMP::Version::V2C, "guru", SNMP::Type::SetRequest); // In SETREQUEST, use node type and set the value. // OUTPUT SWITCH, integer type, 0 is OFF and 1 is ON. @@ -45,6 +53,17 @@ SNMP::Message *MPOD::output(const uint16_t channel, const bool on) { return message; } +// Create an SNMP SETREQUEST message to set the channel voltage +SNMP::Message *MPOD::setTargetVoltage(const uint16_t channel, const float targetVoltage) { + SNMP::Message *message = new SNMP::Message(SNMP::Version::V2C, "guru", SNMP::Type::SetRequest); + // In SETREQUEST, use node type and set the value. + // OUTPUT VOLTAGE, float type. + String snmp_cmd = OID::NAMES[OID::OUTPUTVOLTAGE]; + snmp_cmd += channel; + message->add(snmp_cmd.c_str(), new OpaqueBER(new OpaqueFloatBER(targetVoltage))); + return message; +} + // Parse incoming message bool MPOD::message(const SNMP::Message *message) { unsigned int found = 0; @@ -191,12 +210,9 @@ void initializeSNMP() { Serial.println("SNMP Server Started"); } -/* setChannelAndWait - * Set a channel's parameters and wait until it's settled. - */ -void setChannelAndWait(const IPAddress *ipAddr, const uint16_t channel, const uint8_t output) { +void setChannelStateAndWait(const IPAddress *ipAddr, const uint16_t channel, const uint8_t output) { // Send set command - SNMP::Message *snmp_msg = mpod.output(channel, output); + SNMP::Message *snmp_msg = mpod.setChannelState(channel, output); snmp.send(snmp_msg, *ipAddr, SNMP::Port::SNMP); delete snmp_msg; @@ -213,3 +229,25 @@ void setChannelAndWait(const IPAddress *ipAddr, const uint16_t channel, const ui snmp.loop(); } while (mpod.isOn() != (bool)output || mpod.isRampingDown() || mpod.isRampingUp()); } + +void setChannelVoltageAndWait(const IPAddress *ipAddr, const uint16_t channel, + const float targetVoltage) { + // Send set command + SNMP::Message *snmp_msg = mpod.setTargetVoltage(channel, targetVoltage); + + snmp.send(snmp_msg, *ipAddr, SNMP::Port::SNMP); + delete snmp_msg; + + // wait for channel reply + delay(MPOD_UPDATE_LATENCY); + snmp.loop(); + + // Poll channel until it's settled + do { + SNMP::Message *snmp_msg = mpod.read(channel); + snmp.send(snmp_msg, *ipAddr, SNMP::Port::SNMP); + delete snmp_msg; + delay(MPOD_UPDATE_LATENCY); + snmp.loop(); + } while (mpod.isRampingDown() || mpod.isRampingUp()); +} diff --git a/mpod.hpp b/mpod.hpp index 03c4a4c7355579c8c1924009ad991c86e073ece3..639d993f1525f2fdba331cfe683ec71d68980ccb 100644 --- a/mpod.hpp +++ b/mpod.hpp @@ -62,7 +62,8 @@ class MPOD { // Create an SNMP SETREQUEST message to setup MPOD MPOD(); SNMP::Message* read(const uint16_t); - SNMP::Message* output(const uint16_t, const bool); + SNMP::Message* setChannelState(const uint16_t, const bool); + SNMP::Message* setTargetVoltage(const uint16_t, const float); bool message(const SNMP::Message*); bool isOn() const; bool isRampingUp() const; @@ -95,5 +96,6 @@ extern MPOD mpod; // Event handler to process SNMP messages void onSNMPMessage(const SNMP::Message *message, const IPAddress remote, const uint16_t port); void initializeSNMP(); -void setChannelAndWait(const IPAddress*, uint16_t, uint8_t); +void setChannelStateAndWait(const IPAddress*, const uint16_t, const uint8_t); +void setChannelVoltageAndWait(const IPAddress*, const uint16_t, const float); diff --git a/panel.hpp b/panel.hpp index 1d62bda2e6977738d0995dba0b0b9bdf9fd26d9e..884ba68866b83048f1974c05b4d4f180a06b4507 100644 --- a/panel.hpp +++ b/panel.hpp @@ -1,4 +1,4 @@ -String buildPanel(bool sib, bool plc, bool ups, unsigned long elapsed_secs) { +String buildPanel(const bool sib, const bool plc, const bool ups, const unsigned long elapsed_secs, const String& status, const bool ramping) { // Shows OK if not triggered (ie bool == false) String page = "<!DOCTYPE html><html lang=\"en\">\n"; page += "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n"; @@ -38,8 +38,19 @@ String buildPanel(bool sib, bool plc, bool ups, unsigned long elapsed_secs) { page += "<h3>Last Interrupt: "; page += elapsed_secs; - page += " secs. ago"; - page += "</h3></div></body></html>"; + page += " secs. ago<h3>"; + + page += "<h3>Status: "; + page += status; + page += "</h3>"; + + page += "<div class=\"status-item\">\n"; + page += "<span class=\"status-label\">Ramping:</span>\n"; + if (ramping) { page += "<span class=\"status-value status-error\">Powering Down</span>\n"; } + else { page += "<span class=\"status-value status-ok\">Idle</span>\n"; } + page += "</div>\n"; + + page += "</div></body></html>"; return page; } diff --git a/pins.hpp b/pins.hpp index e07afbf9985fb92c71915765d66f94c4c97388b1..0cd10be8355680ed00f69e82155f5bf4df5fe46f 100644 --- a/pins.hpp +++ b/pins.hpp @@ -21,6 +21,8 @@ struct pins { bool plc; bool ups; int last_triggered; + String stage; + bool ramping; }; extern struct pins PINS; diff --git a/powerproc.cpp b/powerproc.cpp index 51d1ab6ee5d4ff3c1a44bc88335875abf91a6aa2..3264c4ca5ef38ebbb8b964700aa70463e3f596c7 100644 --- a/powerproc.cpp +++ b/powerproc.cpp @@ -7,21 +7,35 @@ PowerProcedure::PowerProcedure(const IPAddress& ipAddr) stages = new Stage[stagesCount]; stages[0].name = "ASICS"; + stages[0].type = SWITCH; stages[0].size = 9; stages[0].channels = new int[stages[0].size]{508, 708, 502, 503, 504, 501, 507, 607, 608}; stages[1].name = "HV"; + stages[1].type = SWITCH; stages[1].size = 10; stages[1].channels = new int[stages[1].size]{1, 2, 101, 103, 102, 104, 105, 106, 107, 108}; - stages[2].name = "PLCSOURCE"; - stages[2].size = 9; - stages[2].channels = new int[stages[2].size]{701, 801, 802, 803, 804, 301, 302, 303, 304}; + stages[2].name = "ISVOLTAGE"; + stages[2].type = VOLTAGE; + stages[2].size = 2; + stages[2].channels = new int[stages[2].size]{1, 2}; + stages[2].onValue = new float[stages[2].size]{10, 10}; + stages[2].offValue = new float[stages[2].size]{2.5, 2.5}; + + stages[3].name = "PLCSOURCE"; + stages[3].type = SWITCH; + stages[3].size = 9; + stages[3].channels = new int[stages[3].size]{701, 801, 802, 803, 804, 301, 302, 303, 304}; } PowerProcedure::~PowerProcedure() { for (size_t stageIdx = 0; stageIdx < stagesCount; stageIdx++) { delete[] stages[stageIdx].channels; + if (stages[stageIdx].type == VOLTAGE) { + delete[] stages[stageIdx].onValue; + delete[] stages[stageIdx].offValue; + } } delete[] stages; } @@ -33,6 +47,9 @@ String PowerProcedure::toJSON() { for (size_t stageIdx = 0; stageIdx < stagesCount; stageIdx++) { Stage* currentStage = &stages[stageIdx]; + if (currentStage->type == VOLTAGE) { + continue; + } json += "{\"name\":\"" + currentStage->name + "\","; json += "\"channels\":[\n"; @@ -67,7 +84,12 @@ bool PowerProcedure::powerOn(const String& stage) { Stage* currentStage = &stages[stageIdx]; if (currentStage->name == stage) { for (size_t chIdx = 0; chIdx < currentStage->size; chIdx++) { - setChannelAndWait(&ipAddr, currentStage->channels[chIdx], 1); // 1 == ON + if (currentStage->type == SWITCH) { + setChannelStateAndWait(&ipAddr, currentStage->channels[chIdx], 1); // 1 == ON + } else if (currentStage->type == VOLTAGE) { + setChannelVoltageAndWait(&ipAddr, currentStage->channels[chIdx], + currentStage->onValue[chIdx]); + } } return true; } @@ -80,7 +102,12 @@ bool PowerProcedure::powerOff(const String& stage) { Stage* currentStage = &stages[stageIdx]; if (currentStage->name == stage) { for (int chIdx = currentStage->size - 1; chIdx >= 0; chIdx--) { - setChannelAndWait(&ipAddr, currentStage->channels[chIdx], 0); // 0 == OFF + if (currentStage->type == SWITCH) { + setChannelStateAndWait(&ipAddr, currentStage->channels[chIdx], 0); // 0 == OFF + } else if (currentStage->type == VOLTAGE) { + setChannelVoltageAndWait(&ipAddr, currentStage->channels[chIdx], + currentStage->offValue[chIdx]); + } } return true; } diff --git a/powerproc.hpp b/powerproc.hpp index e95939f913b862dc1ebe0280083eb171e32ac01a..979df469bee14954ac6bd1b95ccbbee18c5e62a6 100644 --- a/powerproc.hpp +++ b/powerproc.hpp @@ -1,15 +1,23 @@ #pragma once #include <Arduino.h> -#define STAGESCOUNT 3 +#include "pins.hpp" + +#define SWITCH "SWITCH" +#define VOLTAGE "VOLTAGE" +#define STAGESCOUNT 4 + class PowerProcedure { private: String currentStage; // Stores the current stage name int currentChannel; // Stores the current channel struct Stage { - String name; // Name of the stage (e.g., "ASICS") + String name; // Name of the stage (e.g. "ASICS") + String type; // Type of stage (i.e. "SWITCH", "VOLTAGE") int* channels; // Pointer to dynamically allocated channel array + float* onValue; // Pointer to dynamically allocated array, when type == VOLTAGE + float* offValue; // Pointer to dynamically allocated array, when type == VOLTAGE size_t size; // Number of channels in the stage }; diff --git a/rest.cpp b/rest.cpp index cb439c498807cd4a2a768fcbe7ccf0533e8a11db..61af9a28636a2a313a394284e57ef2763a402c7b 100644 --- a/rest.cpp +++ b/rest.cpp @@ -76,12 +76,20 @@ void identify() { unsigned long elapsed_seconds = (millis() - PINS.last_triggered) / 1000; message += "\"sec_since_interrupt\":"; message += elapsed_seconds; - message += "\n}"; + message += "\n},"; // TODO: Add here power sequence status // is_ramping_down // current step // percentage + message += "\"status\":{\n"; + message += "\"ramping\":"; + message += PINS.ramping; + message += ",\n"; + message += "\"stage\":"; + message += PINS.stage; + message += ",\n"; + message += "\n}"; message += "\n}"; @@ -90,7 +98,8 @@ void identify() { void panel() { unsigned long elapsed_seconds = (millis() - PINS.last_triggered) / 1000; - String content = buildPanel(PINS.sib, PINS.plc, PINS.ups, elapsed_seconds); + String content = + buildPanel(PINS.sib, PINS.plc, PINS.ups, elapsed_seconds, PINS.stage, PINS.ramping); restServer.send(200, "text/html", content); } @@ -148,10 +157,10 @@ void sendSNMP() { } } - auto ipAddr = IPAddress(192, 168, 140, 79); + auto ipAddr = pproc.ipAddr; if (output != NONE && channel != 0) { - setChannelAndWait(&ipAddr, channel, output); + setChannelStateAndWait(&ipAddr, channel, output); success = true; // validates input parameters, not MPOD status, reported separately } @@ -231,25 +240,27 @@ void powerAllOff() { } if (success) { - for (uint8_t groupIdx = pproc.stagesCount; groupIdx >= 0; groupIdx--) { + PINS.ramping = true; + for (uint8_t groupIdx = pproc.stagesCount - 1; groupIdx >= 0; groupIdx--) { group = pproc.stages[groupIdx].name; groups += group; groups += ","; + PINS.stage = group; success = pproc.powerOff(group); if (!success) { break; } } + PINS.ramping = false; } String http_msg = "{\n"; - http_msg += "\"arguments\":{"; http_msg += "\"groups\":\"" + groups + "\","; http_msg += "\"success\":" + String(success); - if (success) { - http_msg += ",\n\"status\":\n"; - http_msg += pproc.toJSON(); - } + // if (success) { + // http_msg += ",\n\"status\":\n"; + // http_msg += pproc.toJSON(); + // } http_msg += "\n}"; restServer.send(success ? 200 : 406, "text/json", http_msg); @@ -267,9 +278,8 @@ void pollMPODChannel() { if (!channel) { ret = "\"reason\": \"Invalid channel\""; } else { - auto ipAddr = IPAddress(192, 168, 140, 79); SNMP::Message *snmp_msg = mpod.read(channel); - snmp.send(snmp_msg, ipAddr, SNMP::Port::SNMP); + snmp.send(snmp_msg, pproc.ipAddr, SNMP::Port::SNMP); delete snmp_msg; delay(MPOD_UPDATE_LATENCY);