1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// 
// 
// 

#include "PressureLoop.h"

void PressureLoopClass::Init(float fast_ms, int32_t LoopRatio, void* handle)
{
    DriverContext* dc;
    dc = (DriverContext*)handle;
    hwi = (HW*)dc->hwi;
    dbg = (DebugIfaceClass*)dc->dbg;

    cycle_Loop_LT = hwi->GetMillis();
    _fast_ms = fast_ms;
    _LoopRatio = LoopRatio;
    LoopCounter = 0;
    _PID_P = 25;
    _PID_I = 400;
    _PID_D = 0;
    _PID_P2 = 1.3;
    _PID_I2 = 12;
    _PID_D2 = 0;
    _pid_limit = 0.55;
    _filter_fast = 0.94;// 0.9;
    _filter_slow = 0.7;
    _ValvePWM = 0;

    last_fast_ms = hwi->GetMillis();
    last_slow_ms = hwi->GetMillis();
}



void PressureLoopClass::PID_SLOW_LOOP()
{

    static float pid_error = 0;
    static float pid_integral = 0;
    static float pid_prec = 0;
    static float pid_out = 0;

    float PID_P = _PID_P2;
    float PID_I = _PID_I2;
    float PID_D = _PID_D2;
    static float Pset2 = 0;

    static float pid_outb = 0;

    float Pmeas = 0;

    float dT = ((float)hwi->Get_dT_millis(last_slow_ms)) / 1000.0;<--- Variable 'dT' is assigned a value that is never used.
    last_slow_ms = hwi->GetMillis();

    Pmeas = _pressure_patient;

    if (_Pset == 0) {
        Pset2 = Pmeas;
        pid_integral = 0;
        pid_prec = 0;
        pid_outb = 0;
    }
    else {
        Pset2 = (Pset2 * _filter_slow) + ((1 - _filter_slow) * (_Pset));

        pid_error = Pset2 - Pmeas;
        pid_integral += pid_error;

        if (pid_integral < 0)
            pid_integral = 0;

        pid_out = PID_P * pid_error + PID_I * pid_integral + PID_D * (pid_error - pid_prec);

        pid_outb = pid_out;

        if (pid_outb < Pset2 * _pid_limit)
            pid_outb = Pset2 * _pid_limit;
        if (pid_outb > 50)
            pid_outb = 50;

        pid_prec = pid_error;

        fast_pid_set = pid_outb;
    }

}

float PressureLoopClass::ValveLUT(float pid_value)
{
    float tret = 0;
    // if (pid_value > pid_fast_last)
    {
        if (pid_value == 0)
            tret = 30;
        else
            tret = (0.00126 * pid_value * pid_value) + (0.42098 * pid_value) + 37.32803;
    }
    // else
    // {
    //     tret = (0.00309 * pid_value * pid_value) + (0.2777 * pid_value) + 34.93373;
    // }

    pid_fast_last = pid_value;
    return tret;
}

void PressureLoopClass::PID_FAST_LOOP()
{

    static float pid_error = 0;
    static float pid_integral = 0;
    static float pid_prec = 0;
    static float pid_out = 0;


    static float fw_pid = 0;




    float integral_before = 0;<--- Variable 'integral_before' is assigned a value that is never used.

    float PID_P = _PID_P;
    float PID_I = _PID_I;
    float PID_D = _PID_D;
    static float Pset2 = 0;

    static float pid_outb = 0;

    float Pmeas = 0;

    float dT = ((float)hwi->Get_dT_millis(last_fast_ms)) / 1000.0;<--- Variable 'dT' is assigned a value that is never used.
    last_fast_ms = hwi->GetMillis();

    fast_pid_set = _Pset;
    Pmeas = _pressure_valve;

    if (_Pset == 0) {
        Pset2 = Pmeas;
        //pid_integral = 0;
        pid_prec = 0;
        _ValvePWM = ValveLUT(0);
        pid_outb = 0;
    }
    else {
        Pset2 = (Pset2 * _filter_fast) + ((1 - _filter_fast) * fast_pid_set);

        //Pset2 = fast_pid_set;

        pid_error = Pset2 - Pmeas;
        integral_before = pid_integral;
        pid_integral += pid_error;
        if ((pid_integral * PID_I) > 4095.0)
            pid_integral = (4095.0 / PID_I);
        //if ((pid_integral * PID_I) < -4095.0)
        //    pid_integral = -(4095.0 / PID_I);
        if ((pid_integral * PID_I) < 0)
            pid_integral = 0;

        pid_out = PID_P * pid_error + PID_I * pid_integral + PID_D * (pid_error - pid_prec);

        if (pid_out < 0)
            pid_integral = integral_before;
        //pid_outb = pid_outb * 0.9 + pid_out * 0.1;
        pid_outb = pid_out;
        if (pid_outb < 0)
            pid_outb = 0;
        //pid_outb = pid_outb + 500;
        if (pid_outb > 4095.0)
            pid_outb = 4095.0;



        pid_prec = pid_error;

        if (_Pset == 0)
            _ValvePWM = ValveLUT(1);
        else
            _ValvePWM = ValveLUT(pid_outb * 100.0 / 4095);



    }


}

