PID_Gen.c 14.8 KB
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 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
#include "PID_Gen.h"
// #include "stm32f10x.h"
#include "math.h"

// #include "usart.h"
// #include "dma.h"
// #include "gpio.h"
// #include "protocol.h"

// #include "monitor.h"

#define PI 3.14159265f
#define TWO_PI 6.28318531f

#define D2R (PI / 180.0f)

#define R2D (180.0f / PI)

#define _constrain(amt, low, high) ((amt) < (low) ? (low) : ((amt) > (high) ? (high) : (amt)))

/**
 * @brief Absolute limiting function, compares the given input with the reference value & limiting difference.
 *        If the difference between the input and the reference value is greater than the limiting interpolation,
 *        the reference value + limiting difference is output (the same applies to negative numbers)
 * @param input the input need to be process
 * @param ref referenve value
 * @param err limiting difference
 * @return  the Limited vaule
 */
static float abs_Limit(float input, float ref, float err)
{
    float temp;
    temp = input - ref;   // calibration Coordinate System - Set the ref to zero
    if (fabs(temp) > err) // check the err between input and ref
    {
        if (temp >= 0.0f)     // input bigger than ref
            return ref + err; // output the right Limit
        else
            return ref - err; // output the left Limit
    }
    return input;
}

/**
 * @brief The relative limit function compares a given input to the limit difference.
 *        If the input is greater than the limit interpolation,
 *        Output limit difference (the same applies to negative numbers)
 * @param input the input need to be process
 * @param err limiting difference
 * @return
 */
static float rel_Limit(float input, float err)
{
    if (fabs(input) > err) // check the err between input and ref
    {
        if (input >= 0.0f) // input bigger than ref
            return err;    // output the right Limit
        else
            return -err; // output the left Limit
    }
    return input;
}

//========================== Var PID Content =============================

static float Variable_KP(float err, varPID *conf)
{
    return conf->KP_a + conf->KP_b * (1 - expf(-conf->KP_c * fabsf(err)));
}

static float Variable_KI(float err, varPID *conf)
{
    return conf->KI_a * expf(-conf->KI_c * fabsf(err));
}

static float Variable_KI_Gain(float err, varPID *conf)
{
    if (fabsf(err) > conf->KI_e)
    {
        return 1;
    }
    else
    {
        return conf->KI_1 * expf(-conf->KI_0 * fabsf(err));
    }
}

static float Variable_KD(float err, varPID *conf)
{
    if (conf->KD_a <= conf->KD_b)
    {
        return 0;
    }

    return conf->KD_a - conf->KD_b * (1 - expf(-conf->KD_c * fabsf(err)));
}

//========================== Var PID Content==============================

/**
 * @brief Init PID : Include init the timer ...
 *        (It is best to call it just before starting the PID operation)
 * @param pid the PID need to be init
 */
void GenPID_Init(GenPID_s *pid)
{
    TimeFlash(&pid->time);
}

int GenPID_Set(GenPID_s *pid,GenPID_CONFIG config)
{
    memset(pid,0x00,sizeof(GenPID_s));
    pid->config = config;
    return 0;
}

int GenPID_Set_Needle(GenPID_s *pid,GenPID_CONFIG *config)
{
    memset(pid,0x00,sizeof(GenPID_s));
    pid->config = *config;
    return 0;
}


// float GenPID_Process(GenPID_s *pid, float sv, float cv)
// {
//     float ts, err, Ierr, perr;
//     float p, i, iadd, d;
//     float output;

//     err = sv - cv; // get the err

//     // get the time
//     ts = TimeFlash(&pid->time);

//     // PID parameter transmit DuoWays
//     // pid->sv = sv;
//     // pid->cv = cv;
//     perr = pid->perr;

//     //------------------------------------can combine

//     // p = pid->kp * err;
//     p = Variable_KP(err, &(pid->varPID)) * err;
//     // integral = integral_vel_prev + pid_vel_I * Ts * 0.5 * (error + error_vel_prev);
//     // i = pid->pi + pid->ki * ts * 0.5f * (err + pid->perr);

//     iadd = Variable_KI_Gain(err, &(pid->varPID)) * Variable_KI(err, &(pid->varPID)) * ts * 0.5f * (err + perr);

