03_main.ino 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. char outputStr[100];
  2. int16_t averages[2][AVG_PERIODS];
  3. void loop() {
  4. #ifdef SCAN_MODE
  5. for(byte ch = LOW_CHANNEL; ch <= NUM_CHANNELS; ch++) {
  6. int val = channelVals[ch];
  7. sprintf(outputStr, "Channel-%d:%d\t", ch, val);
  8. Serial.print(outputStr);
  9. }
  10. Serial.println();
  11. #else
  12. int throttle = getThrottle();
  13. throttle = restrictThrottlePower(throttle);
  14. throttle = getAverage(throttle);
  15. sprintf(outputStr, "Throttle:%d", throttle);
  16. Serial.println(outputStr);
  17. digitalWrite(LEFT_MOTOR_DIRECT_PIN, throttle > 0 ? HIGH : LOW);
  18. analogWrite(LEFT_MOTOR_PIN, throttle > 0 ? throttle : -throttle);
  19. #endif
  20. delay(SCAN_DELAY);
  21. }
  22. const int throtDeadHigh = THROT_NONE + THROT_DEAD_ZONE,
  23. throtDeadLow = THROT_NONE - THROT_DEAD_ZONE;
  24. int getThrottle() {
  25. int val = channelVals[THROT_CHANNEL];
  26. #ifndef GRAPH
  27. Serial.println();
  28. Serial.print("Channel value: ");
  29. Serial.print(val);
  30. Serial.print("\t");
  31. #endif
  32. if(val <= THROT_OFF) {
  33. #ifndef GRAPH
  34. Serial.println("Throttle is off");
  35. #endif
  36. return 0;
  37. } else if(val < throtDeadHigh && val > throtDeadLow) {
  38. #ifndef GRAPH
  39. Serial.println("Throttle in dead zone");
  40. #endif
  41. return 0;
  42. }
  43. int throttle = val < THROT_NONE
  44. ? map(val, THROT_MIN, THROT_NONE, -OUTPUT_RANGE, 0)
  45. : map(val, THROT_NONE, THROT_MAX, 0, OUTPUT_RANGE);
  46. throttle = constrain(throttle, -OUTPUT_RANGE, OUTPUT_RANGE);
  47. #ifndef GRAPH
  48. Serial.print("Throttle: ");
  49. Serial.println(throttle);
  50. #endif
  51. return throttle;
  52. }
  53. int restrictThrottlePower(int throttle) {
  54. // Reduce the average voltage to something safe for the motor to consume.
  55. throttle = (throttle * MAX_POWER_PERCENT) / 100;
  56. #ifndef GRAPH
  57. Serial.print("Restricted throttle:\t");
  58. Serial.println(throttle);
  59. #endif
  60. // Use the current value of the knob to reduce the speed.
  61. int speedKnob = channelVals[SPEED_CHANNEL];
  62. speedKnob = map(speedKnob, SPEED_MIN, SPEED_MAX, 0, 100);
  63. speedKnob = constrain(speedKnob, 0, 100);
  64. throttle = (throttle * speedKnob) / 100;
  65. #ifndef GRAPH
  66. Serial.print("Speed knob:\t");
  67. Serial.println(speedKnob);
  68. Serial.print("Speed adjusted throttle:\t");
  69. Serial.println(throttle);
  70. #endif
  71. return throttle;
  72. }
  73. int getAverage(const int throttle) {
  74. int total = throttle;
  75. for(byte i = 0; i < AVG_PERIODS-1; i++) {
  76. total += averages[i];
  77. averages[i] = averages[i+1];
  78. }
  79. averages[AVG_PERIODS-1] = throttle;
  80. total /= AVG_PERIODS;
  81. #ifndef GRAPH
  82. Serial.print("Average adjusted throttle:\t");
  83. Serial.println(total);
  84. #endif
  85. return total;
  86. }