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

Inhale valve frequency changed to 500 Hz on nodemcu header, best PID...

Inhale valve frequency changed to 500 Hz on nodemcu header, best PID parameters updated on CommsDebug.py, longer fill time for the testing
parent b93a7a77
No related merge requests found
......@@ -44,4 +44,4 @@ const int pin_sda = 22;
const int pwm_chan_inhale = 0;
const int pwm_chan_exhale = 1;
const int pwm_resolution = 16; // 8 bit resolution; up to 16 possible
const int pwm_frequency = 900; // frequency in Hz
const int pwm_frequency = 500; // frequency in Hz
......@@ -98,7 +98,7 @@ void BreathingLoop::updateReadings()
float_t _pressure_inhale = adcToMillibarFloat((_readings_sums.pressure_inhale / _readings_N), _calib_avgs.pressure_inhale );
doPID(5, 10., _pressure_inhale, _valve_inhale_PID_percentage, _airway_pressure, _volume, _flow);
doPID(3, 10., _pressure_inhale, _valve_inhale_PID_percentage, _airway_pressure, _volume, _flow);
//_volume = _valve_inhale_PID_percentage;
//_valve_inhale_PID_percentage /= 10.; // In the Labview code the output was defined from 0-10V. It is a simple rescale to keep the same parameters
......@@ -106,6 +106,7 @@ void BreathingLoop::updateReadings()
//airway_pressure = Proportional
//volume = Integral
_flow = _valve_inhale_PID_percentage;
//_flow = _valves_controller.calcValveDutyCycle(pwm_resolution,_valve_inhale_PID_percentage);
_valves_controller.setPIDoutput(_valve_inhale_PID_percentage);
_valves_controller.setValves(VALVE_STATE::CLOSED, VALVE_STATE::CLOSED, VALVE_STATE::PID, VALVE_STATE::CLOSED, VALVE_STATE::CLOSED);
......@@ -585,7 +586,7 @@ void BreathingLoop::doPID(int nsteps, float target_pressure, float process_press
//TODO derivative
float minimum_open_frac = 0.53; //Minimum opening to avoid vibrations on the valve control
float maximum_open_frac = 0.70; //Maximum opening for the PID control
float maximum_open_frac = 0.74; //Maximum opening for the PID control
output = proportional + integral + minimum_open_frac;
......
......@@ -94,7 +94,7 @@ private:
// timeouts
uint32_t calculateDurationExhale();
//durations = {calibration, buff_purge, buff_flush, buff_prefill, buff_fill, buff_loaded, buff_pre_inhale, inhale, pause, exhale_fill, exhale }
states_durations _states_durations = {10000, 600, 600, 100, 600, 0, 0, 1000, 0, 600, 1400};
states_durations _states_durations = {10000, 600, 600, 100, 600, 0, 0, 1600, 0, 1200, 2200};
// readings
void resetReadingSums();
......
......@@ -22,7 +22,7 @@
#define HEV_FORMAT_VERSION 0xA5
//
const float MAX_VALVE_FRAC_OPEN = 0.70;
const float MAX_VALVE_FRAC_OPEN = 0.74;
const uint8_t MAX_PATIENT_PRESSURE = 40; //mbar
// input params
enum PAYLOAD_TYPE : uint8_t {
......
......@@ -51,10 +51,10 @@ async def commsDebug():
#comms.writePayload(cmd)
await asyncio.sleep(1)
cmd = CommandFormat(cmd_type=CMD_TYPE.SET_PID.value, cmd_code=CMD_SET_PID.KP.value, param=0.01) # to set Kp=0.0002, param=200 i.e., micro_Kp
cmd = CommandFormat(cmd_type=CMD_TYPE.SET_PID.value, cmd_code=CMD_SET_PID.KP.value, param=0.004) # 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.0004)#0002) # to set Kp=0.0002, param=200 i.e., micro_Kp
cmd = CommandFormat(cmd_type=CMD_TYPE.SET_PID.value, cmd_code=CMD_SET_PID.KI.value, param=0.0010)#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.0011) # to set Kp=0.0002, param=200 i.e., micro_Kp
......@@ -66,11 +66,11 @@ async def commsDebug():
print('sent cmd start')
toggle = 2
while True:
await asyncio.sleep(120)
await asyncio.sleep(300)
#cmd = CommandFormat(cmd_type=CMD_TYPE.SET_PID.value, cmd_code=CMD_SET_PID.KP.value, param=5) # to set Kp=0.2, param=200 i.e., milli_Kp
#comms.writePayload(cmd)
#print('sent cmd set Kp = 0.2')
await asyncio.sleep(120)
await asyncio.sleep(300)
cmd = CommandFormat(cmd_type=CMD_TYPE.GENERAL.value, cmd_code=toggle, param=0)
if toggle == 2 :
toggle = 1
......
import sys
import re
import queue
import time
#import matplotlib
#matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
counter = 0
history_length = 2000
pressure_buffer = queue.Queue(history_length)
pressure_inhale = queue.Queue(history_length)
pressure_patient = queue.Queue(history_length)
pressure_diff_patient = queue.Queue(history_length)
PID_P = queue.Queue(history_length)
PID_I = queue.Queue(history_length)
PID_D = queue.Queue(history_length)
def derivative(l):
result = []
for i in range(len(l)):
if i != 0 :
result.append(l[i]-l[i-1])
else:
result.append(0)
return result
def derivative_exp(l):
_exp_mean = 0.
result = []
for i in range(len(l)):
if i != 0 :
_exp_mean = (0.7*_exp_mean) + (0.3*(l[i]-l[i-1]))
result.append(_exp_mean)
else:
result.append(0)
return result
def derivative_withthreshold(l, lmin):
result = []
for i in range(len(l)):
if i != 0 :
_result = (l[i]-l[i-1])
if _result < lmin and _result > -1*lmin: _result = 0.
if _result > lmin: _result -= lmin
if _result < -1*lmin: _result += lmin
result.append(_result)
else:
result.append(0)
return result
for i in range(history_length):
pressure_buffer.put(-1)
pressure_inhale.put(-1)
pressure_patient.put(-1)
pressure_diff_patient.put(-50)
PID_P.put(-1)
PID_I.put(-1)
PID_D.put(-1)
#plt.ion()
#for i in range(10): pressure_inhale.put(-1)
fig = plt.figure()
ax = fig.add_subplot(111)
h1, = ax.plot([],[], "+-", label="buffer")
h2, = ax.plot([],[], "+-", label="inhale")
h3, = ax.plot([],[], "+-", label="Proportional")
h4, = ax.plot([],[], "+-", label="Integral")
h5, = ax.plot([],[], "+-", label="Derivative")
h6, = ax.plot([],[], "+-", label="Patient")
h7, = ax.plot([],[], "+-", label="DP_Patient")
h8, = ax.plot([],[], "+-", label="PID_error")
h9, = ax.plot([],[], "+-", label="inhale derivative")
h10, = ax.plot([],[], "+-", label="inhale derivative exp")
h11, = ax.plot([],[], "+-", label="inhale derivative threshold")
#plt.axes()
#an = []
#ai, = ax.plot(range(10), range(10))
#an.append(ai)
#fig.canvas.draw()
#plt.show(block=False)
x = 1
#plt.show()
#time.sleep(10)
#airway_pressure = Proportional
#volume = Integral
#flow = Derivative
logfile =open(sys.argv[1])
_data = logfile.readlines()
#try:
counter += 1.
#data = sys.stdin.readline()
#print("reading file "+sys.argv[1])
if not _data: sys.exit()
#print(data)
for _entry in _data:
data = re.split(",", _entry)
for entry in data:
if "pressure_diff_patient" in entry and not "mean" in entry:
_diff_patient_p = float( entry.strip().split("=")[-1] )
pressure_diff_patient.get()
pressure_diff_patient.put(_diff_patient_p)
#print(len(list(pressure_inhale.queue)))
#fig.canvas.draw()
#fig.canvas.flush_events()
#plt.show()
if "pressure_patient" in entry and not "mean" in entry:
_patient_p = float( entry.strip().split("=")[-1] )
pressure_patient.get()
pressure_patient.put(_patient_p)
#print(len(list(pressure_inhale.queue)))
#fig.canvas.draw()
#fig.canvas.flush_events()
#plt.show()
if "pressure_inhale" in entry and not "mean" in entry:
_inhale_p = float( entry.strip().split("=")[-1] )
pressure_inhale.get()
pressure_inhale.put(_inhale_p)
#print(len(list(pressure_inhale.queue)))
#fig.canvas.draw()
#fig.canvas.flush_events()
#plt.show()
if "pressure_buffer" in entry and not "mean" in entry:
_buffer_p = float( entry.strip().split("=")[-1] )
pressure_buffer.get()
pressure_buffer.put(_buffer_p)
#print(len(list(pressure_inhale.queue)))
#fig.canvas.draw()
#fig.canvas.flush_events()
#plt.show()
if "airway_pressure" in entry and not "mean" in entry:
_PID_P = float( entry.strip().split("=")[-1] )
PID_P.get()
PID_P.put(_PID_P*100)
#print(len(list(pressure_inhale.queue)))
#fig.canvas.draw()
#fig.canvas.flush_events()
#plt.show()
if "volume" in entry and ")" in entry:
_PID_I = float( entry.strip().split("=")[-1][:-1] )
#print(_PID_I)
PID_I.get()
PID_I.put(_PID_I*100)
#print(len(list(pressure_inhale.queue)))
#fig.canvas.draw()
#fig.canvas.flush_events()
#plt.show()
if "flow" in entry:
_PID_D = float( entry.strip().split("=")[-1] )
PID_D.get()
PID_D.put(_PID_D)
#print(len(list(pressure_inhale.queue)))
#fig.canvas.draw()
#fig.canvas.flush_events()
#plt.show()
#sys.stdout.write(data)
#sys.stdout.flush()
#if counter > 10: fig.savefig('test.png')
#except KeyboardInterrupt:
# print('exiting')
# sys.exit()
#h1.set_xdata(np.array(range(history_length)))
#h1.set_ydata(list(pressure_buffer.queue))#list(pressure_inhale.queue))
h2.set_xdata(np.array(range(history_length)))
h2.set_ydata(list(pressure_inhale.queue))#list(pressure_buffer.queue))
#h3.set_xdata(np.array(range(history_length)))
#h3.set_ydata(list(PID_P.queue))#list(pressure_buffer.queue))
#
#h4.set_xdata(np.array(range(history_length)))
#h4.set_ydata(list(PID_I.queue))#list(pressure_buffer.queue))
#
h5.set_xdata(np.array(range(history_length)))
h5.set_ydata(np.array(list(PID_D.queue)))#list(pressure_buffer.queue))
#
#h6.set_xdata(np.array(range(history_length)))
#h6.set_ydata(list(pressure_patient.queue))#list(pressure_buffer.queue))
#
#h7.set_xdata(np.array(range(history_length)))
#h7.set_ydata(list(pressure_diff_patient.queue))#list(pressure_buffer.queue))
#
#h8.set_xdata(np.array(range(history_length)))
#h8.set_ydata(20.-np.array(list(pressure_inhale.queue)))#list(pressure_buffer.queue))
h9.set_xdata(np.array(range(history_length)))
h9.set_ydata(derivative(list(pressure_inhale.queue)))
h10.set_xdata(np.array(range(history_length)))
h10.set_ydata(derivative_exp(list(pressure_inhale.queue)))
h11.set_xdata(np.array(range(history_length)))
h11.set_ydata(derivative_withthreshold(list(pressure_inhale.queue), 2.0))
plt.legend()
#plt.ylim(-2,20)
ax.relim()
ax.autoscale_view(True,True,True)
#fig.canvas.draw()
#fig.canvas.flush_events()
plt.show()
#time.sleep(0.1)
logfile.seek(0)
logfile.close()
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