mc_pot.c 970 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * mc_pot.c
  3. *
  4. * Author: Andrey Koryagin https://blog.avislab.com/
  5. */
  6. #include "u_foc.h"
  7. uint32_t POT_BUF[POT_BUF_LEN];
  8. uint8_t POT_BUF_Index;
  9. uint32_t POT_AVER;
  10. uint16_t POT_SPEED;
  11. void POT_Process() {
  12. uint8_t i;
  13. POT_BUF[POT_BUF_Index] = MC_ADC_GetRegularValue(ADC_POT);
  14. POT_BUF_Index++;
  15. if (POT_BUF_Index >= POT_BUF_LEN) {
  16. POT_BUF_Index = 0;
  17. }
  18. for (i=0; i<POT_BUF_LEN; i++) {
  19. POT_AVER += POT_BUF[i];
  20. }
  21. POT_AVER = POT_AVER / POT_BUF_LEN;
  22. if (POT_AVER > POT_START_VALUE) {
  23. POT_SPEED = POT_MIN_RPM + POT_MAX_RPM * (POT_AVER - POT_START_VALUE) / (POT_MAX_VALUE-POT_START_VALUE);
  24. if (POT_SPEED > POT_MAX_RPM) {
  25. POT_SPEED = POT_MAX_RPM;
  26. }
  27. if (MC_GetState() == MC_STATE_IDLE) {
  28. MC_Start();
  29. }
  30. } else {
  31. if (POT_AVER < POT_STOP_VALUE) {
  32. POT_SPEED = 0;
  33. if (MC_GetState() == MC_STATE_RUN) {
  34. MC_Stop();
  35. }
  36. if (MC_GetState() == MC_STATE_FAULT) {
  37. MC_ResetFault();
  38. }
  39. }
  40. }
  41. MC_SetSpeedRamp(POT_SPEED, 10);
  42. }