This repository has been archived by the owner on Oct 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathebike_app.c
executable file
·872 lines (758 loc) · 30 KB
/
ebike_app.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
/*
* TongSheng TSDZ2 motor controller firmware/
*
* Copyright (C) Casainho, 2018.
*
* Released under the GPL License, Version 3
*/
#include "ebike_app.h"
#include <stdint.h>
#include <stdio.h>
#include "stm8s.h"
#include "stm8s_gpio.h"
#include "main.h"
#include "interrupts.h"
#include "adc.h"
#include "utils.h"
#include "motor.h"
#include "pwm.h"
#include "uart.h"
#include "brake.h"
#include "eeprom.h"
#include "config.h"
#include "utils.h"
#include "lights.h"
#define STATE_NO_PEDALLING 0
#define STATE_STARTUP_PEDALLING 1
#define STATE_PEDALLING 2
#define BOOST_STATE_BOOST_DISABLED 0
#define BOOST_STATE_START_BOOST 1
#define BOOST_STATE_BOOST 2
#define BOOST_STATE_END_BOOST 3
#define BOOST_STATE_FADE 4
#define BOOST_STATE_BOOST_WAIT_TO_RESTART 5
uint8_t ui8_adc_battery_max_current = ADC_BATTERY_CURRENT_MAX;
uint8_t ui8_target_battery_max_power_x10 = ADC_BATTERY_CURRENT_MAX;
volatile uint8_t ui8_throttle = 0;
volatile uint8_t ui8_torque_sensor_value1 = 0;
volatile uint8_t ui8_torque_sensor = 0;
volatile uint8_t ui8_torque_sensor_raw = 0;
volatile uint8_t ui8_adc_torque_sensor_min_value;
volatile uint8_t ui8_adc_torque_sensor_max_value;
volatile uint8_t ui8_adc_battery_current_offset;
volatile uint8_t ui8_ebike_app_state = EBIKE_APP_STATE_MOTOR_STOP;
volatile uint8_t ui8_adc_target_battery_max_current;
uint8_t ui8_adc_battery_current_max;
volatile uint16_t ui16_pas_pwm_cycles_ticks = (uint16_t) PAS_ABSOLUTE_MIN_CADENCE_PWM_CYCLE_TICKS;
volatile uint8_t ui8_pas_direction = 0;
uint8_t ui8_pas_cadence_rpm = 0;
uint8_t ui8_pedal_human_power = 0;
uint8_t ui8_startup_boost_enable = 0;
uint8_t ui8_startup_boost_fade_enable = 0;
uint8_t ui8_startup_boost_state_machine = 0;
uint8_t ui8_startup_boost_no_torque = 0;
uint8_t ui8_startup_boost_timer = 0;
uint8_t ui8_startup_boost_fade_steps = 0;
uint16_t ui16_startup_boost_fade_variable_x256;
uint16_t ui16_startup_boost_fade_variable_step_amount_x256;
// wheel speed
volatile uint16_t ui16_wheel_speed_sensor_pwm_cycles_ticks = (uint16_t) WHEEL_SPEED_SENSOR_MAX_PWM_CYCLE_TICKS;
uint8_t ui8_wheel_speed_max = 0;
float f_wheel_speed_x10;
uint16_t ui16_wheel_speed_x10;
volatile uint32_t ui32_wheel_speed_sensor_tick_counter = 0;
volatile struct_configuration_variables configuration_variables;
// UART
volatile uint8_t ui8_received_package_flag = 0;
volatile uint8_t ui8_rx_buffer[11];
volatile uint8_t ui8_rx_counter = 0;
volatile uint8_t ui8_tx_buffer[22];
volatile uint8_t ui8_tx_counter = 0;
volatile uint8_t ui8_i;
volatile uint8_t ui8_checksum;
volatile uint8_t ui8_byte_received;
volatile uint8_t ui8_state_machine = 0;
volatile uint8_t ui8_uart_received_first_package = 0;
static uint16_t ui16_crc_rx;
static uint16_t ui16_crc_tx;
static uint8_t ui8_master_comm_package_id = 0;
static uint8_t ui8_slave_comm_package_id = 0;
uint8_t ui8_tstr_state_machine = STATE_NO_PEDALLING;
uint8_t ui8_rtst_counter = 0;
uint16_t ui16_adc_motor_temperatured_accumulated = 0;
uint8_t ui8_adc_battery_target_current;
// function prototypes
static void ebike_control_motor (void);
void ebike_app_set_battery_max_current (uint8_t ui8_value);
void ebike_app_set_target_adc_battery_max_current (uint8_t ui8_value);
void communications_controller (void);
void uart_send_package (void);
void calc_wheel_speed (void);
void throttle_read (void);
void torque_sensor_read (void);
void startup_boost (void);
void calc_motor_temperature (void);
void read_pas_cadence (void)
{
// cadence in RPM = 60 / (ui16_pas_timer2_ticks * PAS_NUMBER_MAGNETS * 0.000064)
if (ui16_pas_pwm_cycles_ticks >= ((uint16_t) PAS_ABSOLUTE_MIN_CADENCE_PWM_CYCLE_TICKS)) { ui8_pas_cadence_rpm = 0; }
else
{
ui8_pas_cadence_rpm = (uint8_t) (60 / (((float) ui16_pas_pwm_cycles_ticks) * ((float) PAS_NUMBER_MAGNETS) * 0.000064));
if (ui8_pas_cadence_rpm > configuration_variables.ui8_pas_max_cadence)
{
ui8_pas_cadence_rpm = configuration_variables.ui8_pas_max_cadence;
}
}
}
void torque_sensor_read (void)
{
// map value from 0 up to 255
// map value from 0 up to 255
ui8_torque_sensor_raw = (uint8_t) (map (
UI8_ADC_TORQUE_SENSOR,
(uint8_t) ui8_adc_torque_sensor_min_value,
(uint8_t) ui8_adc_torque_sensor_max_value,
(uint8_t) 0,
(uint8_t) 255));
switch (ui8_tstr_state_machine)
{
// ebike is stopped, wait for throttle signal
case STATE_NO_PEDALLING:
if ((ui8_torque_sensor_raw > 0) &&
(!brake_is_set()))
{
ui8_tstr_state_machine = STATE_STARTUP_PEDALLING;
}
break;
// now count 2 seconds
case STATE_STARTUP_PEDALLING:
if (ui8_rtst_counter++ > 20) // 2 seconds
{
ui8_rtst_counter = 0;
ui8_tstr_state_machine = STATE_PEDALLING;
}
// ebike is not moving, let's return to begin
if (ui16_wheel_speed_x10 == 0)
{
ui8_rtst_counter = 0;
ui8_tstr_state_machine = 0;
}
break;
// wait on this state and reset when ebike stops
case STATE_PEDALLING:
if ((ui16_wheel_speed_x10 == 0) && (ui8_torque_sensor_raw == 0))
{
ui8_tstr_state_machine = STATE_NO_PEDALLING;
}
break;
default:
break;
}
// bike is moving but user doesn't pedal, disable torque sensor signal because user can be resting the feet on the pedals
if ((ui8_tstr_state_machine == STATE_PEDALLING) && (ui8_pas_cadence_rpm == 0))
{
ui8_torque_sensor = 0;
}
else
{
ui8_torque_sensor = ui8_torque_sensor_raw;
}
}
void throttle_read (void)
{
// map value from 0 up to 255
ui8_throttle = (uint8_t) (map (
UI8_ADC_THROTTLE,
(uint8_t) ADC_THROTTLE_MIN_VALUE,
(uint8_t) ADC_THROTTLE_MAX_VALUE,
(uint8_t) 0,
(uint8_t) 255));
}
void startup_boost (void)
{
if (configuration_variables.ui8_startup_motor_power_boost_time > 0)
{
switch (ui8_startup_boost_state_machine)
{
// ebike is stopped, wait for throttle signal to startup boost
case BOOST_STATE_BOOST_DISABLED:
if ((ui8_torque_sensor > 0) &&
(!brake_is_set()))
{
ui8_startup_boost_state_machine = BOOST_STATE_START_BOOST;
}
break;
case BOOST_STATE_START_BOOST:
ui8_startup_boost_enable = 1;
ui8_startup_boost_timer = configuration_variables.ui8_startup_motor_power_boost_time;
ui8_startup_boost_state_machine = BOOST_STATE_BOOST;
break;
case BOOST_STATE_BOOST:
// decrement timer
if (ui8_startup_boost_timer > 0) { ui8_startup_boost_timer--; }
// disable boost if
if ((ui8_torque_sensor == 0) ||
(ui8_startup_boost_timer == 0))
{
ui8_startup_boost_state_machine = BOOST_STATE_END_BOOST;
}
break;
case BOOST_STATE_END_BOOST:
ui8_startup_boost_enable = 0;
// setup variables for fade
ui8_startup_boost_fade_steps = configuration_variables.ui8_startup_motor_power_boost_fade_time;
ui16_startup_boost_fade_variable_x256 = ((uint16_t) ui8_adc_battery_target_current << 8);
ui16_startup_boost_fade_variable_step_amount_x256 = (ui16_startup_boost_fade_variable_x256 / ((uint16_t) ui8_startup_boost_fade_steps));
ui8_startup_boost_fade_enable = 1;
ui8_startup_boost_state_machine = BOOST_STATE_FADE;
break;
case BOOST_STATE_FADE:
if (ui8_startup_boost_fade_steps > 0) { ui8_startup_boost_fade_steps--; }
// disable fade if
if ((ui8_torque_sensor_raw == 0) ||
(ui8_pas_cadence_rpm == 0) ||
(ui8_startup_boost_fade_steps == 0))
{
ui8_startup_boost_fade_enable = 0;
ui8_startup_boost_fade_steps = 0;
ui8_startup_boost_state_machine = BOOST_STATE_BOOST_WAIT_TO_RESTART;
}
break;
// restart when user is not pressing the pedals AND/OR wheel speed = 0
case BOOST_STATE_BOOST_WAIT_TO_RESTART:
// wheel speed must be 0 as also torque sensor
if ((configuration_variables.ui8_startup_motor_power_boost_state & 1) == 0)
{
if ((ui16_wheel_speed_x10 == 0) && (ui8_torque_sensor_raw == 0))
{
ui8_startup_boost_state_machine = BOOST_STATE_BOOST_DISABLED;
}
}
// torque sensor must be 0
if ((configuration_variables.ui8_startup_motor_power_boost_state & 1) > 0)
{
if ((ui8_torque_sensor_raw == 0) || (ui8_pas_cadence_rpm == 0))
{
ui8_startup_boost_state_machine = BOOST_STATE_BOOST_DISABLED;
}
}
break;
default:
break;
}
}
}
void ebike_app_init (void)
{
// init variables with the stored value on EEPROM
eeprom_init_variables ();
ebike_app_set_battery_max_current (ADC_BATTERY_CURRENT_MAX);
}
void ebike_app_controller (void)
{
throttle_read ();
torque_sensor_read ();
read_pas_cadence ();
calc_wheel_speed ();
calc_motor_temperature ();
ebike_control_motor ();
communications_controller ();
}
void communications_controller (void)
{
uint32_t ui32_temp;
#ifndef DEBUG_UART
if (ui8_received_package_flag)
{
// verify crc of the package
ui16_crc_rx = 0xffff;
for (ui8_i = 0; ui8_i < 9; ui8_i++)
{
crc16 (ui8_rx_buffer[ui8_i], &ui16_crc_rx);
}
// see if CRC is ok...
if (((((uint16_t) ui8_rx_buffer [10]) << 8) + ((uint16_t) ui8_rx_buffer [9])) == ui16_crc_rx)
{
ui8_master_comm_package_id = ui8_rx_buffer [1];
// send a variable for each package sent but first verify if the last one was received otherwise, keep repeating
// keep cycling so all variables are sent
#define VARIABLE_ID_MAX_NUMBER 5
if ((ui8_rx_buffer [2]) == ui8_slave_comm_package_id) // last package data ID was receipt, so send the next one
{
ui8_slave_comm_package_id = (ui8_slave_comm_package_id + 1) % VARIABLE_ID_MAX_NUMBER;
}
// assist level
configuration_variables.ui8_power_regular_state_div25 = ui8_rx_buffer [3];
// head light
configuration_variables.ui8_lights = (ui8_rx_buffer [4] & (1 << 0)) ? 1: 0;
lights_set_state (configuration_variables.ui8_lights);
// walk assist
configuration_variables.ui8_walk_assist = (ui8_rx_buffer [4] & (1 << 1)) ? 1: 0;
// battery max current
configuration_variables.ui8_battery_max_current = ui8_rx_buffer [5];
ebike_app_set_battery_max_current (configuration_variables.ui8_battery_max_current);
// target battery max power
configuration_variables.ui8_target_battery_max_power_div25 = ui8_rx_buffer [6];
switch (ui8_master_comm_package_id)
{
case 0:
// battery low voltage cut-off
configuration_variables.ui16_battery_low_voltage_cut_off_x10 = (((uint16_t) ui8_rx_buffer [8]) << 8) + ((uint16_t) ui8_rx_buffer [7]);
// calc the value in ADC steps and set it up
ui32_temp = ((uint32_t) configuration_variables.ui16_battery_low_voltage_cut_off_x10 << 8) / ((uint32_t) ADC8BITS_BATTERY_VOLTAGE_PER_ADC_STEP_INVERSE_X256);
ui32_temp /= 10;
motor_set_adc_battery_voltage_cut_off ((uint8_t) ui32_temp);
break;
case 1:
// wheel perimeter
configuration_variables.ui16_wheel_perimeter = (((uint16_t) ui8_rx_buffer [8]) << 8) + ((uint16_t) ui8_rx_buffer [7]);
break;
case 2:
// wheel max speed
configuration_variables.ui8_wheel_max_speed = ui8_rx_buffer [7];
// PAS max cadence RPM
configuration_variables.ui8_pas_max_cadence = ui8_rx_buffer [8];
break;
case 3:
configuration_variables.ui8_cruise_control = ui8_rx_buffer [7] & 1;
configuration_variables.ui8_motor_voltage_type = (ui8_rx_buffer [7] & 2) >> 1;
configuration_variables.ui8_motor_assistance_startup_without_pedal_rotation = (ui8_rx_buffer [7] & 4) >> 2;
configuration_variables.ui8_throttle_adc_measures_motor_temperature = (ui8_rx_buffer [7] & 8) >> 3;
configuration_variables.ui8_startup_motor_power_boost_state = ui8_rx_buffer [8] & 1;
configuration_variables.ui8_startup_motor_power_boost_limit_to_max_power = (ui8_rx_buffer [8] & 2) >> 1;
break;
case 4:
// startup motor power boost
configuration_variables.ui8_startup_motor_power_boost_div25 = ui8_rx_buffer [7];
// startup motor power boost time
configuration_variables.ui8_startup_motor_power_boost_time = ui8_rx_buffer [8];
break;
case 5:
// startup motor power boost fade time
configuration_variables.ui8_startup_motor_power_boost_fade_time = ui8_rx_buffer [7];
break;
case 6:
// motor temperature min and max values to limit
configuration_variables.ui8_motor_temperature_min_value_to_limit = ui8_rx_buffer [7];
configuration_variables.ui8_motor_temperature_max_value_to_limit = ui8_rx_buffer [8];
break;
case 7:
// offroad mode configuration
configuration_variables.ui8_offroad_func_enabled = ui8_rx_buffer [7] & 1;
configuration_variables.ui8_offroad_enabled_on_startup = (ui8_rx_buffer [7]) & (1 << 1);
configuration_variables.ui8_offroad_speed_limit = ui8_rx_buffer [8];
break;
case 8:
// offroad mode power limit configuration
configuration_variables.ui8_offroad_power_limit_enabled = ui8_rx_buffer [7] & 1;
configuration_variables.ui8_offroad_power_limit_div25 = ui8_rx_buffer [8];
break;
default:
// nothing
break;
}
// verify if any configuration_variables did change and if so, save all of them in the EEPROM
eeprom_write_if_values_changed ();
// signal that we processed the full package
ui8_received_package_flag = 0;
ui8_uart_received_first_package = 1;
}
// enable UART2 receive interrupt as we are now ready to receive a new package
UART2->CR2 |= (1 << 5);
}
uart_send_package ();
#endif
}
void uart_send_package (void)
{
uint16_t ui16_temp;
// send the data to the LCD
// start up byte
ui8_tx_buffer[0] = 0x43;
ui8_tx_buffer[1] = ui8_master_comm_package_id;
ui8_tx_buffer[2] = ui8_slave_comm_package_id;
ui16_temp = motor_get_adc_battery_voltage_filtered_10b ();
// adc 10 bits battery voltage
ui8_tx_buffer[3] = (ui16_temp & 0xff);
ui8_tx_buffer[4] = ((uint8_t) (ui16_temp >> 4)) & 0x30;
// battery current x5
ui8_tx_buffer[5] = (uint8_t) ((float) motor_get_adc_battery_current_filtered_10b () * 0.826);
// wheel speed
ui8_tx_buffer[6] = (uint8_t) (ui16_wheel_speed_x10 & 0xff);
ui8_tx_buffer[7] = (uint8_t) (ui16_wheel_speed_x10 >> 8);
// brake state
if (motor_controller_state_is_set (MOTOR_CONTROLLER_STATE_BRAKE))
{
ui8_tx_buffer[8] |= 1;
}
else
{
ui8_tx_buffer[8] &= ~1;
}
if (configuration_variables.ui8_throttle_adc_measures_motor_temperature)
{
ui8_tx_buffer[9] = UI8_ADC_THROTTLE;
ui8_tx_buffer[10] = configuration_variables.ui8_motor_temperature;
}
else
{
// ADC throttle
ui8_tx_buffer[9] = UI8_ADC_THROTTLE;
// throttle value with offset removed and mapped to 255
ui8_tx_buffer[10] = ui8_throttle;
}
// ADC torque_sensor
ui8_tx_buffer[11] = UI8_ADC_TORQUE_SENSOR;
// torque sensor value with offset removed and mapped to 255
ui8_tx_buffer[12] = ui8_torque_sensor;
// PAS cadence
ui8_tx_buffer[13] = ui8_pas_cadence_rpm;
// pedal human power mapped to 255
ui8_tx_buffer[14] = ui8_pedal_human_power;
// PWM duty_cycle
ui8_tx_buffer[15] = ui8_duty_cycle;
// motor speed in ERPS
ui16_temp = ui16_motor_get_motor_speed_erps ();
ui8_tx_buffer[16] = (uint8_t) (ui16_temp & 0xff);
ui8_tx_buffer[17] = (uint8_t) (ui16_temp >> 8);
// FOC angle
ui8_tx_buffer[18] = ui8_foc_angle;
switch (ui8_slave_comm_package_id)
{
case 0:
// error states
ui8_tx_buffer[19] = 0;
break;
case 1:
// temperature actual limiting value
ui8_tx_buffer[19] = configuration_variables.ui8_temperature_current_limiting_value;
break;
case 2:
// wheel_speed_sensor_tick_counter
ui8_tx_buffer[19] = (uint8_t) (ui32_wheel_speed_sensor_tick_counter & 0xff);
break;
case 3:
// wheel_speed_sensor_tick_counter
ui8_tx_buffer[19] = (uint8_t) ((ui32_wheel_speed_sensor_tick_counter >> 8) & 0xff);
break;
case 4:
// wheel_speed_sensor_tick_counter
ui8_tx_buffer[19] = (uint8_t) ((ui32_wheel_speed_sensor_tick_counter >> 16) & 0xff);
break;
default:
// keep at 0
ui8_tx_buffer[19] = 0;
break;
}
// prepare crc of the package
ui16_crc_tx = 0xffff;
for (ui8_i = 0; ui8_i <= 19; ui8_i++)
{
crc16 (ui8_tx_buffer[ui8_i], &ui16_crc_tx);
}
ui8_tx_buffer[20] = (uint8_t) (ui16_crc_tx & 0xff);
ui8_tx_buffer[21] = (uint8_t) (ui16_crc_tx >> 8) & 0xff;
// send the full package to UART
for (ui8_i = 0; ui8_i <= 21; ui8_i++)
{
putchar (ui8_tx_buffer[ui8_i]);
}
}
static void ebike_control_motor (void)
{
static uint16_t ui16_temp;
uint8_t ui8_temp;
uint8_t _ui8_pas_cadence_rpm;
uint16_t ui16_battery_voltage_filtered;
uint32_t ui32_adc_max_battery_current_boost_state_x4;
uint32_t ui32_adc_max_battery_current_regular_state_x4;
uint32_t ui32_adc_max_battery_current_x4;
uint8_t ui8_adc_max_battery_current = 0;
uint8_t ui8_startup_enable;
uint16_t ui16_adc_battery_target_current_x256;
uint8_t ui8_boost_enable;
uint32_t ui32_temp;
uint8_t ui8_tmp_max_speed;
uint8_t ui8_offroad_mode_max_current;
// calc battery voltage
ui16_battery_voltage_filtered = (uint16_t) motor_get_adc_battery_voltage_filtered_10b () * ADC10BITS_BATTERY_VOLTAGE_PER_ADC_STEP_X512;
ui16_battery_voltage_filtered = ui16_battery_voltage_filtered >> 9;
// calc max battery current for boost state
// calc max battery current for regular state
// calc max battery current (defined by user on LCD3)
ui32_adc_max_battery_current_boost_state_x4 = 0;
ui32_adc_max_battery_current_regular_state_x4 = 0;
if (ui16_battery_voltage_filtered > 15)
{
// 1.6 = 1 / 0.625 (each adc step for current)
// 25 * 1.6 = 40
// 40 * 4 = 160
if (configuration_variables.ui8_startup_motor_power_boost_div25 > 0)
{
ui32_adc_max_battery_current_boost_state_x4 = (((uint32_t) configuration_variables.ui8_startup_motor_power_boost_div25) * 160) / ((uint32_t) ui16_battery_voltage_filtered);
}
if (configuration_variables.ui8_power_regular_state_div25 > 0)
{
ui32_adc_max_battery_current_regular_state_x4 = (((uint32_t) configuration_variables.ui8_power_regular_state_div25) * 160) / ((uint32_t) ui16_battery_voltage_filtered);
}
if (configuration_variables.ui8_target_battery_max_power_div25 > 0)
{
ui32_adc_max_battery_current_x4 = (((uint32_t) configuration_variables.ui8_target_battery_max_power_div25) * 160) / ((uint32_t) ui16_battery_voltage_filtered);
ui8_adc_max_battery_current = ui32_adc_max_battery_current_x4 >> 2;
}
}
// start with disabled
ui8_startup_enable = 0;
// start when we press the pedals
if ((configuration_variables.ui8_power_regular_state_div25 && ui8_torque_sensor)) { ui8_startup_enable = 1; }
_ui8_pas_cadence_rpm = ui8_pas_cadence_rpm;
if (configuration_variables.ui8_motor_assistance_startup_without_pedal_rotation)
{
if (ui8_pas_cadence_rpm < 10) { _ui8_pas_cadence_rpm = 10; }
}
else
{
if (ui8_pas_cadence_rpm < 10) { _ui8_pas_cadence_rpm = 0; }
}
// startup boost state machine
startup_boost ();
if (ui8_startup_boost_enable &&
configuration_variables.ui8_power_regular_state_div25 &&
(_ui8_pas_cadence_rpm > 0))
{
ui8_boost_enable = 1;
}
else
{
ui8_boost_enable = 0;
}
if (ui8_boost_enable)
{
ui32_temp = map ((uint32_t) ui8_torque_sensor,
(uint32_t) 0,
(uint32_t) 255,
(uint32_t) 0,
(uint32_t) ui32_adc_max_battery_current_boost_state_x4);
ui32_temp >>= 2;
if (ui32_temp > 255) { ui8_adc_battery_target_current = 255; }
else { ui8_adc_battery_target_current = (uint8_t) ui32_temp; }
}
else
{
// cadence percentage (in x256)
ui16_temp = (uint16_t) (map (((uint32_t) _ui8_pas_cadence_rpm),
(uint32_t) 0,
(uint32_t) configuration_variables.ui8_pas_max_cadence,
(uint32_t) 0,
(uint32_t) 255));
// human power: pedal torque * pedal cadence
ui8_pedal_human_power = ((((uint16_t) ui8_torque_sensor) * ui16_temp) >> 8);
ui32_temp = map ((uint32_t) ui8_pedal_human_power,
(uint32_t) 0,
(uint32_t) 254, // 254 because max of (255 * 255) >> 8 is 254
(uint32_t) 0,
(uint32_t) ui32_adc_max_battery_current_regular_state_x4);
ui32_temp >>= 2;
if (ui32_temp > 255) { ui8_adc_battery_target_current = 255; }
else { ui8_adc_battery_target_current = (uint8_t) ui32_temp; }
}
// ***********************************************************************************
// make transition from boost to regular level
//
if (ui8_startup_boost_fade_enable)
{
// here we try to converge to the regular value, ramping down or up step by step
ui16_adc_battery_target_current_x256 = ((uint16_t) ui8_adc_battery_target_current) << 8;
if (ui16_startup_boost_fade_variable_x256 > ui16_adc_battery_target_current_x256)
{
ui16_startup_boost_fade_variable_x256 -= ui16_startup_boost_fade_variable_step_amount_x256;
}
else if (ui16_startup_boost_fade_variable_x256 < ui16_adc_battery_target_current_x256)
{
ui16_startup_boost_fade_variable_x256 += ui16_startup_boost_fade_variable_step_amount_x256;
}
ui8_adc_battery_target_current = (uint8_t) (ui16_startup_boost_fade_variable_x256 >> 8);
}
// ***********************************************************************************
// ***********************************************************************************
// throttle: if throttle has higher value, then use it!
//
ui8_temp = (uint8_t) (map ((uint32_t) ui8_throttle,
(uint32_t) 0,
(uint32_t) 255,
(uint32_t) 0,
(uint32_t) ui8_adc_battery_current_max));
ui8_adc_battery_target_current = ui8_max (ui8_adc_battery_target_current, ui8_temp);
// flag that motor assistance should happen because we may be running with throttle
if (ui8_adc_battery_target_current) { ui8_startup_enable = 1; }
// ***********************************************************************************
ui8_tmp_max_speed = configuration_variables.ui8_wheel_max_speed;
// ***********************************************************************************
// offroad mode (limit speed if offroad mode is not active)
//
if (configuration_variables.ui8_offroad_func_enabled && !configuration_variables.ui8_offroad_mode)
{
ui8_tmp_max_speed = configuration_variables.ui8_offroad_speed_limit;
if (configuration_variables.ui8_offroad_power_limit_enabled && configuration_variables.ui8_offroad_power_limit_div25 > 0)
{
ui8_offroad_mode_max_current = (uint8_t) (((((uint32_t) configuration_variables.ui8_offroad_power_limit_div25) * 160) / ((uint32_t) ui16_battery_voltage_filtered)) >> 2);
ui8_adc_battery_target_current = ui8_min (ui8_offroad_mode_max_current, ui8_adc_battery_target_current);
}
}
// ***********************************************************************************
// ***********************************************************************************
// speed limit
//
ui8_adc_battery_target_current = (uint8_t) (map ((uint32_t) ui16_wheel_speed_x10,
(uint32_t) ((ui8_tmp_max_speed * 10) - 20),
(uint32_t) ((ui8_tmp_max_speed * 10) + 20),
(uint32_t) ui8_adc_battery_target_current,
(uint32_t) 0));
// ***********************************************************************************
// ***********************************************************************************
// limit the current to max value defined by user on LCD max power, if:
// - user defined to make that limitation
// - we are not on boost or fade state
if ((configuration_variables.ui8_startup_motor_power_boost_limit_to_max_power == 1) ||
(!((ui8_boost_enable == 1) || (ui8_startup_boost_fade_enable == 1))))
{
// now let's limit the target battery current to battery max current (use min value of both)
ui8_adc_battery_target_current = ui8_min (ui8_adc_battery_target_current, ui8_adc_max_battery_current);
}
// ***********************************************************************************
// ***********************************************************************************
// reduce battery current if motor over temperature
//
if (configuration_variables.ui8_throttle_adc_measures_motor_temperature)
{
// min temperature value can't be equal or higher than max temperature value...
if (configuration_variables.ui8_motor_temperature_min_value_to_limit >= configuration_variables.ui8_motor_temperature_max_value_to_limit)
{
ui8_adc_battery_target_current = 0;
configuration_variables.ui8_temperature_current_limiting_value = 0;
}
else
{
// reduce motor current if over temperature
ui8_adc_battery_target_current = (uint8_t) (map ((uint32_t) configuration_variables.ui16_motor_temperature_x2,
(uint32_t) (((uint16_t) configuration_variables.ui8_motor_temperature_min_value_to_limit) << 1),
(uint32_t) (((uint16_t) configuration_variables.ui8_motor_temperature_max_value_to_limit) << 1),
(uint32_t) ui8_adc_battery_target_current,
(uint32_t) 0));
// get a value linear to the current limitation, just to show to user
configuration_variables.ui8_temperature_current_limiting_value = (uint8_t) (map ((uint32_t) configuration_variables.ui16_motor_temperature_x2,
(uint32_t) (((uint16_t) configuration_variables.ui8_motor_temperature_min_value_to_limit) << 1),
(uint32_t) (((uint16_t) configuration_variables.ui8_motor_temperature_max_value_to_limit) << 1),
(uint32_t) 255,
(uint32_t) 0));
}
}
else
{
// keep ui8_temperature_current_limiting_value = 255 because 255 means no current limiting happening
configuration_variables.ui8_temperature_current_limiting_value = 255;
}
// ***********************************************************************************
// finally set the target battery current to the current controller
ebike_app_set_target_adc_battery_max_current (ui8_adc_battery_target_current);
// set the target duty_cycle to max, as the battery current controller will manage it
// if battery_target_current == 0, put duty_cycle at 0
// if ui8_startup_enable == 0, put duty_cycle at 0
if (ui8_adc_battery_target_current &&
ui8_startup_enable &&
(!brake_is_set()))
{
motor_set_pwm_duty_cycle_target (255);
}
else
{
motor_set_pwm_duty_cycle_target (0);
}
}
// each 1 unit = 0.625 amps
void ebike_app_set_target_adc_battery_max_current (uint8_t ui8_value)
{
// limit max number of amps
if (ui8_value > ui8_adc_battery_current_max)
ui8_value = ui8_adc_battery_current_max;
ui8_adc_target_battery_max_current = ui8_adc_battery_current_offset + ui8_value;
}
// in amps
void ebike_app_set_battery_max_current (uint8_t ui8_value)
{
// each 1 unit = 0.625 amps (0.625 * 256 = 160)
ui8_adc_battery_current_max = ((((uint16_t) ui8_value) << 8) / 160);
if (ui8_adc_battery_current_max > ADC_BATTERY_CURRENT_MAX)
ui8_adc_battery_current_max = ADC_BATTERY_CURRENT_MAX;
}
// This is the interrupt that happens when UART2 receives data. We need it to be the fastest possible and so
// we do: receive every byte and assembly as a package, finally, signal that we have a package to process (on main slow loop)
// and disable the interrupt. The interrupt should be enable again on main loop, after the package being processed
void UART2_IRQHandler(void) __interrupt(UART2_IRQHANDLER)
{
if (UART2_GetFlagStatus(UART2_FLAG_RXNE) == SET)
{
UART2->SR &= (uint8_t)~(UART2_FLAG_RXNE); // this may be redundant
ui8_byte_received = UART2_ReceiveData8 ();
switch (ui8_state_machine)
{
case 0:
if (ui8_byte_received == 0x59) // see if we get start package byte
{
ui8_rx_buffer [ui8_rx_counter] = ui8_byte_received;
ui8_rx_counter++;
ui8_state_machine = 1;
}
else
{
ui8_rx_counter = 0;
ui8_state_machine = 0;
}
break;
case 1:
ui8_rx_buffer [ui8_rx_counter] = ui8_byte_received;
ui8_rx_counter++;
// see if is the last byte of the package
if (ui8_rx_counter > 12)
{
ui8_rx_counter = 0;
ui8_state_machine = 0;
ui8_received_package_flag = 1; // signal that we have a full package to be processed
UART2->CR2 &= ~(1 << 5); // disable UART2 receive interrupt
}
break;
default:
break;
}
}
}
void calc_wheel_speed (void)
{
// calc wheel speed in km/h
if (ui16_wheel_speed_sensor_pwm_cycles_ticks < WHEEL_SPEED_SENSOR_MIN_PWM_CYCLE_TICKS)
{
f_wheel_speed_x10 = ((float) PWM_CYCLES_SECOND) / ((float) ui16_wheel_speed_sensor_pwm_cycles_ticks); // rps
f_wheel_speed_x10 *= configuration_variables.ui16_wheel_perimeter; // millimeters per second
f_wheel_speed_x10 *= 0.036; // ((3600 / (1000 * 1000)) * 10) kms per hour * 10
ui16_wheel_speed_x10 = (uint16_t) f_wheel_speed_x10;
}
else
{
ui16_wheel_speed_x10 = 0;
}
}
struct_configuration_variables* get_configuration_variables (void)
{
return &configuration_variables;
}
void calc_motor_temperature (void)
{
uint16_t ui16_adc_motor_temperatured_filtered_10b;
// low pass filter to avoid possible fast spikes/noise
ui16_adc_motor_temperatured_accumulated -= ui16_adc_motor_temperatured_accumulated >> READ_MOTOR_TEMPERATURE_FILTER_COEFFICIENT;
ui16_adc_motor_temperatured_accumulated += ui16_adc_read_throttle_10b ();
ui16_adc_motor_temperatured_filtered_10b = ui16_adc_motor_temperatured_accumulated >> READ_MOTOR_TEMPERATURE_FILTER_COEFFICIENT;
configuration_variables.ui16_motor_temperature_x2 = (uint16_t) ((float) ui16_adc_motor_temperatured_filtered_10b / 1.024);
configuration_variables.ui8_motor_temperature = (uint8_t) (configuration_variables.ui16_motor_temperature_x2 >> 1);
}