-
Notifications
You must be signed in to change notification settings - Fork 4
/
VESC6_LCD_EBIKE.ino
274 lines (230 loc) · 5.01 KB
/
VESC6_LCD_EBIKE.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
This code is used to gather data from a VESC6 and display on an LCD screen.
Written for Arduino Nano and LCD I2C 1602.
Original VESCUART.h code written by SolidGeek.
*/
#include <VescUart.h>
#include <SimpleKalmanFilter.h>
//Library for the Display
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16, 2);
/** Initiate VescUart class */
VescUart UART;
int rpm;
float voltage;
float current;
int power;
float amphour;
float tach;
float distance;
float velocity;
float watthour;
float batpercentage;
SimpleKalmanFilter Filter1(2, 2, 0.01);
////////// Custom Characters //////////
byte ph[] = {
B11100,
B10100,
B11100,
B10000,
B10101,
B00111,
B00101,
B00101
};
byte mi[] = {
B10001,
B11011,
B10101,
B10001,
B00100,
B00000,
B00100,
B00110
};
byte km[] = {
B01000,
B01010,
B01100,
B01010,
B10001,
B11011,
B10101,
B10001
};
byte ah[] = {
B00100,
B01010,
B01110,
B01010,
B00000,
B01010,
B01110,
B01010
};
byte percent[] = {
B11001,
B11001,
B00010,
B00100,
B01000,
B10011,
B10011,
B00000
};
byte m[] = {
B10001,
B11011,
B10101,
B10001,
B10001,
B00000,
B00000,
B00000
};
byte k[] = {
B01000,
B01010,
B01100,
B01010,
B01010,
B00000,
B00000,
B00000
};
byte bat[] = {
B01110,
B11011,
B10001,
B10001,
B10001,
B11111,
B11111,
B11111
};
byte sprk[] = {
B00000,
B01000,
B00100,
B00010,
B00100,
B01000,
B00100,
B00010
};
void setup() {
/** Setup Serial port to display data */
Serial.begin(115200);
/** Setup UART port (Serial1 on Atmega32u4) */
// Serial1.begin(19200);
while (!Serial) {;}
/** Define which ports to use as UART */
UART.setSerialPort(&Serial);
lcd.init(); // initialize the lcd
lcd.backlight();
lcd.setCursor(3,0);
lcd.print("VESC EBIKE"); // Startup screen (can't be more than 10 characters)
lcd.createChar(5, bat);
lcd.setCursor(1,1);
lcd.write(5);
lcd.createChar(6, bat);
lcd.setCursor(14,1);
lcd.write(6);
lcd.createChar(7, sprk);
lcd.setCursor(1,0);
lcd.write(7);
lcd.createChar(8, sprk);
lcd.setCursor(14,0);
lcd.write(8);
delay(5000);
lcd.clear();
}
void loop() {
////////// Read values //////////
if ( UART.getVescValues() ) {
rpm = (UART.data.rpm)/7; // The '7' is the number of pole pairs in the motor. Most motors have 14 poles, therefore 7 pole pairs
voltage = (UART.data.inpVoltage);
current = (UART.data.avgInputCurrent);
power = voltage*current;
amphour = (UART.data.ampHours);
watthour = amphour*voltage;
tach = (UART.data.tachometerAbs)/42; // The '42' is the number of motor poles multiplied by 3
distance = tach*3.142*(1/1609)*0.72*(16/185); // Motor RPM x Pi x (1 / meters in a mile or km) x Wheel diameter x (motor pulley / wheelpulley)
velocity = rpm*3.142*(60/1609)*0.72*(16/185); // Motor RPM x Pi x (seconds in a minute / meters in a mile) x Wheel diameter x (motor pulley / wheelpulley)
batpercentage = ((voltage-38.4)/12)*100; // ((Battery voltage - minimum voltage) / number of cells) x 100
////////// Filter //////////
// calculate the estimated value with Kalman Filter
float powerfiltered = Filter1.updateEstimate(power);
////////// LCD //////////
// First line
if(velocity < 10){
lcd.setCursor(1,0);
lcd.print(" ");
lcd.print(velocity, 0);
}
else{
lcd.setCursor(1,0);
lcd.print(velocity, 0);
}
lcd.createChar(4, m); // Change 'm' to 'k' for kilometers
lcd.setCursor(3,0);
lcd.write(4);
lcd.createChar(0, ph);
lcd.setCursor(4,0);
lcd.write(0);
lcd.setCursor(7,0);
lcd.print(voltage,0);
lcd.setCursor(9,0);
lcd.print("V");
lcd.setCursor(11,0);
lcd.print(distance, 2);
lcd.createChar(1, mi); // Change 'mi' to 'km' for kilometers
lcd.setCursor(15,0);
lcd.write(1);
// Second line
if(powerfiltered < 10){
lcd.setCursor(0,1);
lcd.print(" ");
lcd.print(powerfiltered, 0);
}
if(powerfiltered > 10 && powerfiltered < 100){
lcd.setCursor(0,1);
lcd.print(" ");
lcd.print(powerfiltered, 0);
}
if(powerfiltered > 100 && powerfiltered < 1000){
lcd.setCursor(0,1);
lcd.print(" ");
lcd.print(powerfiltered, 0);
}
if(powerfiltered > 1000){
lcd.setCursor(0,1);
lcd.print(powerfiltered, 0);
}
lcd.setCursor(4,1);
lcd.print("W");
if(batpercentage < 100){
lcd.setCursor(7,1);
lcd.print(batpercentage,0);
}
else{
lcd.setCursor(6,1);
lcd.print(batpercentage,0);
}
lcd.createChar(3, percent);
lcd.setCursor(9,1);
lcd.write(3);
lcd.setCursor(11,1);
lcd.print(amphour, 2);
lcd.createChar(2, ah);
lcd.setCursor(15,1);
lcd.write(2);
lcd.println();
}
else
{
lcd.println("Failed to get data!");
}
delay(50);
}