-
Notifications
You must be signed in to change notification settings - Fork 0
/
motor.c
executable file
·1075 lines (967 loc) · 30.7 KB
/
motor.c
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
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* TongSheng TSDZ2 motor controller firmware/
*
* Copyright (C) Casainho, 2018.
*
* Released under the GPL License, Version 3
*/
#include <stdint.h>
#include <stdio.h>
#include "motor.h"
#include "interrupts.h"
#include "stm8s_gpio.h"
#include "stm8s_tim1.h"
#include "motor.h"
#include "ebike_app.h"
#include "pins.h"
#include "pwm.h"
#include "config.h"
#include "adc.h"
#include "utils.h"
#include "uart.h"
#include "adc.h"
#include "watchdog.h"
#include "math.h"
#define SVM_TABLE_LEN 256
#define SIN_TABLE_LEN 60
uint8_t ui8_svm_table [SVM_TABLE_LEN] =
{
239 ,
241 ,
242 ,
243 ,
245 ,
246 ,
247 ,
248 ,
249 ,
250 ,
251 ,
251 ,
252 ,
253 ,
253 ,
254 ,
254 ,
254 ,
255 ,
255 ,
255 ,
255 ,
255 ,
255 ,
254 ,
254 ,
254 ,
253 ,
253 ,
252 ,
251 ,
250 ,
250 ,
249 ,
248 ,
247 ,
245 ,
244 ,
243 ,
242 ,
240 ,
239 ,
236 ,
231 ,
227 ,
222 ,
217 ,
212 ,
207 ,
202 ,
197 ,
191 ,
186 ,
181 ,
176 ,
170 ,
165 ,
160 ,
154 ,
149 ,
144 ,
138 ,
133 ,
127 ,
122 ,
116 ,
111 ,
106 ,
100 ,
95 ,
89 ,
84 ,
79 ,
74 ,
68 ,
63 ,
58 ,
53 ,
48 ,
43 ,
38 ,
33 ,
28 ,
23 ,
18 ,
16 ,
14 ,
13 ,
12 ,
10 ,
9 ,
8 ,
7 ,
6 ,
5 ,
4 ,
3 ,
3 ,
2 ,
1 ,
1 ,
1 ,
0 ,
0 ,
0 ,
0 ,
0 ,
0 ,
0 ,
0 ,
0 ,
1 ,
1 ,
2 ,
2 ,
3 ,
4 ,
5 ,
6 ,
6 ,
8 ,
9 ,
10 ,
11 ,
12 ,
14 ,
15 ,
17 ,
15 ,
14 ,
12 ,
11 ,
10 ,
9 ,
8 ,
6 ,
6 ,
5 ,
4 ,
3 ,
2 ,
2 ,
1 ,
1 ,
0 ,
0 ,
0 ,
0 ,
0 ,
0 ,
0 ,
0 ,
0 ,
1 ,
1 ,
1 ,
2 ,
3 ,
3 ,
4 ,
5 ,
6 ,
7 ,
8 ,
9 ,
10 ,
12 ,
13 ,
14 ,
16 ,
18 ,
23 ,
28 ,
33 ,
38 ,
43 ,
48 ,
53 ,
58 ,
63 ,
68 ,
74 ,
79 ,
84 ,
89 ,
95 ,
100 ,
106 ,
111 ,
116 ,
122 ,
127 ,
133 ,
138 ,
144 ,
149 ,
154 ,
160 ,
165 ,
170 ,
176 ,
181 ,
186 ,
191 ,
197 ,
202 ,
207 ,
212 ,
217 ,
222 ,
227 ,
231 ,
236 ,
239 ,
240 ,
242 ,
243 ,
244 ,
245 ,
247 ,
248 ,
249 ,
250 ,
250 ,
251 ,
252 ,
253 ,
253 ,
254 ,
254 ,
254 ,
255 ,
255 ,
255 ,
255 ,
255 ,
255 ,
254 ,
254 ,
254 ,
253 ,
253 ,
252 ,
251 ,
251 ,
250 ,
249 ,
248 ,
247 ,
246 ,
245 ,
243 ,
242 ,
241 ,
239 ,
238 ,
};
uint8_t ui8_sin_table [SVM_TABLE_LEN] =
{
0 ,
3 ,
6 ,
9 ,
12 ,
16 ,
19 ,
22 ,
25 ,
28 ,
31 ,
34 ,
37 ,
40 ,
43 ,
46 ,
49 ,
52 ,
54 ,
57 ,
60 ,
63 ,
66 ,
68 ,
71 ,
73 ,
76 ,
78 ,
81 ,
83 ,
86 ,
88 ,
90 ,
92 ,
95 ,
97 ,
99 ,
101 ,
102 ,
104 ,
106 ,
108 ,
109 ,
111 ,
113 ,
114 ,
115 ,
117 ,
118 ,
119 ,
120 ,
121 ,
122 ,
123 ,
124 ,
125 ,
125 ,
126 ,
126 ,
127
};
uint16_t ui16_PWM_cycles_counter = 1;
uint16_t ui16_PWM_cycles_counter_6 = 1;
uint16_t ui16_PWM_cycles_counter_total = 0xffff;
volatile uint16_t ui16_motor_speed_erps = 0;
uint8_t ui8_motor_over_speed_erps_flag = 0;
uint8_t ui8_svm_table_index = 0;
uint8_t ui8_motor_rotor_absolute_angle;
uint8_t ui8_motor_rotor_angle;
volatile uint8_t ui8_foc_angle = 0;
uint8_t ui8_interpolation_angle = 0;
uint8_t ui8_motor_commutation_type = BLOCK_COMMUTATION;
volatile uint8_t ui8_motor_controller_state = MOTOR_CONTROLLER_STATE_OK;
uint8_t ui8_hall_sensors_state = 0;
uint8_t ui8_hall_sensors_state_last = 0;
uint8_t ui8_half_erps_flag = 0;
volatile uint8_t ui8_duty_cycle = 0;
volatile uint8_t ui8_duty_cycle_target;
uint16_t ui16_duty_cycle_ramp_up_inverse_step;
uint16_t ui16_duty_cycle_ramp_down_inverse_step;
uint16_t ui16_counter_duty_cycle_ramp_up = 0;
uint16_t ui16_counter_duty_cycle_ramp_down = 0;
uint8_t ui8_phase_a_voltage;
uint8_t ui8_phase_b_voltage;
uint8_t ui8_phase_c_voltage;
uint16_t ui16_value;
uint8_t ui8_first_time_run_flag = 1;
volatile uint8_t ui8_adc_battery_voltage_cut_off = 0xff; // safe value so controller will not discharge the battery if not receiving a lower value from the LCD
uint16_t ui16_adc_battery_voltage_accumulated = 0;
uint16_t ui16_adc_battery_voltage_filtered_10b;
uint16_t ui16_adc_battery_current_accumulated = 0;
uint8_t ui8_adc_battery_current_filtered_10b;
uint16_t ui16_foc_angle_accumulated = 0;
uint16_t ui16_adc_battery_current_10b;
volatile uint8_t ui8_adc_battery_current;
volatile uint8_t ui8_adc_motor_phase_current;
uint8_t ui8_current_controller_counter = 0;
uint8_t ui8_current_controller_flag = 0;
volatile uint8_t ui8_adc_target_motor_phase_max_current;
volatile uint8_t ui8_adc_motor_phase_current_offset;
uint8_t ui8_pas_state;
uint8_t ui8_pas_state_old;
uint16_t ui16_pas_counter = (uint16_t) PAS_ABSOLUTE_MIN_CADENCE_PWM_CYCLE_TICKS;
volatile uint16_t ui16_torque_sensor_throttle_processed_value = 0;
uint8_t ui8_torque_sensor_pas_signal_change_counter = 0;
uint16_t ui16_torque_sensor_throttle_max_value = 0;
uint16_t ui16_torque_sensor_throttle_value;
// wheel speed
uint8_t ui8_wheel_speed_sensor_state = 1;
uint8_t ui8_wheel_speed_sensor_state_old = 1;
uint16_t ui16_wheel_speed_sensor_counter = 0;
void read_battery_voltage (void);
void read_battery_current (void);
void calc_foc_angle (void);
uint8_t asin_table (uint8_t ui8_inverted_angle_x128);
void motor_set_phase_current_max (uint8_t ui8_value);
void motor_controller (void)
{
// reads battery voltage and current
read_battery_voltage ();
read_battery_current ();
calc_foc_angle ();
}
// Measures did with a 24V Q85 328 RPM motor, rotating motor backwards by hand:
// Hall sensor A positivie to negative transition | BEMF phase B at max value / top of sinewave
// Hall sensor B positivie to negative transition | BEMF phase A at max value / top of sinewave
// Hall sensor C positive to negative transition | BEMF phase C at max value / top of sinewave
// runs every 64us (PWM frequency)
void TIM1_CAP_COM_IRQHandler(void) __interrupt(TIM1_CAP_COM_IRQHANDLER)
{
static uint8_t ui8_temp;
/****************************************************************************/
// read battery current ADC value | should happen at middle of the PWM duty_cycle
// disable scan mode
ADC1->CR2 &= (uint8_t)(~ADC1_CR2_SCAN);
// clear EOC flag first (selected also channel 5)
ADC1->CSR = 0x05;
// start ADC1 conversion
ADC1->CR1 |= ADC1_CR1_ADON;
while (!(ADC1->CSR & ADC1_FLAG_EOC)) ;
ui16_adc_battery_current_10b = ui16_adc_read_battery_current_10b ();
ui8_adc_battery_current = ui16_adc_battery_current_10b >> 2;
// calculate motor phase current ADC value
if (ui8_duty_cycle > 0)
{
ui8_adc_motor_phase_current = ((ui16_adc_battery_current_10b << 6) / ((uint16_t) ui8_duty_cycle));
}
else
{
ui8_adc_motor_phase_current = 0;
}
/****************************************************************************/
/****************************************************************************/
// trigger ADC conversion of all channels (scan conversion, buffered)
ADC1->CR2 |= ADC1_CR2_SCAN; // enable scan mode
ADC1->CSR = 0x07; // clear EOC flag first (selected also channel 7)
ADC1->CR1 |= ADC1_CR1_ADON; // start ADC1 conversion
/****************************************************************************/
/****************************************************************************/
// read hall sensor signals and:
// - find the motor rotor absolute angle
// - calc motor speed in erps (ui16_motor_speed_erps)
// read hall sensors signal pins and mask other pins
// hall sensors sequence with motor forward rotation: 4, 6, 2, 3, 1, 5
ui8_hall_sensors_state = ((HALL_SENSOR_A__PORT->IDR & HALL_SENSOR_A__PIN) >> 5) |
((HALL_SENSOR_B__PORT->IDR & HALL_SENSOR_B__PIN) >> 1) |
((HALL_SENSOR_C__PORT->IDR & HALL_SENSOR_C__PIN) >> 3);
// make sure we run next code only when there is a change on the hall sensors signal
if (ui8_hall_sensors_state != ui8_hall_sensors_state_last)
{
ui8_hall_sensors_state_last = ui8_hall_sensors_state;
switch (ui8_hall_sensors_state)
{
case 3:
ui8_motor_rotor_absolute_angle = (uint8_t) MOTOR_ROTOR_ANGLE_150;
break;
case 1:
if (ui8_half_erps_flag == 1)
{
ui8_half_erps_flag = 0;
ui16_PWM_cycles_counter_total = ui16_PWM_cycles_counter;
ui16_PWM_cycles_counter = 1;
// this division takes 4.4us and without the cast (uint16_t) PWM_CYCLES_SECOND, would take 111us!! Verified on 2017.11.20
// avoid division by 0
if (ui16_PWM_cycles_counter_total > 0) { ui16_motor_speed_erps = ((uint16_t) PWM_CYCLES_SECOND) / ui16_PWM_cycles_counter_total; }
else { ui16_motor_speed_erps = ((uint16_t) PWM_CYCLES_SECOND); }
// disable flag at every ERPS when ui16_motor_speed_erps is calculated
ui8_motor_over_speed_erps_flag = 0;
// update motor commutation state based on motor speed
if (ui16_motor_speed_erps > MOTOR_ROTOR_ERPS_START_INTERPOLATION_60_DEGREES)
{
if (ui8_motor_commutation_type == BLOCK_COMMUTATION)
{
ui8_motor_commutation_type = SINEWAVE_INTERPOLATION_60_DEGREES;
ui8_ebike_app_state = EBIKE_APP_STATE_MOTOR_RUNNING;
}
}
else
{
if (ui8_motor_commutation_type == SINEWAVE_INTERPOLATION_60_DEGREES)
{
ui8_motor_commutation_type = BLOCK_COMMUTATION;
ui8_foc_angle = 0;
}
}
}
ui8_motor_rotor_absolute_angle = (uint8_t) MOTOR_ROTOR_ANGLE_210;
break;
case 5:
ui8_motor_rotor_absolute_angle = (uint8_t) MOTOR_ROTOR_ANGLE_270;
break;
case 4:
ui8_motor_rotor_absolute_angle = (uint8_t) MOTOR_ROTOR_ANGLE_330;
break;
case 6:
ui8_half_erps_flag = 1;
ui8_motor_rotor_absolute_angle = (uint8_t) MOTOR_ROTOR_ANGLE_30;
break;
// BEMF is always 90 degrees advanced over motor rotor position degree zero
// and here (hall sensor C blue wire, signal transition from positive to negative),
// phase B BEMF is at max value (measured on osciloscope by rotating the motor)
case 2:
ui8_motor_rotor_absolute_angle = (uint8_t) MOTOR_ROTOR_ANGLE_90;
break;
default:
return;
break;
}
ui16_PWM_cycles_counter_6 = 1;
}
/****************************************************************************/
/****************************************************************************/
// count number of fast loops / PWM cycles and reset some states when motor is near zero speed
if (ui16_PWM_cycles_counter < ((uint16_t) PWM_CYCLES_COUNTER_MAX))
{
ui16_PWM_cycles_counter++;
ui16_PWM_cycles_counter_6++;
}
else // happens when motor is stopped or near zero speed
{
ui16_PWM_cycles_counter = 1; // don't put to 0 to avoid 0 divisions
ui16_PWM_cycles_counter_6 = 1;
ui8_half_erps_flag = 0;
ui16_motor_speed_erps = 0;
ui16_PWM_cycles_counter_total = 0xffff;
ui8_foc_angle = 0;
ui8_motor_commutation_type = BLOCK_COMMUTATION;
ui8_hall_sensors_state_last = 0; // this way we force execution of hall sensors code next time
// ebike_app_cruise_control_stop ();
// if (ui8_ebike_app_state == EBIKE_APP_STATE_MOTOR_RUNNING) { ui8_ebike_app_state = EBIKE_APP_STATE_MOTOR_STOP; }
}
/****************************************************************************/
/****************************************************************************/
// - calc interpolation angle and sinewave table index
#define DO_INTERPOLATION 1 // may be usefull to disable interpolation when debugging
#if DO_INTERPOLATION == 1
// calculate the interpolation angle (and it doesn't work when motor starts and at very low speeds)
if (ui8_motor_commutation_type == SINEWAVE_INTERPOLATION_60_DEGREES)
{
// division by 0: ui16_PWM_cycles_counter_total should never be 0
// TODO: verifiy if (ui16_PWM_cycles_counter_6 << 8) do not overflow
ui8_interpolation_angle = (ui16_PWM_cycles_counter_6 << 8) / ui16_PWM_cycles_counter_total; // this operations take 4.4us
ui8_motor_rotor_angle = ui8_motor_rotor_absolute_angle + ui8_interpolation_angle;
ui8_svm_table_index = ui8_motor_rotor_angle + ui8_foc_angle;
}
else
#endif
{
ui8_svm_table_index = ui8_motor_rotor_absolute_angle + ui8_foc_angle;
}
// we need to put phase voltage 90 degrees ahead of rotor position, to get current 90 degrees ahead and have max torque per amp
ui8_svm_table_index -= 63;
/****************************************************************************/
/****************************************************************************/
// PWM duty_cycle controller:
// - limit battery undervoltage
// - limit battery max current
// - limit motor max phase current
// - limit motor max ERPS
// - ramp up/down PWM duty_cycle value
// control current only at every some PWM cycles, otherwise will be to fast, maybe because of low pass filter on hardware about reading the current
ui8_current_controller_counter++;
if (ui8_current_controller_counter > 12)
{
ui8_current_controller_counter = 0;
if ((ui8_adc_battery_current > ui8_adc_target_battery_max_current) || // battery max current, reduce duty_cycle
(ui8_adc_motor_phase_current > ui8_adc_target_motor_phase_max_current)) // motor max phase current, reduce duty_cycle
{
ui8_current_controller_flag = 1;
if (ui8_duty_cycle > 0)
{
ui8_duty_cycle--;
}
}
}
if (ui8_current_controller_flag) // when we control the current, don't execute next ifs otherwise ui8_duty_cycle would be decremented more than one time on each PWM cycle
{
ui8_current_controller_flag = 0;
}
else if (UI8_ADC_BATTERY_VOLTAGE < ui8_adc_battery_voltage_cut_off) // battery voltage under min voltage, reduce duty_cycle
{
if (ui8_duty_cycle > 0)
{
ui8_duty_cycle--;
}
}
else if ((ui16_motor_speed_erps > MOTOR_OVER_SPEED_ERPS) && // motor speed over max ERPS, reduce duty_cycle
(ui8_motor_over_speed_erps_flag == 0))
{
ui8_motor_over_speed_erps_flag = 1;
if (ui8_duty_cycle > 0)
{
ui8_duty_cycle--;
}
}
else // nothing to limit, so, adjust duty_cycle to duty_cycle_target, including ramping
{
if (ui8_duty_cycle_target > ui8_duty_cycle)
{
if (ui16_counter_duty_cycle_ramp_up++ >= ui16_duty_cycle_ramp_up_inverse_step)
{
ui16_counter_duty_cycle_ramp_up = 0;
// don't increase duty_cycle if motor_over_speed_erps
if (ui8_motor_over_speed_erps_flag == 0)
{
ui8_duty_cycle++;
}
}
}
else if (ui8_duty_cycle_target < ui8_duty_cycle)
{
if (ui16_counter_duty_cycle_ramp_down++ >= ui16_duty_cycle_ramp_down_inverse_step)
{
ui16_counter_duty_cycle_ramp_down = 0;
ui8_duty_cycle--;
}
}
}
/****************************************************************************/
/****************************************************************************/
// calc final PWM duty_cycle values to be applied to TIMER1
// scale and apply PWM duty_cycle for the 3 phases
// phase A is advanced 240 degrees over phase B
ui8_temp = ui8_svm_table [(uint8_t) (ui8_svm_table_index + 171 /* 240º */)];
if (ui8_temp > MIDDLE_PWM_DUTY_CYCLE_MAX)
{
ui16_value = ((uint16_t) (ui8_temp - MIDDLE_PWM_DUTY_CYCLE_MAX)) * ui8_duty_cycle;
ui8_temp = (uint8_t) (ui16_value >> 8);
ui8_phase_a_voltage = MIDDLE_PWM_DUTY_CYCLE_MAX + ui8_temp;
}
else
{
ui16_value = ((uint16_t) (MIDDLE_PWM_DUTY_CYCLE_MAX - ui8_temp)) * ui8_duty_cycle;
ui8_temp = (uint8_t) (ui16_value >> 8);
ui8_phase_a_voltage = MIDDLE_PWM_DUTY_CYCLE_MAX - ui8_temp;
}
// phase B as reference phase
ui8_temp = ui8_svm_table [ui8_svm_table_index];
if (ui8_temp > MIDDLE_PWM_DUTY_CYCLE_MAX)
{
ui16_value = ((uint16_t) (ui8_temp - MIDDLE_PWM_DUTY_CYCLE_MAX)) * ui8_duty_cycle;
ui8_temp = (uint8_t) (ui16_value >> 8);
ui8_phase_b_voltage = MIDDLE_PWM_DUTY_CYCLE_MAX + ui8_temp;
}
else
{
ui16_value = ((uint16_t) (MIDDLE_PWM_DUTY_CYCLE_MAX - ui8_temp)) * ui8_duty_cycle;
ui8_temp = (uint8_t) (ui16_value >> 8);
ui8_phase_b_voltage = MIDDLE_PWM_DUTY_CYCLE_MAX - ui8_temp;
}
// phase C is advanced 120 degrees over phase B
ui8_temp = ui8_svm_table [(uint8_t) (ui8_svm_table_index + 85 /* 120º */)];
if (ui8_temp > MIDDLE_PWM_DUTY_CYCLE_MAX)
{
ui16_value = ((uint16_t) (ui8_temp - MIDDLE_PWM_DUTY_CYCLE_MAX)) * ui8_duty_cycle;
ui8_temp = (uint8_t) (ui16_value >> 8);
ui8_phase_c_voltage = MIDDLE_PWM_DUTY_CYCLE_MAX + ui8_temp;
}
else
{
ui16_value = ((uint16_t) (MIDDLE_PWM_DUTY_CYCLE_MAX - ui8_temp)) * ui8_duty_cycle;
ui8_temp = (uint8_t) (ui16_value >> 8);
ui8_phase_c_voltage = MIDDLE_PWM_DUTY_CYCLE_MAX - ui8_temp;
}
// set final duty_cycle value
// phase B
TIM1->CCR3H = (uint8_t) (ui8_phase_b_voltage >> 7);
TIM1->CCR3L = (uint8_t) (ui8_phase_b_voltage << 1);
// phase C
TIM1->CCR2H = (uint8_t) (ui8_phase_c_voltage >> 7);
TIM1->CCR2L = (uint8_t) (ui8_phase_c_voltage << 1);
// phase A
TIM1->CCR1H = (uint8_t) (ui8_phase_a_voltage >> 7);
TIM1->CCR1L = (uint8_t) (ui8_phase_a_voltage << 1);
/****************************************************************************/
/****************************************************************************/
// calc PAS timming between each positive pulses, in PWM cycles ticks
// calc PAS on and off timming of each pulse, in PWM cycles ticks
ui16_pas_counter++;
// detect PAS signal changes
if ((PAS1__PORT->IDR & PAS1__PIN) == 0)
{
ui8_pas_state = 0;
}
else
{
ui8_pas_state = 1;
}
// PAS signal did change
if (ui8_pas_state != ui8_pas_state_old)
{
ui8_pas_state_old = ui8_pas_state;
// consider only when PAS signal transition from 0 to 1
if (ui8_pas_state == 1)
{
// limit PAS cadence to be less than PAS_ABSOLUTE_MAX_CADENCE_PWM_CYCLE_TICKS
// also PAS cadence should be zero if rotating backwards
if ((ui16_pas_counter < ((uint16_t) PAS_ABSOLUTE_MAX_CADENCE_PWM_CYCLE_TICKS)) ||
(ui8_pas_direction))
{
ui16_pas_pwm_cycles_ticks = (uint16_t) PAS_ABSOLUTE_MIN_CADENCE_PWM_CYCLE_TICKS;
}
else
{
ui16_pas_pwm_cycles_ticks = ui16_pas_counter;
}
ui16_pas_counter = 0;
}
else
{
// PAS cadence should be zero if rotating backwards
if ((PAS2__PORT->IDR & PAS2__PIN) != 0)
{
ui8_pas_direction = 1;
ui16_pas_pwm_cycles_ticks = (uint16_t) PAS_ABSOLUTE_MIN_CADENCE_PWM_CYCLE_TICKS;
}
else
{
ui8_pas_direction = 0;
}
}
// filter the torque signal, by saving the max value of each one pedal rotation
ui16_torque_sensor_throttle_value = ui16_adc_read_torque_sensor_10b () - 184;
if (ui16_torque_sensor_throttle_value > 800) ui16_torque_sensor_throttle_value = 0;
ui8_torque_sensor_pas_signal_change_counter++;
if (ui8_torque_sensor_pas_signal_change_counter > (PAS_NUMBER_MAGNETS << 1)) // PAS_NUMBER_MAGNETS*2 means a full pedal rotation
{
ui8_torque_sensor_pas_signal_change_counter = 1; // this is the first cycle
ui16_torque_sensor_throttle_processed_value = ui16_torque_sensor_throttle_max_value; // store the max value on the output variable of this algorithm
ui16_torque_sensor_throttle_max_value = 0; // reset the max value
}
else
{
// store the max value
if (ui16_torque_sensor_throttle_value > ui16_torque_sensor_throttle_max_value)
{
ui16_torque_sensor_throttle_max_value = ui16_torque_sensor_throttle_value;
}
}
}
// limit min PAS cadence
if (ui16_pas_counter > ((uint16_t) PAS_ABSOLUTE_MIN_CADENCE_PWM_CYCLE_TICKS))
{
ui16_pas_pwm_cycles_ticks = (uint16_t) PAS_ABSOLUTE_MIN_CADENCE_PWM_CYCLE_TICKS;
ui16_pas_counter = 0;
ui8_pas_direction = 0;
ui16_torque_sensor_throttle_processed_value = 0;
}
/****************************************************************************/
/****************************************************************************/
// calc wheel speed sensor timming between each positive pulses, in PWM cycles ticks
ui16_wheel_speed_sensor_counter++;
// detect wheel speed sensor signal changes
if (WHEEL_SPEED_SENSOR__PORT->IDR & WHEEL_SPEED_SENSOR__PIN)
{
ui8_wheel_speed_sensor_state = 1;
}
else
{
ui8_wheel_speed_sensor_state = 0;
}
if (ui8_wheel_speed_sensor_state != ui8_wheel_speed_sensor_state_old) // wheel speed sensor signal did change
{
ui8_wheel_speed_sensor_state_old = ui8_wheel_speed_sensor_state;
if (ui8_wheel_speed_sensor_state == 1) // consider only when wheel speed sensor signal transition from 0 to 1
{
ui16_wheel_speed_sensor_pwm_cycles_ticks = ui16_wheel_speed_sensor_counter;
ui16_wheel_speed_sensor_counter = 0;
}
}
// limit min wheel speed
if (ui16_wheel_speed_sensor_counter > ((uint16_t) WHEEL_SPEED_SENSOR_MIN_PWM_CYCLE_TICKS))
{
ui16_wheel_speed_sensor_pwm_cycles_ticks = (uint16_t) WHEEL_SPEED_SENSOR_MIN_PWM_CYCLE_TICKS;
ui16_wheel_speed_sensor_counter = 0;
}
/****************************************************************************/
// /****************************************************************************/
// // reload watchdog timer, every PWM cycle to avoid automatic reset of the microcontroller
// if (ui8_first_time_run_flag)
// { // from the init of watchdog up to first reset on PWM cycle interrupt,
// // it can take up to 250ms and so we need to init here inside the PWM cycle
// ui8_first_time_run_flag = 0;
// watchdog_init ();
// }
// else
// {
// IWDG->KR = IWDG_KEY_REFRESH; // reload watch dog timer counter
// }
// /****************************************************************************/
/****************************************************************************/
// clears the TIM1 interrupt TIM1_IT_UPDATE pending bit
TIM1->SR1 = (uint8_t)(~(uint8_t)TIM1_IT_CC4);
/****************************************************************************/
}
void motor_disable_PWM (void)
{
TIM1_CtrlPWMOutputs(DISABLE);
}
void motor_enable_PWM (void)
{
TIM1_CtrlPWMOutputs(ENABLE);
}
void motor_controller_set_state (uint8_t ui8_state)
{
ui8_motor_controller_state |= ui8_state;
}
void motor_controller_reset_state (uint8_t ui8_state)
{
ui8_motor_controller_state &= ~ui8_state;
}
uint8_t motor_controller_state_is_set (uint8_t ui8_state)
{
return ui8_motor_controller_state & ui8_state;
}
void hall_sensor_init (void)
{
GPIO_Init (HALL_SENSOR_A__PORT, (GPIO_Pin_TypeDef) HALL_SENSOR_A__PIN, GPIO_MODE_IN_FL_NO_IT);
GPIO_Init (HALL_SENSOR_B__PORT, (GPIO_Pin_TypeDef) HALL_SENSOR_B__PIN, GPIO_MODE_IN_FL_NO_IT);
GPIO_Init (HALL_SENSOR_C__PORT, (GPIO_Pin_TypeDef) HALL_SENSOR_C__PIN, GPIO_MODE_IN_FL_NO_IT);
}
void motor_init (void)
{
motor_set_pwm_duty_cycle_ramp_up_inverse_step (PWM_DUTY_CYCLE_RAMP_UP_INVERSE_STEP); // each step = 64us
motor_set_pwm_duty_cycle_ramp_down_inverse_step (PWM_DUTY_CYCLE_RAMP_DOWN_INVERSE_STEP); // each step = 64us
motor_set_phase_current_max (ADC_MOTOR_PHASE_CURRENT_MAX);
}
void motor_set_pwm_duty_cycle_target (uint8_t ui8_value)
{
if (ui8_value > PWM_DUTY_CYCLE_MAX) { ui8_value = PWM_DUTY_CYCLE_MAX; }
// if brake is active, keep duty_cycle target at 0
if (ui8_motor_controller_state & MOTOR_CONTROLLER_STATE_BRAKE) { ui8_value = 0; }
ui8_duty_cycle_target = ui8_value;
}
void motor_set_pwm_duty_cycle_ramp_up_inverse_step (uint16_t ui16_value)
{
ui16_duty_cycle_ramp_up_inverse_step = ui16_value;
}
void motor_set_pwm_duty_cycle_ramp_down_inverse_step (uint16_t ui16_value)
{
ui16_duty_cycle_ramp_down_inverse_step = ui16_value;
}
void motor_set_phase_current_max (uint8_t ui8_value)
{
ui8_adc_target_motor_phase_max_current = ui8_adc_motor_phase_current_offset + ui8_value;
}
uint16_t ui16_motor_get_motor_speed_erps (void)
{
return ui16_motor_speed_erps;
}
void read_battery_voltage (void)
{
// low pass filter the voltage readed value, to avoid possible fast spikes/noise
ui16_adc_battery_voltage_accumulated -= ui16_adc_battery_voltage_accumulated >> READ_BATTERY_VOLTAGE_FILTER_COEFFICIENT;
ui16_adc_battery_voltage_accumulated += ui16_adc_read_battery_voltage_10b ();
ui16_adc_battery_voltage_filtered_10b = ui16_adc_battery_voltage_accumulated >> READ_BATTERY_VOLTAGE_FILTER_COEFFICIENT;
}
void read_battery_current (void)
{
// low pass filter the positive battery readed value (no regen current), to avoid possible fast spikes/noise
ui16_adc_battery_current_accumulated -= ui16_adc_battery_current_accumulated >> READ_BATTERY_CURRENT_FILTER_COEFFICIENT;
ui16_adc_battery_current_accumulated += ui16_adc_battery_current_10b;
ui8_adc_battery_current_filtered_10b = ui16_adc_battery_current_accumulated >> READ_BATTERY_CURRENT_FILTER_COEFFICIENT;
}
void calc_foc_angle (void)
{
uint16_t ui16_temp;
uint32_t ui32_temp;
uint16_t ui16_e_phase_voltage;
uint32_t ui32_i_phase_current_x2;
uint32_t ui32_l_x1048576;
uint32_t ui32_w_angular_velocity_x16;
uint16_t ui16_iwl_128;
struct_configuration_variables *p_configuration_variables;
p_configuration_variables = get_configuration_variables ();
// FOC implementation by calculating the angle between phase current and rotor magnetic flux (BEMF)
// 1. phase voltage is calculate
// 2. I*w*L is calculated, where I is the phase current. L was a measured value for 48V motor.
// 3. inverse sin is calculated of (I*w*L) / phase voltage, were we obtain the angle
// 4. previous calculated angle is applied to phase voltage vector angle and so the
// angle between phase current and rotor magnetic flux (BEMF) is kept at 0 (max torque per amp)
// calc E phase voltage
ui16_temp = ui16_adc_battery_voltage_filtered_10b * ADC10BITS_BATTERY_VOLTAGE_PER_ADC_STEP_X512;
ui16_temp = (ui16_temp >> 8) * ui8_duty_cycle;
ui16_e_phase_voltage = ui16_temp >> 9;
// calc I phase current
if (ui8_duty_cycle > 10)
{
ui16_temp = ((uint16_t) ui8_adc_battery_current_filtered_10b) * ADC_BATTERY_CURRENT_PER_ADC_STEP_X512;
ui32_i_phase_current_x2 = ui16_temp / ui8_duty_cycle;
}
else
{
ui32_i_phase_current_x2 = 0;
}
// calc W angular velocity: erps * 6.3
ui32_w_angular_velocity_x16 = ui16_motor_speed_erps * 101;