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.7KB

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