Advertisement
Bukisoh

Vélo électrique v8

Jun 3rd, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.62 KB | None | 0 0
  1. #include <Servo.h>
  2. #include <Wire.h>
  3. #include <LiquidCrystal_I2C.h>
  4. #include "VescUart.h"
  5. #include "datatypes.h"
  6.  
  7.  
  8.  
  9. LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
  10. Servo esc;
  11.  
  12. /* Constantes pour la taille de l'écran LCD */
  13. const int LCD_NB_ROWS = 4;
  14. const int LCD_NB_COLUMNS = 20;
  15.  
  16. /* Caractères personnalisés */
  17. byte START_DIV_0_OF_4[8] = {
  18.   B01111,
  19.   B11000,
  20.   B10000,
  21.   B10000,
  22.   B10000,
  23.   B10000,
  24.   B11000,
  25.   B01111
  26. }; // Char début 0 / 4
  27.  
  28. byte START_DIV_1_OF_4[8] = {
  29.   B01111,
  30.   B11000,
  31.   B10000,
  32.   B10000,
  33.   B10000,
  34.   B10011,
  35.   B11000,
  36.   B01111
  37. }; // Char début 1 / 4
  38.  
  39. byte START_DIV_2_OF_4[8] = {
  40.   B01111,
  41.   B11000,
  42.   B10000,
  43.   B10000,
  44.   B10111,
  45.   B10011,
  46.   B11000,
  47.   B01111
  48. }; // Char début 2 / 4
  49.  
  50. byte START_DIV_3_OF_4[8] = {
  51.   B01111,
  52.   B11000,
  53.   B10000,
  54.   B10111,
  55.   B10111,
  56.   B10011,
  57.   B11000,
  58.   B01111
  59. }; // Char début 3 / 4
  60.  
  61. byte START_DIV_4_OF_4[8] = {
  62.   B01111,
  63.   B11000,
  64.   B10011,
  65.   B10111,
  66.   B10111,
  67.   B10011,
  68.   B11000,
  69.   B01111
  70. }; // Char début 4 / 4
  71.  
  72. byte DIV_0_OF_8[8] = {
  73.   B11111,
  74.   B00000,
  75.   B00000,
  76.   B00000,
  77.   B00000,
  78.   B00000,
  79.   B00000,
  80.   B11111
  81. }; // Char milieu 0 / 8
  82.  
  83. byte DIV_1_OF_8[8] = {
  84.   B11111,
  85.   B00000,
  86.   B00000,
  87.   B00000,
  88.   B00000,
  89.   B11000,
  90.   B00000,
  91.   B11111
  92. }; // Char milieu 1 / 8
  93.  
  94. byte DIV_2_OF_8[8] = {
  95.   B11111,
  96.   B00000,
  97.   B00000,
  98.   B00000,
  99.   B11000,
  100.   B11000,
  101.   B00000,
  102.   B11111
  103. }; // Char milieu 2 / 8
  104.  
  105. byte DIV_3_OF_8[8] = {
  106.   B11111,
  107.   B00000,
  108.   B00000,
  109.   B11000,
  110.   B11000,
  111.   B11000,
  112.   B00000,
  113.   B11111
  114. }; // Char milieu 3 / 8
  115.  
  116. byte DIV_4_OF_8[8] = {
  117.   B11111,
  118.   B00000,
  119.   B11000,
  120.   B11000,
  121.   B11000,
  122.   B11000,
  123.   B00000,
  124.   B11111
  125. }; // Char milieu 4 / 8
  126.  
  127. byte DIV_5_OF_8[8] = {
  128.   B11111,
  129.   B00000,
  130.   B11000,
  131.   B11000,
  132.   B11000,
  133.   B11011,
  134.   B00000,
  135.   B11111
  136. }; // Char milieu 5 / 8
  137.  
  138. byte DIV_6_OF_8[8] = {
  139.   B11111,
  140.   B00000,
  141.   B11000,
  142.   B11000,
  143.   B11011,
  144.   B11011,
  145.   B00000,
  146.   B11111
  147. }; // Char milieu 6 / 8
  148.  
  149. byte DIV_7_OF_8[8] = {
  150.   B11111,
  151.   B00000,
  152.   B11000,
  153.   B11011,
  154.   B11011,
  155.   B11011,
  156.   B00000,
  157.   B11111
  158. }; // Char milieu 7 / 8
  159.  
  160. byte DIV_8_OF_8[8] = {
  161.   B11111,
  162.   B00000,
  163.   B11011,
  164.   B11011,
  165.   B11011,
  166.   B11011,
  167.   B00000,
  168.   B11111
  169. }; // Char milieu 8 / 8
  170.  
  171. byte END_DIV_0_OF_4[8] = {
  172.   B11110,
  173.   B00011,
  174.   B00001,
  175.   B00001,
  176.   B00001,
  177.   B00001,
  178.   B00011,
  179.   B11110
  180. }; // Char fin 0 / 4
  181.  
  182. byte END_DIV_1_OF_4[8] = {
  183.   B11110,
  184.   B00011,
  185.   B00001,
  186.   B00001,
  187.   B00001,
  188.   B11001,
  189.   B00011,
  190.   B11110
  191. }; // Char fin 1 / 4
  192.  
  193. byte END_DIV_2_OF_4[8] = {
  194.   B11110,
  195.   B00011,
  196.   B00001,
  197.   B00001,
  198.   B11101,
  199.   B11001,
  200.   B00011,
  201.   B11110
  202. }; // Char fin 2 / 4
  203.  
  204. byte END_DIV_3_OF_4[8] = {
  205.   B11110,
  206.   B00011,
  207.   B00001,
  208.   B11101,
  209.   B11101,
  210.   B11001,
  211.   B00011,
  212.   B11110
  213. }; // Char fin 3 / 4
  214.  
  215. byte END_DIV_4_OF_4[8] = {
  216.   B11110,
  217.   B00011,
  218.   B11001,
  219.   B11101,
  220.   B11101,
  221.   B11001,
  222.   B00011,
  223.   B11110
  224. }; // Char fin 4 / 4
  225.  
  226.  
  227. char Str1[21];
  228. char Str2[21];
  229. char Str3[21];
  230. char Str4[21];
  231.  
  232. bool etat = false;
  233. int chrono = 0;
  234. int temps = 0;
  235. double vitesse = 0;
  236.  
  237. int hallSensorPin = 2;
  238. volatile int compteur = 0;
  239.  
  240. char str_vitesse[5];
  241. char str_tension[5];
  242. char str_courant[5];
  243. char str_puissance[5];
  244. char str_wh[5];
  245. char percentleft[5];
  246.  
  247. void setup() {
  248.   Serial.begin(115200);  // VESC
  249.  
  250.   lcd.begin(LCD_NB_COLUMNS, LCD_NB_ROWS);
  251.   setup_progressbar();
  252.   lcd.clear();
  253.  
  254.   esc.attach(9);
  255.  
  256.   pinMode(hallSensorPin, INPUT);
  257.   attachInterrupt(0, detectaimant, LOW);
  258.  
  259.   int percentall = measuredValues.inpVoltage * 10
  260. }
  261. struct bldcMeasure measuredValues;
  262. void loop() {
  263.  
  264.   chrono = millis();
  265.   if (etat == true) {
  266.     etat = false;
  267.  
  268.     chrono = chrono - temps;
  269.     vitesse = (2 / (chrono / 1000.0)) * 3.6;
  270.     delay(5);
  271.     temps = millis();
  272.  
  273.     Serial.println(vitesse);
  274.     dtostrf(vitesse, 4, 2, str_vitesse);
  275.   }
  276.  
  277.  
  278.  
  279.   float tension = measuredValues.inpVoltage;
  280.   dtostrf(tension, 4, 1, str_tension);
  281.  
  282.   float courant = measuredValues.avgMotorCurrent;
  283.   dtostrf(courant, 4, 1, str_courant);
  284.  
  285.   float puissance = measuredValues.power;
  286.   dtostrf(puissance, 4, 0, str_puissance);
  287.  
  288.   float Nmot = measuredValues.RPM;
  289.  
  290.   float wh = measuredValues.ampHours * measuredValues.inpVoltage;
  291.   dtostrf(wh, 4, 0, str_wh);
  292.  
  293.   float percentleft = wh / percentall;
  294.   dtostrf(percentleft, 3, 1, str_percentleft)
  295.  
  296.   delay(10);
  297.   sprintf(Str1, "Vitesse: %s km/h", str_vitesse);
  298.   lcd.setCursor(0, 0);
  299.   lcd.print(Str1);
  300.  
  301.   delay(10);
  302.   sprintf(Str2, "%s V/ %s A/ %s W ", str_tension, str_courant, str_puissance);
  303.   lcd.setCursor(0, 1);
  304.   lcd.print(Str2);
  305.  
  306.   /*
  307.     delay(10);
  308.     sprintf(Str3, "Wh= /%left= ", str_wh, str_percentleft);
  309.     lcd.setCursor(0, 2);
  310.     lcd.print(Str3);
  311.   */
  312.  
  313.   delay(10);
  314.   ligne = 2;
  315.   percent = map(analogRead(A1), 0, 1020, 0, 100);
  316.   draw_progressbar(percent, ligne);
  317.  
  318.   delay(10);
  319.   ligne = 3;
  320.   percent = map(Nmot, 0, 600, 0, 100);     // vitesse max moteur
  321.   draw_progressbar(percent, ligne);
  322. }
  323.  
  324. float PWM()
  325. {
  326.   int PWMa = analogRead(A1);
  327.   PWMa = map(PWM, 0, 1024, 0, 179);
  328.  
  329.   esc.write(PWMa);
  330. }
  331.  
  332. void detectaimant()
  333. {
  334.   etat = true;
  335. }
  336.  
  337. void setup_progressbar() {
  338.  
  339.   /* Enregistre les caractères personnalisés dans la mémoire de l'écran LCD */
  340.   lcd.createChar(0, START_DIV_4_OF_4);
  341.   lcd.createChar(1, DIV_0_OF_8);
  342.   lcd.createChar(2, DIV_8_OF_8);
  343.   lcd.createChar(3, END_DIV_0_OF_4);
  344.   // Les autres caractéres sont configurés dynamiquement
  345. }
  346.  
  347. void switch_progressbar_bank(byte bank) {
  348.  
  349.   // IMPORTANT : Il est nécessaire de faire un lcd.clear() ou un lcd.setCursor() aprés chaque changement de banque.
  350.  
  351.   /* Change de banque de caractères */
  352.   switch (bank) {
  353.     case 0:
  354.       lcd.createChar(4, START_DIV_0_OF_4);
  355.       lcd.createChar(5, START_DIV_1_OF_4);
  356.       lcd.createChar(6, START_DIV_2_OF_4);
  357.       lcd.createChar(7, START_DIV_3_OF_4);
  358.       break;
  359.  
  360.     case 1:
  361.       lcd.createChar(4, DIV_1_OF_8);
  362.       lcd.createChar(5, DIV_2_OF_8);
  363.       lcd.createChar(6, DIV_3_OF_8);
  364.       lcd.createChar(7, DIV_4_OF_8);
  365.       break;
  366.  
  367.     case 2:
  368.       lcd.createChar(4, DIV_4_OF_8);
  369.       lcd.createChar(5, DIV_5_OF_8);
  370.       lcd.createChar(6, DIV_6_OF_8);
  371.       lcd.createChar(7, DIV_7_OF_8);
  372.       break;
  373.  
  374.     case 3:
  375.       lcd.createChar(4, END_DIV_1_OF_4);
  376.       lcd.createChar(5, END_DIV_2_OF_4);
  377.       lcd.createChar(6, END_DIV_3_OF_4);
  378.       lcd.createChar(7, END_DIV_4_OF_4);
  379.       break;
  380.   }
  381. }
  382.  
  383. void draw_progressbar(byte percent, int ligne) {
  384.  
  385.   lcd.setCursor(0, ligne);
  386.  
  387.   /* Map la plage (0 ~ 100) vers la plage (0 ~ LCD_NB_COLUMNS * 2 * 4 - 2 * 4) */
  388.   byte nb_columns = map(percent, 0, 100, 0, LCD_NB_COLUMNS * 2 * 4 - 2 * 4);
  389.   // Chaque caractére affiche 2 barres verticales de 4 pixels de haut, mais le premier et dernier caractére n'en affiche qu'une.
  390.  
  391.   /* Dessine chaque caractére de la ligne */
  392.   for (byte i = 0; i < LCD_NB_COLUMNS; ++i) {
  393.  
  394.     if (i == 0) { // Premiére case
  395.  
  396.       /* Affiche le char de début en fonction du nombre de colonnes */
  397.       if (nb_columns > 4) {
  398.         lcd.write((byte) 0); // Char début 4 / 4
  399.         nb_columns -= 4;
  400.  
  401.       } else if (nb_columns == 4) {
  402.         lcd.write((byte) 0); // Char début 4 / 4
  403.         nb_columns = 0;
  404.  
  405.       } else {
  406.         switch_progressbar_bank(0);
  407.         lcd.setCursor(i, 1);
  408.         lcd.write(nb_columns + 4); // Char début N / 4
  409.         nb_columns = 0;
  410.       }
  411.  
  412.     } else if (i == LCD_NB_COLUMNS - 1) { // Derniére case
  413.  
  414.       /* Affiche le char de fin en fonction du nombre de colonnes */
  415.       if (nb_columns > 0) {
  416.         switch_progressbar_bank(3);
  417.         lcd.setCursor(i, 1);
  418.         lcd.write(nb_columns + 3); // Char fin N / 4
  419.  
  420.       } else {
  421.         lcd.write(3); // Char fin 0 / 4
  422.       }
  423.  
  424.     } else { // Autres cases
  425.  
  426.       /* Affiche le char adéquat en fonction du nombre de colonnes */
  427.       if (nb_columns == 0) {
  428.         lcd.write(1); // Char div 0 / 8
  429.  
  430.       } else if (nb_columns >= 8) {
  431.         lcd.write(2); // Char div 8 / 8
  432.         nb_columns -= 8;
  433.  
  434.       } else if (nb_columns >= 4 && nb_columns < 8) {
  435.         switch_progressbar_bank(2);
  436.         lcd.setCursor(i, 1);
  437.         lcd.write(nb_columns); // Char div N / 8
  438.         nb_columns = 0;
  439.  
  440.       } else if (nb_columns < 4) {
  441.         switch_progressbar_bank(1);
  442.         lcd.setCursor(i, 1);
  443.         lcd.write(nb_columns + 3); // Char div N / 8
  444.         nb_columns = 0;
  445.       }
  446.     }
  447.   }
  448. }
  449.  
  450.  
  451.  
  452. /*
  453.    https://liudr.wordpress.com/2012/01/16/sprintf/
  454.    https://arduino.stackexchange.com/questions/19234/print-string-and-integer-lcd
  455.    https://www.electric-skateboard.builders/t/recording-data-from-vesc-summary-of-tools/7020/13
  456. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement