From b34162d2e9f14223dafc78c98d0aefeb179e0cd3 Mon Sep 17 00:00:00 2001
From: Karol Hennessy <revkarol@gmail.com>
Date: Mon, 13 Apr 2020 18:55:49 +0200
Subject: [PATCH] working comms with fsm loop

---
 arduino/breathing_with_comms/src/main.cpp     | 22 +++++++++----------
 .../breathing_with_comms/src/test_hw_loop.cpp |  9 ++++++--
 .../breathing_with_comms/src/test_hw_loop.h   |  1 +
 .../common/lib/commsControl/commsControl.cpp  |  9 +++-----
 .../common/lib/commsControl/commsControl.h    |  2 +-
 raspberry-dataserver/commsDebug.py            | 18 ++++++++-------
 6 files changed, 32 insertions(+), 29 deletions(-)

diff --git a/arduino/breathing_with_comms/src/main.cpp b/arduino/breathing_with_comms/src/main.cpp
index fe33f6f2..3cb00e08 100644
--- a/arduino/breathing_with_comms/src/main.cpp
+++ b/arduino/breathing_with_comms/src/main.cpp
@@ -6,11 +6,11 @@
 
 int ventilation_mode = HEV_MODE_PS;
 
-const uint16_t report_freq = 1 ; // in Hz
+const uint16_t report_freq = 5 ; // in Hz
 const uint16_t update_freq = 100 ; // in Hz
 
 uint16_t report_cnt = 0;
-
+ 
 float working_pressure = 1;             //?
 float inspiratory_minute_volume = 6000; // ml/min
 float respiratory_rate = 15;            //  10-40 +-1 ;aka breaths_per_min
@@ -97,7 +97,6 @@ void setup()
 
 }
 
-int buga, bugb, bugc  = 0;
 void loop()
 {
     // buzzer
@@ -112,9 +111,9 @@ void loop()
     getValves(vin_air, vin_o2, vinhale, vexhale, vpurge, vatmos);
     data.readback_valve_air_in = vin_air;
     data.readback_valve_o2_in = vin_o2;
-    data.readback_valve_inhale = buga; //vinhale;
-    data.readback_valve_exhale = bugb; //vexhale;
-    data.readback_valve_purge = bugc;  //vpurge;
+    data.readback_valve_inhale = vinhale;
+    data.readback_valve_exhale = vexhale;
+    data.readback_valve_purge = vpurge;
     // data.pressure_o2_supply = freeMemory() & 0xFFFF;
     // data.pressure_o2_regulated = freeMemory() >> 16;
     // TODO ; add to dataFormat
@@ -124,17 +123,17 @@ void loop()
     FSM_breath_cycle();
 
     report_cnt++;
-    // if(report_cnt % (update_freq/report_freq) == 0)
-    // {
+    if(report_cnt % (update_freq/report_freq) == 0)
+    {
         plSend.setType(payloadType::payloadData);
         plSend.setData(&data);
         comms.writePayload(plSend);
-    // }
+    }
     // per cycle sender
     comms.sender();
     // per cycle receiver
     
-    comms.receiver(buga, bugb, bugc);
+    comms.receiver();
 
     uint8_t cmdCode = 0;
     if(comms.readPayload(plReceive)){
@@ -142,7 +141,6 @@ void loop()
           cmdCode = (plReceive.getCmd()->cmdCode);
           plReceive.setType(payloadType::payloadUnset);
       }
-
     }
 
     switch(cmdCode){
@@ -153,5 +151,5 @@ void loop()
         default:
         break;
     }
-    delay(1000);
+    delay(1000/update_freq);
 }