//     // i = abs_Limit(i , 0 , pid->intLimit);
//     if (fabs(pid->pout) > pid->outputLimit)
//     {
//         if ((pid->pi > 0) && (iadd > 0))
//         {
//             iadd = pid->pi;
//         }
//         else
//         {
//             if ((pid->pi < 0) < (iadd < 0))
//             {
//                 iadd = pid->pi;
//             }
//         }
//     }
//     i = pid->pi + iadd;
//     i = _constrain(i, -pid->intLimit, pid->intLimit);

//     // i = pid->ki * Ierr;
//     // d = pid->kd * (err - perr) / ts; // the kd preformence is not verify
//     d = Variable_KD(err, &(pid->varPID)) * (err - perr) / ts;

//     output = p + i + d;
//     output = rel_Limit(output, pid->outputLimit);

//     // PID parameter transmit SingleWay
//     pid->cv = cv;
//     pid->sv = sv;
//     pid->pv = cv;
//     // pid->err = err;
//     pid->perr = err;
//     pid->pi = i;
//     pid->pout = output;

// // debug---------------------------------------------------------
// #if PID_DEBUG_SW
//     // SIGN1_1_UP;

//     // 串口调试发送 - start
//     DebugFrameLoad_F(2, p);
//     DebugFrameLoad_F(3, i);
//     DebugFrameLoad_F(4, d);
//     DebugFrameLoad_F(5, (output * D2R));
//     DebugFrameLoad_F(6, err);
//     DebugFrameLoad_F(7, Ierr);
//     DebugFrameLoad_F(8, pid->outputLimit);
//     DebugFrameLoad_F(9, pid->varPID.KP_a);
//     DebugFrameLoad_F(10, pid->varPID.KP_b);
//     DebugFrameLoad_F(11, pid->varPID.KP_c);
//     DebugFrameLoad_F(12, pid->varPID.KI_a);
//     DebugFrameLoad_F(13, pid->varPID.KI_c);
//     DebugFrameLoad_F(14, pid->varPID.KI_e);
//     DebugFrameLoad_F(15, pid->varPID.KI_0);
//     DebugFrameLoad_F(16, pid->varPID.KI_1);
//     DebugFrameLoad_F(17, pid->varPID.KD_a);
//     DebugFrameLoad_F(18, pid->varPID.KD_b);
//     DebugFrameLoad_F(19, pid->varPID.KD_c);
// #endif
//     // debug---------------------------------------------------------

//     return output;
// }

// float GenPID_ProcessSTD(GenPID_s *pid, float sv, float cv)
// {
//     float ts, err, perr;
//     float p, i, iadd, d;
//     float output;

//     err = sv - cv; // get the err

//     // get the time
//     ts = TimeFlash(&pid->time);

//     perr = pid->perr;

//     //------------------------------------can combine

//     p = pid->kp * err;
//     iadd = pid->ki * ts * 0.5 * (err + perr);

//     // i = abs_Limit(i , 0 , pid->intLimit);
//     if (fabs(pid->pout) >= pid->outputLimit)
//     {
//         if ((pid->pi > 0) && (iadd > 0))
//         {
//             iadd = pid->pi;
//         }
//         else
//         {
//             if ((pid->pi < 0) < (iadd < 0))
//             {
//                 iadd = pid->pi;
//             }
//         }
//     }
//     i = pid->pi + iadd;
//     i = _constrain(i, -pid->intLimit, pid->intLimit);

//     // i = pid->ki * Ierr;
//     d = pid->kd * (err - perr) / ts; // the kd preformence is not verify
//     // d = Variable_KD(err, pid->varPID) * (err - perr) / ts;

//     output = p + i + d;
//     output = rel_Limit(output, pid->outputLimit);

//     // PID parameter transmit SingleWay
//     pid->cv = cv;
//     pid->sv = sv;
//     pid->pv = cv;
//     // pid->err = err;
//     pid->perr = err;
//     pid->pi = i;
//     pid->pout = output;

// // debug---------------------------------------------------------
// #if PID_DEBUG_SW
//     // SIGN1_1_UP;