void PressureLoopClass::SetTargetPressure(float pressure)
{
    _Pset = pressure;
}

void PressureLoopClass::Tick()
{
    if (hwi->Get_dT_millis(cycle_Loop_LT) >= _fast_ms)
    {
        cycle_Loop_LT = hwi->GetMillis();
        if (_LoopRatio == 0)
        {
            PID_FAST_LOOP();
            PID_SLOW_LOOP();
        }
        else
        {
            /*if (LoopCounter >= _LoopRatio)
            {
                PID_SLOW_LOOP();
                LoopCounter = 0;
            }
            else*/
            {
                PID_FAST_LOOP();
                LoopCounter++;
            }
        }
    }
}

void PressureLoopClass::SetPressure(t_pressure_selector ps, float pressure)
{
    if (ps == PRESSURE_VALVE)
    {
        _pressure_valve = pressure;
    }
    else
    {
        if (ps == PRESSURE_PATIENT)
        {
            _pressure_patient = pressure;
        }
    }
}

void PressureLoopClass::ConfigurePidSlow(float P, float I, float D, float pid_limiter)
{
    _PID_P2 = P;
    _PID_I2 = I;
    _PID_D2 = D;
    _pid_limit = pid_limiter;
}
void PressureLoopClass::ConfigurePidFast(float P, float I, float D)
{
    _PID_P = P;
    _PID_I = I;
    _PID_D = D;
}
void PressureLoopClass::GetPidSlow(float* P, float* I, float* D, float* pid_limiter)<--- The function 'GetPidSlow' is never used.
{
    *P = _PID_P2;
    *I = _PID_I2;<--- *I is assigned
    *I = _PID_D2;<--- *I is overwritten
    *pid_limiter = _pid_limit;
}
void PressureLoopClass::GetPidFast(float* P, float* I, float* D)<--- The function 'GetPidFast' is never used.
{
    *P = _PID_P;
    *I = _PID_I;<--- *I is assigned
    *I = _PID_D;<--- *I is overwritten
}
void PressureLoopClass::SetPidFilter(float fast, float slow)<--- The function 'SetPidFilter' is never used.
{
    _filter_fast = fast;
    _filter_slow = slow;
}
void PressureLoopClass::GetPidFilter(float* fast, float* slow)<--- The function 'GetPidFilter' is never used.
{
    *fast = _filter_fast;
    *slow = _filter_slow;
}

float PressureLoopClass::GetValveControl()
{
    return _ValvePWM;
}

void PressureLoopClass::GetPidMonitor(float* slow, float* fast)
{
    *fast = _ValvePWM;
    *slow = fast_pid_set;
}

//                  #     # ### 
//                  ##    #  #  
//                  # #   #  #  
//                  #  #  #  #  
//                  #   # #  #  
//                  #    ##  #  
//                  #     # ### 
//
// Nuclear Instruments 2020 - All rights reserved
// Any commercial use of this code is forbidden
// Contact info@nuclearinstruments.eu