Skip to content
Snippets Groups Projects
Commit c7c5d506 authored by Your Name's avatar Your Name
Browse files

fixes to new cycle data

parent d6c71eec
No related branches found
No related tags found
No related merge requests found
......@@ -96,7 +96,7 @@ void BreathingLoop::updateReadings()
// calc pressure every 1ms
// create averages every 10ms
uint32_t tnow = static_cast<uint32_t>(millis());
if (tnow - _readings_time > _readings_timeout) {
if (tnow - _readings_time >= _readings_timeout) {
_readings_time = tnow;
_readings_N++;
......@@ -119,7 +119,7 @@ void BreathingLoop::updateReadings()
// to make sure the readings correspond only to the same fsm mode
if (_readings_reset) {
resetReadingSums();
} else if (tnow - _readings_avgs_time > _readings_avgs_timeout) {
} else if (tnow - _readings_avgs_time >= _readings_avgs_timeout) {
_readings_avgs.timestamp = static_cast<uint32_t>(_readings_sums.timestamp);
_readings_avgs.pressure_air_supply = adcToMillibarFloat((_readings_sums.pressure_air_supply / _readings_N));
_readings_avgs.pressure_air_regulated = adcToMillibarFloat((_readings_sums.pressure_air_regulated / _readings_N));
......@@ -163,7 +163,7 @@ void BreathingLoop::updateRawReadings()
uint32_t tnow = static_cast<uint32_t>(millis());
// to make sure the readings correspond only to the same fsm mode
if (tnow - _readings_avgs_time > _readings_avgs_timeout) {
if (tnow - _readings_avgs_time >= _readings_avgs_timeout) {
_readings_raw.timestamp = static_cast<uint32_t>(_readings_sums.timestamp);
#ifdef CHIP_ESP32
_readings_raw.pressure_air_supply =analogRead(pin_pressure_air_supply) ;
......@@ -232,7 +232,7 @@ void BreathingLoop::updateCycleReadings()
_cycle_readings.static_compliance = _cycle_readings.tidal_volume / (_cycle_readings.plateau_pressure - _peep);
_cycle_readings.mean_airway_pressure = _airway_pressure;
_cycle_readings.inhalation_pressure = _airway_pressure;
_cycle_readings.apnea_index += _apnea_event ? 1 : 0;
_cycle_readings.apnea_index += (_apnea_event == true) ? 1 : 0;
_cycle_readings.apnea_time = _measured_durations.buff_loaded
+_measured_durations.buff_pre_inhale
+_measured_durations.inhale
......@@ -255,7 +255,7 @@ void BreathingLoop::updateCycleReadings()
void BreathingLoop::setVentilationMode(VENTILATION_MODE mode)
{
_ventilation_mode = mode;
valve_params vp = _valves_controller.getValveParams();
valve_params &vp = _valves_controller.getValveParams();
switch(_ventilation_mode){
case VENTILATION_MODE::HEV_MODE_PC_AC :
......@@ -353,7 +353,7 @@ void BreathingLoop::resetReadingSums()
//This is used to assign the transitions of the fsm
void BreathingLoop::FSM_assignment( ) {
uint32_t tnow = static_cast<uint32_t>(millis());
if (tnow - _fsm_time > _fsm_timeout) {
if (tnow - _fsm_time >= _fsm_timeout) {
BL_STATES next_state;
switch (_bl_state)
{
......@@ -437,7 +437,7 @@ void BreathingLoop::FSM_assignment( ) {
_readings_reset = true;
}
// safety check
if (tnow - _fsm_time > 10) {
if (tnow - _fsm_time >= 10) {
if (_safe == false){
_bl_state = BL_STATES::BUFF_PURGE;
// TODO RAISE ALARM
......@@ -457,7 +457,7 @@ void BreathingLoop::FSM_breathCycle()
} else {
_fsm_timeout = 1000;
}
_valves_controller.setValves(VALVE_STATE::CLOSED, VALVE_STATE::CLOSED, VALVE_STATE::FULLY_CLOSED, VALVE_STATE::CLOSED, VALVE_STATE::CLOSED);
_valves_controller.setValves(VALVE_STATE::CLOSED, VALVE_STATE::CLOSED, VALVE_STATE::FULLY_CLOSED, VALVE_STATE::OPEN, VALVE_STATE::CLOSED);
initCalib();
break;
case BL_STATES::CALIBRATION :
......@@ -560,6 +560,7 @@ void BreathingLoop::FSM_breathCycle()
// TODO - shouldn't get here: raise alarm
break;
}
//logMsg("fsm timeout " + String(_fsm_timeout) + " state "+String(_bl_state));;
safetyCheck();
measure_durations();
}
......@@ -642,7 +643,7 @@ void BreathingLoop::calibrate()
{
// get pressure_air_regulated over last sec of 10s calc mean
uint32_t tnow = static_cast<uint32_t>(millis());
if (tnow - _calib_time > _calib_timeout ) {
if (tnow - _calib_time >= _calib_timeout ) {
_calib_N++;
_calib_sums.pressure_air_regulated += static_cast<float>(analogRead(pin_pressure_air_regulated));
_calib_avgs.pressure_air_regulated = static_cast<float>(_calib_sums.pressure_air_regulated/ _calib_N);
......@@ -667,7 +668,7 @@ void BreathingLoop::initCalib()
{ // do calibration in last sec of calibration step (normally 10s) or default to 10ms
_calibrated = false;
_calib_timeout = 10;
if (_states_durations.calibration - 1000 > 10)
if (_states_durations.calibration - 1000 >= 10)
_calib_timeout = _states_durations.calibration - 1000;
_calib_time = static_cast<uint32_t>(millis());
_calib_sums.pressure_air_regulated = 0;
......@@ -869,20 +870,21 @@ void BreathingLoop::inhaleTrigger()
//_fsm_timeout = _max_exhale_time;
uint32_t tnow = static_cast<uint32_t>(millis());
if((_flow > _valves_controller.getValveParams().inhale_trigger_threshold)
&& (tnow - _valley_flow_time > 10)){ // wait 10ms after the valley
if (tnow - _fsm_time > _min_exhale_time ) {
&& (tnow - _valley_flow_time >= 10)){ // wait 10ms after the valley
if (tnow - _fsm_time >= _min_exhale_time ) {
// TRIGGER
logMsg("inhale trig- " + String(_running_avg_flow) +" "+ String(_valves_controller.getValveParams().inhale_trigger_threshold));
//logMsg("inhale trig- " + String(_running_avg_flow) +" "+ String(_valves_controller.getValveParams().inhale_trigger_threshold));
_fsm_timeout = 0; // go to next state immediately
_apnea_event = false;
_mandatory_inhale = false;
}
} else if (tnow - _fsm_time > _max_exhale_time){
} else if (tnow - _fsm_time >= _max_exhale_time){
// TRIGGER
_apnea_event = true;
logMsg("inhale trigger - max exhale time exceeded");
//logMsg("inhale trigger - max exhale time exceeded");
_fsm_timeout = 0; // go to next state immediately
_mandatory_inhale = true;
_apnea_event = true;
}
} else {
_mandatory_inhale = true;
......@@ -896,14 +898,14 @@ void BreathingLoop::exhaleTrigger()
//logMsg("EXhale trig- " + String(_flow) + " " + String(_running_avg_flow) +" "+ String(_valves_controller.getValveParams().exhale_trigger_threshold)+" "+String(_peak_flow));
if(en == true){
logMsg("exhale trigger");
//logMsg("exhale trigger");
uint32_t tnow = static_cast<uint32_t>(millis());
valve_params vp = _valves_controller.getValveParams();
if((_running_avg_flow < (vp.exhale_trigger_threshold * _peak_flow))
&& (tnow - _peak_flow_time > 100)){ // wait 10ms after peak
&& (tnow - _peak_flow_time >= 100)){ // wait 10ms after peak
//TODO - check we're past 'peak'
logMsg("EXhale trig- " + String(_running_avg_flow) +" "+ String(vp.exhale_trigger_threshold)+" "+String(_peak_flow));
if (tnow - _fsm_time > _min_inhale_time ) {
//logMsg("EXhale trig- " + String(_running_avg_flow) +" "+ String(vp.exhale_trigger_threshold)+" "+String(_peak_flow));
if (tnow - _fsm_time >= _min_inhale_time ) {
// TRIGGER
_fsm_timeout = 0; // go to next state immediately
_mandatory_exhale = false;
......
......@@ -45,7 +45,7 @@ void UILoop::receiveCommands()
void UILoop::reportFastReadings()
{
uint32_t tnow = static_cast<uint32_t>(millis());
if (tnow - _fast_report_time > _fast_report_timeout)
if (tnow - _fast_report_time >= _fast_report_timeout)
{
// TO SWITCH BETWEEN RAW AND MILLIBAR DATA UNCOMMENT BELOW
......@@ -78,7 +78,7 @@ void UILoop::reportFastReadings()
void UILoop::reportReadbackValues()
{
uint32_t tnow = static_cast<uint32_t>(millis());
if (tnow - _readback_report_time > _readback_report_timeout)
if (tnow - _readback_report_time >= _readback_report_timeout)
{
bool vin_air, vin_o2, vpurge;
uint8_t vinhale, vexhale;
......@@ -127,7 +127,7 @@ void UILoop::reportReadbackValues()
void UILoop::reportCycleReadings()
{
uint32_t tnow = static_cast<uint32_t>(millis());
if (tnow - _cycle_report_time > _cycle_report_timeout)
if (tnow - _cycle_report_time >= _cycle_report_timeout)
{
_cycle_data.timestamp = tnow;
......@@ -185,7 +185,7 @@ void UILoop::reportAlarms()
void UILoop::reportIVTReadings()
{
uint32_t tnow = static_cast<uint32_t>(millis());
if (tnow - _ivt_report_time > _ivt_report_timeout)
if (tnow - _ivt_report_time >= _ivt_report_timeout)
{
_ivt_data.timestamp = tnow;
......@@ -216,7 +216,7 @@ void UILoop::reportDebugValues()
{
uint32_t tnow = static_cast<uint32_t>(millis());
if (tnow - _debug_report_time > _debug_report_timeout)
if (tnow - _debug_report_time >= _debug_report_timeout)
{
_debug_data.timestamp = static_cast<uint32_t>(tnow);
......
......@@ -2,6 +2,7 @@
#define COMMON_H
#include <Arduino.h>
#include <limits>
#include "localconf.h"
#include "CommsControl.h"
......@@ -20,7 +21,6 @@
#endif
#define HEV_FORMAT_VERSION 0xA9
#define EXHALE_VALVE_PROPORTIONAL
//
const float MAX_VALVE_FRAC_OPEN = 0.74;
......
......@@ -45,7 +45,7 @@ class Dependant(object):
if payload.getType() == PAYLOAD_TYPE.DATA.value:
#logging.info(f"payload received: {payload}")
#logging.info(f"payload received: {payload.timestamp} pc {payload.flow:3.6f} dc {payload.volume:3.6f} fsm {payload.fsm_state}")
#logging.info(f"Fsm state: {payload.fsm_state}")
logging.info(f"Fsm state: {payload.fsm_state}")
fsm = payload.fsm_state
#if payload.getType() == PAYLOAD_TYPE.IVT.value:
# logging.info(f"IV: air {payload.air_in_current:.3f} o2 {payload.o2_in_current:.3f} purge {payload.purge_current:.3f} inhale {payload.inhale_current:.3f} exhale {payload.exhale_current:.3f} fsm {fsm} ")
......@@ -56,8 +56,8 @@ class Dependant(object):
logging.info(f"payload received: {payload} {fsm}")
#if payload.getType() == PAYLOAD_TYPE.DEBUG.value:
# logging.info(f" PID {payload.kp:3.6f} {payload.ki:3.6f} {payload.kd:3.6f} {payload.proportional:3.6f} {payload.integral:3.6f} {payload.derivative:3.6f} {payload.valve_duty_cycle:3.6f} {payload.target_pressure:3.6f} {payload.process_pressure:3.6f} fsm {fsm}")
# if payload.getType() == PAYLOAD_TYPE.LOGMSG.value:
# logging.info(f"LOGMSG {payload.message} ")
if payload.getType() == PAYLOAD_TYPE.LOGMSG.value:
logging.info(f"LOGMSG {payload.message} ")
#if hasattr(payload, 'ventilation_mode'):
# logging.info(f"payload received: {payload.ventilation_mode}")
#if hasattr(payload, 'duration_inhale'):
......@@ -72,69 +72,69 @@ async def commsDebug():
#comms.writePayload(cmd)
await asyncio.sleep(5)
# await asyncio.sleep(1)
# cmd = CommandFormat(cmd_type=CMD_TYPE.SET_PID.value, cmd_code=CMD_SET_PID.KP.value, param= 0.8*0.001)#0.0108/5) # 108/4) # to set Kp=0.0002, param=200 i.e., micro_Kp
# comms.writePayload(cmd)
# await asyncio.sleep(1)
# cmd = CommandFormat(cmd_type=CMD_TYPE.SET_PID.value, cmd_code=CMD_SET_PID.KI.value, param=0.8*0.0003)#0.00162*0.4)#0.0054/2) # 0004)#0002) # to set Kp=0.0002, param=200 i.e., micro_Kp
# comms.writePayload(cmd)
# await asyncio.sleep(1)
# cmd = CommandFormat(cmd_type=CMD_TYPE.SET_PID.value, cmd_code=CMD_SET_PID.KD.value, param=0.0)#0.00162*1.5)#0.0054/2) # to set Kp=0.0002, param=200 i.e., micro_Kp
# comms.writePayload(cmd)
# await asyncio.sleep(1)
# cmd = CommandFormat(cmd_type=CMD_TYPE.SET_PID.value, cmd_code=CMD_SET_PID.TARGET_FINAL_PRESSURE.value, param=20)#set Kp=0.0002, param=200 i.e., micro_Kp
# comms.writePayload(cmd)
# await asyncio.sleep(1)
# cmd = CommandFormat(cmd_type=CMD_TYPE.SET_PID.value, cmd_code=CMD_SET_PID.NSTEPS.value, param=3) # to set Kp=0.0002, param=200 i.e., micro_Kp
# comms.writePayload(cmd)
# await asyncio.sleep(1)
# # Enable inhale trigger
# cmd = CommandFormat(cmd_type=CMD_TYPE.SET_VALVE.value, cmd_code=CMD_SET_VALVE.INHALE_TRIGGER_ENABLE.value, param=0.) # to set Kp=0.0002, param=200 i.e., micro_Kp
# comms.writePayload(cmd)
# await asyncio.sleep(1)
# # Enable exhale trigger
# cmd = CommandFormat(cmd_type=CMD_TYPE.SET_VALVE.value, cmd_code=CMD_SET_VALVE.EXHALE_TRIGGER_ENABLE.value, param=0.) # to set Kp=0.0002, param=200 i.e., micro_Kp
# comms.writePayload(cmd)
# await asyncio.sleep(1)
# # Enable inhale trigger threshold
# cmd = CommandFormat(cmd_type=CMD_TYPE.SET_VALVE.value, cmd_code=CMD_SET_VALVE.INHALE_TRIGGER_THRESHOLD.value, param=0.005) # to set Kp=0.0002, param=200 i.e., micro_Kp
# comms.writePayload(cmd)
# await asyncio.sleep(1)
# # Enable exhale trigger threshold
# cmd = CommandFormat(cmd_type=CMD_TYPE.SET_VALVE.value, cmd_code=CMD_SET_VALVE.EXHALE_TRIGGER_THRESHOLD.value, param=0.1) # to set Kp=0.0002, param=200 i.e., micro_Kp
# comms.writePayload(cmd)
# await asyncio.sleep(1)
# # Change DURATION of breathing cycle (BUFF-PRE-INHALE)
# cmd = CommandFormat(cmd_type=CMD_TYPE.SET_DURATION.value, cmd_code=CMD_SET_DURATION.BUFF_PRE_INHALE.value, param=0.) #
# comms.writePayload(cmd)
# await asyncio.sleep(1)
# # Change DURATION of breathing cycle (INHALE)
# cmd = CommandFormat(cmd_type=CMD_TYPE.SET_DURATION.value, cmd_code=CMD_SET_DURATION.INHALE.value, param=1200.) #
# comms.writePayload(cmd)
# await asyncio.sleep(1)
# # Change DURATION of breathing cycle (PAUSE)
# cmd = CommandFormat(cmd_type=CMD_TYPE.SET_DURATION.value, cmd_code=CMD_SET_DURATION.PAUSE.value, param=0.) #
# comms.writePayload(cmd)
# await asyncio.sleep(1)
# # Change DURATION of breathing cycle (EXHALE-FILL)
# cmd = CommandFormat(cmd_type=CMD_TYPE.SET_DURATION.value, cmd_code=CMD_SET_DURATION.EXHALE_FILL.value, param=1600.) #
# comms.writePayload(cmd)
# await asyncio.sleep(1)
# # Change DURATION of breathing cycle (EXHALE)
# cmd = CommandFormat(cmd_type=CMD_TYPE.SET_DURATION.value, cmd_code=CMD_SET_DURATION.EXHALE.value, param=200.) #
# comms.writePayload(cmd)
# await asyncio.sleep(1)
# # Start the cycles
cmd = CommandFormat(cmd_type=CMD_TYPE.SET_MODE.value, cmd_code=VENTILATION_MODE.LAB_MODE_BREATHE.value, param=0)
#await asyncio.sleep(1)
cmd = CommandFormat(cmd_type=CMD_TYPE.SET_PID.value, cmd_code=CMD_SET_PID.KP.value, param= 0.8*0.001)#0.0108/5) # 108/4) # to set Kp=0.0002, param=200 i.e., micro_Kp
comms.writePayload(cmd)
#await asyncio.sleep(1)
cmd = CommandFormat(cmd_type=CMD_TYPE.SET_PID.value, cmd_code=CMD_SET_PID.KI.value, param=0.8*0.0003)#0.00162*0.4)#0.0054/2) # 0004)#0002) # to set Kp=0.0002, param=200 i.e., micro_Kp
comms.writePayload(cmd)
#await asyncio.sleep(1)
cmd = CommandFormat(cmd_type=CMD_TYPE.SET_PID.value, cmd_code=CMD_SET_PID.KD.value, param=0.0)#0.00162*1.5)#0.0054/2) # to set Kp=0.0002, param=200 i.e., micro_Kp
comms.writePayload(cmd)
#await asyncio.sleep(1)
cmd = CommandFormat(cmd_type=CMD_TYPE.SET_PID.value, cmd_code=CMD_SET_PID.TARGET_FINAL_PRESSURE.value, param=20)#set Kp=0.0002, param=200 i.e., micro_Kp
comms.writePayload(cmd)
#await asyncio.sleep(1)
cmd = CommandFormat(cmd_type=CMD_TYPE.SET_PID.value, cmd_code=CMD_SET_PID.NSTEPS.value, param=3) # to set Kp=0.0002, param=200 i.e., micro_Kp
comms.writePayload(cmd)
#await asyncio.sleep(1)
# Enable inhale trigger
cmd = CommandFormat(cmd_type=CMD_TYPE.SET_VALVE.value, cmd_code=CMD_SET_VALVE.INHALE_TRIGGER_ENABLE.value, param=0.) # to set Kp=0.0002, param=200 i.e., micro_Kp
comms.writePayload(cmd)
#await asyncio.sleep(1)
# Enable exhale trigger
cmd = CommandFormat(cmd_type=CMD_TYPE.SET_VALVE.value, cmd_code=CMD_SET_VALVE.EXHALE_TRIGGER_ENABLE.value, param=0.) # to set Kp=0.0002, param=200 i.e., micro_Kp
comms.writePayload(cmd)
#await asyncio.sleep(1)
# Enable inhale trigger threshold
cmd = CommandFormat(cmd_type=CMD_TYPE.SET_VALVE.value, cmd_code=CMD_SET_VALVE.INHALE_TRIGGER_THRESHOLD.value, param=0.005) # to set Kp=0.0002, param=200 i.e., micro_Kp
comms.writePayload(cmd)
#await asyncio.sleep(1)
# Enable exhale trigger threshold
cmd = CommandFormat(cmd_type=CMD_TYPE.SET_VALVE.value, cmd_code=CMD_SET_VALVE.EXHALE_TRIGGER_THRESHOLD.value, param=0.1) # to set Kp=0.0002, param=200 i.e., micro_Kp
comms.writePayload(cmd)
#await asyncio.sleep(1)
# Change DURATION of breathing cycle (BUFF-PRE-INHALE)
cmd = CommandFormat(cmd_type=CMD_TYPE.SET_DURATION.value, cmd_code=CMD_SET_DURATION.BUFF_PRE_INHALE.value, param=0.) #
comms.writePayload(cmd)
#await asyncio.sleep(1)
# Change DURATION of breathing cycle (INHALE)
cmd = CommandFormat(cmd_type=CMD_TYPE.SET_DURATION.value, cmd_code=CMD_SET_DURATION.INHALE.value, param=1200.) #
comms.writePayload(cmd)
#await asyncio.sleep(1)
# Change DURATION of breathing cycle (PAUSE)
cmd = CommandFormat(cmd_type=CMD_TYPE.SET_DURATION.value, cmd_code=CMD_SET_DURATION.PAUSE.value, param=0.) #
comms.writePayload(cmd)
#await asyncio.sleep(1)
# Change DURATION of breathing cycle (EXHALE-FILL)
cmd = CommandFormat(cmd_type=CMD_TYPE.SET_DURATION.value, cmd_code=CMD_SET_DURATION.EXHALE_FILL.value, param=1600.) #
comms.writePayload(cmd)
#await asyncio.sleep(1)
# Change DURATION of breathing cycle (EXHALE)
cmd = CommandFormat(cmd_type=CMD_TYPE.SET_DURATION.value, cmd_code=CMD_SET_DURATION.EXHALE.value, param=200.) #
comms.writePayload(cmd)
#await asyncio.sleep(1)
# Start the cycles
cmd = CommandFormat(cmd_type=CMD_TYPE.SET_MODE.value, cmd_code=VENTILATION_MODE.HEV_MODE_PC_PSV.value, param=0)
comms.writePayload(cmd)
cmd = CommandFormat(cmd_type=CMD_TYPE.GENERAL.value, cmd_code=CMD_GENERAL.START.value, param=0)
comms.writePayload(cmd)
print('sent cmd start')
# await asyncio.sleep(1)
# cmd = CommandFormat(cmd_type=CMD_TYPE.SET_VALVE.value, cmd_code=CMD_SET_VALVE.INHALE_TRIGGER_ENABLE.value, param=0)
# comms.writePayload(cmd)
# cmd = CommandFormat(cmd_type=CMD_TYPE.SET_VALVE.value, cmd_code=CMD_SET_VALVE.EXHALE_TRIGGER_ENABLE.value, param=0)
# comms.writePayload(cmd)
await asyncio.sleep(1)
#cmd = CommandFormat(cmd_type=CMD_TYPE.SET_VALVE.value, cmd_code=CMD_SET_VALVE.INHALE_TRIGGER_ENABLE.value, param=1)
#comms.writePayload(cmd)
#cmd = CommandFormat(cmd_type=CMD_TYPE.SET_VALVE.value, cmd_code=CMD_SET_VALVE.EXHALE_TRIGGER_ENABLE.value, param=1)
#comms.writePayload(cmd)
#print('sent inhale + exhale trigger -> 1')
toggle = 2
while True:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment