From 4f3c386d1d733873fd13d9762093792c463e8307 Mon Sep 17 00:00:00 2001
From: Cyril Danilevski <cyril.danilevski@xfel.eu>
Date: Fri, 14 Feb 2025 18:19:05 +0100
Subject: [PATCH] Add channel target voltage set in power procedure

---
 mpod.cpp      | 40 +++++++++++++++++++++++++++++++++++++---
 mpod.hpp      |  6 ++++--
 powerproc.cpp | 49 ++++++++++++++++++++++++++++++++++++-------------
 powerproc.hpp |  8 +++++++-
 rest.cpp      |  2 +-
 5 files changed, 85 insertions(+), 20 deletions(-)

diff --git a/mpod.cpp b/mpod.cpp
index 04c602f..61940bb 100644
--- a/mpod.cpp
+++ b/mpod.cpp
@@ -35,7 +35,7 @@ SNMP::Message *MPOD::read(uint16_t channel) {
 }
 
 // Create an SNMP SETREQUEST message to switch on or off the specified channel
-SNMP::Message *MPOD::output(const uint16_t channel, const bool on) {
+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 +45,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;
@@ -194,9 +205,9 @@ void initializeSNMP() {
 /* 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 +224,26 @@ 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());  // TODO check applied voltage close to target voltage
+}
diff --git a/mpod.hpp b/mpod.hpp
index 03c4a4c..639d993 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/powerproc.cpp b/powerproc.cpp
index e74432a..3264c4c 100644
--- a/powerproc.cpp
+++ b/powerproc.cpp
@@ -6,19 +6,25 @@ PowerProcedure::PowerProcedure(const IPAddress& ipAddr)
     : ipAddr(ipAddr), stagesCount(STAGESCOUNT), currentStage(""), currentChannel(-1) {
     stages = new Stage[stagesCount];
 
-    stages[0].name = "AON";
-    stages[0].size = 4;
-    stages[0].channels = new int[stages[0].size]{604, 705, 706, 707};
-
-    stages[1].name = "ASICS";
-    stages[1].size = 9;
-    stages[1].channels = new int[stages[1].size]{508, 708, 502, 503, 504, 501, 507, 607, 608};
-
-    stages[2].name = "HV";
-    stages[2].size = 10;
-    stages[2].channels = new int[stages[2].size]{1, 2, 101, 103, 102, 104, 105, 106, 107, 108};
+    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 = "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};
 }
@@ -26,6 +32,10 @@ PowerProcedure::PowerProcedure(const IPAddress& ipAddr)
 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;
 }
@@ -37,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";
@@ -71,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;
         }
@@ -84,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 5f0236f..979df46 100644
--- a/powerproc.hpp
+++ b/powerproc.hpp
@@ -3,15 +3,21 @@
 
 #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 8a5a577..61af9a2 100644
--- a/rest.cpp
+++ b/rest.cpp
@@ -160,7 +160,7 @@ void sendSNMP() {
     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
     }
 
-- 
GitLab