Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Servo.h>
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- #include "VescUart.h"
- #include "datatypes.h"
- LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
- Servo esc;
- /* Constantes pour la taille de l'écran LCD */
- const int LCD_NB_ROWS = 4;
- const int LCD_NB_COLUMNS = 20;
- /* Caractères personnalisés */
- byte START_DIV_0_OF_4[8] = {
- B01111,
- B11000,
- B10000,
- B10000,
- B10000,
- B10000,
- B11000,
- B01111
- }; // Char début 0 / 4
- byte START_DIV_1_OF_4[8] = {
- B01111,
- B11000,
- B10000,
- B10000,
- B10000,
- B10011,
- B11000,
- B01111
- }; // Char début 1 / 4
- byte START_DIV_2_OF_4[8] = {
- B01111,
- B11000,
- B10000,
- B10000,
- B10111,
- B10011,
- B11000,
- B01111
- }; // Char début 2 / 4
- byte START_DIV_3_OF_4[8] = {
- B01111,
- B11000,
- B10000,
- B10111,
- B10111,
- B10011,
- B11000,
- B01111
- }; // Char début 3 / 4
- byte START_DIV_4_OF_4[8] = {
- B01111,
- B11000,
- B10011,
- B10111,
- B10111,
- B10011,
- B11000,
- B01111
- }; // Char début 4 / 4
- byte DIV_0_OF_8[8] = {
- B11111,
- B00000,
- B00000,
- B00000,
- B00000,
- B00000,
- B00000,
- B11111
- }; // Char milieu 0 / 8
- byte DIV_1_OF_8[8] = {
- B11111,
- B00000,
- B00000,
- B00000,
- B00000,
- B11000,
- B00000,
- B11111
- }; // Char milieu 1 / 8
- byte DIV_2_OF_8[8] = {
- B11111,
- B00000,
- B00000,
- B00000,
- B11000,
- B11000,
- B00000,
- B11111
- }; // Char milieu 2 / 8
- byte DIV_3_OF_8[8] = {
- B11111,
- B00000,
- B00000,
- B11000,
- B11000,
- B11000,
- B00000,
- B11111
- }; // Char milieu 3 / 8
- byte DIV_4_OF_8[8] = {
- B11111,
- B00000,
- B11000,
- B11000,
- B11000,
- B11000,
- B00000,
- B11111
- }; // Char milieu 4 / 8
- byte DIV_5_OF_8[8] = {
- B11111,
- B00000,
- B11000,
- B11000,
- B11000,
- B11011,
- B00000,
- B11111
- }; // Char milieu 5 / 8
- byte DIV_6_OF_8[8] = {
- B11111,
- B00000,
- B11000,
- B11000,
- B11011,
- B11011,
- B00000,
- B11111
- }; // Char milieu 6 / 8
- byte DIV_7_OF_8[8] = {
- B11111,
- B00000,
- B11000,
- B11011,
- B11011,
- B11011,
- B00000,
- B11111
- }; // Char milieu 7 / 8
- byte DIV_8_OF_8[8] = {
- B11111,
- B00000,
- B11011,
- B11011,
- B11011,
- B11011,
- B00000,
- B11111
- }; // Char milieu 8 / 8
- byte END_DIV_0_OF_4[8] = {
- B11110,
- B00011,
- B00001,
- B00001,
- B00001,
- B00001,
- B00011,
- B11110
- }; // Char fin 0 / 4
- byte END_DIV_1_OF_4[8] = {
- B11110,
- B00011,
- B00001,
- B00001,
- B00001,
- B11001,
- B00011,
- B11110
- }; // Char fin 1 / 4
- byte END_DIV_2_OF_4[8] = {
- B11110,
- B00011,
- B00001,
- B00001,
- B11101,
- B11001,
- B00011,
- B11110
- }; // Char fin 2 / 4
- byte END_DIV_3_OF_4[8] = {
- B11110,
- B00011,
- B00001,
- B11101,
- B11101,
- B11001,
- B00011,
- B11110
- }; // Char fin 3 / 4
- byte END_DIV_4_OF_4[8] = {
- B11110,
- B00011,
- B11001,
- B11101,
- B11101,
- B11001,
- B00011,
- B11110
- }; // Char fin 4 / 4
- char Str1[21];
- char Str2[21];
- char Str3[21];
- char Str4[21];
- bool etat = false;
- int chrono = 0;
- int temps = 0;
- double vitesse = 0;
- int hallSensorPin = 2;
- volatile int compteur = 0;
- char str_vitesse[5];
- char str_tension[5];
- char str_courant[5];
- char str_puissance[5];
- char str_wh[5];
- char percentleft[5];
- void setup() {
- Serial.begin(115200); // VESC
- lcd.begin(LCD_NB_COLUMNS, LCD_NB_ROWS);
- setup_progressbar();
- lcd.clear();
- esc.attach(9);
- pinMode(hallSensorPin, INPUT);
- attachInterrupt(0, detectaimant, LOW);
- int percentall = measuredValues.inpVoltage * 10
- }
- struct bldcMeasure measuredValues;
- void loop() {
- chrono = millis();
- if (etat == true) {
- etat = false;
- chrono = chrono - temps;
- vitesse = (2 / (chrono / 1000.0)) * 3.6;
- delay(5);
- temps = millis();
- Serial.println(vitesse);
- dtostrf(vitesse, 4, 2, str_vitesse);
- }
- float tension = measuredValues.inpVoltage;
- dtostrf(tension, 4, 1, str_tension);
- float courant = measuredValues.avgMotorCurrent;
- dtostrf(courant, 4, 1, str_courant);
- float puissance = measuredValues.power;
- dtostrf(puissance, 4, 0, str_puissance);
- float Nmot = measuredValues.RPM;
- float wh = measuredValues.ampHours * measuredValues.inpVoltage;
- dtostrf(wh, 4, 0, str_wh);
- float percentleft = wh / percentall;
- dtostrf(percentleft, 3, 1, str_percentleft)
- delay(10);
- sprintf(Str1, "Vitesse: %s km/h", str_vitesse);
- lcd.setCursor(0, 0);
- lcd.print(Str1);
- delay(10);
- sprintf(Str2, "%s V/ %s A/ %s W ", str_tension, str_courant, str_puissance);
- lcd.setCursor(0, 1);
- lcd.print(Str2);
- /*
- delay(10);
- sprintf(Str3, "Wh= /%left= ", str_wh, str_percentleft);
- lcd.setCursor(0, 2);
- lcd.print(Str3);
- */
- delay(10);
- ligne = 2;
- percent = map(analogRead(A1), 0, 1020, 0, 100);
- draw_progressbar(percent, ligne);
- delay(10);
- ligne = 3;
- percent = map(Nmot, 0, 600, 0, 100); // vitesse max moteur
- draw_progressbar(percent, ligne);
- }
- float PWM()
- {
- int PWMa = analogRead(A1);
- PWMa = map(PWM, 0, 1024, 0, 179);
- esc.write(PWMa);
- }
- void detectaimant()
- {
- etat = true;
- }
- void setup_progressbar() {
- /* Enregistre les caractères personnalisés dans la mémoire de l'écran LCD */
- lcd.createChar(0, START_DIV_4_OF_4);
- lcd.createChar(1, DIV_0_OF_8);
- lcd.createChar(2, DIV_8_OF_8);
- lcd.createChar(3, END_DIV_0_OF_4);
- // Les autres caractéres sont configurés dynamiquement
- }
- void switch_progressbar_bank(byte bank) {
- // IMPORTANT : Il est nécessaire de faire un lcd.clear() ou un lcd.setCursor() aprés chaque changement de banque.
- /* Change de banque de caractères */
- switch (bank) {
- case 0:
- lcd.createChar(4, START_DIV_0_OF_4);
- lcd.createChar(5, START_DIV_1_OF_4);
- lcd.createChar(6, START_DIV_2_OF_4);
- lcd.createChar(7, START_DIV_3_OF_4);
- break;
- case 1:
- lcd.createChar(4, DIV_1_OF_8);
- lcd.createChar(5, DIV_2_OF_8);
- lcd.createChar(6, DIV_3_OF_8);
- lcd.createChar(7, DIV_4_OF_8);
- break;
- case 2:
- lcd.createChar(4, DIV_4_OF_8);
- lcd.createChar(5, DIV_5_OF_8);
- lcd.createChar(6, DIV_6_OF_8);
- lcd.createChar(7, DIV_7_OF_8);
- break;
- case 3:
- lcd.createChar(4, END_DIV_1_OF_4);
- lcd.createChar(5, END_DIV_2_OF_4);
- lcd.createChar(6, END_DIV_3_OF_4);
- lcd.createChar(7, END_DIV_4_OF_4);
- break;
- }
- }
- void draw_progressbar(byte percent, int ligne) {
- lcd.setCursor(0, ligne);
- /* Map la plage (0 ~ 100) vers la plage (0 ~ LCD_NB_COLUMNS * 2 * 4 - 2 * 4) */
- byte nb_columns = map(percent, 0, 100, 0, LCD_NB_COLUMNS * 2 * 4 - 2 * 4);
- // Chaque caractére affiche 2 barres verticales de 4 pixels de haut, mais le premier et dernier caractére n'en affiche qu'une.
- /* Dessine chaque caractére de la ligne */
- for (byte i = 0; i < LCD_NB_COLUMNS; ++i) {
- if (i == 0) { // Premiére case
- /* Affiche le char de début en fonction du nombre de colonnes */
- if (nb_columns > 4) {
- lcd.write((byte) 0); // Char début 4 / 4
- nb_columns -= 4;
- } else if (nb_columns == 4) {
- lcd.write((byte) 0); // Char début 4 / 4
- nb_columns = 0;
- } else {
- switch_progressbar_bank(0);
- lcd.setCursor(i, 1);
- lcd.write(nb_columns + 4); // Char début N / 4
- nb_columns = 0;
- }
- } else if (i == LCD_NB_COLUMNS - 1) { // Derniére case
- /* Affiche le char de fin en fonction du nombre de colonnes */
- if (nb_columns > 0) {
- switch_progressbar_bank(3);
- lcd.setCursor(i, 1);
- lcd.write(nb_columns + 3); // Char fin N / 4
- } else {
- lcd.write(3); // Char fin 0 / 4
- }
- } else { // Autres cases
- /* Affiche le char adéquat en fonction du nombre de colonnes */
- if (nb_columns == 0) {
- lcd.write(1); // Char div 0 / 8
- } else if (nb_columns >= 8) {
- lcd.write(2); // Char div 8 / 8
- nb_columns -= 8;
- } else if (nb_columns >= 4 && nb_columns < 8) {
- switch_progressbar_bank(2);
- lcd.setCursor(i, 1);
- lcd.write(nb_columns); // Char div N / 8
- nb_columns = 0;
- } else if (nb_columns < 4) {
- switch_progressbar_bank(1);
- lcd.setCursor(i, 1);
- lcd.write(nb_columns + 3); // Char div N / 8
- nb_columns = 0;
- }
- }
- }
- }
- /*
- https://liudr.wordpress.com/2012/01/16/sprintf/
- https://arduino.stackexchange.com/questions/19234/print-string-and-integer-lcd
- https://www.electric-skateboard.builders/t/recording-data-from-vesc-summary-of-tools/7020/13
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement