ALGO_PID_ATY.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * @file ALGO_PID_ATY.c
  3. *
  4. * @param Project ALGO_Algorithm_ATY_LIB
  5. *
  6. * @author ATY
  7. *
  8. * @copyright
  9. * - Copyright 2017 - 2026 MZ-ATY
  10. * - This code follows:
  11. * - MZ-ATY Various Contents Joint Statement -
  12. * <a href="https://mengze.top/MZ-ATY_VCJS">
  13. * https://mengze.top/MZ-ATY_VCJS</a>
  14. * - CC 4.0 BY-NC-SA -
  15. * <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
  16. * https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
  17. * - Your use will be deemed to have accepted the terms of this statement.
  18. *
  19. * @brief functions of PID for all codes
  20. *
  21. * @version
  22. * - 1_01_200318 > ATY
  23. * -# Preliminary version, first Release
  24. * - 1_02_200318 > ATY
  25. * -# Add Conditional Compilation
  26. * -# Add user port
  27. **********************************************************************************
  28. */
  29. #ifndef __ALGO_PID_ATY_C
  30. #define __ALGO_PID_ATY_C
  31. #include "ALGO_PID_ATY.h"
  32. /******************************* For user *************************************/
  33. /******************************************************************************/
  34. /**
  35. * @brief Calculater PID
  36. * @param pidStruct pid struct
  37. * @param nextPoint next point
  38. * @return pidStruct
  39. */
  40. double PidCalc(PID* pidStruct, double nextPoint){
  41. double dError, Error;
  42. Error = pidStruct->SetPoint - nextPoint; // Deviation
  43. pidStruct->SumError += Error; // Integral
  44. dError = pidStruct->LastError - pidStruct->PrevError; // Current differential
  45. pidStruct->PrevError = pidStruct->LastError;
  46. pidStruct->LastError = Error;
  47. return (pidStruct->Proportion * Error // Proportional
  48. + pidStruct->Integral * pidStruct->SumError // Integral item
  49. + pidStruct->Derivative * dError); // Differential item
  50. }
  51. /**
  52. * @brief Init pid to zero
  53. * @param pidStruct pid struct
  54. */
  55. void PidInitZero(PID* pidStruct){
  56. pidStruct->SetPoint = 0;
  57. pidStruct->Proportion = 0;
  58. pidStruct->Integral = 0;
  59. pidStruct->Derivative = 0;
  60. pidStruct->LastError = 0;
  61. pidStruct->PrevError = 0;
  62. pidStruct->SumError = 0;
  63. // memset(pidStruct, 0, sizeof(PID));
  64. }
  65. /**
  66. * @brief Init pid to zero
  67. * @param pidStruct pid struct
  68. */
  69. void PidClearZero(PID* pidStruct){
  70. // pidStruct->SetPoint = 0;
  71. // pidStruct->Proportion = 0;
  72. // pidStruct->Integral = 0;
  73. // pidStruct->Derivative = 0;
  74. pidStruct->LastError = 0;
  75. pidStruct->PrevError = 0;
  76. pidStruct->SumError = 0;
  77. }
  78. #ifdef ALGO_PID_ATY_Test_ATY
  79. double pidCurNum = 0, pidAdjust = 0;
  80. PID sPID = {-10.0, 1.0, 0.1, 0.01, 0, 0, 0};
  81. /**
  82. * @brief Pid control, put at cycle
  83. */
  84. void ALGO_PID_ATY_Test(void){
  85. // sPID.SetPoint = pidSetNum;
  86. pidAdjust = PidCalc(&sPID, pidCurNum);
  87. pidCurNum += pidAdjust * 0.1; // simulate the delay
  88. printf_ATY_D("\r\npidAdjust = %f, pidCurNum = %f\r\n", pidAdjust, pidCurNum);
  89. }
  90. #endif /* ALGO_PID_ATY_Test_ATY */
  91. #endif /* __ALGO_PID_ATY_C */
  92. /******************************** End Of File *********************************/