//     // 串口调试发送 - start
//     DebugFrameLoad_F(9, p);
//     DebugFrameLoad_F(10, i);
//     DebugFrameLoad_F(11, d);
//     DebugFrameLoad_F(12, output);
//     DebugFrameLoad_F(13, err);
//     DebugFrameLoad_F(14, iadd);
//     // DebugFrameLoad_F(8, pid->outputLimit);

// #endif
//     // debug---------------------------------------------------------

//     return output;
// }


float GenPID_Process(GenPID_s *pid, float sv, float cv, OBS_GenPID *obs)
{

    float Ts = TimeFlash(&(pid->time));

    float err = sv - cv;
    // u(s) = (P + I/s + Ds)e(s)
    // Discrete implementations
    // proportional part
    // u_p  = P *e(k)
    float proportional = Variable_KP(err, &(pid->config.varPID)) * err;
    // Tustin transform of the integral part
    // u_ik = u_ik_1  + I*Ts/2*(ek + ek_1)
    float integral = pid->pi + Variable_KI_Gain(err, &(pid->config.varPID)) * Variable_KI(err, &(pid->config.varPID)) * Ts * 0.5f * (err + pid->perr);
    // antiwindup - limit the output
    integral = _constrain(integral, -pid->config.intLimit, pid->config.intLimit);
    // Discrete derivation
    // u_dk = D(ek - ek_1)/Ts
    float derivative = Variable_KD(err, &(pid->config.varPID)) * (err - pid->perr) / Ts;

    // sum all the components
    float output = proportional + integral + derivative;
    // antiwindup - limit the output variable
    output = _constrain(output, -pid->config.outputLimit, pid->config.outputLimit);

    // if output ramp defined
    if (pid->config.output_ramp > 0)
    {
        // limit the acceleration by ramping the output
        float output_rate = (output - pid->pout) / Ts;
        if (output_rate > pid->config.output_ramp)
            output = pid->pout + pid->config.output_ramp * Ts;
        else if (output_rate < -pid->config.output_ramp)
            output = pid->pout - pid->config.output_ramp * Ts;
    }
    // saving for the next pass
    // integral_prev = integral;
    pid->pi = integral;
    // output_prev = output;
    pid->pout = output;
    // error_prev = error;
    pid->perr = err;

    // OBS Aera
    if (obs != NULL)
    {
        obs->p = proportional;
        obs->i = integral;
        obs->d = derivative;
        obs->output = output;
        obs->err = err;
        obs->pi = pid->pi;
        obs->Ts = Ts;
    }


    return output;
}

float GenPID_ProcessSTD(GenPID_s *pid, float sv, float cv, OBS_GenPID *obs)
{

    float Ts = TimeFlash(&(pid->time));

    float err = sv - cv;
    // u(s) = (P + I/s + Ds)e(s)
    // Discrete implementations
    // proportional part
    // u_p  = P *e(k)
    float proportional = pid->config.kp * err;
    // Tustin transform of the integral part
    // u_ik = u_ik_1  + I*Ts/2*(ek + ek_1)
    float integral = pid->pi + pid->config.ki * Ts * 0.5f * (err + pid->perr);
    // antiwindup - limit the output
    integral = _constrain(integral, -pid->config.intLimit, pid->config.intLimit);
    // Discrete derivation
    // u_dk = D(ek - ek_1)/Ts
    float derivative = pid->config.kd * (err - pid->perr) / Ts;

    // sum all the components
    float output = proportional + integral + derivative;
    // antiwindup - limit the output variable
    output = _constrain(output, -pid->config.outputLimit, pid->config.outputLimit);

    // // if output ramp defined
    // if (pid->output_ramp > 0)
    // {
    //     // limit the acceleration by ramping the output
    //     float output_rate = (output - pid->pout) / Ts;
    //     if (output_rate > pid->output_ramp)
    //         output = pid->pout + pid->output_ramp * Ts;
    //     else if (output_rate < -pid->output_ramp)
    //         output = pid->pout - pid->output_ramp * Ts;
    // }
    // saving for the next pass
    // integral_prev = integral;
    pid->pi = integral;
    // output_prev = output;
    pid->pout = output;
    // error_prev = error;
    pid->perr = err;

    // OBS Aera
    if (obs != NULL)
    {
        obs->p = proportional;
        obs->i = integral;
        obs->d = derivative;
        obs->output = output;
        obs->err = err;
        obs->pi = pid->pi;
        obs->Ts = Ts;
    }


    return output;
}

