forked from GrandviewIoT/MotorLib_Embedded
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMotorLib_Basic.c
More file actions
3068 lines (2530 loc) · 124 KB
/
Copy pathMotorLib_Basic.c
File metadata and controls
3068 lines (2530 loc) · 124 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
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
//*******1*********2*********3*********4*********5*********6*********7**********
//
// MotorLib_Basic.c
//
// BDC Motor Control Libarary - API and high level routines.
//
// This is a portable code library, designed to work across a number of
// processors, including: TI Tiva Cortex-M
// TI MSP432 with Cortex-M core
// TI MSP430 F5529, FR5969, FR6989, FR5994
// STM32 F3, F4, L4, L7 Cortex-M series
//
// Provides support for the following BDC controllers:
// - L6239 H-Bridge IC,
// - TI SN754410 H-Bridge IC,
// - DRV8848 Dual H-Bridge controller. used on the TI DRV8848 Boosterpack.
// - L6206 Dual H-Bridge controller. used on the STM32 Xnucleo dual brush
// expansion boards.
//
// 6 / 9 / 12 Volts is supplied to DC motors via BDC Controller.
// Motors are driven by PWMs, which are used to modulate the speed.
//
//
// Key differences between Brushed DC (BDC) and Brushless DC (BLDC) motors:
// ------------------------------------------------------------------------
// (1) Brushed DC motors do not require any sensors for spinning (commutating)
// the motor. BLDC motors require either Hall sensors, or BEMF sensing
// through current feedback sensor to detemine rotor position, so that
// it can properly fire the electronic commutation sequence.
// (2) BDC motors effectively have only one tuning know - duty cycle, which
// controls both spped and torque, e.g. to get more spped, increase duty
// cycle. BLDC motors effectively have two tuning knobs:
// - commutation speed (how fast it fires electronic commutation sequence)
// - torque control (amount of power applied) via duty cycle.
// True, if the motor's speed gets bogged down because of heavy load, the
// duty cycle does need to be increased to compensate, but in general a BLDC
// motor can be viewed as having two tuning knobs: speed and power (torque).
// - Speed is controlled via the motor_set_speed_rps() call.
// - Torque (power) is contro0lled via the motor_set_duty_cycle() call.
//
//
//
// THE FOLLOWING ONLY BELONGS IN THE LOW LEVEL PLATFORM DRIVER FOR THE CHIP
//
// DRV8305 BP Usage PWM Tmr LP Conn MSP432
// ------------------- ------- ------- ------
// Vm Motor 6 V supply -
// Gnd Battery Ground -
//
// PWM_AH Phase A Hi PWM TA0.4 J4-1 P2.7
// PWM_AL Phase A Lo PWM TA0.3 J4-2 P2.6
// PWM_BH Phase B Hi PWM TA0.1 J4-3 P2.4
// PWM_BL Phase B Lo PWM TA2.1 J4-4 P5.6
// PWM_CH Phase C Hi PWM TA2.3 J4-5 P6.6
// PWM_CL Phase C Lo PWM TA2.4 J4-6 P6.7
//
// Voltage Sense A ADC A14 J3-3 P6.1 BEMF
// Voltage Sense B ADC A13 J3-4 P4.0 BEMF
// Voltage Sense C ADC A11 J3-5 P4.2 BEMF
// Voltage Sense Vdd ADC A9 J3-6 P4.4
// Current Sense A ADC A8 J3-7 P4.5
// Current Sense B ADC A6 J3-8 P4.7
// Current Sense C ADC A1 J3-9 P5.4 -- Last ADC --
//
// nFAULT GPIO J1-3 P3.2 Input
// PwrGd GPIO J2-5 RST <-- Is this a show stopper ?
// EnGate GPIO J2-8 P5.0 Output
// Wake GPIO J2-9 P5.2 Output
//
// Speed Ctl Pot ADC J1-2 P6.0 A15 Grove J1-2 -> J3-7/27
// Fwd/Reverse Slider J1-4 P3.3 GPIO Grove UART connector
//
// Hall Sensor - Phase A J1-5 P4.1 GPIO rupt
// Hall Sensor Right Phase B J1-6 P4.3 GPIO rupt
// Hall Sensor Left Phase C J1-8 P4.6 GPIO rupt
// White lead = Signal, Red lead = +3.3 Black lead = Gnd
//
// Dagu BDC Motor: 4.5 - 6.0 Volts (absolute max = 8.4 v)
// No Load: 150ma Stall Current: 2.75 A at 6V
// Measured Motor Resistance (Rload): 5.7 ohms
//
// Hall Sensors: 3.0 - 24.0 Volts (Open drain, requiring 10K pullups)
// Encoder Disk: 8 pole neodymium magnet
// 625 state changes per wheel revolution
//
// CAUTION: MSP432 is _NOT_ 5 volt tolerant ! Max input = 4.0 V on GPIOs
//
// 5V from Launchpad 5V (via USB) ==> must be tethered to USB cable
// 5V from LM7805 regulator wired to battery pack
//
// 3.3V from Launchpad (via USB) ==> must be tethered to USB cable
// 3.3V from LM1086-3.3 regulator wired to battery pack
//
// History:
// 05/16/16 - Created as part of Motor Control open source. Duquaine
// 06/20/16 - Tweaked APIs to make it easier for BDC, Stepper, and BLDC
// motors to share APIs and share common support. Duquaine
// 08/13/16 - Add MotorLib_Get_Vbus_Voltage() based on PSoC4 review. Duquaine
//
// - - - - - - - - - - - - - - - - - - - -
//
// The MIT License (MIT)
//
// Copyright (c) 2016 Wayne Duquaine / Grandview Systems
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//******************************************************************************
#include "MotorLib_Api.h" // pull in common definitions
#if defined(MOTOR_DRIVER_L6474)
#include "l6474.h"
void L6474_StepClockHandler (uint8_t motor_id);
void L6474_ApplySpeed (MOTOR_BLOCK *mtr_blk, uint8_t pwmId, uint16_t newSpeed);
#endif
//------------------------------------
// BLDC Motor Blocks - 1 per motor
//------------------------------------
MOTOR_BLOCK motor_blk [MAX_NUMBER_OF_MOTORS];
uint8_t _g_motor_id = 0;
uint8_t _g_motorlib_chip_init = 0; // denotes if basic functions done
extern int _g_PWM_rc; // located in BLDC_motor_ctl_lib.c
CALLBACK_RTN _g_ADC_callback_ptr; // located in xxxx_ISRs.c ???
#if (MOTOR_IS_BLDC)
extern unsigned char Hall_CW_DIR_sequence[]; // fwd ref
#endif
#if defined(TI_CCS_COMPILER)
//void __attribute__((weak)) MOTOR_HANDLERS * DRV8848_GetMotorHandle(void) {return ((MOTOR_HANDLERS*) 0);}
// This fights with the MOTOR_HANDLERS *_g_mtrdrvr = &drv8848_table; definition below
#endif
//---------------------------------------------
// pick up the appropriate Motor Driver support
//---------------------------------------------
#if defined(MOTOR_DRIVER_STSPIN250)
extern MOTOR_HANDLERS drv6206_table; // battery based BDC
MOTOR_HANDLERS *_g_mtrdrvr = &drv6206_table;//is simlar to L6206
#elif defined(MOTOR_DRIVER_L6206)
extern MOTOR_HANDLERS drv6206_table; // moderate duty BDC
MOTOR_HANDLERS *_g_mtrdrvr = &drv6206_table;
#elif defined(MOTOR_DRIVER_SN754410) || defined(MOTOR_DRIVER_L293D)
extern MOTOR_HANDLERS drv754410_table; // light duty BDC
MOTOR_HANDLERS *_g_mtrdrvr = &drv754410_table;
#elif defined(MOTOR_DRIVER_DRV8848)
extern MOTOR_HANDLERS drv8848_table; // light duty BDC
MOTOR_HANDLERS *_g_mtrdrvr = &drv8848_table;
#elif defined(MOTOR_DRIVER_STSPIN220)
extern MOTOR_HANDLERS drv6474_table; // battery based Stepper
MOTOR_HANDLERS *_g_mtrdrvr = &drv6474_table;//is simlar to L6474
#elif defined(MOTOR_DRIVER_L6470)
extern MOTOR_HANDLERS drv6470_table; // Dual Stepper
MOTOR_HANDLERS *_g_mtrdrvr = &drv6470_table;
#elif defined(MOTOR_DRIVER_L6474)
extern MOTOR_HANDLERS drv6474_table; // light duty Stepper
MOTOR_HANDLERS *_g_mtrdrvr = &drv6474_table;
#elif defined(MOTOR_DRIVER_POWERSTEP01)
extern MOTOR_HANDLERS drvPOWERSTEP01_table; // Heavy duty Stepper
MOTOR_HANDLERS *_g_mtrdrvr = &drvPOWERSTEP01_table;
#elif defined(MOTOR_DRIVER_DRV8711)
extern MOTOR_HANDLERS drv8711_table; // moderate duty Stepper
MOTOR_HANDLERS *_g_mtrdrvr = &drv8711_table;
#elif defined(MOTOR_DRIVER_STSPIN230)
extern MOTOR_HANDLERS drv6230_table; // battery based BLDC
MOTOR_HANDLERS *_g_mtrdrvr = &drv6230_table;//is simlar to L6230
#elif defined(MOTOR_DRIVER_L6230)
extern MOTOR_HANDLERS drv6230_table; // light duty BLDC
MOTOR_HANDLERS *_g_mtrdrvr = &drv6230_table;
#elif defined(MOTOR_DRIVER_L6398)
extern MOTOR_HANDLERS drv6398_table; // heavy duty BLDC
MOTOR_HANDLERS *_g_mtrdrvr = &drv6398_table;
#elif defined(MOTOR_DRIVER_DRV8301)
extern MOTOR_HANDLERS drv8301_table; // moderate duty BLDC
MOTOR_HANDLERS *_g_mtrdrvr = &drv8301_table;
#elif defined(MOTOR_DRIVER_DRV8305)
extern MOTOR_HANDLERS drv8305_table; // heavy duty BLDC
MOTOR_HANDLERS *_g_mtrdrvr = &drv8305_table;
#elif defined(PSOC4_CY8CKIT_037_BLDC) || defined(PSOC4_CY8CKIT_037_STEPPER)
extern MOTOR_HANDLERS drvPSOC037_table; // moderate duty BLDC
MOTOR_HANDLERS *_g_mtrdrvr = &drvPSOC037_table;
#else
error "You must define a MOTOR_DRIVER entry"
#endif
#if defined(ST_GNU_IAR_COMPILERS)
// Cypress KEIL compiler barfs on __weak keyword
// Get motor handle for STSPIN250 BDC
__weak MOTOR_HANDLERS * STSPIN250_GetMotorHandle(void) {return ((MOTOR_HANDLERS*) 0);}
// Get motor handle for L6206 BDC
__weak MOTOR_HANDLERS * L6206_GetMotorHandle(void) {return ((MOTOR_HANDLERS*) 0);}
// Get motor handle for L6206 BDC
__weak MOTOR_HANDLERS * L293D_GetMotorHandle(void) {return ((MOTOR_HANDLERS*) 0);}
// Get motor handle for L6206 BDC
__weak MOTOR_HANDLERS * SN754410_GetMotorHandle(void) {return ((MOTOR_HANDLERS*) 0);}
// Get motor handle for L6206 BDC
__weak MOTOR_HANDLERS * DRV8848_GetMotorHandle(void) {return ((MOTOR_HANDLERS*) 0);}
// Get motor handle for STSPIN220 stepper
__weak MOTOR_HANDLERS * STSPIN220_GetMotorHandle(void) {return ((MOTOR_HANDLERS*) 0);}
// Get motor handle for L6470 stepper
__weak MOTOR_HANDLERS * L6470_GetMotorHandle(void) {return ((MOTOR_HANDLERS*) 0);}
// Get motor handle for L6474 stepper
__weak MOTOR_HANDLERS * L6474_GetMotorHandle(void) {return ((MOTOR_HANDLERS*) 0);}
// Get motor handle for Powerstep stepper
__weak MOTOR_HANDLERS * Powerstep01_GetMotorHandle(void) {return ((MOTOR_HANDLERS*) 0);}
// Get motor handle for DRV8711
__weak MOTOR_HANDLERS * DRV8711_GetMotorHandle(void) {return ((MOTOR_HANDLERS*) 0);}
// Get motor handle for STSPIN230 BLDC
__weak MOTOR_HANDLERS * STSPIN230_GetMotorHandle(void) {return ((MOTOR_HANDLERS*) 0);}
// Get motor handle for L6208 BLDC
__weak MOTOR_HANDLERS * L6208_GetMotorHandle(void) {return ((MOTOR_HANDLERS*) 0);}
// Get motor handle for L6230 BLDC
__weak MOTOR_HANDLERS * L6230_GetMotorHandle(void) {return ((MOTOR_HANDLERS*) 0);}
// Get motor handle for L6398 BLDC
__weak MOTOR_HANDLERS * L6398_GetMotorHandle(void) {return ((MOTOR_HANDLERS*) 0);}
// Get motor handle for DRV8301 BLDC
__weak MOTOR_HANDLERS * DRV8301_GetMotorHandle(void) {return ((MOTOR_HANDLERS*) 0);}
// Get motor handle for DRV8305 BLDC
__weak MOTOR_HANDLERS * DRV8305_GetMotorHandle(void) {return ((MOTOR_HANDLERS*) 0);}
// Get motor handle for PSOC037 BLDC
__weak MOTOR_HANDLERS * PSOC037_GetMotorHandle(void) {return ((MOTOR_HANDLERS*) 0);}
#endif
#if defined (CYPRESS_COMPILER)
// Module Prototype Defs
MOTOR_HANDLERS * STSPIN250_GetMotorHandle(void);
MOTOR_HANDLERS * L6206_GetMotorHandle(void);
MOTOR_HANDLERS * L293D_GetMotorHandle(void);
MOTOR_HANDLERS * SN754410_GetMotorHandle(void);
MOTOR_HANDLERS * DRV8848_GetMotorHandle(void);
MOTOR_HANDLERS * STSPIN220_GetMotorHandle(void);
MOTOR_HANDLERS * L6470_GetMotorHandle(void);
MOTOR_HANDLERS * L6474_GetMotorHandle(void);
MOTOR_HANDLERS * Powerstep01_GetMotorHandle(void);
MOTOR_HANDLERS * DRV8711_GetMotorHandle(void);
MOTOR_HANDLERS * STSPIN230_GetMotorHandle(void);
MOTOR_HANDLERS * L6208_GetMotorHandle(void);
MOTOR_HANDLERS * L6230_GetMotorHandle(void);
MOTOR_HANDLERS * L6398_GetMotorHandle(void);
MOTOR_HANDLERS * DRV8301_GetMotorHandle(void);
MOTOR_HANDLERS * DRV8305_GetMotorHandle(void);
MOTOR_HANDLERS * PSOC037_GetMotorHandle(void);
#endif
//-----------------------------------------------------
// values updated by ADC/GPIO ISRs.
//
// These externs reside in BDC_motor_ctl_ISRs_xxx.c
//-----------------------------------------------------
extern int g_adc_current_VREF_raw_value; // DRV8848 value for VREF from ADC
extern int g_adc_speed_raw_value; // speed value from ADC input
const long long_100 = 100; // CONSTANTS
// VARIABLES
int adc_timer = 0;
int _g_motor_init_complete = 0;
long _g_mtr_mcu_speed = 0; // MCU speed (SMCLK)
uint32_t _g_pwm_period_ticks = 0;
float _g_period_ticks_ratio; // scaling factor ticks : %
int _g_motor_ramp_active_flag = 0; // used by SysTick ISR to see any ramp in progress
//---------------------------------
// Encoder related variables
//---------------------------------
int encoder_timer = 0; // Used by Systick handler
int encoder_one_tenth_rupts = 0; // counts how many 0.1 interrupts
uint32_t encoder_start_interval = 0; // 1 second measurement interval
uint32_t encoder_end_interval = 0; // updated once per second
int encoder_LEFT_1_sec_agg = 0; // Aggregate counts for 1 sec
int encoder_RIGHT_1_sec_agg = 0;
float encoder_LEFT_speed_rps = 0; // speed in revs / second
float encoder_RIGHT_speed_rps = 0;
float encoder_pulses_per_revolution = (PULSES_PER_REVOLUTION / 10.0);
float revs_per_sec = 0.0; // revs/sec from Hall sensors
int motor_ctl_adc_init (void); // internal routines prototypes
void motor_adc_process_speed_control (void);
int MotorLib_Init_Chip (long mcu_speed);
void ramp_done_callback (int motor_num);
//void motor_ramp_update_check (void);
void process_hall_sensors (void);
//******************************************************************************
//******************************************************************************
//******************************************************************************
//
// GENERIC MOTOR APIs and ROUTINEs
//
//******************************************************************************
//******************************************************************************
//******************************************************************************
//******************************************************************************
//******************************************************************************
//******************************************************************************
// MotorLib_Init_Chip INTERNAL ROUTINE
//
// Initialize basic motor library facilities, such as the Motor Chip
// itself, to ensure it is properly reset and enabled, ADCs setup, ...
//******************************************************************************
//******************************************************************************
//******************************************************************************
int MotorLib_Init_Chip (long mcu_speed) // also pass driver type (DRV9948, SN754419, ... ?)
{ // or make external .h parm config instead ?
if (_g_motorlib_chip_init == 1)
return (0); // we were previously called. no need to repeat
_g_mtr_mcu_speed = mcu_speed;
// this assumes that board_init() was called by the app
// to ensure CPU clocks and base GPIO clocks were setuup
// should we call board-Init() just in case and have it check a
// global _g_board_init_complete flag ???
// PROBABLY
//-------------------------------------------------------------------
// do any chip level initialization (e.g. reset then enablbe chip)
//-------------------------------------------------------------------
_g_mtrdrvr->motor_init_controller_chip(); // Reset/Enable chip if needed
//-------------------------------------------------------------------
// Initialize MOTOR CHIP related GPIO input and output pins (and SPI)
//-------------------------------------------------------------------
// Initialize Motor chip related inputs/outputs (Enable, Fault, ...)
_g_mtrdrvr->motor_init_gpio_ctl_inputs(); // call MCU dependent GPIO init inputs handler
_g_mtrdrvr->motor_init_gpio_ctl_outputs(); // call MCU dependent GPIO init outputs handler
//---------------------------------------------------------------------
// Initialize MOTOR CHIP related ADC inputs (motor current, BEMF, ...)
//---------------------------------------------------------------------
// call MCU dependent ADC speed ctl init handler
if (_g_mtrdrvr->motor_init_adc_ctl_inputs != 0L)
_g_mtrdrvr->motor_init_adc_ctl_inputs(); // call MCU dependent ADC init
return (0); // denote succeeded
}
//******************************************************************************
// MotorLib_Init_Motor
//
// Configure Motor, including setting up PWM timers for Motor operation
//******************************************************************************
int MotorLib_Init_Motor (uint8_t motor_id, int pwm_speed, int pole_pairs)
{
MOTOR_BLOCK *mtr_blk;
unsigned long int cpu_ticks_per_sec; // have to use long _int_ to handle
unsigned long int period_ticks; // MSP430 compiler goofiness
unsigned long int pwm_speed_long;
//-----------------------------------------------------------------------
// First, ensure that the basic functions in MotorLib, especially
// the MOTOR CONTROLLER CHIP has been initialized (including any SPI)
//-----------------------------------------------------------------------
if (_g_motorlib_chip_init == 0)
{ MotorLib_Init_Chip (MCU_SPEED); // do any chip-wide initialization
_g_motorlib_chip_init = 1; // denote chip has been intialized
}
//-----------------------------------------------------------------------
// init our internal MOTOR_BLOCK, which is used to control motor states
//-----------------------------------------------------------------------
mtr_blk = &motor_blk [motor_id];
// memset (mtr_blk, 0, sizeof(MOTOR_BLOCK)); // ensure is cleared out
//return (0); // 08/28/16 - brute force tests to see why stack getting clobbered - STACK IS FUCKED UP AT THIS POINT ==> MEMSET LENGTH IS WRONG
// in future, add to table (ensure no competing entry)
mtr_blk->mtr_id = motor_id;
mtr_blk->mtr_cb_sig = MOTOR_DRIVER;
mtr_blk->mtr_state = MOTOR_STATE_STOPPED; // denote are currently stopped
mtr_blk->mtr_direction = DIRECTION_CW;
mtr_blk->mtr_pole_pairs = pole_pairs; // save # pole pairs in motor
// derive and compute any additional stuff based on # pole pairs
#if defined(MOTOR_IS_BLDC)
mtr_blk->mtr_Trapezoidal_Index = 1; // set BLDC startup defaults
mtr_blk->mtr_hall_sector = Hall_CW_DIR_sequence[1];
#endif // defined(MOTOR_IS_BLDC)
//----------------------------------------------------------------------
// Using the MCU clock, get the correct PWM tick count for 20 KHz PWM
//----------------------------------------------------------------------
cpu_ticks_per_sec = board_system_clock_get_frequency();
// MSP430 CCS Compiler CAUTION: turning on "Local Optimization" or above
// causes their optimizer to optimize out the following 3
// statements, causing period_ticks value to be totally screwed up
pwm_speed_long = pwm_speed; // ensure everything in 32 bit long for DIV
period_ticks = cpu_ticks_per_sec / pwm_speed_long;
period_ticks--; // deduct by 1 for Modulo 0 counters
_g_period_ticks_ratio = ((float) period_ticks) / 100.00; // create 100% ratio
mtr_blk->mtr_pwm_period_ticks = period_ticks; // save our PWM perid
_g_pwm_period_ticks = period_ticks; // and a global copy/DEBUG
//----------------------------------------------------------------------
// Inbitialize the PWMs that will be driving the motor
//----------------------------------------------------------------------
_g_mtrdrvr->motor_init_pwms_comm (mtr_blk, period_ticks); // call MCU dependent PWM init
#if ! defined(USES_L6230) // SKIP OVER IF BLDC for now
if (_g_mtrdrvr->motor_init_commute_timer != 0L) // BLDC
_g_mtrdrvr->motor_init_commute_timer (mtr_blk, 10000); // or is larger/smaller value needed for 20,000 RPM ?
#if defined(F5529_OPEN_LOOP)
//------------------------------------------------------------------
// Configure Timer A PWMs:
// PWMH A/U TA2.2 P2.5 J4-1
// PWML A/U TA2.1 P2.4 J4-2
// PWMH B/V TA0.4 P1.5 J4-3
// PWML B/V TA0.3 P1.4 J4-4
// PWMH C/W TA0.2 P1.3 J4-5
// PWML C/W TA0.1 P1.2 J4-6
//------------------------------------------------------------------
// Setup startup PWM default configuration
// Setup HighSide MOSFETs (TB0.2/4/6) as PWMs (set/reset)
// setup LowSide MOSFETs (TB0.1/3/5) as logic 'high' Z (off)
TA0CCR0 = TIMER_PWM_PERIOD-1; // CCR0 = PWM Period = 15640
TA0CCTL4 = OUTMOD_7; // PWMH V CCR4 = PWM mode reset/set
TA0CCTL3 = OUTMOD_0; // PWML V CCR3 = OUT mode and Low
TA0CCTL2 = OUTMOD_7; // PWMH W CCR2 = PWM mode reset/set
TA0CCTL1 = OUTMOD_0; // PWML W CCR1 = OUT mode and Low
Current_PWM_DutyCycle = MIN_PWM_DUTYCYCLE; // Initial Dutycycle = 1023
// Init HighSide PWM outputs (TA2.2, TA0.4, TA0.2) with initial duty cycle
TA2CCR2 = Current_PWM_DutyCycle;
TA0CCR4 = Current_PWM_DutyCycle;
TA0CCR2 = Current_PWM_DutyCycle;
// Check with Vbus is good to proceed with motor start
if (Avg_vBUS < 0xFF)
{
// Wait until vBUS increases 3.3V?
}
//-----------------------------------------
// Do initial read of Hall Sensor inputs
//-----------------------------------------
Hall_IN = P1IN;
Hall_IN = ((Hall_IN & 0x0E) >> 1);
//-------------------------------------------------------
// Setup PWMs based on wehere Hall Encoder is at startup
//-------------------------------------------------------
PreDriver_Sequence = Hall_DIR_sequence [Hall_IN]; // set initial state based on Hall
PWM_update (PreDriver_Sequence);
#endif // defined(F5529_OPEN_LOOP)
#endif // ! defined(USES_L6230)
return (0); // denote succeeded
}
//******************************************************************************
// MotorLib_Adc_Init_Speed_Control
//
// Configure ADC to read potentiometer used for speed control setting.
// Uses A15 on pin P6.0
//
// ADC GPIO port/pin related stuff is in motor_config.h file
//******************************************************************************
int MotorLib_Adc_Init_Speed_Control (int ADC_channel, CALLBACK_RTN cb_ptr)
{
//------------------------------------------------------------------
// call MCU dependent ADC speed ctl init handler
//------------------------------------------------------------------
if (_g_mtrdrvr->motor_adc_speed_ctl_init != 0L)
_g_mtrdrvr->motor_adc_speed_ctl_init (ADC_channel, cb_ptr);
_g_ADC_callback_ptr = cb_ptr; // save pointer to any callback function
return (0); // denote succeeded
}
//******************************************************************************
// MotorLib_Encoder_Init
//
// Configure inputs for Motor Encode (Hall/QEI) Sensors (interrupt driven)
//
// Encoder_Type = WHEEL_ENCODER_RIGHT/LEFT, HALL_SENSOR, QEI
// GPIO port/pin related stuff is in motor_config.h file
//******************************************************************************
int MotorLib_Encoder_Init (uint8_t motor_id, int Encoder_Type)
{
MOTOR_BLOCK *mtr_blk;
mtr_blk = &motor_blk[motor_id];
//------------------------------------------------------------------
// call MCU dependent GPIO encoder init handler
//------------------------------------------------------------------
_g_mtrdrvr->motor_init_encoder_gpio (mtr_blk, Encoder_Type);
return (0); // denote succeeded
}
//*****************************************************************************
// motor_read_hall_sensors
//
// read in the hall sensor values, and convert to standard Hall index
//*****************************************************************************
int motor_read_hall_sensors (uint8_t motor_id)
{
int hall_index;
MOTOR_BLOCK *mtr_blk;
mtr_blk = &motor_blk[motor_id];
if (_g_mtrdrvr->motor_read_hall_sensors_handler != 0L)
hall_index = _g_mtrdrvr->motor_read_hall_sensors_handler (mtr_blk);
else hall_index = -1;
return (hall_index);
}
//******************************************************************************
// process_hall_sensors
//
// Check Hall Sensors and update current speed/velocity value,
// as well as approx distance travelled fwd/backwd.
//******************************************************************************
void process_hall_sensors (void)
{
// compute revs_per_sec (converted to float)
}
//*****************************************************************************
// motor_HW_test
//
// Allow caller to run a hardware test on the board, to ensure all
// GPIOs, ADCs, PWMs are functioning correctly
//
//*****************************************************************************
int motor_HW_test (uint8_t motor_id, int duty_cycle_percent)
{
int trc;
int actual_duty_count;
long period_ticks;
MOTOR_BLOCK *mtr_blk;
mtr_blk = &motor_blk[motor_id];
period_ticks = mtr_blk->mtr_pwm_period_ticks;
actual_duty_count = duty_cycle_percent; // convert to long
actual_duty_count *= period_ticks; // convert to ticks
actual_duty_count /= long_100; // eliminate * 100 percent piece
if (actual_duty_count > period_ticks)
actual_duty_count = period_ticks; // do not exceed max period
trc = _g_mtrdrvr->motor_HW_check (mtr_blk, actual_duty_count);
return (0); // denote worked OK
}
// Need a CONTROL / ISSUE_CMD type interface to engage/disengage Stepper motor, etc
// Is advanced, but seperate and distict from start
//******************************************************************************
// MotorLib_Check_Is_Started
//
// Checks if the motor has been successfully started
//******************************************************************************
int MotorLib_Check_Is_Started (uint8_t motor_id)
{
MOTOR_BLOCK *mtr_blk;
mtr_blk = &motor_blk[motor_id];
if (mtr_blk->mtr_state == MOTOR_STATE_RUN)
return (1); // motor is started
return (0); // motr start not complete
// ??? !!! in FUTURE: return Negative number if start failed ??? !!!
}
//******************************************************************************
// MotorLib_Motor_Engage - THIS IS A CRAPPY NAME - change it !!!!
//
// Issue special control actions to the motor control chip.
//
// @brief Requests the motor to mark the current position as the home position (ABS_POSITION = 0)
// @brief Issues the Enable command to the motor driver of the specified device
// @brief Issue the Disable command to the motor driver of the specified device
//
// @note For brush DC motor, when input of different brigdes are parallelized
// together, the disabling of one bridge leads to the disabling
// of the second one.
//
// This is routine used by BDC and STEPPER, but not by BLDC L6230.
//
// Valid actions: ENGAGE_MOTOR / DISENGAGE_MOTOR
//******************************************************************************
int MotorLib_Motor_Engage (uint8_t motor_id, int on_off, int flags)
{
MOTOR_BLOCK *mtr_blk;
mtr_blk = &motor_blk[motor_id];
if (_g_mtrdrvr != 0L && _g_mtrdrvr->motor_do_action_handler != 0L)
{
if (on_off == 0) // dis-engage the (stepper) motor ?
_g_mtrdrvr->motor_do_action_handler (mtr_blk, CMD_DISENGAGE_MOTOR, flags);
else _g_mtrdrvr->motor_do_action_handler (mtr_blk, CMD_ENGAGE_MOTOR, flags);
}
else return (ERROR_INVALID_CONTROL_ACTION);
return (0);
}
/******************************************************//**\
* MotorLib_Check_Status
*
* @brief Checks if at least one device has an alarm flag set
* by reading flag pin position.
* The flag pin is shared between all devices.
* @retval One if at least one device has an alarm flag set ,
* otherwise zero
**********************************************************/
int MotorLib_Check_Status (uint8_t motor_id, int type_status, int flags)
{
int value;
MOTOR_BLOCK *mtr_blk;
mtr_blk = &motor_blk[motor_id];
if ((_g_mtrdrvr != 0) && (_g_mtrdrvr->motor_check_status != 0))
value = _g_mtrdrvr->motor_check_status (mtr_blk, type_status, flags);
else return (ERROR_UNSUPPORTED_FUNCTION);
return (value);
}
//******************************************************************************
// MotorLib_Get_Acceleration
//
// Returns the current acceleration (in PPS) of the specified motor
//******************************************************************************
uint16_t MotorLib_Get_Acceleration (uint8_t motor_id)
{
return (motor_blk[motor_id].mtr_acceleration_pc); // is in PPS squared
}
//******************************************************************************
// MotorLib_Get_Current_Speed
//
// Returns the current speed (in PPS or RPM) of the specified motor
//******************************************************************************
uint16_t MotorLib_Get_Current_Speed (uint8_t motor_id, int speed_type)
{
if (speed_type == SPEED_PPS)
return (motor_blk[motor_id].mtr_current_velocity_pc);
else if (speed_type == SPEED_RPM)
; // 09/03/16 - NEED TO ADD THIS ??? !!!
else return (ERROR_INVALID_SPEED_TYPE);
}
//******************************************************************************
// MotorLib_Get_Deceleration
//
//
// Returns the current deceleration (in PPS) of the specified motor
//******************************************************************************
uint16_t MotorLib_Get_Deceleration (uint8_t motor_id)
{
return (motor_blk[motor_id].mtr_deceleration_pc);
}
//******************************************************************************
// MotorLib_Get_Distance_Moved
//
// Returns the distance move in the last MOVE or GOTO operation.
// To do this, it must convert steps (or rotations) moved
// into the equivalent distance.
//
// Used by BLDC motors with Encoders and Stepper Motors.
// Can also be used by BDC motors that have HALL encoded shafts/wheels
//
// motor_id (from 0 to 2)
// direction FORWARD or BACKWARD
// distance distance to move
// units units used for the distance: MM, CM, INCHES, ROTATIONS
//
// Returns a float, that will be positive if the move was forward,
// or negative if the move was backward.
//******************************************************************************
float MotorLib_Get_Distance_Moved (uint8_t motor_id, UNITS_t units)
{
float distance_moved;
float steps_moved;
float rotations_moved;
MOTOR_BLOCK *mtr_blk;
// CAUTION: for smart controllers like powerSTEP01, we can directly send the command to the chip. It will do the motion profile
mtr_blk = &motor_blk[motor_id];
// need to have staging variables that record:
// last move type (ROTATIONS vs STEPS/DISTANCE)
// last start position in steps or rotations
// last end position in steps or rotations
if (mtr_blk->mtr_move_type == MOVE_N_ROTATIONS)
{
rotations_moved = (float) mtr_blk->mtr_move_begin_rotation - mtr_blk->mtr_move_end_rotation;
distance_moved = rotations_moved * mtr_blk->mtr_distance_per_rotation_mm;
}
else
{ // all other moves end up being in steps
steps_moved = (float) mtr_blk->mtr_move_begin_position - mtr_blk->mtr_move_end_position;
distance_moved = steps_moved * mtr_blk->mtr_distance_per_rotation_mm;
}
//----------------------------------------------------------------
// convert the distance moved into the apporpriate output measurement.
//----------------------------------------------------------------
switch (units)
{ case UNITS_INCH:
// convert MM into INCHES
// 1 mm = 0.0393701 inches
distance_moved = distance_moved * 0.0393701;
break;
case UNITS_CM:
// convert MM into CM
distance_moved = distance_moved / 10.0; // are 10 MM in each 1 CM
break;
case UNITS_MM:
// already in MM, no distance conversion needed.
break;
default:
return (ERROR_INVALID_UNITS);
}
return (distance_moved);
}
/******************************************************//**
* @brief Returns the mark position of the specified device
* @param[in] deviceId (from 0 to MAX_NUMBER_OF_DEVICES - 1)
* For L6208: dummy parameter for compatibility with motor.h
* @retval Mark register value converted in a 32b signed integer
**********************************************************/
int32_t MotorLib_Get_Mark (uint8_t motor_id)
{
int32_t mark;
MOTOR_BLOCK *mtr_blk;
mtr_blk = &motor_blk[motor_id];
mark = 0;
if (_g_mtrdrvr != 0L && _g_mtrdrvr->motor_get_mark_pos != 0L)
mark = _g_mtrdrvr->motor_get_mark_pos (mtr_blk);
else return (ERROR_UNSUPPORTED_FUNCTION);
return (mark);
}
//******************************************************************************
// MotorLib_Get_Max_Speed
//
//
// Returns the current max speed setting for the specified motor
//******************************************************************************
uint16_t MotorLib_Get_Max_Speed (uint8_t motor_id)
{
// but should it be returned in PPS, RPM, DUTY, ... need UNITS parm
return (motor_blk[motor_id].mtr_max_velocity_pc);
}
//******************************************************************************
// MotorLib_Get_Min_Speed
//
//
// Returns the current min speed setting for the specified motor
//******************************************************************************
uint16_t MotorLib_Get_Min_Speed (uint8_t motor_id)
{
// but should it be returned in PPS, RPM, DUTY, ... need UNITS parm
return (motor_blk[motor_id].mtr_min_velocity_pc);
}
/******************************************************//**\
* MotorLib_Get_Motor_Status
*
* @brief Issue a CmdStatus to Motor to get its actual state
* The flag pin is shared between all devices.
* @retval One if at least one device has an alarm flag set ,
* otherwise zero
**********************************************************/
int32_t MotorLib_Get_Motor_Status (uint8_t motor_id, int type_status)
{
int value;
MOTOR_BLOCK *mtr_blk;
mtr_blk = &motor_blk[motor_id];
if ((_g_mtrdrvr != 0) && (_g_mtrdrvr->motor_check_status != 0))
value = _g_mtrdrvr->motor_get_motor_status (mtr_blk, type_status);
else return (ERROR_UNSUPPORTED_FUNCTION);
return (value);
}
//*****************************************************************************
// MotorLib_Get_Period_Ticks
//
// Get the PWM's actual period in MCU ticks
//*****************************************************************************
long MotorLib_Get_Period_Ticks (uint8_t motor_id)
{
long pwm_period_ticks;
MOTOR_BLOCK *mtr_blk;
mtr_blk = &motor_blk[motor_id];
pwm_period_ticks = mtr_blk->mtr_pwm_period_ticks;
return (pwm_period_ticks);
}
//******************************************************************************
// MotorLib_Get_Position
//
// Get Position
// @brief Returns the ABS_POSITION of the specified motor
// @param[in] motor_id (from 0 to 2)
// @retval ABS_POSITION register value converted in a 32b signed integer
//******************************************************************************
int32_t MotorLib_Get_Position (uint8_t motor_id)
{
int32_t abs_position;
MOTOR_BLOCK *mtr_blk;
mtr_blk = &motor_blk[motor_id];
abs_position = _g_mtrdrvr->motor_get_actual_position (mtr_blk);
return (abs_position);
}
//*****************************************************************************
// MotorLib_Get_Speed_Angular_Velocity
// This sets the motor's desired speed in terms of Angular velocity.
// This can be derived bu multiplying RPS * 2 PI * ???
//
// Parms:
// motor_num: number of the motor to update (1 or 2)
// target_velovity_per_sec: requested angular velocity per second.
// actual_velovity_per_sec: current, measured angular velocity per second
//*****************************************************************************
void MotorLib_Get_Speed_Angular_Velocity (uint8_t motor_id,
uint16_t *target_velovity_per_sec,
uint16_t *actual_velovity_per_sec)
{
MOTOR_BLOCK *mtr_blk;
mtr_blk = &motor_blk[motor_id];
*target_velovity_per_sec = mtr_blk->mtr_angular_velocity_setpt;
*actual_velovity_per_sec = mtr_blk->mtr_angular_velocity_actual;
}
//*****************************************************************************
// MotorLib_Get_Speed_Rpm motor_get_speed_rps
//
// This gets the motor's current speed in (mechanical) revolutions
// per minute.
//
// Parms:
// motor_num: number of the motor to update (1 or 2)
// target_rps_speed: requested speed in (mechanical) revolutions per second
// actual_rps_speed: current, actual measured speed.
//*****************************************************************************
void MotorLib_Get_Speed_Rpm (uint8_t motor_id, uint16_t *target_rpm_speed,
uint16_t *actual_rpm_speed)
{
// or make this a by SPEED_UNITs call ??? RPM / PPS / DUTY ???
MOTOR_BLOCK *mtr_blk;
mtr_blk = &motor_blk[motor_id];
*target_rpm_speed = mtr_blk->mtr_rpm_setpt;
*actual_rpm_speed = mtr_blk->mtr_rpm_actual;
}
//*****************************************************************************
// MotorLib_Get_Speed_Reference_Value
//
// This gets the current Speed Control (Potentiometer) value.
// It is used to set or vary the speed of the motor.
//
// Parms:
// motor_num: number of the motor to check (1 or 2)
//*****************************************************************************
uint16_t MotorLib_Get_Speed_Reference_Value (uint8_t motor_id)
{
uint16_t speed_pot_value;
MOTOR_BLOCK *mtr_blk;
mtr_blk = &motor_blk[motor_id];
if (_g_mtrdrvr->motor_get_speed_pot_value != 0L)
speed_pot_value = _g_mtrdrvr->motor_get_speed_pot_value (mtr_blk);
else speed_pot_value = 0;
return (speed_pot_value);
}