brazos-trapezoidal
TrapSless.c
Go to the documentation of this file.
1
15//-------------------------------------- Include Files ----------------------------------------------------------------
16#include "TrapSless.h"
17#include "includes.h"
18
19//-------------------------------------- PUBLIC (Variables) -----------------------------------------------------------
22
23//-------------------------------------- PRIVATE (Variables, Constants & Defines) -------------------------------------
24#ifdef SIMU
25int32_t Vdc_Ad_Forced_Value[2];
26int32_t Iu_Ad_Forced_Value[2];
27int32_t Iv_Ad_Forced_Value[2];
28int32_t Iw_Ad_Forced_Value[2];
29int32_t Vu_Ad_Forced_Value[2];
30int32_t Vv_Ad_Forced_Value[2];
31int32_t Vw_Ad_Forced_Value[2];
32
33DUTY_PWM_TYPE DutyPwm;
34
35#define POLE_UP DutyPwm.UP
36#define POLE_UN DutyPwm.UN
37#define POLE_VP DutyPwm.VP
38#define POLE_VN DutyPwm.VN
39#define POLE_WP DutyPwm.WP
40#define POLE_WN DutyPwm.WN
41#endif
42
44#define PATTERN_BEMF_U_MASK (0x04)
45#define PATTERN_BEMF_V_MASK (0x02)
46#define PATTERN_BEMF_W_MASK (0x01)
48volatile uint8_t AUTO_COMM_CNTER = 255;
49volatile uint8_t AUTO_COMM_CNTER_BREAK = 255;
53typedef enum {
94const uint8_t PortStateCW[8][2] = {{UVW_STATE_HI_DL_FL,UVW_STATE_DH_LO_FL}, /*0 MASK_PWM_DUTYH */
95 {UVW_STATE_DH_FL_LO,UVW_STATE_HI_FL_DL}, /*1 MASK_PWM_DUTYH */
96 {UVW_STATE_FL_HI_DL,UVW_STATE_FL_DH_LO}, /*2 MASK_PWM_LOW__ */
97 {UVW_STATE_LO_DH_FL,UVW_STATE_DL_HI_FL}, /*3 MASK_PWM_FLOAT */
98 {UVW_STATE_DL_FL_HI,UVW_STATE_LO_FL_DH}, /*4 MASK_PWM_LOW__ */
99 {UVW_STATE_FL_LO_DH,UVW_STATE_FL_DL_HI}, /*5 MASK_PWM_FLOAT */
100 {UVW_STATE_FL_FL_FL,UVW_STATE_FL_FL_FL}, /*6 MASK_PWM_FLOAT */
102
121const uint8_t PortStateCCW[8][2] = {{UVW_STATE_DH_LO_FL,UVW_STATE_HI_DL_FL}, //0 MASK_PWM_DUTYH */
122 {UVW_STATE_FL_DL_HI,UVW_STATE_FL_LO_DH}, //1 MASK_PWM_DUTYH */
123 {UVW_STATE_LO_FL_DH,UVW_STATE_DL_FL_HI}, //2 MASK_PWM_LOW__ */
124 {UVW_STATE_DL_HI_FL,UVW_STATE_LO_DH_FL}, //3 MASK_PWM_FLOAT */
125 {UVW_STATE_FL_DH_LO,UVW_STATE_FL_HI_DL}, //4 MASK_PWM_LOW__ */
126 {UVW_STATE_HI_FL_DL,UVW_STATE_DH_FL_LO}, //5 MASK_PWM_FLOAT */
127 {UVW_STATE_FL_FL_FL,UVW_STATE_FL_FL_FL}, //6 MASK_PWM_FLOAT */
129
130/* CW
131 * PATTERN DRIVE IDX
132 * 5 A+B- 1
133 * 4 A+C- 2
134 * 6 B+C- 3
135 * 2 B+A- 4
136 * 3 C+A- 5
137 * 1 C+B- 6
138 * */
139
141const uint8_t TrapSlessBemfSeqCW[][2] = {{5,4},{4,6},{6,2},{2,3},{3,1},{1,5}};
143const uint8_t TrapSlessBemfSeqCCW[][2] = {{4,5},{5,1},{1,3},{3,2},{2,6},{6,4}};
144
146const uint8_t TrapSlessCmtSeqCW[] = {0,1,2,3,4,5};
148const uint8_t TrapSlessNxtCmtSeqCW[] = {1,2,3,4,5,0};
149
152const uint8_t TrapSlessCmtSeqCCW[] = {0,5,4,3,2,1};
155const uint8_t TrapSlessNxtCmtSeqCCW[] = {5,4,3,2,1,0};
156
158uint16_t OffsetTimeout = 2500;
170int32_t OutMaxVbus;
171static uint16_t surgeCounter = 1000;
174//-------------------------------------- PRIVATE (Function Prototypes) ------------------------------------------------
175inline void outputPwmExec();
176void AvgOffset();
177//=====================================================================================================================
178//-------------------------------------- Public Functions -------------------------------------------------------------
179//=====================================================================================================================
180
184void TrapSless__Init(void) {
186
187 TrapSlessData.piKp = CAL_GET_SPEED_LOOP_KP(0);
188 TrapSlessData.piKi = CAL_GET_SPEED_LOOP_KI(0);
189 TrapSlessData.fmtPiPropSum = K2_SPEED_LOOP_FMT_PROPSUM;
190 TrapSlessData.fmtPiIn = K2_SPEED_LOOP_FMT_IN;
191 TrapSlessData.fmtPiOut = K2_SPEED_LOOP_FMT_OUT;
192 TrapSlessData.fmtPiKp = K2_SPEED_LOOP_FMT_KP;
193 TrapSlessData.fmtPiKi = K2_SPEED_LOOP_FMT_KI;
195
196 TrapSlessData.vRefMax = (TrapSlessData.bus_volt * K2_MAX_VREF_Q15) >> 15;
197 TrapSlessData.vRefMin = (TrapSlessData.bus_volt * K2_MIN_VREF_Q15) >> 15;
198
199 TrapDutyMaxCnts = K2_TRAP_PWM_AT_DUTY_MAX_CNTS;
200 PwmHalfMaxCnts = K2_TRAP_PWM_HALF_MAX_CNTS;
201 PwmMaxMinusDtCnts = K2_TRAP_PWM_MAX_MINUS_DT_CNTS;
202 ParamPwmFreqKHz = K2_PWM_FREQ_KHZ;
203 ParamDegZcToCmt = CAL_GET_DEG_ZC_TO_CMT(0);
204 ParamMinPreCarrierCnt = K2_MIN_PRE_CARRIER_CNT;
205 ParamMaxPreCarrierCnt = K2_MAX_PRE_CARRIER_CNT;
206 ParamSpdCalcNum = GET_PARAM_FXP(((int64_t)K2_SPEED_CALC_BASE),K2_SPD_RAD_BASE);
207 CurrClampValue = CAL_GET_CURR_CLAMP(0);
208 ParamOpenLoopSpdBemfCnt = K2_MAX_BEMF_CNT;
209 ParamMaxSpdBemfCnt = K2_MAX_SPD_BEMF_CNT;
210
211 TrapSlessData.openLoopTimeThld = (uint32_t)(GET_PARAM_FXP((ParamPwmFreqKHz*1000*K2_TWOPI/12), K2_SPD_RAD_BASE) / K2_SPDMIN_RPM_FXP);
212
213 Feedbacks__Init();
214}
215
223 uint8_t pattern_after_zc, pattern_before_zc, idx;
224
225 /* Clamping some variables */
228
229 /* Extract the bemf pattern for previous commutation*/
231 if(idx == 0) idx = 5;
232 else idx -= 1;
233
234 if(TrapSlessData.dir == ROT_CW) {
235 pattern_before_zc = TrapSlessBemfSeqCW[idx][1];
236 }
237 else if(TrapSlessData.dir == ROT_CCW) {
238 pattern_before_zc = TrapSlessBemfSeqCCW[idx][1];
239 }
240
241 /* Check too early */
242 else TrapSlessData.too_early = 0;
243 if(TrapSlessData.actual_bemf_pattern == pattern_before_zc) TrapSlessData.too_early = 1;
244
245 return(TrapSlessData.too_early);
246}
247
256uint8_t TrapSless__GetCmtToNum(uint8_t cmt, uint8_t dir) {
257 uint8_t response;
258
259 if (dir == ROT_CW) {
260 response = cmt;
261 }
262 else if(dir == ROT_CCW) {
263 switch (cmt) {
264 case 0:
265 response = 0;
266 break;
267 case 1:
268 response = 5;
269 break;
270 case 2:
271 response = 4;
272 break;
273 case 3:
274 response = 3;
275 break;
276 case 4:
277 response = 2;
278 break;
279 case 5:
280 response = 1;
281 break;
282 default:
283 break;
284 }
285 }
286 return response;
287}
288
296uint8_t TrapSless__GetBemfToNum(uint8_t bemf, uint8_t after_zc, uint8_t dir) {
297 uint8_t response;
298
299 if (dir == ROT_CW) {
300 if(after_zc) {
301 switch (bemf) {
302 case 4:
303 response = 0;
304 break;
305 case 6:
306 response = 1;
307 break;
308 case 2:
309 response = 2;
310 break;
311 case 3:
312 response = 3;
313 break;
314 case 1:
315 response = 4;
316 break;
317 case 5:
318 response = 5;
319 break;
320 default:
321 break;
322 }
323 }
324 else { //before zc
325 switch (bemf) {
326 case 4:
327 response = 1;
328 break;
329 case 6:
330 response = 2;
331 break;
332 case 2:
333 response = 3;
334 break;
335 case 3:
336 response = 4;
337 break;
338 case 1:
339 response = 5;
340 break;
341 case 5:
342 response = 0;
343 break;
344 default:
345 break;
346 }
347 }
348 }
349 else if (dir == ROT_CCW) {
350 if(after_zc) {
351 switch (bemf) {
352 case 5:
353 response = 0;
354 break;
355 case 1:
356 response = 5;
357 break;
358 case 3:
359 response = 4;
360 break;
361 case 2:
362 response = 3;
363 break;
364 case 6:
365 response = 2;
366 break;
367 case 4:
368 response = 1;
369 break;
370 default:
371 break;
372 }
373 }
374 else { //before zc
375 switch (bemf) {
376 case 5:
377 response = 5;
378 break;
379 case 1:
380 response = 4;
381 break;
382 case 3:
383 response = 3;
384 break;
385 case 2:
386 response = 2;
387 break;
388 case 6:
389 response = 1;
390 break;
391 case 4:
392 response = 0;
393 break;
394 default:
395 break;
396 }
397 }
398 }
399
400 return response;
401}
402
410uint8_t TrapSless__GetCmtFromBemf(uint8_t dir, uint8_t bemf, uint8_t after_zc) {
411 uint8_t response;
412 if (dir == ROT_CW) response = TrapSlessCmtSeqCW[TrapSless__GetBemfToNum(bemf,after_zc,dir)];
413 else if (dir == ROT_CCW) response = TrapSlessCmtSeqCCW[TrapSless__GetBemfToNum(bemf,after_zc,dir)];
414 return response;
415}
416
424uint8_t TrapSless__GetBemfFromCmt(uint8_t dir, uint8_t CmtNum, uint8_t after_zc) {
425 uint8_t response;
426
427 if (CmtNum > 5) CmtNum = 5;
428 if (dir == ROT_CW) response = TrapSlessBemfSeqCW[CmtNum][after_zc];
429 else if (dir == ROT_CCW) response = TrapSlessBemfSeqCCW[CmtNum][after_zc];
430
431 return response;
432}
433
438 static uint8_t nextCmtPattern;
439 uint8_t after_zc;
440 uint8_t nextCmtNum;
441 int8_t tmp_s8;
442 uint8_t BemfPattterAfterZc;
443 int32_t tmp32, factor_sub;
444 uint32_t TimeCntBemf;
445
446 /* Feedbacks */
447 Feedbacks__Handler();
448
449 /* Get bus current */
451
452 /* Get va vb vc*/
454
455 if (TrapSlessData.State <= STATE_TRAP_BOOT) {
456 //TrapSlessData.TimeCntBemfAvg = 0; /*!< Time count for bemf average events (for speed calc) is zero */
461 /* before zero cross is flagged */
462 after_zc = 0;
463
464 if (TrapSlessData.State == STATE_TRAP_IDLE) {
466 }
467
469 {
470 /* 30 deg is elapsed */
471 /* PWM duty set and pattern change */
472 if(TrapSlessData.dir == ROT_CW) {
474 } else if(TrapSlessData.dir == ROT_CCW) {
476 }
477 nextCmtNum = TrapSless__GetCmtToNum(nextCmtPattern, TrapSlessData.dir);
478 TrapSlessData.actualCmtNum = nextCmtNum;
479
480 /* Time counter for initial movement is zeroed */
482 }
483
484 /* Ensure there is no zero time for speed calculation as this variable is on the denominator of equation */
486 TrapSlessData.TimeCntBemf = K2_MAX_BEMF_CNT;
487 TrapSlessData.TimeCntBemfLast = K2_MAX_BEMF_CNT;
488 }
489
490 /* Update bemf time counter and calculate speed
491 * TrapSlessData.TimeCntBemf is updated with the TICK counter */
496
497 /* Blank is the time set as a minimum that should be elapsed before monitoring the zero crossing signal - bemf
498 * Value is set based on maximum speed operation (period), through ParamMinPreCarrierCnt, and calculated for 15 deg displacement */
500 /* Zero the counter when at closed loop */
502
504 }
505 else if (TrapSlessData.State == STATE_TRAP_CLOSEDLOOP) {
506 /* Closed-Loop Operation */
507
508 /* Run Down Counters for Blank, Cmt */
511
512 /* Ensure there is no zero time for speed calculation as this variable is on the denominator of equation */
514 TrapSlessData.TimeCntBemf = K2_MAX_BEMF_CNT;
515 TrapSlessData.TimeCntBemfLast = K2_MAX_BEMF_CNT;
516 }
517
519 /* Long time for TimeCnt is clamped to minimum speed */
522 }
523
524 /* Check if Blank Time is elapsed */
526 /* Look for Too early event
527 * If present, declare a ZC and setup a minimum time to CMT
528 * For every ZC event the CMT is programmed
529 * Get the actual bemf pattern to be verified */
531
532 /* Assume initially that we are before the zc event*/
533 after_zc = 0;
535 /* ZC event passed */
536 after_zc = 1;
537 }
538 /* Get the bemf pattern expected based on commutation and after zero cross*/
540
544 }
545
546 /* Check if ZC happens and not a too early */
547 if( (BemfPattterAfterZc == TrapSlessData.actual_bemf_pattern) &&
549 (!TrapSlessData.zcross) ) {
550 /* signal a zcross event */
552 /* No late event */
554
555 /* Calculate time btw tow bemf*/
557 TimeCntBemf = TrapSlessData.TimeCntBemf;
558
559 TrapSlessData.TimeCntBemfAvg = (uint32_t)((uint64_t)TrapSlessData.TimeCntBemfLast + (uint64_t)TimeCntBemf)/2;
560 /* Long time for TimeCnt is clamped to minimum speed - This avoid too slow commutations*/
563 }
564
565 TrapSlessData.TimeCntBemfLast = TimeCntBemf;
566 TrapSlessData.TimeCntBemf = 0; /* COUNTER START OVER */
568
569
570 /* Setup time to CMT
571 * 50% from Bemf Avg */
572 /* Time from Zcross to Commutation point - 50% of 60 degree is the ideal point - in general 5 to 10 degree*/
574 {
576 }
577 else {
579 }
580
581 }
582
583 /* too early: next Cmt is ahead by two CmtNum at least*/
585 tmp_s8 = (int8_t)nextCmtNum - (int8_t)TrapSlessData.actualCmtNum;
586 if(tmp_s8 < 0) tmp_s8 = 6 - (256 - tmp_s8);
587 if( (tmp_s8 > 0) &&
591 TrapSlessData.too_early = 1; /* keep too_early flagged till next cmt */
592
593 /* Calculate time btw bemf*/
595 TimeCntBemf = TrapSlessData.TimeCntBemf;
596
597 TrapSlessData.TimeCntBemfAvg = (uint32_t)((uint64_t)TrapSlessData.TimeCntBemfLast + (uint64_t)TimeCntBemf)/2;
598 /* Long time for TimeCnt is clamped to minimum speed */
601 }
602
603 TrapSlessData.TimeCntBemfLast = TimeCntBemf;
604 TrapSlessData.TimeCntBemf = 0; /* COUNTER START OVER */
606
607 /* Setup time to CMT
608 * 50% from Bemf Avg */
609 /* Time from Zcross to Commutation point - 50% of 60 degree is the ideal point*/
611 {
613 }
614 else
615 {
617 }
618 }
619
622
623 /* Calculate time btw bemf*/
625 TimeCntBemf = TrapSlessData.TimeCntBemf;
626 TrapSlessData.TimeCntBemfAvg = (uint32_t)((uint64_t)TrapSlessData.TimeCntBemfLast + (uint64_t)TimeCntBemf)/2;
627 TrapSlessData.TimeCntBemfLast = TimeCntBemf;
628 TrapSlessData.TimeCntBemf = 0; /* COUNTER START OVER */
630
631 if(ParamOpenLoopSpdBemfCntVar > ((ParamOpenLoopSpdBemfCnt*105)/100)) { /* 5% above the target */
633 if(factor_sub < 1) factor_sub = 1;
634 ParamOpenLoopSpdBemfCntVar -= factor_sub;
635 }
636 else {
638 }
640
643
646 }
647
648 /* Time to commutate */
655
658 }
659 else if(!TrapSlessData.TimeoutForCmt) {
662 }
663 /* PWM duty set and pattern change */
664
665 /*Curr clamp*/
667
671 }
672
673 } //if(!TrapSlessData.blankTimeDownCnter)
674 }
675
676 /* Too Early Detection */
677
678 /* Too Late first Detection - Reload CmtTimeout for a second chance */
679
680 /* If Too Early or Too Late (second time)
681 * Declare a zero cross event: capture Bemf Period, do Average
682 * Commutation is right away (Forced) for too early, respect the min Zc to Cmt time
683 * Commutation is programmed for too late */
684
685 /* If Bemf has changed to the expected pattern
686 * Program the commutation instant with a specific timer out
687 * Block additional detections */
688
689}
690
695 /* Call Rate of 1ms */
696 int32_t save_speedRad;
697 int32_t vRef = 0;
698 static uint32_t TimeoutBootStrap; /* Amount of time at lowers on to charge bootstrap capacitors */
699
700 /* Set the reference maximum voltage based on bus voltage */
701 TrapSlessData.vRefMax = (TrapSlessData.bus_volt * K2_MAX_VREF_Q15) >> 15;
702 /* Set the reference minimum voltage based on bus voltage */
703 TrapSlessData.vRefMin = (TrapSlessData.bus_volt * K2_MIN_VREF_Q15) >> 15;
704
705 switch (TrapSlessData.State) {
706 case STATE_TRAP_IDLE:
707 /* Reference voltage set to zero */
709 /* Actual commutation index */
711 /* Set pattern and pwm */
713//#ifdef SIMU
714 /* Set timeout for boot strap charge*/
715 TimeoutBootStrap = 500*0; /* 500ms */
716
717 /* Set timeout for current offset sampling */
718 OffsetTimeout = 1250; /* 1250ms */
719//#else
720// TimeoutBootStrap = 0;
721// OffsetTimeout = 0;
722//#endif
723 AvgOffset();
724
725 /* Check for Bus Voltage Stabilization and Surge Relay activation */
726 if(IQ_TO_V(TrapSless__GetVars()->bus_volt) > K2_VBUS_MIN_VOLT) {
727 /* Bus voltage is above the minimum for operation */
728 if(surgeCounter) surgeCounter--;
729 if(!surgeCounter) {
730 /* Vbus voltage debounced, close the relay */
731 LSI__Load_On(SURGE_RELAY);
732 }
733 }
734 else {
735 surgeCounter = 1000;
736 LSI__Load_Off(SURGE_RELAY);
737 }
738
739 break;
740 case STATE_TRAP_BOOT:
741 /* Offset calc */
742 if(OffsetTimeout) {
744 AvgOffset();
745 }
746 else {
747 /* Do Lowers On - for bootstrap charging */
751 if(TimeoutBootStrap) TimeoutBootStrap--;
752 else {
754
755 /* Leave a remainder of about 10% of the maximum output (>> 7 factor)
756 * This avoids getting to zero output voltage - which stall the motor */
759 }
760
763 TrapSlessData.State = STATE_TRAP_CLOSEDLOOP;
764 if(TrapSlessData.SpdTarget == 0) {
765 TrapSlessData.State = STATE_TRAP_IDLE;
766 }
767 }
768 }
769 break;
770 case STATE_TRAP_CLOSEDLOOP:
772
773 /* Set the maximum output voltage with 5% of margin*/
774 OutMaxVbus = MATHLIB_MPY(TrapSlessData.bus_volt, _IQ(0.95f)); /* 5% of margin*/
775
776 /* Do speed loop PI calculation */
778
779 /* Assign the reference voltage out of the pi controller */
780 vRef = TrapSlessData.piOut;
781
782 /* Voltage reference below the minimum, then assign a minimum voltage */
783 if(vRef < TrapSlessData.vRefMin) vRef = TrapSlessData.vRefMin;
784
785 /* Voltage reference above the maximum value, then clamp it to maximum */
787 if(vRef > TrapSlessData.vRefMax) vRef = TrapSlessData.vRefMax;
788 }
789
790 /* Leave a remainder of about 10% of the maximum output (>> 7 factor)
791 * This avoids getting to zero output voltage - which stall the motor
792 * Applied to the first 30 commutations from the start of closed loop
793 * */
796 vRef = (TrapSlessData.bus_volt >> 4);
797 }
798
801 vRef = 0;
804 }
805
806 TrapSlessData.vRef = vRef;
807
808 break;
809 default:
810 break;
811 }
812
813 /* Execute ramping of the speed reference */
814 if(TrapSlessData.State < STATE_TRAP_CLOSEDLOOP) {
815 TrapSlessData.SpdTargetActual = K2_SPDMIN_RPM_FXP;
816 } else {
818 }
819
820 if (TrapSlessData.State == STATE_TRAP_CLOSEDLOOP) {
821 /* At auto commutation */
825 }
829 }
830 if((TrapSlessData.SpdTargetActual > 0) && (TrapSlessData.spdRef < K2_SPDMIN_RPM_FXP)) TrapSlessData.spdRef = K2_SPDMIN_RPM_FXP;
831 else if((TrapSlessData.SpdTargetActual < 0) && (TrapSlessData.spdRef > -K2_SPDMIN_RPM_FXP)) TrapSlessData.spdRef = -K2_SPDMIN_RPM_FXP;
832 }
833}
834
842uint8_t TrapSless__GetBemfSignal(int32_t va, int32_t vb, int32_t vc) {
843 uint8_t response;
844 int32_t vn;
845
846 response = 0;
847
848 vn = (va+vb+vc)/3;
849
850 if(va > vn) response = response | PATTERN_BEMF_U_MASK;
851 else response = response & ~PATTERN_BEMF_U_MASK;
852 if(vb > vn) response = response | PATTERN_BEMF_V_MASK;
853 else response = response & ~PATTERN_BEMF_V_MASK;
854 if(vc > vn) response = response | PATTERN_BEMF_W_MASK;
855 else response = response & ~PATTERN_BEMF_W_MASK;
856
857 return response;
858}
859
865 int64_t tmp;
867 if(!tmp) {
869 }
870 else {
871 TrapSlessData.speedRad = (int32_t)( ParamSpdCalcNum/ (tmp*6));
872 }
873
874 /* Make sure feedback follows the reference during forced commutation */
875 if(TrapSlessData.State < STATE_TRAP_CLOSEDLOOP)
877
878 return TrapSlessData.speedRad;
879}
880
887 TrapSlessData.State = STATE_TRAP_IDLE;
891 TrapSlessData.actualCmtNum = 6; //float
892}
893
899void TrapSless__SetSpd(int32_t spd_rpm, int32_t ramp_rpm) {
900 if((TrapSlessData.SpdTarget == 0) || (spd_rpm == 0))
902
903 TrapSlessData.SpdTarget = (int32_t)GET_PARAM_FXP((int64_t)spd_rpm,K2_SPD_RPM_BASE);
904 TrapSlessData.RampIncSpdAt1ms = (int32_t)GET_PARAM_FXP((int64_t)(ramp_rpm),K2_SPD_RPM_BASE)/1000;
905
906 /* Inc shall be different than zero */
907 if(TrapSlessData.RampIncSpdAt1ms == 0) TrapSlessData.RampIncSpdAt1ms = K2_MINRATE_EVERY_MS_RPM_FXP;
909
911
912 if(TrapSlessData.State == STATE_TRAP_IDLE) {
913 if(!surgeCounter) {
914 /* Surge relay is stable */
915 TrapSlessData.State = STATE_TRAP_BOOT;
916 }
917 }
918}
919
924void TrapSless__SetPatternAndPwm(uint8_t after_zc) {
925 if(TrapSlessData.dir == ROT_CW) {
927 }
928 else if(TrapSlessData.dir == ROT_CCW) {
930 }
931
933}
934
939 uint32_t pwm_duty;
940 int32_t tmp32;
941
942
943 tmp32 = TrapSlessData.bus_volt;
944 if(tmp32 == 0) tmp32 = 1;
945
946 pwm_duty = (TrapSlessData.vRef * TrapDutyMaxCnts) / tmp32 + K2_TRAP_PWM_DT_CNTS;
947
948 if(pwm_duty > TrapDutyMaxCnts) pwm_duty = TrapDutyMaxCnts;
949
950 TrapSlessData.dutyOut = pwm_duty;
951
952#ifndef SIMU
957
962
967
968#else
970 SRMCPWM_LOADPWM[0](TrapSlessData.dutyOut,K2_TRAP_PWM_HALF_MAX_CNTS, K2_TRAP_PWM_HALF_MAX_CNTS);
972 SRMCPWM_LOADPWM[0]((K2_TRAP_PWM_MAX_MINUS_DT_CNTS-TrapSlessData.dutyOut),K2_TRAP_PWM_HALF_MAX_CNTS, K2_TRAP_PWM_HALF_MAX_CNTS);
973
975 SRMCPWM_LOADPWM[0](K2_TRAP_PWM_HALF_MAX_CNTS, TrapSlessData.dutyOut, K2_TRAP_PWM_HALF_MAX_CNTS);
977 SRMCPWM_LOADPWM[0](K2_TRAP_PWM_HALF_MAX_CNTS, (K2_TRAP_PWM_MAX_MINUS_DT_CNTS-TrapSlessData.dutyOut), K2_TRAP_PWM_HALF_MAX_CNTS);
978
980 SRMCPWM_LOADPWM[0](K2_TRAP_PWM_HALF_MAX_CNTS, K2_TRAP_PWM_HALF_MAX_CNTS, TrapSlessData.dutyOut);
982 SRMCPWM_LOADPWM[0](K2_TRAP_PWM_HALF_MAX_CNTS, K2_TRAP_PWM_HALF_MAX_CNTS, (K2_TRAP_PWM_MAX_MINUS_DT_CNTS-TrapSlessData.dutyOut));
983
984#endif
985}
986
993void TrapSless__PiCalc(int32_t curr_clamp) {
994 int64_t err;
995 int64_t prop;
996 int64_t propsum;
997 int64_t out;
998 int64_t limit;
999 int32_t tmp;
1000
1001 if(curr_clamp) {
1002 if(TrapSlessData.bus_curr > curr_clamp) {
1003 /* Need to clamp output */
1005 if(tmp <= 0) tmp = 1;
1006 TrapSlessData.piOutMax -= tmp;
1009 }
1010 }
1011 else {
1014
1016 }
1017
1018 }
1019 else
1020 {
1021 /* Input: err = spdRef - speedRad*/
1023
1024 /* Proportional Calculation */
1026 /* Integral Calculation */
1028 limit = (1ULL << (TrapSlessData.fmtPiPropSum)) - 1;
1029 propsum += prop;
1030
1031 if(propsum > limit) propsum = limit;
1032
1033 out = propsum >> (TrapSlessData.fmtPiPropSum - TrapSlessData.fmtPiOut);
1034
1035 /* integral part limitation */
1036 /* memory will be adjusted */
1037 if(out > TrapSlessData.piOutMax) {
1038 out = TrapSlessData.piOutMax;
1039 propsum = ((int64_t)TrapSlessData.piOutMax << (TrapSlessData.fmtPiPropSum - TrapSlessData.fmtPiOut)) - prop;
1040 }
1041
1042 if(propsum < 0) propsum = 0;
1043 TrapSlessData.piPropSum = propsum;
1044 TrapSlessData.piOut = out;
1045 TrapSlessData.piErr = err;
1046 }
1047}
1048
1053 int32_t curr_a, curr_b, curr_c, curr_bus;
1054
1055#ifdef SIMU
1056 #define K_BUS_CURRENT_SCALING (20.0f/4095.0f) /* for bus current scaling */
1057 /* Get ibus */
1058 TrapSlessData.bus_curr = (int32_t)(Iu_Ad_Forced_Value[0] * (K_BUS_CURRENT_SCALING * 32768 / K2_CURR_BASE));
1059 /* Get vbus */
1060 TrapSlessData.bus_volt = (int32_t)(Vdc_Ad_Forced_Value[0] * (int32_t)(32768.0f / K2_VOLT_BASE)); /* Vdc */
1061#else
1062 /* Get the bus voltage */
1063 TrapSlessData.bus_volt = ((int32_t)(ADC_Samples[ADC_VBUS]-FACTOROFF_AD_COUNT_TO_DC_BUS_0) * (int32_t)(_IQX(FACTOR_AD_COUNT_TO_DC_BUS_0,25)/K2_VOLT_BASE))>>(FACTOR_AD_COUNT_TO_DC_BUS_FMT_0-15);
1064
1065 /* Get the three phase currents and remove the offset */
1066 curr_a = (((int32_t)ADC_Samples[ADC_IA]-TrapSlessData.curr_off_a) * (int32_t)FACTOR_ADC_COUNT_TO_AMP_SLOPE_0)>>(FACTOR_ADC_COUNT_TO_AMP_SLOPE_FMT_0-15);
1067 curr_b = (((int32_t)ADC_Samples[ADC_IB]-TrapSlessData.curr_off_b) * (int32_t)FACTOR_ADC_COUNT_TO_AMP_SLOPE_0)>>(FACTOR_ADC_COUNT_TO_AMP_SLOPE_FMT_0-15);
1068 curr_c = (((int32_t)ADC_Samples[ADC_IC]-TrapSlessData.curr_off_c) * (int32_t)FACTOR_ADC_COUNT_TO_AMP_SLOPE_0)>>(FACTOR_ADC_COUNT_TO_AMP_SLOPE_FMT_0-15);
1069 if (curr_a < 0) curr_a = -curr_a;
1070 if (curr_b < 0) curr_b = -curr_b;
1071 if (curr_c < 0) curr_c = -curr_c;
1072 curr_bus = curr_b;
1073 if(curr_a > curr_bus) {
1074 curr_bus = curr_a;
1075 }
1076 if(curr_c > curr_bus) {
1077 curr_bus = curr_c;
1078 }
1079
1080 TrapSlessData.bus_curr = curr_bus;
1081#endif
1082}
1083
1088#ifdef SIMU
1089 TrapSlessData.va = (int32_t)( ((float32_t)Vu_Ad_Forced_Value[0] * ( (int32_t)(FACTOR_AD_COUNT_TO_DC_BUS_0 * 32768.0f))) / K2_VOLT_BASE);
1090 TrapSlessData.vb = (int32_t)( ((float32_t)Vv_Ad_Forced_Value[0] * ( (int32_t)(FACTOR_AD_COUNT_TO_DC_BUS_0 * 32768.0f))) / K2_VOLT_BASE);
1091 TrapSlessData.vc = (int32_t)( ((float32_t)Vw_Ad_Forced_Value[0] * ( (int32_t)(FACTOR_AD_COUNT_TO_DC_BUS_0 * 32768.0f))) / K2_VOLT_BASE);
1092#else
1093 /* Leave Q15 to the output voltage */
1094 TrapSlessData.va = (ADC_Samples[ADC_VA] * (int32_t)V_TO_IQY((FACTOR_AD_COUNT_TO_DC_BUS_0), 25))>>10;
1095 TrapSlessData.vb = (ADC_Samples[ADC_VB] * (int32_t)V_TO_IQY((FACTOR_AD_COUNT_TO_DC_BUS_0), 25))>>10;
1096 TrapSlessData.vc = (ADC_Samples[ADC_VC] * (int32_t)V_TO_IQY((FACTOR_AD_COUNT_TO_DC_BUS_0), 25))>>10;
1097
1098#endif
1099}
1100
1105 static uint32_t CntLast;
1106 uint32_t Cnter;
1107 Cnter = TICK->CNT;
1108 TrapSlessData.TimeCntBemf += Cnter - CntLast;
1109 CntLast = Cnter;
1110}
1111
1117 return &TrapSlessData;
1118}
1119
1124 uint8_t nextCmtPattern;
1125 uint8_t nextCmtNum;
1126 uint8_t after_zc;
1127
1129 if(TrapSlessData.dir == ROT_CW) {
1131 } else if(TrapSlessData.dir == ROT_CCW) {
1133 }
1134 nextCmtNum = TrapSless__GetCmtToNum(nextCmtPattern, TrapSlessData.dir);
1135
1136
1138 {
1139 TrapSlessData.TimeoutForCmt = ParamOpenLoopSpdBemfCntVar * ParamPwmFreqKHz * 60 / BEMF_CNTER_FREQ_KHZ / 60;
1140 }
1141 else {
1143 }
1144
1147 /* Calculate time btw bemf*/
1148
1149 /* Calculate time btw bemf*/
1152 /* Long time for TimeCnt is clamped to minimum speed */
1155 }
1157 TrapSlessData.TimeCntBemf = 0; /* COUNTER START OVER */
1159
1160 /* Setup time to CMT
1161 * 50% from Bemf Avg */
1162 TrapSlessData.TimeoutForCmt = TrapSlessData.TimeCntBemfAvg * ParamPwmFreqKHz * ParamDegZcToCmt / BEMF_CNTER_FREQ_KHZ / 60; /* Setup time for too late event*/
1164 }
1165
1167 TrapSlessData.actualCmtPattern = nextCmtPattern;
1168 TrapSlessData.actualCmtNum = nextCmtNum;
1169
1170 TrapSlessData.blankTimeDownCnter = TrapSlessData.TimeCntBemfAvg * ParamPwmFreqKHz / BEMF_CNTER_FREQ_KHZ / 4; //15deg
1172
1174
1175 /* Do not let Cmt timeout being zero, otherwise fast cmt will happen */
1178 }
1179 /* Do not let Cmt timeout being too big, otherwise too slow cmt will happen */
1181 {
1184 }
1185 }
1186
1187 /* Execute the commutation */
1188 after_zc = 0;
1190 after_zc = 1;
1191 }
1192
1196 }
1198}
1199
1200//=====================================================================================================================
1201//-------------------------------------- Private Functions ------------------------------------------------------------
1202//=====================================================================================================================
1203
1204
1210#ifndef SIMU
1211 switch (state) {
1212 case UVW_STATE_HI_DL_FL:
1213 XBAR->PR_CFG = PR_CFG_UVW_STATE_HI_DL_FL;
1214 GPIO_UVW_STATE_HI_DL_FL();
1215 break;
1216 case UVW_STATE_DH_LO_FL:
1217 XBAR->PR_CFG = PR_CFG_UVW_STATE_DH_LO_FL;
1218 GPIO_UVW_STATE_DH_LO_FL();
1219 break;
1220 case UVW_STATE_DH_FL_LO:
1221 XBAR->PR_CFG = PR_CFG_UVW_STATE_DH_FL_LO;
1222 GPIO_UVW_STATE_DH_FL_LO();
1223 break;
1224 case UVW_STATE_HI_FL_DL:
1225 XBAR->PR_CFG = PR_CFG_UVW_STATE_HI_FL_DL;
1226 GPIO_UVW_STATE_HI_FL_DL();
1227 break;
1228 case UVW_STATE_FL_HI_DL:
1229 XBAR->PR_CFG = PR_CFG_UVW_STATE_FL_HI_DL;
1230 GPIO_UVW_STATE_FL_HI_DL();
1231 break;
1232 case UVW_STATE_FL_DH_LO:
1233 XBAR->PR_CFG = PR_CFG_UVW_STATE_FL_DH_LO;
1234 GPIO_UVW_STATE_FL_DH_LO();
1235 break;
1236 case UVW_STATE_LO_DH_FL:
1237 XBAR->PR_CFG = PR_CFG_UVW_STATE_LO_DH_FL;
1238 GPIO_UVW_STATE_LO_DH_FL();
1239 break;
1240 case UVW_STATE_DL_HI_FL:
1241 XBAR->PR_CFG = PR_CFG_UVW_STATE_DL_HI_FL;
1242 GPIO_UVW_STATE_DL_HI_FL();
1243 break;
1244 case UVW_STATE_DL_FL_HI:
1245 XBAR->PR_CFG = PR_CFG_UVW_STATE_DL_FL_HI;
1246 GPIO_UVW_STATE_DL_FL_HI();
1247 break;
1248 case UVW_STATE_LO_FL_DH:
1249 XBAR->PR_CFG = PR_CFG_UVW_STATE_LO_FL_DH;
1250 GPIO_UVW_STATE_LO_FL_DH();
1251 break;
1252 case UVW_STATE_FL_LO_DH:
1253 XBAR->PR_CFG = PR_CFG_UVW_STATE_FL_LO_DH;
1254 GPIO_UVW_STATE_FL_LO_DH();
1255 break;
1256 case UVW_STATE_FL_DL_HI:
1257 XBAR->PR_CFG = PR_CFG_UVW_STATE_FL_DL_HI;
1258 GPIO_UVW_STATE_FL_DL_HI();
1259 break;
1260 case UVW_STATE_FL_FL_FL:
1261 XBAR->PR_CFG = PR_CFG_UVW_STATE_FL_FL_FL;
1262 GPIO_UVW_STATE_FL_FL_FL();
1263 break;
1264 case UVW_STATE_LO_LO_LO:
1265 XBAR->PR_CFG = PR_CFG_UVW_STATE_LO_LO_LO;
1266 GPIO_UVW_STATE_LO_LO_LO();
1267 break;
1268 default:
1269 break;
1270 }
1271#endif
1272}
1273
1277inline void outputPwmExec(void) {
1279
1281
1282 /*Execute PWM update if required*/
1283 if(TrapSlessData.State > STATE_TRAP_IDLE) {
1285 }
1286}
1287
1299 int32_t tmp1, tmp2;
1300
1301 /* Averaging Current Offset ==> A */
1303
1304 tmp2 = (int32_t)ADC_Samples[ADC_IA];
1305
1306 tmp2 -= (tmp1 >> 16); /* Subtract zero offset */
1307 tmp1 += tmp2 * 200; /* this make 400ms time constant for 16kHz */
1308 /* sampling frequency, i.e. reach final value in 6 x 400ms */
1310 tmp1 = tmp1 >> 16;
1312
1313 /* Averaging Current Offset ==> B */
1315
1316 tmp2 = (int32_t)ADC_Samples[ADC_IB];
1317
1318 tmp2 -= (tmp1 >> 16); /* Subtract zero offset */
1319 tmp1 += tmp2 * 200; /* this make 400ms time constant for 16kHz */
1320 /* sampling frequency, i.e. reach final value in 6 x 400ms */
1321
1323 tmp1 = tmp1 >> 16;
1325
1326 /* Averaging Current Offset ==> C */
1328
1329 tmp2 = (int32_t)ADC_Samples[ADC_IC];
1330
1331 tmp2 -= (tmp1 >> 16); /* Subtract zero offset */
1332 tmp1 += tmp2 * 200; /* this make 400ms time constant for 16kHz */
1333 /* sampling frequency, i.e. reach final value in 6 x 400ms */
1334
1336 tmp1 = tmp1 >> 16;
1338}
#define ADC_IC
#define ADC_IA
#define ADC_VC
uint16_t ADC_Samples[NUM_ADC_SAMPLES]
Definition: main.c:61
#define ADC_VB
#define ADC_IB
#define ADC_VA
#define ADC_VBUS
void LSI__Load_On(LOAD_NAME_TYPE load)
It turns on the select load.
Definition: LSI.c:101
void LSI__Load_Off(LOAD_NAME_TYPE load)
It turns off the select load.
Definition: LSI.c:110
void TrapSless__SlowHandler()
Handler for Trapezoidal control - 1KHz call rate.
Definition: TrapSless.c:694
mc_t ParamMaxPreCarrierCnt
Definition: TrapSless.c:165
const uint8_t PortStateCCW[8][2]
Definition: TrapSless.c:121
const uint8_t TrapSlessBemfSeqCW[][2]
Definition: TrapSless.c:141
#define PATTERN_BEMF_U_MASK
Definition: TrapSless.c:44
int32_t OutMaxVbus
Definition: TrapSless.c:170
void TrapSless__SetSpd(int32_t spd_rpm, int32_t ramp_rpm)
BLDC motor rotation command.
Definition: TrapSless.c:899
uint8_t TrapSless__GetBemfFromCmt(uint8_t dir, uint8_t CmtNum, uint8_t after_zc)
Returns the bemf pattern given a commutation index.
Definition: TrapSless.c:424
mc_t ParamMinPreCarrierCnt
Definition: TrapSless.c:164
void TrapSless__ResetVars()
Resetting of trapezoidal key variables.
Definition: TrapSless.c:885
mc_t ParamDegZcToCmt
Definition: TrapSless.c:163
int32_t TrapSless__CalcSpeed()
Speed computation based on Bemf Zcross events.
Definition: TrapSless.c:864
volatile uint8_t AUTO_COMM_CNTER
Definition: TrapSless.c:48
const uint8_t TrapSlessBemfSeqCCW[][2]
Definition: TrapSless.c:143
uint8_t TrapSless__GetBemfToNum(uint8_t bemf, uint8_t after_zc, uint8_t dir)
Returns the index of the given bemf pattern.
Definition: TrapSless.c:296
TRAPSLESS_PATTERN_TYPE * TrapSless__GetVars(void)
Obtaine the trapezoidal variables.
Definition: TrapSless.c:1116
void TrapSless__Init(void)
Initialization of TrapSless module.
Definition: TrapSless.c:184
uint8_t TrapSless__GetBemfSignal(int32_t va, int32_t vb, int32_t vc)
Returns the bemf pattern based on pole voltage reading [a,b,c].
Definition: TrapSless.c:842
mc_t ParamPwmFreqKHz
Definition: TrapSless.c:162
uint32_t ParamOpenLoopSpdBemfCntVar
Definition: TrapSless.c:172
uint32_t PwmHalfMaxCnts
Definition: TrapSless.c:160
void TrapSless__BemfCnterUpdate(void)
Accumulate the counter for bemf.
Definition: TrapSless.c:1104
const uint8_t TrapSlessNxtCmtSeqCCW[]
Definition: TrapSless.c:155
void TrapSless__SetPatternAndPwm(uint8_t after_zc)
Set pwm duty cycle and output pattern.
Definition: TrapSless.c:924
int64_t ParamSpdCalcNum
Definition: TrapSless.c:166
uint32_t PwmMaxMinusDtCnts
Definition: TrapSless.c:161
uint8_t TrapSless__GetCmtToNum(uint8_t cmt, uint8_t dir)
Returns the index of the given commutation pattern For CW rotation the index matches the commutation ...
Definition: TrapSless.c:256
uint8_t TrapSless__GetCmtFromBemf(uint8_t dir, uint8_t bemf, uint8_t after_zc)
Returns the commutation pattern given a bemf pattern.
Definition: TrapSless.c:410
uint32_t ParamMaxSpdBemfCnt
Definition: TrapSless.c:169
const uint8_t TrapSlessNxtCmtSeqCW[]
Definition: TrapSless.c:148
volatile uint8_t AUTO_COMM_CNTER_BREAK
Definition: TrapSless.c:49
int32_t CurrClampValue
Definition: TrapSless.c:167
void TrapSless__SetVbusIbus(void)
Read ADC and Set Vbus and Ibus variables.
Definition: TrapSless.c:1052
void outputPwmExec()
Update the pwm outputs.
Definition: TrapSless.c:1277
TRAPSLESS_PATTERN_TYPE TrapSlessData
Definition: TrapSless.c:21
UVW_STATE_TYPE
Definition: TrapSless.c:53
@ UVW_STATE_FL_FL_FL
Definition: TrapSless.c:72
@ UVW_STATE_LO_DH_FL
Definition: TrapSless.c:63
@ UVW_STATE_DL_FL_HI
Definition: TrapSless.c:66
@ UVW_STATE_FL_DH_LO
Definition: TrapSless.c:61
@ UVW_STATE_FL_HI_DL
Definition: TrapSless.c:60
@ UVW_STATE_HI_FL_DL
Definition: TrapSless.c:58
@ UVW_STATE_FL_LO_DH
Definition: TrapSless.c:69
@ UVW_STATE_DL_HI_FL
Definition: TrapSless.c:64
@ UVW_STATE_FL_DL_HI
Definition: TrapSless.c:70
@ UVW_STATE_DH_LO_FL
Definition: TrapSless.c:55
@ UVW_STATE_HI_DL_FL
Definition: TrapSless.c:54
@ UVW_STATE_DH_FL_LO
Definition: TrapSless.c:57
@ UVW_STATE_LO_FL_DH
Definition: TrapSless.c:67
@ UVW_STATE_LO_LO_LO
Definition: TrapSless.c:73
mc_t TrapDutyMaxCnts
Definition: TrapSless.c:159
void decodeOutput(UVW_STATE_TYPE state)
Set the outputs according to the pole voltage state.
Definition: TrapSless.c:1209
#define PATTERN_BEMF_W_MASK
Definition: TrapSless.c:46
const uint8_t TrapSlessCmtSeqCCW[]
Definition: TrapSless.c:152
void TrapSless__Handler()
Handler for Trapezoidal control - 16KHz call rate.
Definition: TrapSless.c:437
uint16_t OffsetTimeout
Definition: TrapSless.c:158
void TrapSless__ExecuteCmt(void)
Execution of commutation.
Definition: TrapSless.c:1123
const uint8_t TrapSlessCmtSeqCW[]
Definition: TrapSless.c:146
void AvgOffset()
Definition: TrapSless.c:1298
#define PATTERN_BEMF_V_MASK
Definition: TrapSless.c:45
uint32_t ParamOpenLoopSpdBemfCnt
Definition: TrapSless.c:168
void TrapSless__SetPwm(void)
Set pwm duty cycle.
Definition: TrapSless.c:938
const uint8_t PortStateCW[8][2]
Definition: TrapSless.c:94
uint8_t TrapSless__GetTooEarly(void)
Too early detection mechanism For every commutation there is an expected zcross at a given phase....
Definition: TrapSless.c:222
void TrapSless__PiCalc(int32_t curr_clamp)
Pi regulator If curr_clamp is different than zero, then only runs the clamp algorithm If curr_clamp i...
Definition: TrapSless.c:993
void TrapSless__SetVabc(void)
Read ADC and set pole voltage variables.
Definition: TrapSless.c:1087
uint32_t openLoopPatternTimeCnt
Definition: TrapSless.h:197
uint8_t actual_bemf_pattern
Definition: TrapSless.h:173
uint32_t blankTimeDownCnter
Definition: TrapSless.h:202
uint32_t TimeCntBemfLast
Definition: TrapSless.h:186
uint32_t TimeoutToolateForCmt
Definition: TrapSless.h:182
uint32_t auto_comm_cnter
Definition: TrapSless.h:229
uint32_t openLoopTimeThld
Definition: TrapSless.h:194
uint32_t TimeoutForCmtSaved
Definition: TrapSless.h:181