float GenPID_ProcessSTD_LPIn(GenPID_s *pid, float sv, float cv, float cv_LP,OBS_GenPID *obs)
{

    float Ts = TimeFlash(&(pid->time));

    float err = sv - cv;
    float err_LP = sv - cv_LP;
    // u(s) = (P + I/s + Ds)e(s)
    // Discrete implementations
    // proportional part
    // u_p  = P *e(k)
    float proportional = pid->config.kp * err_LP;
    // Tustin transform of the integral part
    // u_ik = u_ik_1  + I*Ts/2*(ek + ek_1)
    float integral = pid->pi + pid->config.ki * Ts * 0.5f * (err + pid->perr);
    // antiwindup - limit the output
    integral = _constrain(integral, -pid->config.intLimit, pid->config.intLimit);
    // Discrete derivation
    // u_dk = D(ek - ek_1)/Ts
    float derivative = pid->config.kd * (err_LP - pid->perr) / Ts;

    // sum all the components
    float output = proportional + integral + derivative;
    // antiwindup - limit the output variable
    output = _constrain(output, -pid->config.outputLimit, pid->config.outputLimit);

    // if output ramp defined
    if (pid->config.output_ramp > 0)
    {
        // limit the acceleration by ramping the output
        float output_rate = (output - pid->pout) / Ts;
        if (output_rate > pid->config.output_ramp)
            output = pid->pout + pid->config.output_ramp * Ts;
        else if (output_rate < -pid->config.output_ramp)
            output = pid->pout - pid->config.output_ramp * Ts;
    }
    
    // saving for the next pass
    // integral_prev = integral;
    pid->pi = integral;
    // output_prev = output;
    pid->pout = output;
    // error_prev = error;
    pid->perr = err;
    pid->perrLP = err_LP;

    // OBS Aera
    if (obs != NULL)
    {
        obs->p = proportional;
        obs->i = integral;
        obs->d = derivative;
        obs->output = output;
        obs->err = err;
        obs->errLP = err_LP;
        obs->pi = pid->pi;
        obs->Ts = Ts;
    }


    return output;
}



float GenPID_ProcessSTD_ErrIN(GenPID_s *pid, float err,OBS_GenPID *obs)
{
    float Ts = TimeFlash(&(pid->time));

    // u(s) = (P + I/s + Ds)e(s)
    // Discrete implementations
    // proportional part
    // u_p  = P *e(k)
    float proportional = pid->config.kp * err;
    // Tustin transform of the integral part
    // u_ik = u_ik_1  + I*Ts/2*(ek + ek_1)
    float integral = pid->pi + pid->config.ki * Ts * 0.5f * (err + pid->perr);
    // antiwindup - limit the output
    integral = _constrain(integral, -pid->config.intLimit, pid->config.intLimit);
    // Discrete derivation
    // u_dk = D(ek - ek_1)/Ts
    float derivative = pid->config.kd * (err - pid->perr) / Ts;

    // sum all the components
    float output = proportional + integral + derivative;
    // antiwindup - limit the output variable
    output = _constrain(output, -pid->config.outputLimit, pid->config.outputLimit);

    // // if output ramp defined
    // if (pid->output_ramp > 0)
    // {
    //     // limit the acceleration by ramping the output
    //     float output_rate = (output - pid->pout) / Ts;
    //     if (output_rate > pid->output_ramp)
    //         output = pid->pout + pid->output_ramp * Ts;
    //     else if (output_rate < -pid->output_ramp)
    //         output = pid->pout - pid->output_ramp * Ts;
    // }
    // saving for the next pass
    // integral_prev = integral;
    pid->pi = integral;
    // output_prev = output;
    pid->pout = output;
    // error_prev = error;
    pid->perr = err;

    // OBS Aera
    if (obs != NULL)
    {
        obs->p = proportional;
        obs->i = integral;
        obs->d = derivative;
        obs->output = output;
        obs->err = err;
        obs->pi = pid->pi;
        obs->Ts = Ts;
    }

    return output;
}