diff --git a/arduino/breathing_with_comms/src/test_hw_loop.cpp b/arduino/breathing_with_comms/src/test_hw_loop.cpp
index 14597919..dbe08358 100644
--- a/arduino/breathing_with_comms/src/test_hw_loop.cpp
+++ b/arduino/breathing_with_comms/src/test_hw_loop.cpp
@@ -27,8 +27,8 @@ void FSM_assignment( ) {
         {
         case BS_IDLE:
             if (running == true) {
+                // FSM_time = millis();
                 next_state = BS_BUFF_PREFILL;
-                FSM_time = millis();
                 // Serial.println("Exit IDLE") ;
             } else {
                 next_state = BS_IDLE;
@@ -100,7 +100,7 @@ void FSM_breath_cycle()
         case BS_IDLE:
             
             if (running == true) {
-                FSM_time = millis();
+                // FSM_time = millis();
             } else {
                 timeout = 1000; 
             }
@@ -177,4 +177,9 @@ void do_start()
 void do_stop()
 {
     running = false;
+}
+
+bool get_running()
+{
+    return running;
 }
\ No newline at end of file
diff --git a/arduino/breathing_with_comms/src/test_hw_loop.h b/arduino/breathing_with_comms/src/test_hw_loop.h
index 80ea6e76..2aa1c0e3 100644
--- a/arduino/breathing_with_comms/src/test_hw_loop.h
+++ b/arduino/breathing_with_comms/src/test_hw_loop.h
@@ -6,6 +6,7 @@ void FSM_assignment();
 void FSM_breath_cycle();
 void do_start();
 void do_stop();
+bool get_running();
 
 // states
 enum BS_STATES : byte
diff --git a/arduino/common/lib/commsControl/commsControl.cpp b/arduino/common/lib/commsControl/commsControl.cpp
index d904363a..091f93a8 100644
--- a/arduino/common/lib/commsControl/commsControl.cpp
+++ b/arduino/common/lib/commsControl/commsControl.cpp
@@ -57,7 +57,7 @@ void commsControl::sender() {
 
 // main function to always try to receive data
 // TODO: needs switch on data type with global timeouts on data pushing
-void commsControl::receiver(int &buga, int &bugb, int &bugc) {
+void commsControl::receiver() {
     uint8_t currentTransIndex;
 
     // check if any data in waiting
@@ -96,17 +96,14 @@ void commsControl::receiver(int &buga, int &bugb, int &bugc) {
                                     // received NACK
                                     // TODO: modify timeout for next sent frame?
                                     // resendPacket(&address);
-                                    buga++;
                                     break;
                                 case COMMS_CONTROL_ACK:
                                     // received ACK
                                     finishPacket(type);
-                                    bugb++;
                                     break;
                                 default:
                                     uint8_t tmpSequenceReceive = (control >> 1 ) & 0x7F;
                                     tmpSequenceReceive += 1;
-                                    bugc++;
                                     // received DATA
                                     if (receivePacket(type)) {
                                         commsAck_->setAddress(commsTmp_.getAddress());
@@ -135,7 +132,7 @@ void commsControl::receiver(int &buga, int &bugb, int &bugc) {
                 lastTransIndex_ = 0;
             }
         }
-    }
+    } 
 }
 
 bool commsControl::writePayload(payload &pl) {
@@ -249,7 +246,7 @@ void commsControl::sendPacket(commsFormat *packet) {
     if (encoder(packet->getData(), packet->getSize()) ) {
         if (Serial.availableForWrite() >= commsSendSize_) {
             Serial.write(commsSend_, commsSendSize_);
-        }
+        } 
     }
 }
 
diff --git a/arduino/common/lib/commsControl/commsControl.h b/arduino/common/lib/commsControl/commsControl.h
index 155641d3..21dc3d55 100644
--- a/arduino/common/lib/commsControl/commsControl.h
+++ b/arduino/common/lib/commsControl/commsControl.h
@@ -23,7 +23,7 @@ public:
     bool readPayload (payload &pl);
 
     void sender();
-    void receiver(int&, int&, int&);
+    void receiver();
 
 private:
     RingBuf<commsFormat *,CONST_MAX_SIZE_RB_SENDING> *getQueue(payloadType &type);
diff --git a/raspberry-dataserver/commsDebug.py b/raspberry-dataserver/commsDebug.py
index 22d17ed7..3d7553bb 100755
--- a/raspberry-dataserver/commsDebug.py
+++ b/raspberry-dataserver/commsDebug.py
@@ -16,8 +16,9 @@ class Dependant(object):
         self._lli.bind_to(self.update_llipacket)
 
     def update_llipacket(self, payload):
-        # logging.info(f"payload received: {payload}")
-        logging.info(f"payload received: {payload._readback_valve_inhale} {payload._readback_valve_exhale} {payload._readback_valve_purge} {payload._fsm_state}")
+        logging.info(f"payload received: {payload}")
+        #logging.info(f"payload received: {payload._fsm_state}")
+        #logging.info(f"payload received: {payload._readback_valve_o2_in} {payload._readback_valve_inhale} {payload._readback_valve_exhale} {payload._readback_valve_purge} {payload._fsm_state}")
         self._llipacket = payload.getDict() # returns a dict
         # pop from queue - protects against Dependant going down and not receiving packets
         self._lli.pop_payloadrecv()
@@ -27,17 +28,18 @@ start = 0x1
 stop =  0x2
 
 cmd = commandFormat()
-cmd.cmdCode = start
+cmd._cmdCode = start
 cmd.toByteArray()
 
 time.sleep(4)
 comms.writePayload(cmd)
 #comms.sender()
+print('sent cmd start')
 while True:
-    # time.sleep(1)
-    # cmd.cmdCode = stop
-    # cmd.toByteArray()
-    # comms.writePayload(cmd)
-    #comms.registerData(stop)
+    time.sleep(20)
+    cmd._cmdCode = stop
+    cmd.toByteArray()
+    comms.writePayload(cmd)
+    print('sent cmd stop')
     pass
 
-- 
GitLab