Skip to content
Snippets Groups Projects
Commit b34162d2 authored by Karol Hennessy's avatar Karol Hennessy
Browse files

working comms with fsm loop

parent dbc002eb
Branches
No related merge requests found
......@@ -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);
}
......@@ -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
......@@ -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
......
......@@ -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_);
}
}
}
}
......
......@@ -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);
......
......@@ -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
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment