fork download
  1. #include <stdio.h>
  2. #include <stdint.h>
  3.  
  4. #define MOTORVEL_HISTORY_LENGTH (40)
  5. static int position;
  6. static int motorVelIndex;
  7. static int motorVel;
  8. static int motorVelHistory[MOTORVEL_HISTORY_LENGTH];
  9. static void PID_RunUpdateMotorVel(void)
  10. {
  11. /*
  12.  * Also, since the trajectory generation has big variations, we will
  13.  * feedback the motor velocity directly here.
  14.  *
  15.  * The maximum speed we go should fit in 16 bits, so overflows should
  16.  * not matter here.
  17.  *
  18.  * We make this selectable, since the low pass in the current PI loop
  19.  * may take care of this for us. We may have to implement an error
  20.  * dithering to account for range, will test to find out.
  21.  */
  22. const int32_t motorpos = position; /* Cache the volatile */
  23. if (motorVelIndex >= MOTORVEL_HISTORY_LENGTH - 1)
  24. {
  25. motorVelIndex = 0;
  26. }
  27. else
  28. {
  29. motorVelIndex++;
  30. }
  31. motorVel = motorpos - motorVelHistory[motorVelIndex];
  32.  
  33. motorVelHistory[motorVelIndex] = motorpos;
  34. }
  35.  
  36. int main(void) {
  37. // your code goes here
  38. for (int i = 0; i < MOTORVEL_HISTORY_LENGTH + 2; i++)
  39. {
  40. position = i;
  41. PID_RunUpdateMotorVel();
  42. printf("Motor Vel=%d\n", motorVel);
  43. }
  44. return 0;
  45. }
  46.  
Success #stdin #stdout 0.01s 5324KB
stdin
Standard input is empty
stdout
Motor Vel=0
Motor Vel=1
Motor Vel=2
Motor Vel=3
Motor Vel=4
Motor Vel=5
Motor Vel=6
Motor Vel=7
Motor Vel=8
Motor Vel=9
Motor Vel=10
Motor Vel=11
Motor Vel=12
Motor Vel=13
Motor Vel=14
Motor Vel=15
Motor Vel=16
Motor Vel=17
Motor Vel=18
Motor Vel=19
Motor Vel=20
Motor Vel=21
Motor Vel=22
Motor Vel=23
Motor Vel=24
Motor Vel=25
Motor Vel=26
Motor Vel=27
Motor Vel=28
Motor Vel=29
Motor Vel=30
Motor Vel=31
Motor Vel=32
Motor Vel=33
Motor Vel=34
Motor Vel=35
Motor Vel=36
Motor Vel=37
Motor Vel=38
Motor Vel=39
Motor Vel=40
Motor Vel=40