diff --git a/arduino/hev_prototype_v1/src/BreathingLoop.cpp b/arduino/hev_prototype_v1/src/BreathingLoop.cpp index 8e54d070dddc091f402c8fbdcb3430e697ddb77b..9172cea1e811872f71812aed0934ec1336a0dabb 100644 --- a/arduino/hev_prototype_v1/src/BreathingLoop.cpp +++ b/arduino/hev_prototype_v1/src/BreathingLoop.cpp @@ -115,8 +115,15 @@ float BreathingLoop::getFlow(){ if ((dp_raw -0.5) == 0.000) sign = 0.0; float dp = sign*pow(((dp_raw/0.4)-1.25),2) * 5.25; + // float flow = dp; - return dp; + + + // NLPM - normal litres per minute = 1 Si Litre per minute * (293.15/T)*(P/1013.25) + float T = 25+273.15; //_readings_avgs.temperature_buffer; + float P = _readings_avgs.pressure_patient; + float nlpm_factor = (293.15/T)*(P/1013.25); + return flow; /* float R = 0.08206 * 1/0.98692 *1000; // mbar *l * mol-1 *K-1 float T = 25+273.15; //_readings_avgs.temperature_buffer; @@ -127,7 +134,7 @@ float BreathingLoop::getFlow(){ float n1 = _readings_avgs.pressure_buffer * V_buffer/(R*T); float n2 = _readings_avgs.pressure_inhale * V_tube/(R*T); float M = 15.99; // molar mass O2 g.mol-1 - float rho = 1.42 * 1000; // density ) 2kg/m3 @ 25 deg + float rho = 1.42 * 1000; // density 1.2kg/m3 @ 25 deg */ } @@ -135,7 +142,7 @@ float BreathingLoop::getFlow(){ float BreathingLoop::getIERatio(){ // TODO : check with Oscar/Xavier float total_inhale_time = _states_durations.inhale + _states_durations.pause; - float total_exhale_time = _states_durations.exhale_fill + _states_durations.exhale_fill; + float total_exhale_time = _states_durations.exhale_fill + _states_durations.exhale; return total_inhale_time/total_exhale_time; } @@ -339,8 +346,8 @@ void BreathingLoop::FSM_breathCycle() break; case BL_STATES::EXHALE: // TODO: exhale timeout based on - // (inhale_time* (Exhale/Inhale ratio)) - fill time - _states_durations.exhale = calculateTimeoutExhale(); + // (inhale_time* (Exhale/Inhale ratio)) - exhale fill time + _states_durations.exhale = calculateDurationExhale(); _valves_controller.setValves(VALVE_STATE::CLOSED, VALVE_STATE::CLOSED, VALVE_STATE::CLOSED, 0.9 * VALVE_STATE::OPEN, VALVE_STATE::CLOSED); _fsm_timeout = _states_durations.exhale; //update total cycle time @@ -430,9 +437,13 @@ states_durations &BreathingLoop::getDurations() { return _states_durations; } -// FIXME 1/1 has to be replaced using exhale/inhale ratio -uint32_t BreathingLoop::calculateTimeoutExhale() { - return static_cast<uint32_t>(_states_durations.inhale * ( 1/ 1) ) - _states_durations.buff_fill; +uint32_t BreathingLoop::calculateDurationExhale() { + // TODO : should have sane minimum times + // for now min = 100ms + uint32_t exhale_duration = (_states_durations.inhale * getIERatio()) - _states_durations.exhale_fill ; + if (exhale_duration < 100) + exhale_duration = 100; + return static_cast<uint32_t>(exhale_duration); } ValvesController* BreathingLoop::getValvesController() diff --git a/arduino/hev_prototype_v1/src/BreathingLoop.h b/arduino/hev_prototype_v1/src/BreathingLoop.h index aec23130edeaa109f351f4434eb6791d25cd0b5b..c97cbbdbb4bd1dd9351cca57bbbf639fd1cd2f96 100644 --- a/arduino/hev_prototype_v1/src/BreathingLoop.h +++ b/arduino/hev_prototype_v1/src/BreathingLoop.h @@ -88,7 +88,7 @@ private: readings<uint16_t> _calib_avgs; // timeouts - uint32_t calculateTimeoutExhale(); + uint32_t calculateDurationExhale(); states_durations _states_durations = {10000, 600, 600, 100, 600, 100, 100, 1000, 500, 600, 400}; // readings diff --git a/arduino/hev_prototype_v1/src/common.h b/arduino/hev_prototype_v1/src/common.h index d3e82014d64208008a11d1665cbf74100359625f..a533973527339f43f4bb66927bed17ade2b854f6 100644 --- a/arduino/hev_prototype_v1/src/common.h +++ b/arduino/hev_prototype_v1/src/common.h @@ -178,7 +178,7 @@ struct readback_data_format { uint8_t valve_inhale = 0; uint8_t valve_exhale = 0; uint8_t valve_purge = 0; - uint8_t ventilation_mode = 0; + uint8_t ventilation_mode = CMD_SET_MODE::HEV_MODE_PS; uint8_t valve_inhale_percent = 0; // replaced by a min level and a max level; bias inhale level. very slightly open at "closed" position uint8_t valve_exhale_percent = 0; diff --git a/arduino/hev_prototype_v1/src/main.cpp b/arduino/hev_prototype_v1/src/main.cpp index d9dd0633e7ede0aa7813b40f6d4504e031c42d84..ed9112d8ac1ff0462a128f00037861dd88acbd7e 100644 --- a/arduino/hev_prototype_v1/src/main.cpp +++ b/arduino/hev_prototype_v1/src/main.cpp @@ -106,8 +106,8 @@ void loop() breathing_loop.FSM_breathCycle(); ui_loop.reportFastReadings(); - // ui_loop.reportReadbackValues(); - // ui_loop.reportCycleReadings(); + ui_loop.reportReadbackValues(); + ui_loop.reportCycleReadings(); // per cycle sender comms.sender(); diff --git a/raspberry-dataserver/CommsCommon.py b/raspberry-dataserver/CommsCommon.py index 0446c1adc421a7280b19361b0f9aa270db7a6fc9..38481c7fb1cdce4d23555d80081163b75ed9b903 100644 --- a/raspberry-dataserver/CommsCommon.py +++ b/raspberry-dataserver/CommsCommon.py @@ -46,10 +46,10 @@ class CMD_SET_TIMEOUT(Enum): EXHALE = 11 class CMD_SET_MODE(Enum): - HEV_MODE_PS = auto() - HEV_MODE_CPAP = auto() - HEV_MODE_PRVC = auto() - HEV_MODE_TEST = auto() + HEV_MODE_PS = 1 + HEV_MODE_CPAP = 2 + HEV_MODE_PRVC = 3 + HEV_MODE_TEST = 4 @unique class ALARM_TYPE(Enum): @@ -192,7 +192,7 @@ class BaseFormat(): @dataclass class DataFormat(BaseFormat): # subclass dataformat - _dataStruct = Struct("<BIBBHHHHHHHHHHHIII") + _dataStruct = Struct("<BIBBHHHHHHHHHHHfff") _type = PAYLOAD_TYPE.DATA # subclass member variables @@ -251,7 +251,7 @@ class DataFormat(BaseFormat): # ======================================= @dataclass class ReadbackFormat(BaseFormat): - _dataStruct = Struct("<BIBHHHHHHHHHHHBBBBBBBBBBBBBBI") + _dataStruct = Struct("<BIBHHHHHHHHHHHBBBBBBBBBBBBBBf") _type = PAYLOAD_TYPE.DATA data_type: int = DATA_TYPE.READBACK @@ -272,7 +272,7 @@ class ReadbackFormat(BaseFormat): valve_inhale: int = 0 valve_exhale: int = 0 valve_purge: int = 0 - ventilation_mode: int = 0 + ventilation_mode: int = 0 #CMD_SET_MODE.HEV_MODE_PS valve_inhale_percent: int = 0 valve_exhale_percent: int = 0 @@ -327,7 +327,7 @@ class ReadbackFormat(BaseFormat): @dataclass class CycleFormat(BaseFormat): # subclass dataformat - _dataStruct = Struct("<BIBIIIIIIIIIHHHHBHHB") + _dataStruct = Struct("<BIBfffffffffHHHHBHHB") _type = PAYLOAD_TYPE.DATA data_type: int = DATA_TYPE.CYCLE diff --git a/raspberry-dataserver/CommsControl.py b/raspberry-dataserver/CommsControl.py index 2d22a926e38090272b53df1078aa71fd873240c0..214f2f7827df4d7c9639bc903890fd0c83707fc0 100644 --- a/raspberry-dataserver/CommsControl.py +++ b/raspberry-dataserver/CommsControl.py @@ -232,7 +232,7 @@ class CommsControl(): payload = CommsCommon.ReadbackFormat() elif data_type == CommsCommon.DATA_TYPE.CYCLE: payload = CommsCommon.CycleFormat() - elif data_type == CommsCommon.DATA_TYPE.THRESHOLD: + elif data_type == CommsCommon.DATA_TYPE.THRESHOLDS: # FIXME: nothing yet defined, TBD!! return False else: diff --git a/raspberry-dataserver/CommsDebug.py b/raspberry-dataserver/CommsDebug.py index 1070b46c262f62201209565e70db0e1d3ae24aaf..790481023a53e2f2ef2b5e1a4eb004d2632b973c 100755 --- a/raspberry-dataserver/CommsDebug.py +++ b/raspberry-dataserver/CommsDebug.py @@ -16,8 +16,8 @@ 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.fsm_state}") + logging.info(f"payload received: {payload}") + #logging.info(f"payload received: {payload.fsm_state}") #logging.info(f"payload received: {payload.timestamp}") #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 @@ -28,7 +28,6 @@ dep = Dependant(comms) # initialise as start command, automatically executes toByteArray() cmd = CommandFormat(cmd_type=CMD_TYPE.GENERAL.value, cmd_code=CMD_GENERAL.START.value, param=0) -print(cmd) time.sleep(4) comms.writePayload(cmd) print('sent cmd start')