instaspin_foc
proj_lab11b.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2015, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
36 
38 
39 
44 
45 // **************************************************************************
46 // the includes
47 
48 // system includes
49 #include <math.h>
50 #include "main.h"
51 
53 
54 #ifdef FLASH
55 #pragma CODE_SECTION(mainISR,"ramfuncs");
56 #endif
57 
58 // Include header files used in the main function
59 
60 // **************************************************************************
61 // the defines
62 
63 // **************************************************************************
64 // the globals
65 
68 
71 
74 
76 
79 
82 uint16_t pidCntSpeed;
83 
86 
89 
92 
95 
98 
101 
102 #ifdef F2802xF
103 #pragma DATA_SECTION(halHandle,"rom_accessed_data");
104 #endif
106 
107 HAL_PwmData_t gPwmData = {_IQ(0.0), _IQ(0.0), _IQ(0.0)};
108 
110 
111 MATH_vec3 gOffsets_I_pu = {_IQ(0.0), _IQ(0.0), _IQ(0.0)};
112 
113 MATH_vec3 gOffsets_V_pu = {_IQ(0.0), _IQ(0.0), _IQ(0.0)};
114 
115 MATH_vec2 gIdq_ref_pu = {_IQ(0.0), _IQ(0.0)};
116 
117 MATH_vec2 gVdq_out_pu = {_IQ(0.0), _IQ(0.0)};
118 
119 MATH_vec2 gIdq_pu = {_IQ(0.0), _IQ(0.0)};
120 
121 #ifdef F2802xF
122 #pragma DATA_SECTION(gUserParams,"rom_accessed_data");
123 #endif
125 
127 
128 #ifdef FLASH
129 // Used for running BackGround in flash, and ISR in RAM
130 extern uint16_t *RamfuncsLoadStart, *RamfuncsLoadEnd, *RamfuncsRunStart;
131 
132 #ifdef F2802xF
133 extern uint16_t *econst_start, *econst_end, *econst_ram_load;
134 extern uint16_t *switch_start, *switch_end, *switch_ram_load;
135 #endif
136 
137 #endif
138 
139 // set the offset, default value of 1 microsecond
140 int16_t gCmpOffset = (int16_t)(1.0 * USER_SYSTEM_FREQ_MHz);
141 
142 MATH_vec3 gIavg = {_IQ(0.0), _IQ(0.0), _IQ(0.0)};
143 uint16_t gIavg_shift = 1;
144 
145 #ifdef DRV8301_SPI
146 // Watch window interface to the 8301 SPI
147 DRV_SPI_8301_Vars_t gDrvSpi8301Vars;
148 #endif
149 
150 #ifdef DRV8305_SPI
151 // Watch window interface to the 8305 SPI
152 DRV_SPI_8305_Vars_t gDrvSpi8305Vars;
153 #endif
154 
156 
158 
160 
162 
164 
166 
168 
172 
173 uint32_t gOffsetCalcCount = 0;
174 
175 volatile bool gFlag_enableRsOnLine = false;
176 
177 volatile bool gFlag_updateRs = false;
178 
179 volatile _iq gRsOnLineFreq_Hz = _IQ(0.2);
180 
181 volatile _iq gRsOnLineId_mag_A = _IQ(0.5);
182 
183 volatile _iq gRsOnLinePole_Hz = _IQ(0.2);
184 
186 
187 _iq vib_comp_reserved[400]; //<! this buffer is required to reserve memory used by the vibration compensation module
188 
189 #ifdef F2802xF
190 #pragma DATA_SECTION(gSpeed_fbk_out,"ebss_extension");
191 #endif
192 _iq gSpeed_fbk_out[360]; //<! this buffer is used to store the output of the speed controller vs mechanical angle
193 
194 uint16_t gSpeed_array_index = 0; //<! index used for gSpeed_fbk_out[] buffer
195 
196 _iq gSpeed_max_pu = _IQ(0.0); //<! variable used to store the maximum speed for speed variation calculation
197 
198 _iq gSpeed_min_pu = _IQ(1.0); //<! variable used to store the minimum speed for speed variation calculation
199 
200 _iq gSpeed_delta_krpm = _IQ(0.0); //<! variable used to store the speed variation calculation
201 
202 volatile bool gFlag_speedStatsReset = false; //<! this flag is used to reset the speed variation calculation
203 
204 _iq gAbsAngle_elec_pu = _IQ(0.0); //<! variable used for electrical angle from _IQ(0.0) to _IQ(1.0)
205 
206 _iq gAbsAngle_mech_pu = _IQ(0.0); //<! variable used for mechanical angle from _IQ(0.0) to _IQ(1.0)
207 
208 _iq gAngle_mech_poles = _IQ(0.0); //<! variable used for mechanical angle from _IQ(0.0) to _IQ(USER_MOTOR_NUM_POLE_PAIRS)
209 
210 _iq gAngle_z1_pu = _IQ(0.0); //<! variable used for electrical angle in previous sample
211 
212 volatile _iq gAlpha = _IQ(0.99); //<! learning rate of the vibration compensation module from _IQ(0.0) to _IQ(1.0)
213 
214 volatile int16_t gAdvIndexDelta = 10; //<! phase advance of the vibration compensation module from 0 to 360
215 
216 volatile bool gFlag_enableOutput = false; //<! flag that enables the output from the vibration compensation module to the Iq reference
217 
218 volatile bool gFlag_enableUpdates = true; //<! flag that enables learning of the load, and storing the load profile internally in the vibration compensation module
219 
220 volatile bool gFlag_resetVibComp = false; //<! flag that resets the learned load curve
221 
222 // **************************************************************************
223 // the functions
224 void main(void)
225 {
226  // Only used if running from FLASH
227  // Note that the variable FLASH is defined by the project
228  #ifdef FLASH
229  // Copy time critical code and Flash setup code to RAM
230  // The RamfuncsLoadStart, RamfuncsLoadEnd, and RamfuncsRunStart
231  // symbols are created by the linker. Refer to the linker files.
232  memCopy((uint16_t *)&RamfuncsLoadStart,(uint16_t *)&RamfuncsLoadEnd,(uint16_t *)&RamfuncsRunStart);
233 
234  #ifdef F2802xF
235  //copy .econst to unsecure RAM
236  if(*econst_end - *econst_start)
237  {
238  memCopy((uint16_t *)&econst_start,(uint16_t *)&econst_end,(uint16_t *)&econst_ram_load);
239  }
240 
241  //copy .switch ot unsecure RAM
242  if(*switch_end - *switch_start)
243  {
244  memCopy((uint16_t *)&switch_start,(uint16_t *)&switch_end,(uint16_t *)&switch_ram_load);
245  }
246  #endif
247 
248  #endif
249 
250  // initialize the hardware abstraction layer
251  halHandle = HAL_init(&hal,sizeof(hal));
252 
253  // check for errors in user parameters
254  USER_checkForErrors(&gUserParams);
255 
256 
257  // store user parameter error in global variable
258  gMotorVars.UserErrorCode = USER_getErrorCode(&gUserParams);
259 
260 
261  // do not allow code execution if there is a user parameter error
262  if(gMotorVars.UserErrorCode != USER_ErrorCode_NoError)
263  {
264  for(;;)
265  {
266  gMotorVars.Flag_enableSys = false;
267  }
268  }
269 
270  // initialize the Clarke modules
271  clarkeHandle_I = CLARKE_init(&clarke_I,sizeof(clarke_I));
272  clarkeHandle_V = CLARKE_init(&clarke_V,sizeof(clarke_V));
273 
274  // initialize the estimator
275  estHandle = EST_init((void *)USER_EST_HANDLE_ADDRESS, 0x200);
276 
277  // initialize the user parameters
278  USER_setParams(&gUserParams);
279 
280  // set the hardware abstraction layer parameters
281  HAL_setParams(halHandle,&gUserParams);
282 
283 #ifdef FAST_ROM_V1p6
284  {
285  CTRL_Handle ctrlHandle = CTRL_init((void *)USER_CTRL_HANDLE_ADDRESS, 0x200);
286  CTRL_Obj *obj = (CTRL_Obj *)ctrlHandle;
287  obj->estHandle = estHandle;
288 
289  // initialize the estimator through the controller
290  CTRL_setParams(ctrlHandle,&gUserParams);
291  CTRL_setUserMotorParams(ctrlHandle);
292  CTRL_setupEstIdleState(ctrlHandle);
293  }
294 #else
295  {
296  // initialize the estimator
297  EST_setEstParams(estHandle,&gUserParams);
299  }
300 #endif
301 
302  // disable Rs recalculation by default
303  gMotorVars.Flag_enableRsRecalc = false;
305 
306  // configure RsOnLine
307  EST_setFlag_enableRsOnLine(estHandle,gFlag_enableRsOnLine);
308  EST_setFlag_updateRs(estHandle,gFlag_updateRs);
311 
312  // Calculate coefficients for all filters
313  {
314  _iq b0 = _IQmpy(gRsOnLinePole_Hz, _IQ(1.0/USER_ISR_FREQ_Hz));
315  _iq a1 = b0 - _IQ(1.0);
316  EST_setRsOnLineFilterParams(estHandle,EST_RsOnLineFilterType_Current,b0,a1,_IQ(0.0),b0,a1,_IQ(0.0));
317  EST_setRsOnLineFilterParams(estHandle,EST_RsOnLineFilterType_Voltage,b0,a1,_IQ(0.0),b0,a1,_IQ(0.0));
318  }
319 
320  // set the number of current sensors
321  setupClarke_I(clarkeHandle_I,USER_NUM_CURRENT_SENSORS);
322 
323  // set the number of voltage sensors
324  setupClarke_V(clarkeHandle_V,USER_NUM_VOLTAGE_SENSORS);
325 
326  // set the pre-determined current and voltage feeback offset values
327  gOffsets_I_pu.value[0] = _IQ(I_A_offset);
328  gOffsets_I_pu.value[1] = _IQ(I_B_offset);
329  gOffsets_I_pu.value[2] = _IQ(I_C_offset);
330  gOffsets_V_pu.value[0] = _IQ(V_A_offset);
331  gOffsets_V_pu.value[1] = _IQ(V_B_offset);
332  gOffsets_V_pu.value[2] = _IQ(V_C_offset);
333 
334  // initialize the PID controllers
335  {
336  _iq maxCurrent_pu = _IQ(USER_MOTOR_MAX_CURRENT / USER_IQ_FULL_SCALE_CURRENT_A);
337  _iq maxVoltage_pu = _IQ(USER_MAX_VS_MAG_PU * USER_VD_SF);
338  float_t fullScaleCurrent = USER_IQ_FULL_SCALE_CURRENT_A;
339  float_t fullScaleVoltage = USER_IQ_FULL_SCALE_VOLTAGE_V;
340  float_t IsrPeriod_sec = 1.0 / USER_ISR_FREQ_Hz;
341  float_t Ls_d = USER_MOTOR_Ls_d;
342  float_t Ls_q = USER_MOTOR_Ls_q;
343  float_t Rs = USER_MOTOR_Rs;
344  float_t RoverLs_d = Rs/Ls_d;
345  float_t RoverLs_q = Rs/Ls_q;
346  _iq Kp_Id = _IQ((0.25*Ls_d*fullScaleCurrent)/(IsrPeriod_sec*fullScaleVoltage));
347  _iq Ki_Id = _IQ(RoverLs_d*IsrPeriod_sec);
348  _iq Kp_Iq = _IQ((0.25*Ls_q*fullScaleCurrent)/(IsrPeriod_sec*fullScaleVoltage));
349  _iq Ki_Iq = _IQ(RoverLs_q*IsrPeriod_sec);
350 
351  pidHandle[0] = PID_init(&pid[0],sizeof(pid[0]));
352  pidHandle[1] = PID_init(&pid[1],sizeof(pid[1]));
353  pidHandle[2] = PID_init(&pid[2],sizeof(pid[2]));
354 
355  PID_setGains(pidHandle[0],_IQ(3.0),_IQ(0.03),_IQ(0.0));
356  PID_setMinMax(pidHandle[0],-maxCurrent_pu,maxCurrent_pu);
357  PID_setUi(pidHandle[0],_IQ(0.0));
358  pidCntSpeed = 0;
359 
360  PID_setGains(pidHandle[1],Kp_Id,Ki_Id,_IQ(0.0));
361  PID_setMinMax(pidHandle[1],-maxVoltage_pu,maxVoltage_pu);
362  PID_setUi(pidHandle[1],_IQ(0.0));
363 
364  PID_setGains(pidHandle[2],Kp_Iq,Ki_Iq,_IQ(0.0));
365  PID_setMinMax(pidHandle[2],_IQ(0.0),_IQ(0.0));
366  PID_setUi(pidHandle[2],_IQ(0.0));
367  }
368 
369  {
370  uint16_t cnt;
371 
372  // initialize the handle for vibration compensation
373  vib_compHandle = VIB_COMP_init(&vib_comp_reserved, VIB_COMP_getSizeOfObject());
374 
375  VIB_COMP_setParams(vib_compHandle, gAlpha, gAdvIndexDelta);
376 
377  VIB_COMP_reset(vib_compHandle);
378 
379  for(cnt=0;cnt<360;cnt++)
380  {
381  gSpeed_fbk_out[cnt] = _IQ(0.0);
382  }
383 
384  }
385 
386  // initialize the speed reference in kilo RPM where base speed is USER_IQ_FULL_SCALE_FREQ_Hz
387  gMotorVars.SpeedRef_krpm = _IQmpy(_IQ(10.0), gSpeed_hz_to_krpm_sf);
388 
389  // initialize the inverse Park module
390  iparkHandle = IPARK_init(&ipark,sizeof(ipark));
391 
392  // initialize and configure offsets using filters
393  {
394  uint16_t cnt = 0;
395  _iq b0 = _IQ(gUserParams.offsetPole_rps/(float_t)gUserParams.ctrlFreq_Hz);
396  _iq a1 = (b0 - _IQ(1.0));
397  _iq b1 = _IQ(0.0);
398 
399  for(cnt=0;cnt<6;cnt++)
400  {
401  filterHandle[cnt] = FILTER_FO_init(&filter[cnt],sizeof(filter[0]));
402  FILTER_FO_setDenCoeffs(filterHandle[cnt],a1);
403  FILTER_FO_setNumCoeffs(filterHandle[cnt],b0,b1);
404  FILTER_FO_setInitialConditions(filterHandle[cnt],_IQ(0.0),_IQ(0.0));
405  }
406 
407  gMotorVars.Flag_enableOffsetcalc = false;
408  }
409 
410  // initialize the space vector generator module
411  svgenHandle = SVGEN_init(&svgen,sizeof(svgen));
412 
413  // Initialize and setup the 100% SVM generator
414  svgencurrentHandle = SVGENCURRENT_init(&svgencurrent,sizeof(svgencurrent));
415 
416  // setup svgen current
417  {
418  float_t minWidth_microseconds = 2.0;
419  uint16_t minWidth_counts = (uint16_t)(minWidth_microseconds * USER_SYSTEM_FREQ_MHz);
420 
421  SVGENCURRENT_setMinWidth(svgencurrentHandle, minWidth_counts);
422  SVGENCURRENT_setIgnoreShunt(svgencurrentHandle, use_all);
423  }
424 
425  // initialize the speed reference trajectory
426  trajHandle_spd = TRAJ_init(&traj_spd,sizeof(traj_spd));
427 
428  // configure the speed reference trajectory
429  TRAJ_setTargetValue(trajHandle_spd,_IQ(0.0));
430  TRAJ_setIntValue(trajHandle_spd,_IQ(0.0));
431  TRAJ_setMinValue(trajHandle_spd,_IQ(-1.0));
432  TRAJ_setMaxValue(trajHandle_spd,_IQ(1.0));
434 
435  // initialize the Id reference trajectory
436  trajHandle_Id = TRAJ_init(&traj_Id,sizeof(traj_Id));
437 
438  if(USER_MOTOR_TYPE == MOTOR_Type_Pm)
439  {
440  // configure the Id reference trajectory
441  TRAJ_setTargetValue(trajHandle_Id,_IQ(0.0));
442  TRAJ_setIntValue(trajHandle_Id,_IQ(0.0));
443  TRAJ_setMinValue(trajHandle_Id,_IQ(-USER_MOTOR_MAX_CURRENT / USER_IQ_FULL_SCALE_CURRENT_A));
444  TRAJ_setMaxValue(trajHandle_Id,_IQ(USER_MOTOR_MAX_CURRENT / USER_IQ_FULL_SCALE_CURRENT_A));
446 
447  // Initialize field weakening
448  fwHandle = FW_init(&fw,sizeof(fw));
449  FW_setFlag_enableFw(fwHandle, false); // Disable field weakening
450  FW_clearCounter(fwHandle); // Clear field weakening counter
451  FW_setNumIsrTicksPerFwTick(fwHandle, FW_NUM_ISR_TICKS_PER_CTRL_TICK); // Set the number of ISR per field weakening ticks
452  FW_setDeltas(fwHandle, FW_INC_DELTA, FW_DEC_DELTA); // Set the deltas of field weakening
453  FW_setOutput(fwHandle, _IQ(0.0)); // Set initial output of field weakening to zero
454  FW_setMinMax(fwHandle,_IQ(USER_MAX_NEGATIVE_ID_REF_CURRENT_A/USER_IQ_FULL_SCALE_CURRENT_A),_IQ(0.0)); // Set the field weakening controller limits
455  }
456  else
457  {
458  // configure the Id reference trajectory
459  TRAJ_setTargetValue(trajHandle_Id,_IQ(0.0));
460  TRAJ_setIntValue(trajHandle_Id,_IQ(0.0));
461  TRAJ_setMinValue(trajHandle_Id,_IQ(0.0));
464  }
465 
466  // initialize the CPU usage module
467  cpu_usageHandle = CPU_USAGE_init(&cpu_usage,sizeof(cpu_usage));
468  CPU_USAGE_setParams(cpu_usageHandle,
469  (uint32_t)USER_SYSTEM_FREQ_MHz * 1000000, // timer period, cnts
470  (uint32_t)USER_ISR_FREQ_Hz); // average over 1 second of ISRs
471 
472  // setup faults
473  HAL_setupFaults(halHandle);
474 
475  // initialize the interrupt vector table
476  HAL_initIntVectorTable(halHandle);
477 
478  // enable the ADC interrupts
479  HAL_enableAdcInts(halHandle);
480 
481  // enable global interrupts
482  HAL_enableGlobalInts(halHandle);
483 
484  // enable debug interrupts
485  HAL_enableDebugInt(halHandle);
486 
487  // disable the PWM
488  HAL_disablePwm(halHandle);
489 
490  // compute scaling factors for flux and torque calculations
491  gFlux_pu_to_Wb_sf = USER_computeFlux_pu_to_Wb_sf();
492  gFlux_pu_to_VpHz_sf = USER_computeFlux_pu_to_VpHz_sf();
493  gTorque_Ls_Id_Iq_pu_to_Nm_sf = USER_computeTorque_Ls_Id_Iq_pu_to_Nm_sf();
494  gTorque_Flux_Iq_pu_to_Nm_sf = USER_computeTorque_Flux_Iq_pu_to_Nm_sf();
495 
496  // enable the system by default
497  gMotorVars.Flag_enableSys = true;
498 
499 #ifdef DRV8301_SPI
500  // turn on the DRV8301 if present
501  HAL_enableDrv(halHandle);
502  // initialize the DRV8301 interface
503  HAL_setupDrvSpi(halHandle,&gDrvSpi8301Vars);
504 #endif
505 
506 #ifdef DRV8305_SPI
507  // turn on the DRV8305 if present
508  HAL_enableDrv(halHandle);
509  // initialize the DRV8305 interface
510  HAL_setupDrvSpi(halHandle,&gDrvSpi8305Vars);
511 #endif
512 
513  // Begin the background loop
514  for(;;)
515  {
516  // Waiting for enable system flag to be set
517  while(!(gMotorVars.Flag_enableSys));
518 
519  // loop while the enable system flag is true
520  while(gMotorVars.Flag_enableSys)
521  {
522  if(gMotorVars.Flag_Run_Identify)
523  {
524  // disable Rs recalculation
526 
527  // update estimator state
529 
530  #ifdef FAST_ROM_V1p6
531  // call this function to fix 1p6
533  #endif
534 
535  // enable the PWM
536  HAL_enablePwm(halHandle);
537 
538  // set trajectory target for speed reference
539  TRAJ_setTargetValue(trajHandle_spd,_IQmpy(gMotorVars.SpeedRef_krpm, gSpeed_krpm_to_pu_sf));
540 
541  if(USER_MOTOR_TYPE == MOTOR_Type_Pm)
542  {
543  // set trajectory target for Id reference
544  TRAJ_setTargetValue(trajHandle_Id,gIdq_ref_pu.value[0]);
545  }
546  else
547  {
548  if(gMotorVars.Flag_enablePowerWarp)
549  {
550  _iq Id_target_pw_pu = EST_runPowerWarp(estHandle,TRAJ_getIntValue(trajHandle_Id),gIdq_pu.value[1]);
551  TRAJ_setTargetValue(trajHandle_Id,Id_target_pw_pu);
553  TRAJ_setMaxDelta(trajHandle_Id,_IQ(USER_MOTOR_MAGNETIZING_CURRENT * 0.3 / USER_IQ_FULL_SCALE_CURRENT_A / USER_ISR_FREQ_Hz));
554  }
555  else
556  {
557  // set trajectory target for Id reference
559  TRAJ_setMinValue(trajHandle_Id,_IQ(0.0));
561  }
562  }
563  }
564  else if(gMotorVars.Flag_enableRsRecalc)
565  {
566  // set angle to zero
568 
569  // enable or disable Rs recalculation
571 
572  // update estimator state
574 
575  #ifdef FAST_ROM_V1p6
576  // call this function to fix 1p6
578  #endif
579 
580  // enable the PWM
581  HAL_enablePwm(halHandle);
582 
583  // set trajectory target for speed reference
584  TRAJ_setTargetValue(trajHandle_spd,_IQ(0.0));
585 
586  // set trajectory target for Id reference
588 
589  // if done with Rs recalculation, disable flag
590  if(EST_getState(estHandle) == EST_State_OnLine) gMotorVars.Flag_enableRsRecalc = false;
591  }
592  else
593  {
594  // set estimator to Idle
596 
597  // disable the PWM
598  HAL_disablePwm(halHandle);
599 
600  // clear the speed reference trajectory
601  TRAJ_setTargetValue(trajHandle_spd,_IQ(0.0));
602  TRAJ_setIntValue(trajHandle_spd,_IQ(0.0));
603 
604  // clear the Id reference trajectory
605  TRAJ_setTargetValue(trajHandle_Id,_IQ(0.0));
606  TRAJ_setIntValue(trajHandle_Id,_IQ(0.0));
607 
608  // configure trajectory Id defaults depending on motor type
609  if(USER_MOTOR_TYPE == MOTOR_Type_Pm)
610  {
611  TRAJ_setMinValue(trajHandle_Id,_IQ(-USER_MOTOR_MAX_CURRENT / USER_IQ_FULL_SCALE_CURRENT_A));
613  }
614  else
615  {
616  TRAJ_setMinValue(trajHandle_Id,_IQ(0.0));
618  }
619 
620  // clear integral outputs
621  PID_setUi(pidHandle[0],_IQ(0.0));
622  PID_setUi(pidHandle[1],_IQ(0.0));
623  PID_setUi(pidHandle[2],_IQ(0.0));
624 
625  // clear Id and Iq references
626  gIdq_ref_pu.value[0] = _IQ(0.0);
627  gIdq_ref_pu.value[1] = _IQ(0.0);
628 
629  // disable RsOnLine flags
630  gFlag_enableRsOnLine = false;
631  gFlag_updateRs = false;
632 
633  // disable PowerWarp flag
634  gMotorVars.Flag_enablePowerWarp = false;
635  }
636 
637  // update the global variables
639 
640  // set field weakening enable flag depending on user's input
641  FW_setFlag_enableFw(fwHandle,gMotorVars.Flag_enableFieldWeakening);
642 
643  // update CPU usage
644  updateCPUusage();
645 
646  // enable/disable the forced angle
648 
649  // enable or disable RsOnLine
650  EST_setFlag_enableRsOnLine(estHandle,gFlag_enableRsOnLine);
651 
652  // set slow rotating frequency for RsOnLine
653  EST_setRsOnLineAngleDelta_pu(estHandle,_IQmpy(gRsOnLineFreq_Hz, _IQ(1.0/USER_ISR_FREQ_Hz)));
654 
655  // set current amplitude for RsOnLine
657 
658  // set flag that updates Rs from RsOnLine value
659  EST_setFlag_updateRs(estHandle,gFlag_updateRs);
660 
661  // clear Id for RsOnLine if disabled
662  if(!gFlag_enableRsOnLine) EST_setRsOnLineId_pu(estHandle,_IQ(0.0));
663 
664 #ifdef DRV8301_SPI
665  HAL_writeDrvData(halHandle,&gDrvSpi8301Vars);
666 
667  HAL_readDrvData(halHandle,&gDrvSpi8301Vars);
668 #endif
669 #ifdef DRV8305_SPI
670  HAL_writeDrvData(halHandle,&gDrvSpi8305Vars);
671 
672  HAL_readDrvData(halHandle,&gDrvSpi8305Vars);
673 #endif
674 
675  VIB_COMP_setAlpha(vib_compHandle, gAlpha);
676 
677  VIB_COMP_setAdvIndexDelta(vib_compHandle, gAdvIndexDelta);
678 
679  VIB_COMP_setFlag_enableOutput(vib_compHandle, gFlag_enableOutput);
680 
681  VIB_COMP_setFlag_enableUpdates(vib_compHandle, gFlag_enableUpdates);
682 
683  if(gFlag_resetVibComp)
684  {
685  gFlag_resetVibComp = false;
686  VIB_COMP_reset(vib_compHandle);
687  }
688 
689  if(gFlag_speedStatsReset)
690  {
691  gFlag_speedStatsReset = false;
692  gSpeed_max_pu = _IQ(0.0);
693  gSpeed_min_pu = _IQ(1.0);
694  }
695 
696  } // end of while(gFlag_enableSys) loop
697 
698  // disable the PWM
699  HAL_disablePwm(halHandle);
700 
701  gMotorVars.Flag_Run_Identify = false;
702  } // end of for(;;) loop
703 } // end of main() function
704 
705 
707 interrupt void mainISR(void)
708 {
709  _iq angle_pu = _IQ(0.0);
710  _iq speed_pu = _IQ(0.0);
711  _iq oneOverDcBus;
712  MATH_vec2 Iab_pu;
713  MATH_vec2 Vab_pu;
714  MATH_vec2 phasor;
715  uint32_t timer1Cnt;
716 
717  // read the timer 1 value and update the CPU usage module
718  timer1Cnt = HAL_readTimerCnt(halHandle,1);
719  CPU_USAGE_updateCnts(cpu_usageHandle,timer1Cnt);
720 
721  // acknowledge the ADC interrupt
722  HAL_acqAdcInt(halHandle,ADC_IntNumber_1);
723 
724  // convert the ADC data
725  HAL_readAdcDataWithOffsets(halHandle,&gAdcData);
726 
727  // remove offsets
728  gAdcData.I.value[0] = gAdcData.I.value[0] - gOffsets_I_pu.value[0];
729  gAdcData.I.value[1] = gAdcData.I.value[1] - gOffsets_I_pu.value[1];
730  gAdcData.I.value[2] = gAdcData.I.value[2] - gOffsets_I_pu.value[2];
731  gAdcData.V.value[0] = gAdcData.V.value[0] - gOffsets_V_pu.value[0];
732  gAdcData.V.value[1] = gAdcData.V.value[1] - gOffsets_V_pu.value[1];
733  gAdcData.V.value[2] = gAdcData.V.value[2] - gOffsets_V_pu.value[2];
734 
735  // run the current reconstruction algorithm
737 
738  // run Clarke transform on current
739  CLARKE_run(clarkeHandle_I,&gAdcData.I,&Iab_pu);
740 
741  // run Clarke transform on voltage
742  CLARKE_run(clarkeHandle_V,&gAdcData.V,&Vab_pu);
743 
744  // run a trajectory for Id reference, so the reference changes with a ramp instead of a step
745  TRAJ_run(trajHandle_Id);
746 
747  // run a trajectory for speed reference, so the reference changes with a ramp instead of a step
748  TRAJ_run(trajHandle_spd);
749 
750  // run the estimator
752  &Iab_pu,
753  &Vab_pu,
754  gAdcData.dcBus,
755  TRAJ_getIntValue(trajHandle_spd));
756 
757  // generate the motor electrical angle
758  angle_pu = EST_getAngle_pu(estHandle);
759  speed_pu = EST_getFm_pu(estHandle);
760 
761  // calculate absolute electrical angle
762  gAbsAngle_elec_pu = getAbsElecAngle(angle_pu);
763 
764  // calculate absolute mechanical angle
765  gAbsAngle_mech_pu = getAbsMechAngle(&gAngle_mech_poles, &gAngle_z1_pu, gAbsAngle_elec_pu);
766 
767  // get Idq from estimator to avoid sin and cos
768  EST_getIdq_pu(estHandle,&gIdq_pu);
769 
770  // run the appropriate controller
771  if((gMotorVars.Flag_Run_Identify) || (gMotorVars.Flag_enableRsRecalc))
772  {
773  _iq refValue;
774  _iq fbackValue;
775  _iq outMax_pu;
776 
777  // when appropriate, run the PID speed controller
779  {
780  // calculate Id reference squared
781  _iq Id_ref_squared_pu = _IQmpy(PID_getRefValue(pidHandle[1]),PID_getRefValue(pidHandle[1]));
782 
783  // Take into consideration that Iq^2+Id^2 = Is^2
784  _iq Iq_Max_pu = _IQsqrt(gIs_Max_squared_pu - Id_ref_squared_pu);
785 
786  // clear counter
787  pidCntSpeed = 0;
788 
789  // Set new min and max for the speed controller output
790  PID_setMinMax(pidHandle[0], -Iq_Max_pu, Iq_Max_pu);
791 
792  // run speed controller
793  PID_run_spd(pidHandle[0],TRAJ_getIntValue(trajHandle_spd),speed_pu,&(gIdq_ref_pu.value[1]));
794 
795  if(speed_pu > gSpeed_max_pu)
796  {
797  gSpeed_max_pu = speed_pu;
798  }
799 
800  if(speed_pu < gSpeed_min_pu)
801  {
802  gSpeed_min_pu = speed_pu;
803  }
804  }
805 
806  // get the reference value from the trajectory module
807  refValue = TRAJ_getIntValue(trajHandle_Id) + EST_getRsOnLineId_pu(estHandle);
808 
809  // get the feedback value
810  fbackValue = gIdq_pu.value[0];
811 
812  // run the Id PID controller
813  PID_run(pidHandle[1],refValue,fbackValue,&(gVdq_out_pu.value[0]));
814 
815  // set Iq reference to zero when doing Rs recalculation
816  if(gMotorVars.Flag_enableRsRecalc) gIdq_ref_pu.value[1] = _IQ(0.0);
817 
818  // get the Iq reference value plus vibration compensation
819  refValue = gIdq_ref_pu.value[1] + VIB_COMP_run(vib_compHandle, gAbsAngle_mech_pu, gIdq_pu.value[1]);
820  gSpeed_fbk_out[VIB_COMP_getIndex(vib_compHandle)] = gIdq_ref_pu.value[1];
821 
822  // get the feedback value
823  fbackValue = gIdq_pu.value[1];
824 
825  // calculate Iq controller limits, and run Iq controller
826  outMax_pu = _IQsqrt(_IQ(USER_MAX_VS_MAG_PU * USER_MAX_VS_MAG_PU) - _IQmpy(gVdq_out_pu.value[0],gVdq_out_pu.value[0]));
827  PID_setMinMax(pidHandle[2],-outMax_pu,outMax_pu);
828  PID_run(pidHandle[2],refValue,fbackValue,&(gVdq_out_pu.value[1]));
829 
830  // compensate angle for PWM delay
831  angle_pu = angleDelayComp(speed_pu, angle_pu);
832 
833  // compute the sin/cos phasor
834  phasor.value[0] = _IQcosPU(angle_pu);
835  phasor.value[1] = _IQsinPU(angle_pu);
836 
837  // set the phasor in the inverse Park transform
838  IPARK_setPhasor(iparkHandle,&phasor);
839 
840  // run the inverse Park module
841  IPARK_run(iparkHandle,&gVdq_out_pu,&Vab_pu);
842 
843  // run the space Vector Generator (SVGEN) module
844  oneOverDcBus = EST_getOneOverDcBus_pu(estHandle);
845  Vab_pu.value[0] = _IQmpy(Vab_pu.value[0],oneOverDcBus);
846  Vab_pu.value[1] = _IQmpy(Vab_pu.value[1],oneOverDcBus);
847  SVGEN_run(svgenHandle,&Vab_pu,&(gPwmData.Tabc));
848  }
849  else if(gMotorVars.Flag_enableOffsetcalc == true)
850  {
852  }
853  else
854  {
855  // disable the PWM
856  HAL_disablePwm(halHandle);
857 
858  // Set the PWMs to 50% duty cycle
859  gPwmData.Tabc.value[0] = _IQ(0.0);
860  gPwmData.Tabc.value[1] = _IQ(0.0);
861  gPwmData.Tabc.value[2] = _IQ(0.0);
862  }
863 
864  // write the PWM compare values
865  HAL_writePwmData(halHandle,&gPwmData);
866 
867  // run the current ignore algorithm
869 
870  // run function to set next trigger
871  if(!gMotorVars.Flag_enableRsRecalc) runSetTrigger();
872 
873  // run field weakening
874  if(USER_MOTOR_TYPE == MOTOR_Type_Pm) runFieldWeakening();
875 
876  // read the timer 1 value and update the CPU usage module
877  timer1Cnt = HAL_readTimerCnt(halHandle,1);
878  CPU_USAGE_updateCnts(cpu_usageHandle,timer1Cnt);
879 
880  // run the CPU usage module
881  CPU_USAGE_run(cpu_usageHandle);
882 
883  return;
884 } // end of mainISR() function
885 
886 
887 _iq angleDelayComp(const _iq fm_pu, const _iq angleUncomp_pu)
888 {
889  _iq angleDelta_pu = _IQmpy(fm_pu,_IQ(USER_IQ_FULL_SCALE_FREQ_Hz/(USER_PWM_FREQ_kHz*1000.0)));
890  _iq angleCompFactor = _IQ(1.0 + (float_t)USER_NUM_PWM_TICKS_PER_ISR_TICK * 0.5);
891  _iq angleDeltaComp_pu = _IQmpy(angleDelta_pu, angleCompFactor);
892  uint32_t angleMask = ((uint32_t)0xFFFFFFFF >> (32 - GLOBAL_Q));
893  _iq angleComp_pu;
894  _iq angleTmp_pu;
895 
896  // increment the angle
897  angleTmp_pu = angleUncomp_pu + angleDeltaComp_pu;
898 
899  // mask the angle for wrap around
900  // note: must account for the sign of the angle
901  angleComp_pu = _IQabs(angleTmp_pu) & angleMask;
902 
903  // account for sign
904  if(angleTmp_pu < _IQ(0.0))
905  {
906  angleComp_pu = -angleComp_pu;
907  }
908 
909  return(angleComp_pu);
910 } // end of angleDelayComp() function
911 
912 
914 {
915  uint16_t cmp1 = HAL_readPwmCmpA(halHandle,PWM_Number_1);
916  uint16_t cmp2 = HAL_readPwmCmpA(halHandle,PWM_Number_2);
917  uint16_t cmp3 = HAL_readPwmCmpA(halHandle,PWM_Number_3);
918  uint16_t cmpM1 = HAL_readPwmCmpAM(halHandle,PWM_Number_1);
919  uint16_t cmpM2 = HAL_readPwmCmpAM(halHandle,PWM_Number_2);
920  uint16_t cmpM3 = HAL_readPwmCmpAM(halHandle,PWM_Number_3);
921 
922  // run the current ignore algorithm
923  SVGENCURRENT_RunIgnoreShunt(svgencurrentHandle,cmp1,cmp2,cmp3,cmpM1,cmpM2,cmpM3);
924 
925  return;
926 } // end of runCurrentIgnore() function
927 
928 
930 {
931  SVGENCURRENT_IgnoreShunt_e ignoreShuntThisCycle = SVGENCURRENT_getIgnoreShunt(svgencurrentHandle);
932  SVGENCURRENT_RunRegenCurrent(svgencurrentHandle, (MATH_vec3 *)(gAdcData.I.value));
933 
934  gIavg.value[0] += (gAdcData.I.value[0] - gIavg.value[0])>>gIavg_shift;
935  gIavg.value[1] += (gAdcData.I.value[1] - gIavg.value[1])>>gIavg_shift;
936  gIavg.value[2] += (gAdcData.I.value[2] - gIavg.value[2])>>gIavg_shift;
937 
938  if(ignoreShuntThisCycle == ignore_ab)
939  {
940  gAdcData.I.value[0] = gIavg.value[0];
941  gAdcData.I.value[1] = gIavg.value[1];
942  }
943  else if(ignoreShuntThisCycle == ignore_ac)
944  {
945  gAdcData.I.value[0] = gIavg.value[0];
946  gAdcData.I.value[2] = gIavg.value[2];
947  }
948  else if(ignoreShuntThisCycle == ignore_bc)
949  {
950  gAdcData.I.value[1] = gIavg.value[1];
951  gAdcData.I.value[2] = gIavg.value[2];
952  }
953 
954  return;
955 } // end of runCurrentReconstruction() function
956 
957 
959 {
960  if(FW_getFlag_enableFw(fwHandle) == true)
961  {
962  FW_incCounter(fwHandle);
963 
964  if(FW_getCounter(fwHandle) > FW_getNumIsrTicksPerFwTick(fwHandle))
965  {
966  _iq refValue;
967  _iq fbackValue;
968 
969  FW_clearCounter(fwHandle);
970 
971  refValue = gMotorVars.VsRef;
972 
973  fbackValue = gMotorVars.Vs;
974 
975  FW_run(fwHandle, refValue, fbackValue, &(gIdq_ref_pu.value[0]));
976 
977  gMotorVars.IdRef_A = _IQmpy(gIdq_ref_pu.value[0], _IQ(USER_IQ_FULL_SCALE_CURRENT_A));
978  }
979  }
980  else
981  {
982  gIdq_ref_pu.value[0] = _IQmpy(gMotorVars.IdRef_A, _IQ(1.0/USER_IQ_FULL_SCALE_CURRENT_A));
983  }
984 
985  return;
986 } // end of runFieldWeakening() function
987 
988 
990 {
991  uint16_t cnt;
992 
993  // enable the PWM
994  HAL_enablePwm(halHandle);
995 
996  for(cnt=0;cnt<3;cnt++)
997  {
998  // Set the PWMs to 50% duty cycle
999  gPwmData.Tabc.value[cnt] = _IQ(0.0);
1000 
1001  // reset offsets used
1002  gOffsets_I_pu.value[cnt] = _IQ(0.0);
1003  gOffsets_V_pu.value[cnt] = _IQ(0.0);
1004 
1005  // run offset estimation
1006  FILTER_FO_run(filterHandle[cnt],gAdcData.I.value[cnt]);
1007  FILTER_FO_run(filterHandle[cnt+3],gAdcData.V.value[cnt]);
1008  }
1009 
1010  if(gOffsetCalcCount++ >= gUserParams.ctrlWaitTime[CTRL_State_OffLine])
1011  {
1012  gMotorVars.Flag_enableOffsetcalc = false;
1013  gOffsetCalcCount = 0;
1014 
1015  for(cnt=0;cnt<3;cnt++)
1016  {
1017  // get calculated offsets from filter
1018  gOffsets_I_pu.value[cnt] = FILTER_FO_get_y1(filterHandle[cnt]);
1019  gOffsets_V_pu.value[cnt] = FILTER_FO_get_y1(filterHandle[cnt+3]);
1020 
1021  // clear filters
1022  FILTER_FO_setInitialConditions(filterHandle[cnt],_IQ(0.0),_IQ(0.0));
1023  FILTER_FO_setInitialConditions(filterHandle[cnt+3],_IQ(0.0),_IQ(0.0));
1024  }
1025  }
1026 
1027  return;
1028 } // end of runOffsetsCalculation() function
1029 
1030 
1032 {
1034  float_t Ls_coarse_max = _IQ30toF(EST_getLs_coarse_max_pu(handle));
1035  int_least8_t lShift = ceil(log(USER_MOTOR_Ls_d/(Ls_coarse_max*fullScaleInductance))/log(2.0));
1036  uint_least8_t Ls_qFmt = 30 - lShift;
1037  float_t L_max = fullScaleInductance * pow(2.0,lShift);
1038  _iq Ls_d_pu = _IQ30(USER_MOTOR_Ls_d / L_max);
1039  _iq Ls_q_pu = _IQ30(USER_MOTOR_Ls_q / L_max);
1040 
1041 
1042  // store the results
1043  EST_setLs_d_pu(handle,Ls_d_pu);
1044  EST_setLs_q_pu(handle,Ls_q_pu);
1045  EST_setLs_qFmt(handle,Ls_qFmt);
1046 
1047  return;
1048 } // end of softwareUpdate1p6() function
1049 
1050 
1051 void runSetTrigger(void)
1052 {
1053  int16_t minwidth = SVGENCURRENT_getMinWidth(svgencurrentHandle);
1054  SVGENCURRENT_IgnoreShunt_e ignoreShuntNextCycle = SVGENCURRENT_getIgnoreShunt(svgencurrentHandle);
1055 
1056  // Set trigger point in the middle of the low side pulse
1057  HAL_setTrigger(halHandle,ignoreShuntNextCycle,minwidth,gCmpOffset);
1058 
1059  return;
1060 } // end of runSetTrigger() function
1061 
1062 
1066 void setupClarke_I(CLARKE_Handle handle,const uint_least8_t numCurrentSensors)
1067 {
1068  _iq alpha_sf,beta_sf;
1069 
1070  // initialize the Clarke transform module for current
1071  if(numCurrentSensors == 3)
1072  {
1073  alpha_sf = _IQ(MATH_ONE_OVER_THREE);
1074  beta_sf = _IQ(MATH_ONE_OVER_SQRT_THREE);
1075  }
1076  else if(numCurrentSensors == 2)
1077  {
1078  alpha_sf = _IQ(1.0);
1079  beta_sf = _IQ(MATH_ONE_OVER_SQRT_THREE);
1080  }
1081  else
1082  {
1083  alpha_sf = _IQ(0.0);
1084  beta_sf = _IQ(0.0);
1085  }
1086 
1087  // set the parameters
1088  CLARKE_setScaleFactors(handle,alpha_sf,beta_sf);
1089  CLARKE_setNumSensors(handle,numCurrentSensors);
1090 
1091  return;
1092 } // end of setupClarke_I() function
1093 
1094 
1098 void setupClarke_V(CLARKE_Handle handle,const uint_least8_t numVoltageSensors)
1099 {
1100  _iq alpha_sf,beta_sf;
1101 
1102  // initialize the Clarke transform module for voltage
1103  if(numVoltageSensors == 3)
1104  {
1105  alpha_sf = _IQ(MATH_ONE_OVER_THREE);
1106  beta_sf = _IQ(MATH_ONE_OVER_SQRT_THREE);
1107  }
1108  else
1109  {
1110  alpha_sf = _IQ(0.0);
1111  beta_sf = _IQ(0.0);
1112  }
1113 
1114  // set the parameters
1115  CLARKE_setScaleFactors(handle,alpha_sf,beta_sf);
1116  CLARKE_setNumSensors(handle,numVoltageSensors);
1117 
1118  return;
1119 } // end of setupClarke_V() function
1120 
1121 
1125 {
1126  // get the speed estimate
1127  gMotorVars.Speed_krpm = EST_getSpeed_krpm(handle);
1128 
1129  // get the torque estimate
1130  {
1131  _iq Flux_pu = EST_getFlux_pu(handle);
1132  _iq Id_pu = PID_getFbackValue(pidHandle[1]);
1133  _iq Iq_pu = PID_getFbackValue(pidHandle[2]);
1134  _iq Ld_minus_Lq_pu = _IQ30toIQ(EST_getLs_d_pu(handle)-EST_getLs_q_pu(handle));
1135  _iq Torque_Flux_Iq_Nm = _IQmpy(_IQmpy(Flux_pu,Iq_pu),gTorque_Flux_Iq_pu_to_Nm_sf);
1136  _iq Torque_Ls_Id_Iq_Nm = _IQmpy(_IQmpy(_IQmpy(Ld_minus_Lq_pu,Id_pu),Iq_pu),gTorque_Ls_Id_Iq_pu_to_Nm_sf);
1137  _iq Torque_Nm = Torque_Flux_Iq_Nm + Torque_Ls_Id_Iq_Nm;
1138 
1139  gMotorVars.Torque_Nm = Torque_Nm;
1140  }
1141 
1142  // get the magnetizing current
1143  gMotorVars.MagnCurr_A = EST_getIdRated(handle);
1144 
1145  // get the rotor resistance
1146  gMotorVars.Rr_Ohm = EST_getRr_Ohm(handle);
1147 
1148  // get the stator resistance
1149  gMotorVars.Rs_Ohm = EST_getRs_Ohm(handle);
1150 
1151  // get the online stator resistance
1152  gMotorVars.RsOnLine_Ohm = EST_getRsOnLine_Ohm(handle);
1153 
1154  // get the stator inductance in the direct coordinate direction
1155  gMotorVars.Lsd_H = EST_getLs_d_H(handle);
1156 
1157  // get the stator inductance in the quadrature coordinate direction
1158  gMotorVars.Lsq_H = EST_getLs_q_H(handle);
1159 
1160  // get the flux in V/Hz in floating point
1161  gMotorVars.Flux_VpHz = EST_getFlux_VpHz(handle);
1162 
1163  // get the flux in Wb in fixed point
1164  gMotorVars.Flux_Wb = _IQmpy(EST_getFlux_pu(handle),gFlux_pu_to_Wb_sf);
1165 
1166  // get the estimator state
1167  gMotorVars.EstState = EST_getState(handle);
1168 
1169  // Get the DC buss voltage
1170  gMotorVars.VdcBus_kV = _IQmpy(gAdcData.dcBus,_IQ(USER_IQ_FULL_SCALE_VOLTAGE_V/1000.0));
1171 
1172  // read Vd and Vq vectors per units
1173  gMotorVars.Vd = gVdq_out_pu.value[0];
1174  gMotorVars.Vq = gVdq_out_pu.value[1];
1175 
1176  // calculate vector Vs in per units
1177  gMotorVars.Vs = _IQsqrt(_IQmpy(gMotorVars.Vd, gMotorVars.Vd) + _IQmpy(gMotorVars.Vq, gMotorVars.Vq));
1178 
1179  // read Id and Iq vectors in amps
1180  gMotorVars.Id_A = _IQmpy(gIdq_pu.value[0], _IQ(USER_IQ_FULL_SCALE_CURRENT_A));
1181  gMotorVars.Iq_A = _IQmpy(gIdq_pu.value[1], _IQ(USER_IQ_FULL_SCALE_CURRENT_A));
1182 
1183  // calculate vector Is in amps
1184  gMotorVars.Is_A = _IQsqrt(_IQmpy(gMotorVars.Id_A, gMotorVars.Id_A) + _IQmpy(gMotorVars.Iq_A, gMotorVars.Iq_A));
1185 
1186  // calculate maximum speed variation
1187  gSpeed_delta_krpm = _IQmpy(gSpeed_max_pu - gSpeed_min_pu, _IQ(USER_IQ_FULL_SCALE_FREQ_Hz * 60.0 / (float_t)USER_MOTOR_NUM_POLE_PAIRS / 1000.0));
1188 
1189  return;
1190 } // end of updateGlobalVariables() function
1191 
1192 
1193 void updateCPUusage(void)
1194 {
1195  uint32_t minDeltaCntObserved = CPU_USAGE_getMinDeltaCntObserved(cpu_usageHandle);
1196  uint32_t avgDeltaCntObserved = CPU_USAGE_getAvgDeltaCntObserved(cpu_usageHandle);
1197  uint32_t maxDeltaCntObserved = CPU_USAGE_getMaxDeltaCntObserved(cpu_usageHandle);
1198  uint16_t pwmPeriod = HAL_readPwmPeriod(halHandle,PWM_Number_1);
1199  float_t cpu_usage_den = (float_t)pwmPeriod * (float_t)USER_NUM_PWM_TICKS_PER_ISR_TICK * 2.0;
1200 
1201  // calculate the minimum cpu usage percentage
1202  gCpuUsagePercentageMin = (float_t)minDeltaCntObserved / cpu_usage_den * 100.0;
1203 
1204  // calculate the average cpu usage percentage
1205  gCpuUsagePercentageAvg = (float_t)avgDeltaCntObserved / cpu_usage_den * 100.0;
1206 
1207  // calculate the maximum cpu usage percentage
1208  gCpuUsagePercentageMax = (float_t)maxDeltaCntObserved / cpu_usage_den * 100.0;
1209 
1210  return;
1211 } // end of updateCPUusage() function
1212 
1213 
1217 _iq getAbsElecAngle(const _iq angle_pu)
1218 {
1219  _iq angle_abs_pu;;
1220 
1221  // make angle possitive only
1222  if(angle_pu < _IQ(0.0))
1223  {
1224  angle_abs_pu = angle_pu + _IQ(1.0);
1225  }
1226  else
1227  {
1228  angle_abs_pu = angle_pu;
1229  }
1230 
1231  return(angle_abs_pu);
1232 } // end of getAbsElecAngle() function
1233 
1234 
1239 _iq getAbsMechAngle(_iq *pAngle_mech_poles, _iq *pAngle_z1_pu, const _iq angle_pu)
1240 {
1241  _iq angle_elec_delta_pu = _IQ(0.0); // electrical angle delta, angle*z^0 - angle*z^-1
1242  _iq tmp_mech_pu = _IQ(0.0); // temporary value for intermediate calculations
1243  _iq angle_mech_out = _IQ(0.0); // temporary mechanical angle used to store the value to be returned by the function
1244 
1245  // calculate angle delta
1246  angle_elec_delta_pu = angle_pu - *pAngle_z1_pu;
1247 
1248  // calculate new mechanical angle
1249  tmp_mech_pu = *pAngle_mech_poles + angle_elec_delta_pu;
1250 
1251  // take care of delta calculations when electrical angle wraps around from _IQ(1.0) to _IQ(0.0) or from _IQ(0.0) to _IQ(1.0)
1252  if(angle_elec_delta_pu < _IQ(-0.5))
1253  {
1254  tmp_mech_pu = tmp_mech_pu + _IQ(1.0);
1255  }
1256  else if(angle_elec_delta_pu > _IQ(0.5))
1257  {
1258  tmp_mech_pu = tmp_mech_pu - _IQ(1.0);
1259  }
1260 
1261  // take care of wrap around of the mechanical angle, so that angle_mech_poles stays within _IQ(-USER_MOTOR_NUM_POLE_PAIRS) to _IQ(USER_MOTOR_NUM_POLE_PAIRS)
1262  if(tmp_mech_pu >= _IQ(USER_MOTOR_NUM_POLE_PAIRS))
1263  {
1264  tmp_mech_pu = tmp_mech_pu - _IQ(USER_MOTOR_NUM_POLE_PAIRS);
1265  }
1266  else if(tmp_mech_pu <= _IQ(-USER_MOTOR_NUM_POLE_PAIRS))
1267  {
1268  tmp_mech_pu = tmp_mech_pu + _IQ(USER_MOTOR_NUM_POLE_PAIRS);
1269  }
1270 
1271  // store value in angle_mech_poles
1272  *pAngle_mech_poles = tmp_mech_pu;
1273 
1274  // scale the mechanical angle so that final output is from _IQ(-1.0) to _IQ(1.0)
1275  tmp_mech_pu = _IQmpy(tmp_mech_pu, _IQ(1.0/USER_MOTOR_NUM_POLE_PAIRS));
1276 
1277  // make the final mechanical angle a positive only values from _IQ(0.0) to _IQ(1.0)
1278  if(tmp_mech_pu < _IQ(0.0))
1279  {
1280  angle_mech_out = tmp_mech_pu + _IQ(1.0);
1281  }
1282  else
1283  {
1284  angle_mech_out = tmp_mech_pu;
1285  }
1286 
1287  // store the angle so next time this function is called we have the angle from the previous sample
1288  *pAngle_z1_pu = angle_pu;
1289 
1290  // returned the calculated mechanical angle from _IQ(0.0) to _IQ(1.0)
1291  return(angle_mech_out);
1292 } // end of getAbsMechAngle() function
1293 
1294 
1296 // end of file
1297 
1298 
float_t EST_getFlux_VpHz(EST_Handle handle)
USER_Params gUserParams
The user parameters.
Definition: proj_lab11b.c:124
#define USER_SYSTEM_FREQ_MHz
CLOCKS & TIMERS.
Definition: user.h:140
void runSetTrigger(void)
Definition: proj_lab11b.c:1051
float_t EST_getRsOnLine_Ohm(EST_Handle handle)
volatile bool gFlag_speedStatsReset
Definition: proj_lab11b.c:202
#define USER_NUM_CURRENT_SENSORS
Defines the number of current sensors used.
Definition: user.h:116
void EST_setFlag_enableForceAngle(EST_Handle handle, const bool state)
void updateGlobalVariables(EST_Handle handle)
Update the global variables (gMotorVars).
Definition: proj_lab11b.c:1124
IPARK_Handle IPARK_init(void *pMemory, const size_t numBytes)
_iq EST_getLs_coarse_max_pu(EST_Handle handle)
static void TRAJ_setTargetValue(TRAJ_Handle handle, const _iq targetValue)
CLARKE_Handle clarkeHandle_V
the handle for the voltage Clarke transform
Definition: proj_lab11b.c:69
#define _IQsinPU(A)
volatile int16_t gAdvIndexDelta
Definition: proj_lab11b.c:214
void HAL_enableGlobalInts(HAL_Handle handle)
void HAL_enableAdcInts(HAL_Handle handle)
static _iq PID_getRefValue(PID_Handle handle)
USER_ErrorCode_e UserErrorCode
Definition: main.h:150
#define USER_MAX_NEGATIVE_ID_REF_CURRENT_A
LIMITS.
Definition: user.h:239
float_t RsOnLine_Ohm
Definition: main.h:170
float_t Lsq_H
Definition: main.h:172
uint32_t gOffsetCalcCount
Definition: proj_lab11b.c:173
#define USER_MOTOR_Ls_d
Definition: user.h:401
_iq gAbsAngle_elec_pu
Definition: proj_lab11b.c:204
static void HAL_readAdcDataWithOffsets(HAL_Handle handle, HAL_AdcData_t *pAdcData)
_iq USER_computeFlux_pu_to_VpHz_sf(void)
Computes the scale factor needed to convert from per unit to V/Hz.
#define _IQ(A)
static void TRAJ_setMaxDelta(TRAJ_Handle handle, const _iq maxDelta)
PID_Handle pidHandle[3]
three objects for PID controllers 0 - Speed, 1 - Id, 2 - Iq
Definition: proj_lab11b.c:81
_iq Iq_A
Definition: main.h:195
static void PID_setUi(PID_Handle handle, const _iq Ui)
#define FW_INC_DELTA
bool Flag_Run_Identify
Definition: main.h:137
EST_Handle estHandle
the handle for the estimator
Definition: proj_lab11b.c:75
_iq EST_getSpeed_krpm(EST_Handle handle)
void EST_setRsOnLineAngleDelta_pu(EST_Handle handle, const _iq angleDelta_pu)
#define FW_DEC_DELTA
static uint16_t HAL_readPwmCmpAM(HAL_Handle handle, const PWM_Number_e pwmNumber)
int16_t VIB_COMP_getIndex(VIB_COMP_Handle handle)
void EST_setRsOnLineId_mag_pu(EST_Handle handle, const _iq Id_mag_pu)
FW_Handle FW_init(void *pMemory, const size_t numBytes)
static _iq TRAJ_getIntValue(TRAJ_Handle handle)
void EST_setupEstIdleState(EST_Handle handle)
static bool FW_getFlag_enableFw(FW_Handle fwHandle)
static uint32_t FW_getNumIsrTicksPerFwTick(FW_Handle fwHandle)
_iq Iq_Max_pu
Definition: proj_lab09.c:117
_iq gSpeed_max_pu
Definition: proj_lab11b.c:196
bool Flag_enablePowerWarp
Definition: main.h:144
static void HAL_writePwmData(HAL_Handle handle, HAL_PwmData_t *pPwmData)
static void FW_setOutput(FW_Handle fwHandle, const _iq output)
_iq value[3]
void runCurrentIgnore(void)
Definition: proj_lab11b.c:913
FILTER_FO_Handle filterHandle[6]
the handles for the 3-current and 3-voltage filters for offset calculation
Definition: proj_lab11b.c:87
uint16_t gIavg_shift
Definition: proj_lab11b.c:143
TRAJ_Handle trajHandle_spd
the handle for the speed reference trajectory
Definition: proj_lab11b.c:99
struct _EST_Obj_ * EST_Handle
void EST_setIdle(EST_Handle handle)
void HAL_writeDrvData(HAL_Handle handle, DRV_SPI_8301_Vars_t *Spi_8301_Vars)
#define USER_VD_SF
Defines the direct voltage (Vd) scale factor.
Definition: user.h:165
static void HAL_acqAdcInt(HAL_Handle handle, const ADC_IntNumber_e intNumber)
uint_least32_t ctrlWaitTime[CTRL_numStates]
void setupClarke_I(CLARKE_Handle handle, const uint_least8_t numCurrentSensors)
Setup the Clarke transform for either 2 or 3 sensors.
Definition: proj_lab11b.c:1066
TRAJ_Obj traj_spd
the speed reference trajectory object
Definition: proj_lab11b.c:100
#define USER_IQ_FULL_SCALE_VOLTAGE_V
Defines full scale value for the IQ30 variable of Voltage inside the system.
Definition: user.h:88
#define _IQcosPU(A)
volatile bool gFlag_updateRs
Definition: proj_lab11b.c:177
#define USER_MOTOR_NUM_POLE_PAIRS
Definition: user.h:398
volatile _iq gRsOnLinePole_Hz
Definition: proj_lab11b.c:183
static void FW_run(FW_Handle fwHandle, const _iq refValue, const _iq fbackValue, _iq *pOutValue)
void memCopy(uint16_t *srcStartAddr, uint16_t *srcEndAddr, uint16_t *dstAddr)
EST_Handle estHandle
void VIB_COMP_reset(VIB_COMP_Handle handle)
bool Flag_enableForceAngle
Definition: main.h:139
#define USER_PWM_FREQ_kHz
Defines the Pulse Width Modulation (PWM) frequency, kHz.
Definition: user.h:147
_iq gFlux_pu_to_Wb_sf
Definition: proj_lab11b.c:155
_iq gSpeed_min_pu
Definition: proj_lab11b.c:198
static void PID_setMinMax(PID_Handle handle, const _iq outMin, const _iq outMax)
static void SVGEN_run(SVGEN_Handle handle, const MATH_vec2 *pVab, MATH_vec3 *pT)
static uint32_t CPU_USAGE_getAvgDeltaCntObserved(CPU_USAGE_Handle handle)
_iq gAngle_z1_pu
Definition: proj_lab11b.c:210
static _iq FILTER_FO_run(FILTER_FO_Handle handle, const _iq inputValue)
static _iq PID_getFbackValue(PID_Handle handle)
#define V_B_offset
Definition: user.h:133
uint16_t pidCntSpeed
count variable to decimate the execution of the speed PID controller
Definition: proj_lab11b.c:82
SVGEN_Obj svgen
the space vector generator object
Definition: proj_lab11b.c:94
long _iq
void VIB_COMP_setFlag_enableUpdates(VIB_COMP_Handle handle, const bool state)
interrupt void mainISR(void)
The main ISR that implements the motor control.
Definition: proj_lab11b.c:707
#define MATH_ONE_OVER_THREE
void USER_setParams(USER_Params *pUserParams)
Sets the user parameter values.
void EST_setLs_qFmt(EST_Handle handle, const uint_least8_t Ls_qFmt)
SVGENCURRENT_Handle svgencurrentHandle
Definition: proj_lab11b.c:91
EST_State_e EstState
Definition: main.h:148
volatile _iq gAlpha
Definition: proj_lab11b.c:212
#define USER_MAX_ACCEL_Hzps
Defines the starting maximum acceleration AND deceleration for the speed profiles, Hz/s.
Definition: user.h:258
void EST_setFlag_updateRs(EST_Handle handle, const bool state)
static _iq FILTER_FO_get_y1(FILTER_FO_Handle handle)
float_t Rs_Ohm
Definition: main.h:169
FILTER_FO_Handle FILTER_FO_init(void *pMemory, const size_t numBytes)
#define V_A_offset
ADC voltage offsets for A, B, and C phases.
Definition: user.h:132
CPU_USAGE_Obj cpu_usage
Definition: proj_lab11b.c:73
_iq EST_getLs_d_pu(EST_Handle handle)
#define I_A_offset
ADC current offsets for A, B, and C phases.
Definition: user.h:125
MATH_vec2 gVdq_out_pu
contains the output Vd and Vq from the current controllers
Definition: proj_lab11b.c:117
static void CLARKE_setScaleFactors(CLARKE_Handle handle, const _iq alpha_sf, const _iq beta_sf)
Defines the structures, global initialization, and functions used in MAIN.
#define USER_MOTOR_Rs
Definition: user.h:400
void setupClarke_V(CLARKE_Handle handle, const uint_least8_t numVoltageSensors)
Setup the Clarke transform for either 2 or 3 sensors.
Definition: proj_lab11b.c:1098
HAL_PwmData_t gPwmData
contains the three pwm values -1.0 - 0%, 1.0 - 100%
Definition: proj_lab11b.c:107
TRAJ_Handle TRAJ_init(void *pMemory, const size_t numBytes)
CTRL_Handle CTRL_init(void *pMemory, const size_t numBytes)
void HAL_setupFaults(HAL_Handle handle)
EST_Handle EST_init(void *pMemory, const size_t numBytes)
_iq angleDelayComp(const _iq fm_pu, const _iq angleUncomp_pu)
The angleDelayComp function compensates for the delay introduced.
Definition: proj_lab11b.c:887
float_t EST_getLs_d_H(EST_Handle handle)
_iq gAbsAngle_mech_pu
Definition: proj_lab11b.c:206
FW_Obj fw
Definition: proj_lab11b.c:78
void EST_setFlag_enableRsRecalc(EST_Handle handle, const bool state)
void main(void)
Definition: proj_lab11b.c:224
void runFieldWeakening(void)
Definition: proj_lab11b.c:958
static void TRAJ_setIntValue(TRAJ_Handle handle, const _iq intValue)
volatile bool gFlag_resetVibComp
Definition: proj_lab11b.c:220
MATH_vec2 gIdq_pu
contains the Id and Iq measured values
Definition: proj_lab11b.c:119
MATH_vec3 Tabc
_iq gIs_Max_squared_pu
Definition: proj_lab11b.c:167
static void PID_setGains(PID_Handle handle, const _iq Kp, const _iq Ki, const _iq Kd)
static void HAL_enablePwm(HAL_Handle handle)
#define USER_MAX_VS_MAG_PU
Defines the maximum Voltage vector (Vs) magnitude allowed. This value sets the maximum magnitude for ...
Definition: user.h:157
void CTRL_setParams(CTRL_Handle handle, USER_Params *pUserParams)
#define _IQmpy(A, B)
void runOffsetsCalculation(void)
Definition: proj_lab11b.c:989
HAL_Handle halHandle
the handle for the hardware abstraction layer (HAL)
Definition: proj_lab11b.c:105
USER_ErrorCode_e USER_getErrorCode(USER_Params *pUserParams)
Gets the error code in the user parameters.
void HAL_enableDrv(HAL_Handle handle)
_iq value[2]
_iq VdcBus_kV
Definition: main.h:192
void VIB_COMP_setAlpha(VIB_COMP_Handle handle, const _iq alpha)
MATH_vec3 gOffsets_V_pu
contains the offsets for the voltage feedback
Definition: proj_lab11b.c:113
_iq Torque_Nm
Definition: main.h:165
void FILTER_FO_setNumCoeffs(FILTER_FO_Handle handle, const _iq b0, const _iq b1)
_iq SpeedRef_krpm
Definition: main.h:157
_iq IdRef_A
Definition: main.h:154
_iq VsRef
Definition: main.h:191
volatile _iq gRsOnLineId_mag_A
Definition: proj_lab11b.c:181
_iq EST_getOneOverDcBus_pu(EST_Handle handle)
_iq EST_getFlux_pu(EST_Handle handle)
void HAL_setupDrvSpi(HAL_Handle handle, DRV_SPI_8301_Vars_t *Spi_8301_Vars)
float_t EST_getLs_q_H(EST_Handle handle)
#define USER_EST_HANDLE_ADDRESS
Defines the address of estimator handle.
Definition: user.h:161
void HAL_enableDebugInt(HAL_Handle handle)
void CTRL_setupEstIdleState(CTRL_Handle handle)
#define USER_NUM_CTRL_TICKS_PER_SPEED_TICK
Defines the number of controller clock ticks per speed controller clock tick.
Definition: user.h:206
CLARKE_Handle CLARKE_init(void *pMemory, const size_t numBytes)
static void FW_incCounter(FW_Handle fwHandle)
_iq gSpeed_krpm_to_pu_sf
Definition: proj_lab11b.c:163
void CTRL_setUserMotorParams(CTRL_Handle handle)
#define I_C_offset
Definition: user.h:127
SVGENCURRENT_IgnoreShunt_e
float_t Flux_VpHz
Definition: main.h:173
#define _IQ30toIQ(A)
static void IPARK_run(IPARK_Handle handle, const MATH_vec2 *pInVec, MATH_vec2 *pOutVec)
#define FW_NUM_ISR_TICKS_PER_CTRL_TICK
float_t offsetPole_rps
void VIB_COMP_setAdvIndexDelta(VIB_COMP_Handle handle, const int16_t adv_index_delta)
EST_State_e EST_getState(EST_Handle handle)
bool Flag_enableOffsetcalc
Definition: main.h:143
float_t gCpuUsagePercentageMin
Definition: proj_lab11b.c:169
#define I_B_offset
Definition: user.h:126
FILTER_FO_Obj filter[6]
the 3-current and 3-voltage filters for offset calculation
Definition: proj_lab11b.c:88
_iq EST_getFm_pu(EST_Handle handle)
TRAJ_Handle trajHandle_Id
the handle for the id reference trajectory
Definition: proj_lab11b.c:96
_iq gFlux_pu_to_VpHz_sf
Definition: proj_lab11b.c:157
TRAJ_Obj traj_Id
the id reference trajectory object
Definition: proj_lab11b.c:97
#define MOTOR_Vars_INIT
Initialization values of global variables.
Definition: main.h:76
int16_t gCmpOffset
Definition: proj_lab11b.c:140
#define _IQabs(A)
#define V_C_offset
Definition: user.h:134
void EST_setFlag_enableRsOnLine(EST_Handle handle, const bool state)
MATH_vec3 gIavg
Definition: proj_lab11b.c:142
_iq USER_computeTorque_Flux_Iq_pu_to_Nm_sf(void)
Computes the scale factor needed to convert from torque created by flux and Iq, from per unit to Nm...
_iq Speed_krpm
Definition: main.h:160
CLARKE_Handle clarkeHandle_I
the handle for the current Clarke transform
Definition: proj_lab11b.c:66
float_t EST_getIdRated(EST_Handle handle)
float_t gCpuUsagePercentageMax
Definition: proj_lab11b.c:171
_iq gSpeed_fbk_out[360]
Definition: proj_lab11b.c:192
_iq Is_A
Definition: main.h:196
#define USER_MOTOR_RES_EST_CURRENT
Definition: user.h:405
void softwareUpdate1p6(EST_Handle handle)
Updates version 1p6 of library.
Definition: proj_lab11b.c:1031
static uint32_t HAL_readTimerCnt(HAL_Handle handle, const uint_least8_t timerNumber)
_iq EST_getLs_q_pu(EST_Handle handle)
static void CLARKE_setNumSensors(CLARKE_Handle handle, const uint_least8_t numSensors)
float_t Rr_Ohm
Definition: main.h:168
float_t EST_getRs_Ohm(EST_Handle handle)
float_t Lsd_H
Definition: main.h:171
VIB_COMP_Handle vib_compHandle
Definition: proj_lab11b.c:185
#define USER_MOTOR_MAGNETIZING_CURRENT
Definition: user.h:404
#define _IQ30(A)
CPU_USAGE_Handle cpu_usageHandle
Definition: proj_lab11b.c:72
#define USER_IQ_FULL_SCALE_FREQ_Hz
CURRENTS AND VOLTAGES.
Definition: user.h:78
volatile bool gFlag_enableUpdates
Definition: proj_lab11b.c:218
void CPU_USAGE_setParams(CPU_USAGE_Handle handle, const uint32_t timerPeriod_cnts, const uint32_t numDeltaCntsAvg)
static void FW_setFlag_enableFw(FW_Handle fwHandle, const bool state)
size_t VIB_COMP_getSizeOfObject(void)
#define USER_ISR_FREQ_Hz
Defines the Interrupt Service Routine (ISR) frequency, Hz.
Definition: user.h:173
static void IPARK_setPhasor(IPARK_Handle handle, const MATH_vec2 *pPhasor)
static void FW_clearCounter(FW_Handle fwHandle)
CPU_USAGE_Handle CPU_USAGE_init(void *pMemory, const size_t numBytes)
HAL_AdcData_t gAdcData
contains three current values, three voltage values and one DC buss value
Definition: proj_lab11b.c:109
_iq EST_getAngle_pu(EST_Handle handle)
static void HAL_disablePwm(HAL_Handle handle)
_iq gAngle_mech_poles
Definition: proj_lab11b.c:208
float_t EST_runPowerWarp(EST_Handle handle, const float_t Id_int_A, const float_t Iq_A)
void FILTER_FO_setInitialConditions(FILTER_FO_Handle handle, const _iq x1, const _iq y1)
static void TRAJ_setMaxValue(TRAJ_Handle handle, const _iq maxValue)
VIB_COMP_Handle VIB_COMP_init(void *pMemory, const size_t numBytes)
static void FW_setDeltas(FW_Handle fwHandle, const _iq delta_inc, const _iq delta_dec)
struct _VIB_COMP_Obj_ * VIB_COMP_Handle
volatile bool gFlag_enableRsOnLine
Definition: proj_lab11b.c:175
PID_Handle PID_init(void *pMemory, const size_t numBytes)
HAL_Handle HAL_init(void *pMemory, const size_t numBytes)
float_t gCpuUsagePercentageAvg
Definition: proj_lab11b.c:170
_iq vib_comp_reserved[400]
Definition: proj_lab11b.c:187
static void CLARKE_run(CLARKE_Handle handle, const MATH_vec3 *pInVec, MATH_vec2 *pOutVec)
void EST_setAngle_pu(EST_Handle handle, const _iq angle_pu)
static uint32_t CPU_USAGE_getMaxDeltaCntObserved(CPU_USAGE_Handle handle)
uint16_t gSpeed_array_index
Definition: proj_lab11b.c:194
#define _IQsqrt(A)
static uint16_t HAL_readPwmCmpA(HAL_Handle handle, const PWM_Number_e pwmNumber)
#define USER_MOTOR_TYPE
Definition: user.h:397
_iq gTorque_Ls_Id_Iq_pu_to_Nm_sf
Definition: proj_lab11b.c:159
#define USER_VOLTAGE_FILTER_POLE_rps
Defines the analog voltage filter pole location, rad/s.
Definition: user.h:305
CLARKE_Obj clarke_I
the current Clarke transform object
Definition: proj_lab11b.c:67
#define USER_NUM_VOLTAGE_SENSORS
Defines the number of voltage (phase) sensors.
Definition: user.h:120
void EST_setLs_q_pu(EST_Handle handle, const _iq Ls_q_pu)
uint_least32_t ctrlFreq_Hz
#define USER_IQ_FULL_SCALE_CURRENT_A
Defines the full scale current for the IQ variables, A.
Definition: user.h:102
float_t MagnCurr_A
Definition: main.h:167
HAL_Obj hal
static void HAL_setTrigger(HAL_Handle handle, const SVGENCURRENT_IgnoreShunt_e ignoreShunt, const int16_t minwidth, const int16_t cmpOffset)
_iq getAbsMechAngle(_iq *pAngle_mech_poles, _iq *pAngle_z1_pu, const _iq angle_pu)
Calculates mechanical angle from electrical angle.
Definition: proj_lab11b.c:1239
SVGEN_Handle svgenHandle
the handle for the space vector generator
Definition: proj_lab11b.c:93
static void PID_run_spd(PID_Handle handle, const _iq refValue, const _iq fbackValue, _iq *pOutValue)
CTRL_Handle ctrlHandle
The controller handle.
_iq EST_getRsOnLineId_pu(EST_Handle handle)
static void CPU_USAGE_updateCnts(CPU_USAGE_Handle handle, const uint32_t cnt)
bool Flag_enableSys
Definition: main.h:136
_iq Id_A
Definition: main.h:194
volatile bool gFlag_enableOutput
Definition: proj_lab11b.c:216
void runCurrentReconstruction(void)
Definition: proj_lab11b.c:929
FW_Handle fwHandle
Definition: proj_lab11b.c:77
CLARKE_Obj clarke_V
the voltage Clarke transform object
Definition: proj_lab11b.c:70
static uint32_t CPU_USAGE_getMinDeltaCntObserved(CPU_USAGE_Handle handle)
#define MATH_ONE_OVER_SQRT_THREE
static uint32_t FW_getCounter(FW_Handle fwHandle)
bool Flag_enableRsRecalc
Definition: main.h:141
_iq gTorque_Flux_Iq_pu_to_Nm_sf
Definition: proj_lab11b.c:161
static void HAL_initIntVectorTable(HAL_Handle handle)
_iq VIB_COMP_run(VIB_COMP_Handle handle, const _iq angle_mech_pu, const _iq Iq_in_pu)
void FILTER_FO_setDenCoeffs(FILTER_FO_Handle handle, const _iq a1)
IPARK_Handle iparkHandle
the handle for the inverse Park transform
Definition: proj_lab11b.c:84
SVGENCURRENT_Handle SVGENCURRENT_init(void *pMemory, const size_t numBytes)
static void FW_setNumIsrTicksPerFwTick(FW_Handle fwHandle, const uint32_t numIsrTicksPerFwTick)
_iq getAbsElecAngle(const _iq angle_pu)
Calculates the absolute electrical angle.
Definition: proj_lab11b.c:1217
#define USER_MOTOR_Ls_q
Definition: user.h:402
static uint16_t HAL_readPwmPeriod(HAL_Handle handle, const PWM_Number_e pwmNumber)
bool EST_updateState(EST_Handle handle, const _iq Id_target_pu)
float_t EST_getRr_Ohm(EST_Handle handle)
MATH_vec2 gIdq_ref_pu
contains the Id and Iq references
Definition: proj_lab11b.c:115
_iq gSpeed_delta_krpm
Definition: proj_lab11b.c:200
void HAL_setParams(HAL_Handle handle, const USER_Params *pUserParams)
static void CPU_USAGE_run(CPU_USAGE_Handle handle)
void EST_setRsOnLineId_pu(EST_Handle handle, const _iq Id_pu)
void HAL_readDrvData(HAL_Handle handle, DRV_SPI_8301_Vars_t *Spi_8301_Vars)
void EST_setRsOnLineFilterParams(EST_Handle handle, const EST_RsOnLineFilterType_e filterType, const _iq filter_0_b0, const _iq filter_0_a1, const _iq filter_0_y1, const _iq filter_1_b0, const _iq filter_1_a1, const _iq filter_1_y1)
volatile _iq gRsOnLineFreq_Hz
Definition: proj_lab11b.c:179
bool Flag_enableFieldWeakening
Definition: main.h:140
static void TRAJ_run(TRAJ_Handle handle)
MATH_vec3 gOffsets_I_pu
contains the offsets for the current feedback
Definition: proj_lab11b.c:111
#define USER_MOTOR_MAX_CURRENT
Definition: user.h:407
void VIB_COMP_setParams(VIB_COMP_Handle handle, const _iq alpha, const int16_t adv_index_delta)
static void FW_setMinMax(FW_Handle fwHandle, const _iq outMin, const _iq outMax)
void EST_getIdq_pu(EST_Handle handle, MATH_vec2 *pIdq_pu)
float _IQ30toF(long A)
_iq gSpeed_hz_to_krpm_sf
Definition: proj_lab11b.c:165
void USER_checkForErrors(USER_Params *pUserParams)
Checks for errors in the user parameter values.
void updateCPUusage(void)
Updates CPU usage.
Definition: proj_lab11b.c:1193
SVGEN_Handle SVGEN_init(void *pMemory, const size_t numBytes)
void EST_setEstParams(EST_Handle handle, USER_Params *pUserParams)
_iq Flux_Wb
Definition: main.h:164
#define GLOBAL_Q
_iq USER_computeTorque_Ls_Id_Iq_pu_to_Nm_sf(void)
Computes the scale factor needed to convert from torque created by Ld, Lq, Id and Iq...
PID_Obj pid[3]
three handles for PID controllers 0 - Speed, 1 - Id, 2 - Iq
Definition: proj_lab11b.c:80
_iq USER_computeFlux_pu_to_Wb_sf(void)
Computes the scale factor needed to convert from per unit to Wb.
void EST_setLs_d_pu(EST_Handle handle, const _iq Ls_d_pu)
float float_t
volatile MOTOR_Vars_t gMotorVars
the global motor variables that are defined in main.h and used for display in the debugger's watch wi...
Definition: proj_lab11b.c:126
SVGENCURRENT_Obj svgencurrent
Definition: proj_lab11b.c:90
static void PID_run(PID_Handle handle, const _iq refValue, const _iq fbackValue, _iq *pOutValue)
static void TRAJ_setMinValue(TRAJ_Handle handle, const _iq minValue)
void EST_run(EST_Handle handle, const MATH_vec2 *pIab_pu, const MATH_vec2 *pVab_pu, const _iq dcBus_pu, const _iq speed_ref_pu)
#define USER_NUM_PWM_TICKS_PER_ISR_TICK
DECIMATION.
Definition: user.h:184
void VIB_COMP_setFlag_enableOutput(VIB_COMP_Handle handle, const bool state)
IPARK_Obj ipark
the inverse Park transform object
Definition: proj_lab11b.c:85