ALGO_PID_ATY.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 - 2023 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 Familiar 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. * -Undone
  28. **********************************************************************************
  29. */
  30. #ifndef __ALGO_PID_ATY_C
  31. #define __ALGO_PID_ATY_C
  32. #include "ALGO_PID_ATY.h"
  33. /******************************* For user *************************************/
  34. /******************************************************************************/
  35. /**
  36. * @brief Calculater PID
  37. * @param pidStruct pid struct
  38. * @param nextPoint next point
  39. * @return pidStruct
  40. */
  41. double PidCalc(PID* pidStruct, double nextPoint)
  42. {
  43. double dError, Error;
  44. Error = pidStruct->SetPoint - nextPoint; //Deviation
  45. pidStruct->SumError += Error; //Integral
  46. dError = pidStruct->LastError - pidStruct->PrevError; //Current differential
  47. pidStruct->PrevError = pidStruct->LastError;
  48. pidStruct->LastError = Error;
  49. return (pidStruct->Proportion * Error //Proportional
  50. + pidStruct->Integral * pidStruct->SumError //Integral item
  51. + pidStruct->Derivative * dError); //Differential item
  52. }
  53. /**
  54. * @brief Init pid to zero
  55. * @param pidStruct pid struct
  56. */
  57. void PidInitZero(PID* pidStruct)
  58. {
  59. pidStruct->SetPoint = 0;
  60. pidStruct->Proportion = 0;
  61. pidStruct->Integral = 0;
  62. pidStruct->Derivative = 0;
  63. pidStruct->LastError = 0;
  64. pidStruct->PrevError = 0;
  65. pidStruct->SumError = 0;
  66. // memset(pidStruct, 0, sizeof(PID));
  67. }
  68. /**
  69. * @brief Init pid to zero
  70. * @param pidStruct pid struct
  71. */
  72. void PidClearZero(PID* pidStruct)
  73. {
  74. // pidStruct->SetPoint = 0;
  75. // pidStruct->Proportion = 0;
  76. // pidStruct->Integral = 0;
  77. // pidStruct->Derivative = 0;
  78. pidStruct->LastError = 0;
  79. pidStruct->PrevError = 0;
  80. pidStruct->SumError = 0;
  81. }
  82. #ifdef __DEBUG_ALGO_PID_ATY
  83. double pidSetNum = 0, pidCurNum = 0;
  84. /**
  85. * @brief Calculate pidCurNum
  86. * @param rDelta - rDelta
  87. */
  88. void Actuator(double rDelta)
  89. {
  90. pidCurNum += rDelta;
  91. }
  92. /**
  93. * @brief Pid control
  94. */
  95. void PidControl(void)
  96. {
  97. PID sPID[2]; //PID Control Struct
  98. double rOut[2]; //PID Response (Output)
  99. double rIn[2]; //PID Feedback (Input)
  100. uint8_t tempPid_i = 0;
  101. PidInitZero(&sPID[0]); //Initialize Struct
  102. sPID[0].Proportion = 0.5; //Set PID Coefficients
  103. sPID[0].Integral = 0.5;
  104. sPID[0].Derivative = 0;
  105. sPID[0].SetPoint = pidSetNum; //Set PID Setpoint (Goal)
  106. while(1) //Better torite in timer interrupt call back function
  107. { //Mock Up of PID processing
  108. if(tempPid_i > 50)
  109. {
  110. tempPid_i = 0;
  111. pidSetNum += 10;
  112. if(pidSetNum > 100)
  113. pidSetNum = 0;
  114. }
  115. tempPid_i++;
  116. sPID[0].SetPoint = pidSetNum;
  117. rIn[0] = pidCurNum; //Reads the input variable function (Read Input )
  118. rOut[0] = PidCalc(&sPID[0], rIn[0]); //PID calculation function (Perform PID Interation)
  119. Actuator(rOut[0]); //Output variable control function (Effect Needed Changes)
  120. // printf("In: %f\nOut: %f\nPidCur: %f\n\n", rIn[0], rOut[0], pidCurNum);
  121. // printf("%f\n", pidCurNum);
  122. DelayMs(1);
  123. }
  124. }
  125. #endif /* __DEBUG_ALGO_PID_ATY */
  126. #endif /* __ALGO_PID_ATY_C */
  127. /******************************** End Of File *********************************/