ESC stuff
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bikecontroller.ino 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include <Servo.h>
  2. #include <SSD1306Ascii.h>
  3. #include <SSD1306AsciiAvrI2c.h>
  4. #define PIN_POT A0
  5. #define PIN_ESC 5
  6. #define PIN_THERM A7
  7. #define PIN_BAT A2
  8. #define MIN_SPEED 1000
  9. #define MAX_SPEED 1700
  10. #define ZERO_SPEED 900
  11. #define POT_MIN 220
  12. #define POT_MAX 1023
  13. #define STEP 10
  14. #define DISPLAY_I2C 0x3C
  15. #define DISPLAY_CHARS 11
  16. #define BAT_MAX 4.2
  17. #define BAT_MIN 3.6
  18. #define BAT_CELLS 6
  19. #define BAT_MULT 5
  20. #define THERM_RESISTOR 10000
  21. #define STATS_TIMER 3
  22. #define THRESHOLD 5
  23. #define DELAY 50
  24. #define USE_DISPLAY
  25. Servo esc;
  26. #ifdef USE_DISPLAY
  27. SSD1306AsciiAvrI2c display;
  28. #endif
  29. void setup() {
  30. Serial.begin(115200);
  31. Serial.println("Hello");
  32. #ifdef USE_DISPLAY
  33. Serial.println("Looking for i3c 0x"+String(DISPLAY_I2C, 16)+"...");
  34. display.begin(&Adafruit128x32, DISPLAY_I2C);
  35. display.setFont(Adafruit5x7);
  36. display.clear();
  37. display.set2X();
  38. display.println("SuperSkuter");
  39. display.println("3000");
  40. #endif
  41. pinMode(PIN_POT, INPUT);
  42. pinMode(PIN_THERM, INPUT);
  43. pinMode(PIN_BAT, INPUT);
  44. esc.attach(PIN_ESC);
  45. esc.writeMicroseconds(ZERO_SPEED);
  46. delay(4000);
  47. Serial.println("ready");
  48. }
  49. int prev = -1;
  50. float measureTemp() {
  51. int val = analogRead(PIN_THERM);
  52. float r2 = (THERM_RESISTOR / 10.0) / (1023.0 / (float)val - 1.0);
  53. float logR2 = log(r2);
  54. float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;
  55. float t = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2));
  56. float celsius = t - 273.15;
  57. return celsius;
  58. }
  59. int measureBat() {
  60. int val = analogRead(PIN_BAT);
  61. float volt = val * (5.0 / 1023.0);
  62. float currV = volt * BAT_MULT;
  63. float minV = BAT_MIN * BAT_CELLS;
  64. float maxV = BAT_MAX * BAT_CELLS;
  65. if (currV < minV)
  66. currV = minV;
  67. if (currV > maxV)
  68. currV = maxV;
  69. currV -= minV;
  70. maxV -= minV;
  71. return (currV / maxV) * 100;
  72. }
  73. void printTwo(String s1, String s2) {
  74. #ifdef USE_DISPLAY
  75. int nspaces = DISPLAY_CHARS - (s1.length() + s2.length());
  76. char spaces[DISPLAY_CHARS + 1];
  77. memset(spaces, ' ', DISPLAY_CHARS);
  78. spaces[nspaces] = '\0';
  79. display.println(s1+String(spaces)+s2);
  80. #endif
  81. }
  82. int statsCount = 1;
  83. void showStats(int actualSpeed, int potmeterVal) {
  84. statsCount -= 1;
  85. if (statsCount != 0)
  86. return;
  87. statsCount = STATS_TIMER + 1;
  88. int throttle = ((double)(actualSpeed - MIN_SPEED) / (double)(MAX_SPEED - MIN_SPEED)) * 100;
  89. #ifdef USE_DISPLAY
  90. display.setCursor(0, 0);
  91. display.set2X();
  92. #endif
  93. String pot = String(potmeterVal);
  94. String thr = String(throttle)+"%";
  95. String spd = String(actualSpeed);
  96. String tmp = String(round(measureTemp()))+"C";
  97. String bat = String(measureBat())+"%";
  98. printTwo(thr, pot+" ");
  99. printTwo(tmp, bat+" ");
  100. }
  101. void loop() {
  102. int pressed = 0;
  103. int reachedMax = 0;
  104. pressed = 1;
  105. int pot = analogRead(PIN_POT);
  106. int speed = map(pot, POT_MIN, POT_MAX, 0, MAX_SPEED - MIN_SPEED);
  107. speed = speed / STEP;
  108. speed = speed * STEP;
  109. int actualSpeed = speed + MIN_SPEED;
  110. if (abs(actualSpeed - ZERO_SPEED) < THRESHOLD)
  111. actualSpeed = ZERO_SPEED;
  112. else if (actualSpeed > MAX_SPEED)
  113. actualSpeed = MAX_SPEED;
  114. else if (actualSpeed < MIN_SPEED)
  115. actualSpeed = MIN_SPEED;
  116. if (actualSpeed != prev) {
  117. esc.writeMicroseconds(actualSpeed);
  118. prev = actualSpeed;
  119. }
  120. showStats(actualSpeed, pot);
  121. delay(DELAY);
  122. }