Skip to content

Instantly share code, notes, and snippets.

@danielsuo
Created July 16, 2020 01:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielsuo/49c7b965e6f710286581674fca83b579 to your computer and use it in GitHub Desktop.
Save danielsuo/49c7b965e6f710286581674fca83b579 to your computer and use it in GitHub Desktop.
diff --git a/pid.txt b/pid.txt
index e453170..3015dbf 100644
--- a/pid.txt
+++ b/pid.txt
@@ -1,6 +1,6 @@
-def _PID_update(self, dt):
+def _Predictive_PID_update(self, dt):
'''
- This instantiates the PID control algorithms.
+ This instantiates the Predictive PID control algorithms.
During the breathing cycle, it goes through the four states:
1) Rise to PIP, while controlling dP/dt
2) Sustain PIP pressure
@@ -18,26 +18,22 @@ def _PID_update(self, dt):
self._DATA_VOLUME += dt * self._DATA_Qout # Integrate what has happened within the last few seconds from flow out
self._DATA_PRESSURE = np.mean(self._DATA_PRESSURE_LIST)
-
if cycle_phase < self.__SET_I_PHASE:
- self.__KP = 2*(self.__SET_PIP_GAIN-0.95)
- self.__KI = 2.0
+ self.__KP = max(0.5,3*np.exp(-cycle_phase / (0.15*self.__SET_I_PHASE)))
+ self.__KI = 6*(1-np.exp(-cycle_phase / (0.075*self.__SET_I_PHASE)))
self.__KD = 0
self.__PID_OFFSET = 0
- self.__get_PID_error(yis = self._DATA_PRESSURE, ytarget = self.__SET_PIP, dt = dt, RC = 0.3)
- self.__calculate_control_signal_in(dt = dt)
- self.__control_signal_out = 0
+ self.__control_signal_in = self._adaptivecontroller.feed(self._DATA_PRESSURE, now)
+ self.__control_signal_out = 0 # close out valve
elif cycle_phase < self.__SET_PEEP_TIME + self.__SET_I_PHASE: # then, we drop pressure to PEEP
if PEEP_VALVE_SET:
self.__control_signal_in = 0
self.__control_signal_out = 1
+
else:
- self.__KP = 0.1
- self.__KI = 2.0
- self.__KD = 0
target_pressure = self.__SET_PIP - (cycle_phase - self.__SET_I_PHASE) * (self.__SET_PIP - self.__SET_PEEP) / self.__SET_PEEP_TIME
self.__get_PID_error(yis = self._DATA_PRESSURE, ytarget = target_pressure, dt = dt)
self.__calculate_control_signal_in()
@@ -48,12 +44,10 @@ def _PID_update(self, dt):
elif cycle_phase < self.__SET_CYCLE_DURATION:
if PEEP_VALVE_SET:
+ #self.__control_signal_in = 5 # Controlled by mechanical peep valve, gentle flow in
self.__control_signal_out = 1
- self.__control_signal_in = 5 *(1 - np.exp( 5*((self.__SET_PEEP_TIME + self.__SET_I_PHASE) - cycle_phase )) ) # Make this nice and smooth.
+ self.__control_signal_in = 5* (1 - np.exp( 5*((self.__SET_PEEP_TIME + self.__SET_I_PHASE) - cycle_phase )) )
else:
- self.__KP = 0.1
- self.__KI = 2.0
- self.__KD = 0
self.__get_PID_error(yis = self._DATA_PRESSURE, ytarget = self.__SET_PEEP, dt = dt)
self.__calculate_control_signal_in()
if self._DATA_PRESSURE > self.__SET_PEEP + 0.5:
@@ -61,13 +55,6 @@ def _PID_update(self, dt):
else:
self.__control_signal_out = 0
- if self._DATA_PRESSURE < self.__SET_PEEP - self.breath_pressure_drop: #breath!
- print("Autonomous breath detected; starting next cycle.")
- self._cycle_start = time.time() # New cycle starts
- self._DATA_VOLUME = 0 # ... start at zero volume in the lung
- self._DATA_dpdt = 0 # and restart the rolling average for the dP/dt estimation
- next_cycle = True
-
else:
self._cycle_start = time.time() # New cycle starts
self._DATA_VOLUME = 0 # ... start at zero volume in the lung
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment