-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBLDC-Benchmark.ino
More file actions
752 lines (618 loc) · 14.9 KB
/
BLDC-Benchmark.ino
File metadata and controls
752 lines (618 loc) · 14.9 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
#include <LiquidCrystal.h>
#include <Adafruit_TiCoServo.h>
#include "HX711.h"
#include "menu.h"
#include <GyverFilters.h>
#include "GyverEncoder.h"
// HX711 circuit wiring
#define DOUT A3
#define SCK A2
#define FACTOR -361.5 // scale factor
//ESC output
#define ESC 10
// 1602 LCD wiring
#define V0 5
#define RS 12
#define EN 11
#define D4 9
#define D5 8
#define D6 7
#define D7 6
// inputs wiring
#define EXT 3
#define LED 13
#define VIN A1
#define CIN A0
// buttons wiring
#define ENCL A5
#define ENCR A4
#define SEL 4
#define SERVO_MIN 1000 // 1 ms pulse
#define SERVO_MAX 2000 // 2 ms pulse
// current sensor scale
#define C_SCALE 0.066
#define R1 100000.0 // resistance of R1 (100K) -see text!
#define R2 10600.0 // resistance of R2 (10K) - see text!
#define VCC 5.0
#include "Tacho.h"
Tacho tacho;
Adafruit_TiCoServo servo;
HX711 scale;
LiquidCrystal lcd(RS, EN, D4, D5, D6, D7);
Encoder encoder(ENCR, ENCL, SEL, TYPE2);
int throttle = -1; // servo position
volatile byte elapsed = 0;
// Settings
byte mode = 0; // handheld by default.
byte polesCount = 2, screen = 1;
int rpm = 0;
int maxRpm = 0;
float thrust = 0.0;
float voltage = 0.0;
float current = 0.0;
GMedian<3, float> voltageFilter;
GMedian<5, float> currentFilter;
GMedian<5, float> thrustFilter;
byte uiPage = 0;
byte led;
byte scaleEnable = 0;
byte lastSelState = 1;
const int motorTypesCount = 2;
const int motorTypeIdxs[motorTypesCount] = {7, 6};
byte motorType = 0;
bool encoderRight = false;
bool encoderLeft = false;
bool clicked = false;
void StartMain()
{
if (screen > 0)
{
screen = 0;
attachInterrupt(digitalPinToInterrupt(EXT), RPM_ISR, RISING); // ADD A HIGH PRIORITY ACTION ( AN INTERRUPT) WHEN THE SENSOR GOES FROM LOW TO HIGH
}
}
void MainScreen()
{
int _kv = (int)(rpm /voltage);
// print to serial
Serial.print(";G");
Serial.print(throttle);
Serial.print(";R");
Serial.print(rpm);
Serial.print(";V");
Serial.print(voltage);
Serial.print(";A");
Serial.print(current);
Serial.print(";T");
Serial.print(thrust);
Serial.print(";K");
Serial.print(_kv);
Serial.println();
lcd.clear();
switch(mode)
{
case 0: // Handheld tachometer
// throttle
lcd.print(throttle > 0 ? throttle : 0, DEC);
lcd.print(F("%"));
printBlades();
lcd.setCursor(0, 1);
lcd.print(rpm, DEC);
lcd.setCursor(8, 1);
lcd.print(maxRpm, DEC);
break;
case 1: // Connected to PC stand
// Thrust
if(thrust > 999)
lcd.print((int)thrust, DEC);
else
lcd.print(thrust, 1);
lcd.print(F("g"));
lcd.setCursor(7, 0);
// voltage
lcd.print(voltage, 1);
lcd.print(F("v"));
lcd.setCursor(12, 0);
// KV
lcd.print(_kv, DEC);
lcd.setCursor(0, 1);
//rpm
lcd.print(rpm, DEC);
lcd.setCursor(7, 1);
// current
lcd.print(current, 1);
lcd.print(F("a"));
lcd.setCursor(12, 1);
// throttle
lcd.print(throttle > 0 ? throttle : 0, DEC);
lcd.print(F("%"));
break;
}
}
bool clearLcd = true;
byte settingsScreen = 0; // 0 - select mode, 1 - select poles, 2 - scale enable
bool editMode = false;
void SettingsScreen()
{
static byte mode_edited = mode;
static byte enableScale_edited = scaleEnable;
//zero throttle for safety reason
if(throttle > 0)
setThrottle(0);
maxRpm = 0;
if(!editMode)
{
if(encoderRight)
{
clearEncoderFlags();
settingsScreen = cycleValue(settingsScreen, 0, 2);
clearLcd = true;
}
if(encoderLeft)
{
clearEncoderFlags();
settingsScreen = cycleValue(settingsScreen, 0, 2, 0, 1);
clearLcd = true;
}
if(clicked)
{
clearEncoderFlags();
editMode = true;
}
}
switch(mode)
{
case 0: // Handheld tacho
if(clearLcd)
{
lcd.clear();
lcd.print(readPgmString(pgm_read_word(&l_settings_hh[settingsScreen]))); // header
clearLcd = false;
}
if(settingsScreen == 0)
{
if(editMode)
{
if(encoderRight)
{
clearEncoderFlags();
mode_edited = cycleValue(mode_edited, 0, 1);
}
if(encoderLeft)
{
clearEncoderFlags();
mode_edited = cycleValue(mode_edited, 0, 1, 0, 1);
}
if(clicked)
{
clearEncoderFlags();
editMode = false;
clearLcd = true; // when switching from edit to view - clear lcd!
mode = mode_edited; //apply selected mode
polesCount = mode ? motorTypeIdxs[motorType] : polesCount;
}
printEditIndicator();
}
lcd.setCursor(3, 1);
lcd.print(readPgmString(pgm_read_word(&l_modes[mode_edited]))); // print selected mode
}
else if(settingsScreen == 1)
{
if(editMode)
{
if(encoderRight)
{
clearEncoderFlags();
polesCount = cycleValue(polesCount, 1, 7);
}
if(encoderLeft)
{
clearEncoderFlags();
polesCount = cycleValue(polesCount, 1, 7, 0, 1);
}
if(clicked)
{
clearEncoderFlags();
editMode = false;
clearLcd = true; // when switching from edit to view - clear lcd!
}
printEditIndicator();
}
lcd.setCursor(7, 1);
lcd.print(polesCount); // selected poles
}
else
{
if(editMode)
{
if(encoderRight)
{
clearEncoderFlags();
enableScale_edited = cycleValue(enableScale_edited, 0, 1);
}
if(encoderLeft)
{
clearEncoderFlags();
enableScale_edited = cycleValue(enableScale_edited, 0, 1, 0, 1);
}
if(clicked)
{
clearEncoderFlags();
editMode = false;
clearLcd = true; // when switching from edit to view - clear lcd!
scaleEnable = enableScale_edited > 0; //apply
}
printEditIndicator();
}
lcd.setCursor(3, 1);
lcd.print(readPgmString(pgm_read_word(&l_enableScale_options[enableScale_edited]))); // selected scale mode
}
break;
case 1: // Connected to PC stand
if(clearLcd)
{
lcd.clear();
lcd.print(readPgmString(pgm_read_word(&l_settings_stand[settingsScreen]))); // header
clearLcd = false;
}
if(settingsScreen == 0)
{
if(editMode)
{
if(encoderRight)
{
clearEncoderFlags();
mode_edited = cycleValue(mode, 0, 1);
}
if(encoderLeft)
{
clearEncoderFlags();
mode_edited = cycleValue(mode, 0, 1, 0, 1);
}
if(clicked)
{
clearEncoderFlags();
editMode = false;
clearLcd = true; // when switching from edit to view - clear lcd!
mode = mode_edited; //apply selected mode
}
printEditIndicator();
}
lcd.setCursor(3, 1);
lcd.print(readPgmString(pgm_read_word(&l_modes[mode_edited]))); // selected mode
}
else if(settingsScreen == 1)
{
if(editMode)
{
if(encoderRight)
{
clearEncoderFlags();
motorType = cycleValue(motorType, 0, 1);
}
if(encoderLeft)
{
clearEncoderFlags();
motorType = cycleValue(motorType, 0, 1, 0, 1);
}
if(clicked)
{
clearEncoderFlags();
editMode = false;
clearLcd = true; // when switching from edit to view - clear lcd!
polesCount = motorTypeIdxs[motorType]; //apply selected motor type
}
printEditIndicator();
}
lcd.setCursor(4, 1);
lcd.print(readPgmString(pgm_read_word(&l_motorTypes[motorType]))); // selected motor type
}
else
{
if(editMode)
{
if(encoderRight)
{
clearEncoderFlags();
enableScale_edited = cycleValue(enableScale_edited, 0, 1);
}
if(encoderLeft)
{
clearEncoderFlags();
enableScale_edited = cycleValue(enableScale_edited, 0, 1, 0, 1);
}
if(clicked)
{
clearEncoderFlags();
editMode = false;
clearLcd = true; // when switching from edit to view - clear lcd!
scaleEnable = enableScale_edited; //apply
}
printEditIndicator();
}
lcd.setCursor(3, 1);
lcd.print(readPgmString(pgm_read_word(&l_enableScale_options[enableScale_edited]))); // selected scale mode
}
break;
}
}
void printEditIndicator()
{
lcd.setCursor(0, 1);
lcd.print(readPgmString(&s_edit[0]));
}
void printBlades()
{
lcd.setCursor(14, 0);
if(polesCount == 1)
{
lcd.setCursor(15, 0);
lcd.write(byte(0));
}
else if(polesCount == 2)
{
lcd.write(byte(6));
lcd.write(byte(4));
lcd.setCursor(14, 1);
lcd.write(byte(2));
lcd.write(byte(7));
}
else if(polesCount == 3)
{
lcd.write(byte(1));
lcd.write(byte(0));
lcd.setCursor(14, 1);
lcd.write(byte(2));
lcd.write(byte(3));
}
else if(polesCount == 4)
{
lcd.write(byte(5));
lcd.write(byte(4));
lcd.setCursor(14, 1);
lcd.write(byte(2));
lcd.write(byte(3));
}
else
{
lcd.print(polesCount, DEC);
}
}
void GetReadings()
{
float _thrust = scaleEnable ? abs(scale.get_units(3)) : 0.0;
// min thrust - 2gr
if (_thrust < 2)
_thrust = 0;
thrust = thrustFilter.filtered(_thrust);
float _current = analogRead(CIN) * C_SCALE;
if (_current < C_SCALE)
_current = 0.0;
current = currentFilter.filtered(_current);
float _voltage = ((analogRead(VIN) * VCC) / 1023.0) / (R2 / (R1 + R2));
voltage = voltageFilter.filtered(_voltage);
rpm = tacho.getRPM();
if(rpm > maxRpm)
maxRpm = rpm;
}
void RPM_ISR()
{
// tick every 1 full turn
if (++elapsed >= polesCount)
{
elapsed = 0;
tacho.tick();
if (led == LOW)
{
led = HIGH; // TOGGLE STATUS LED
}
else
{
led = LOW;
}
digitalWrite(LED, led);
}
}
void initScale()
{
// INITIATE LOADCELL
scale.begin(DOUT, SCK);
// Scale initialization and tare
scale.read_average(5);
scale.set_scale(FACTOR); // this value is obtained by calibrating the scale with known weights; see the README for details
scale.tare(); // reset the scale to 0
}
void initLcd()
{
// INITIATE LCD
lcd.begin(16, 2);
lcd.createChar(0, customChar0);
lcd.createChar(1, customChar1);
lcd.createChar(2, customChar2);
lcd.createChar(3, customChar3);
lcd.createChar(4, customChar4);
lcd.createChar(5, customChar5);
lcd.createChar(6, customChar6);
lcd.createChar(7, customChar7);
}
void setup()
{
Serial.begin(38400); // GET VALUES USING SERIAL MONITOR
Serial.setTimeout(100);
pinMode(V0, OUTPUT);
analogWrite(V0, 115);
initLcd();
// INPUTS
pinMode(VIN, INPUT);
pinMode(CIN, INPUT);
pinMode(EXT, INPUT);
pinMode(LED, OUTPUT);
servo.attach(ESC, SERVO_MIN, SERVO_MAX);
if(scaleEnable)
{
initScale();
}
rpm = 0;
// send greetings to pc
Serial.write(101);
StartMain();
}
byte cycleValue(byte val, byte min, byte max)
{
return cycleValue(val, min, max, 1, 1);
}
byte cycleValue(byte val, byte min, byte max, byte dir, byte step)
{
if(dir) // increment
{
return val > max - step ? min : val + step;
}
return val - step < min ? max : val - step;
}
void clearEncoderFlags()
{
encoderRight = false;
encoderLeft = false;
clicked = false;
}
int throttleVAl = 0;
void adjustThrottle()
{
if(encoderLeft)
{
clearEncoderFlags();
setThrottle(throttle - 10);
}
if(encoderRight)
{
clearEncoderFlags();
setThrottle(throttle < 0 ? 10 : throttle + 10);
}
}
void collectEncoderStates()
{
if(encoder.isRight())
{
clearEncoderFlags();
encoderRight = true;
}
if(encoder.isLeft())
{
clearEncoderFlags();
encoderLeft = true;
}
if(encoder.isClick())
{
clearEncoderFlags();
clicked = true;
}
}
void loop()
{
static unsigned long lastSettingsUpdate = 0;
static unsigned long lastThrottleUpdate = 0;
static unsigned long lastReadingsUpdate = 0;
static unsigned long lastUIUpdate = 0;
encoder.tick();
// long press should open/close settings
if(encoder.isHolded())
{
clearEncoderFlags();
clearLcd = true;
screen = cycleValue(screen, 0, 1);
}
collectEncoderStates();
if (screen == 0)
{
if (millis() - lastThrottleUpdate > 250)
{
adjustThrottle();
lastThrottleUpdate = millis();
}
if (millis() - lastReadingsUpdate > 250)
{
GetReadings();
lastReadingsUpdate = millis();
}
if (millis() - lastUIUpdate > 500)
{
MainScreen();
lastUIUpdate = millis();
}
// click to cycle blades count
if(mode == 0 && clicked)
{
polesCount = cycleValue(polesCount, 1, 4);
clearEncoderFlags();
}
}
else
{
if (millis() - lastSettingsUpdate > 200)
{
SettingsScreen();
lastSettingsUpdate = millis();
}
}
}
void setThrottle(int percent)
{
if (percent < 0)
{
percent = 0;
}
if (percent > 100)
{
percent = 100;
}
if(throttle != percent)
{
throttle = percent;
servo.write(map(throttle, 0, 100, 0, 180));
// send throttle position upstream to update ui
Serial.print("102,");
Serial.print(throttle, DEC);
Serial.println();
}
}
void serialEvent()
{
/* PROTOCOL
"101,[POLES COUNT],[SCALE ENABLE];"
"102,[THROTTLE POSITION];"
*/
char data[32];
int bytesRead = Serial.readBytesUntil(';', data, 32);
if(bytesRead > 1) // waiting for packet at least few bytes
{
data[bytesRead] = NULL;
int content[5] = {0, 0, 0, 0, 0};
int idx = 0;
char* offset = data;
while(true)
{
content[idx++] = atoi(offset);
offset = strchr(offset, ','); // next delimiter
if(offset)
offset++;
else
break;
}
switch(content[0])
{
case 101: // settings
mode = 1; // if settings received - then we are connected
polesCount = (byte)(content[1] / 2);
scaleEnable = (byte)content[2];
if(scaleEnable)
{
initScale();
}
Serial.write(101); // confirm that we recieved data
break;
case 102: // regular packet
setThrottle(content[1]);
break;
}
}
}