instaspin_foc
proj_lab10a.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2012, 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 
52 #ifdef FLASH
53 #pragma CODE_SECTION(mainISR,"ramfuncs");
54 #endif
55 
56 // Include header files used in the main function
57 
58 
59 // **************************************************************************
60 // the defines
61 
62 #define LED_BLINK_FREQ_Hz 5
63 
64 
65 // **************************************************************************
66 // the globals
67 
68 uint_least16_t gCounter_updateGlobals = 0;
69 
71 
73 
74 #ifdef F2802xF
75 #pragma DATA_SECTION(halHandle,"rom_accessed_data");
76 #endif
78 
79 #ifdef F2802xF
80 #pragma DATA_SECTION(gUserParams,"rom_accessed_data");
81 #endif
83 
84 HAL_PwmData_t gPwmData = {_IQ(0.0), _IQ(0.0), _IQ(0.0)};
85 
87 
89 
90 #ifdef FAST_ROM_V1p6
92 #else
93 #ifdef F2802xF
94 #pragma DATA_SECTION(ctrl,"rom_accessed_data");
95 #endif
96 CTRL_Obj ctrl; //v1p7 format
97 #endif
98 
99 uint16_t gLEDcnt = 0;
100 
102 
103 
104 #ifdef FLASH
105 // Used for running BackGround in flash, and ISR in RAM
106 extern uint16_t *RamfuncsLoadStart, *RamfuncsLoadEnd, *RamfuncsRunStart;
107 
108 #ifdef F2802xF
109 extern uint16_t *econst_start, *econst_end, *econst_ram_load;
110 extern uint16_t *switch_start, *switch_end, *switch_ram_load;
111 #endif
112 
113 #endif
114 
117 
118 // set the offset, default value of 1 microsecond
119 int16_t gCmpOffset = (int16_t)(1.0 * USER_SYSTEM_FREQ_MHz);
120 
121 MATH_vec3 gIavg = {_IQ(0.0), _IQ(0.0), _IQ(0.0)};
122 uint16_t gIavg_shift = 1;
123 
124 
125 #ifdef DRV8301_SPI
126 // Watch window interface to the 8301 SPI
127 DRV_SPI_8301_Vars_t gDrvSpi8301Vars;
128 #endif
129 
130 #ifdef DRV8305_SPI
131 // Watch window interface to the 8305 SPI
132 DRV_SPI_8305_Vars_t gDrvSpi8305Vars;
133 #endif
134 
136 
138 
140 
142 
143 // **************************************************************************
144 // the functions
145 
146 void main(void)
147 {
148  uint_least8_t estNumber = 0;
149 
150 #ifdef FAST_ROM_V1p6
151  uint_least8_t ctrlNumber = 0;
152 #endif
153 
154  // Only used if running from FLASH
155  // Note that the variable FLASH is defined by the project
156  #ifdef FLASH
157  // Copy time critical code and Flash setup code to RAM
158  // The RamfuncsLoadStart, RamfuncsLoadEnd, and RamfuncsRunStart
159  // symbols are created by the linker. Refer to the linker files.
160  memCopy((uint16_t *)&RamfuncsLoadStart,(uint16_t *)&RamfuncsLoadEnd,(uint16_t *)&RamfuncsRunStart);
161 
162  #ifdef F2802xF
163  //copy .econst to unsecure RAM
164  if(*econst_end - *econst_start)
165  {
166  memCopy((uint16_t *)&econst_start,(uint16_t *)&econst_end,(uint16_t *)&econst_ram_load);
167  }
168 
169  //copy .switch ot unsecure RAM
170  if(*switch_end - *switch_start)
171  {
172  memCopy((uint16_t *)&switch_start,(uint16_t *)&switch_end,(uint16_t *)&switch_ram_load);
173  }
174  #endif
175 
176  #endif
177 
178  // initialize the hardware abstraction layer
179  halHandle = HAL_init(&hal,sizeof(hal));
180 
181 
182  // check for errors in user parameters
183  USER_checkForErrors(&gUserParams);
184 
185 
186  // store user parameter error in global variable
187  gMotorVars.UserErrorCode = USER_getErrorCode(&gUserParams);
188 
189 
190  // do not allow code execution if there is a user parameter error
191  if(gMotorVars.UserErrorCode != USER_ErrorCode_NoError)
192  {
193  for(;;)
194  {
195  gMotorVars.Flag_enableSys = false;
196  }
197  }
198 
199 
200  // initialize the user parameters
201  USER_setParams(&gUserParams);
202 
203 
204  // set the hardware abstraction layer parameters
205  HAL_setParams(halHandle,&gUserParams);
206 
207 
208  // initialize the controller
209 #ifdef FAST_ROM_V1p6
210  ctrlHandle = CTRL_initCtrl(ctrlNumber, estNumber); //v1p6 format (06xF and 06xM devices)
211 #else
212  ctrlHandle = CTRL_initCtrl(estNumber,&ctrl,sizeof(ctrl)); //v1p7 format default
213 #endif
214 
215  {
216  CTRL_Version version;
217 
218  // get the version number
219  CTRL_getVersion(ctrlHandle,&version);
220 
221  gMotorVars.CtrlVersion = version;
222  }
223 
224 
225  // set the default controller parameters
226  CTRL_setParams(ctrlHandle,&gUserParams);
227 
228 
229  // Initialize and setup the 100% SVM generator
230  svgencurrentHandle = SVGENCURRENT_init(&svgencurrent,sizeof(svgencurrent));
231 
232  // setup svgen current
233  {
234  float_t minWidth_microseconds = 2.0;
235  uint16_t minWidth_counts = (uint16_t)(minWidth_microseconds * USER_SYSTEM_FREQ_MHz);
236 
237  SVGENCURRENT_setMinWidth(svgencurrentHandle, minWidth_counts);
238  SVGENCURRENT_setIgnoreShunt(svgencurrentHandle, use_all);
239  }
240 
241 
242  // set overmodulation to maximum value
243  gMotorVars.OverModulation = _IQ(MATH_TWO_OVER_THREE);
244 
245 
246  // setup faults
247  HAL_setupFaults(halHandle);
248 
249 
250  // initialize the interrupt vector table
251  HAL_initIntVectorTable(halHandle);
252 
253 
254  // enable the ADC interrupts
255  HAL_enableAdcInts(halHandle);
256 
257 
258  // enable global interrupts
259  HAL_enableGlobalInts(halHandle);
260 
261 
262  // enable debug interrupts
263  HAL_enableDebugInt(halHandle);
264 
265 
266  // disable the PWM
267  HAL_disablePwm(halHandle);
268 
269 
270 #ifdef DRV8301_SPI
271  // turn on the DRV8301 if present
272  HAL_enableDrv(halHandle);
273  // initialize the DRV8301 interface
274  HAL_setupDrvSpi(halHandle,&gDrvSpi8301Vars);
275 #endif
276 
277 #ifdef DRV8305_SPI
278  // turn on the DRV8305 if present
279  HAL_enableDrv(halHandle);
280  // initialize the DRV8305 interface
281  HAL_setupDrvSpi(halHandle,&gDrvSpi8305Vars);
282 #endif
283 
284 
285  // enable DC bus compensation
286  CTRL_setFlag_enableDcBusComp(ctrlHandle, true);
287 
288 
289  // compute scaling factors for flux and torque calculations
290  gFlux_pu_to_Wb_sf = USER_computeFlux_pu_to_Wb_sf();
291  gFlux_pu_to_VpHz_sf = USER_computeFlux_pu_to_VpHz_sf();
292  gTorque_Ls_Id_Iq_pu_to_Nm_sf = USER_computeTorque_Ls_Id_Iq_pu_to_Nm_sf();
293  gTorque_Flux_Iq_pu_to_Nm_sf = USER_computeTorque_Flux_Iq_pu_to_Nm_sf();
294 
295 
296  for(;;)
297  {
298  // Waiting for enable system flag to be set
299  while(!(gMotorVars.Flag_enableSys));
300 
301  // Enable the Library internal PI. Iq is referenced by the speed PI now
302  CTRL_setFlag_enableSpeedCtrl(ctrlHandle, true);
303 
304  // loop while the enable system flag is true
305  while(gMotorVars.Flag_enableSys)
306  {
307  CTRL_Obj *obj = (CTRL_Obj *)ctrlHandle;
308 
309  // increment counters
311 
312  // enable/disable the use of motor parameters being loaded from user.h
314 
315  // enable/disable Rs recalibration during motor startup
317 
318  // enable/disable automatic calculation of bias values
319  CTRL_setFlag_enableOffset(ctrlHandle,gMotorVars.Flag_enableOffsetcalc);
320 
321 
322  if(CTRL_isError(ctrlHandle))
323  {
324  // set the enable controller flag to false
325  CTRL_setFlag_enableCtrl(ctrlHandle,false);
326 
327  // set the enable system flag to false
328  gMotorVars.Flag_enableSys = false;
329 
330  // disable the PWM
331  HAL_disablePwm(halHandle);
332  }
333  else
334  {
335  // update the controller state
336  bool flag_ctrlStateChanged = CTRL_updateState(ctrlHandle);
337 
338  // enable or disable the control
339  CTRL_setFlag_enableCtrl(ctrlHandle, gMotorVars.Flag_Run_Identify);
340 
341  if(flag_ctrlStateChanged)
342  {
343  CTRL_State_e ctrlState = CTRL_getState(ctrlHandle);
344 
345  if(ctrlState == CTRL_State_OffLine)
346  {
347  // enable the PWM
348  HAL_enablePwm(halHandle);
349  }
350  else if(ctrlState == CTRL_State_OnLine)
351  {
352  if(gMotorVars.Flag_enableOffsetcalc == true)
353  {
354  // update the ADC bias values
355  HAL_updateAdcBias(halHandle);
356  }
357  else
358  {
359  // set the current bias
360  HAL_setBias(halHandle,HAL_SensorType_Current,0,_IQ(I_A_offset));
361  HAL_setBias(halHandle,HAL_SensorType_Current,1,_IQ(I_B_offset));
362  HAL_setBias(halHandle,HAL_SensorType_Current,2,_IQ(I_C_offset));
363 
364  // set the voltage bias
365  HAL_setBias(halHandle,HAL_SensorType_Voltage,0,_IQ(V_A_offset));
366  HAL_setBias(halHandle,HAL_SensorType_Voltage,1,_IQ(V_B_offset));
367  HAL_setBias(halHandle,HAL_SensorType_Voltage,2,_IQ(V_C_offset));
368  }
369 
370  // Return the bias value for currents
371  gMotorVars.I_bias.value[0] = HAL_getBias(halHandle,HAL_SensorType_Current,0);
372  gMotorVars.I_bias.value[1] = HAL_getBias(halHandle,HAL_SensorType_Current,1);
373  gMotorVars.I_bias.value[2] = HAL_getBias(halHandle,HAL_SensorType_Current,2);
374 
375  // Return the bias value for voltages
376  gMotorVars.V_bias.value[0] = HAL_getBias(halHandle,HAL_SensorType_Voltage,0);
377  gMotorVars.V_bias.value[1] = HAL_getBias(halHandle,HAL_SensorType_Voltage,1);
378  gMotorVars.V_bias.value[2] = HAL_getBias(halHandle,HAL_SensorType_Voltage,2);
379 
380  // enable the PWM
381  HAL_enablePwm(halHandle);
382  }
383  else if(ctrlState == CTRL_State_Idle)
384  {
385  // disable the PWM
386  HAL_disablePwm(halHandle);
387  gMotorVars.Flag_Run_Identify = false;
388  }
389 
390  if((CTRL_getFlag_enableUserMotorParams(ctrlHandle) == true) &&
391  (ctrlState > CTRL_State_Idle) &&
392  (gMotorVars.CtrlVersion.minor == 6))
393  {
394  // call this function to fix 1p6
395  USER_softwareUpdate1p6(ctrlHandle);
396  }
397 
398  }
399  }
400 
401 
403  {
404  _iq Id_squared_pu = _IQmpy(CTRL_getId_ref_pu(ctrlHandle),CTRL_getId_ref_pu(ctrlHandle));
405 
406 
407  //Set the maximum current controller output for the Iq and Id current controllers to enable
408  //over-modulation.
409  //An input into the SVM above 1/SQRT(3) = 0.5774 is in the over-modulation region. An input of 0.5774 is where
410  //the crest of the sinewave touches the 100% duty cycle. At an input of 2/3, the SVM generator
411  //produces a trapezoidal waveform touching every corner of the hexagon
412  CTRL_setMaxVsMag_pu(ctrlHandle,gMotorVars.OverModulation);
413 
414  // set the current ramp
416  gMotorVars.Flag_MotorIdentified = true;
417 
418  // set the speed reference
419  CTRL_setSpd_ref_krpm(ctrlHandle,gMotorVars.SpeedRef_krpm);
420 
421  // set the speed acceleration
423 
424  // set the Id reference
425  CTRL_setId_ref_pu(ctrlHandle, _IQmpy(gMotorVars.IdRef_A, _IQ(1.0/USER_IQ_FULL_SCALE_CURRENT_A)));
426 
428  {
430 
431  USER_calcPIgains(ctrlHandle);
432 
433  // initialize the watch window kp and ki current values with pre-calculated values
434  gMotorVars.Kp_Idq = CTRL_getKp(ctrlHandle,CTRL_Type_PID_Id);
435  gMotorVars.Ki_Idq = CTRL_getKi(ctrlHandle,CTRL_Type_PID_Id);
436  }
437 
438  }
439  else
440  {
442 
443  // initialize the watch window kp and ki values with pre-calculated values
444  gMotorVars.Kp_spd = CTRL_getKp(ctrlHandle,CTRL_Type_PID_spd);
445  gMotorVars.Ki_spd = CTRL_getKi(ctrlHandle,CTRL_Type_PID_spd);
446 
447 
448  // the estimator sets the maximum current slope during identification
450  }
451 
452 
453  // when appropriate, update the global variables
455  {
456  // reset the counter
458 
459  updateGlobalVariables_motor(ctrlHandle);
460  }
461 
462 
463  // update Kp and Ki gains
464  updateKpKiGains(ctrlHandle);
465 
466  // enable/disable the forced angle
468 
469  // enable or disable power warp
470  CTRL_setFlag_enablePowerWarp(ctrlHandle,gMotorVars.Flag_enablePowerWarp);
471 
472 #ifdef DRV8301_SPI
473  HAL_writeDrvData(halHandle,&gDrvSpi8301Vars);
474 
475  HAL_readDrvData(halHandle,&gDrvSpi8301Vars);
476 #endif
477 #ifdef DRV8305_SPI
478  HAL_writeDrvData(halHandle,&gDrvSpi8305Vars);
479 
480  HAL_readDrvData(halHandle,&gDrvSpi8305Vars);
481 #endif
482  } // end of while(gFlag_enableSys) loop
483 
484 
485  // disable the PWM
486  HAL_disablePwm(halHandle);
487 
488  // set the default controller parameters (Reset the control to re-identify the motor)
489  CTRL_setParams(ctrlHandle,&gUserParams);
490  gMotorVars.Flag_Run_Identify = false;
491 
492  } // end of for(;;) loop
493 
494 } // end of main() function
495 
496 
497 interrupt void mainISR(void)
498 {
499  SVGENCURRENT_IgnoreShunt_e ignoreShuntThisCycle = SVGENCURRENT_getIgnoreShunt(svgencurrentHandle);
500 
501  // toggle status LED
502  if(gLEDcnt++ > (uint_least32_t)(USER_ISR_FREQ_Hz / LED_BLINK_FREQ_Hz))
503  {
504  HAL_toggleLed(halHandle,(GPIO_Number_e)HAL_Gpio_LED2);
505  gLEDcnt = 0;
506  }
507 
508 
509  // acknowledge the ADC interrupt
510  HAL_acqAdcInt(halHandle,ADC_IntNumber_1);
511 
512 
513  // convert the ADC data
514  HAL_readAdcData(halHandle,&gAdcData);
515 
516 
517  // run the current reconstruction algorithm
518  SVGENCURRENT_RunRegenCurrent(svgencurrentHandle, (MATH_vec3 *)(gAdcData.I.value));
519 
520  gIavg.value[0] += (gAdcData.I.value[0] - gIavg.value[0])>>gIavg_shift;
521  gIavg.value[1] += (gAdcData.I.value[1] - gIavg.value[1])>>gIavg_shift;
522  gIavg.value[2] += (gAdcData.I.value[2] - gIavg.value[2])>>gIavg_shift;
523 
524  if(ignoreShuntThisCycle == ignore_ab)
525  {
526  gAdcData.I.value[0] = gIavg.value[0];
527  gAdcData.I.value[1] = gIavg.value[1];
528  }
529  else if(ignoreShuntThisCycle == ignore_ac)
530  {
531  gAdcData.I.value[0] = gIavg.value[0];
532  gAdcData.I.value[2] = gIavg.value[2];
533  }
534  else if(ignoreShuntThisCycle == ignore_bc)
535  {
536  gAdcData.I.value[1] = gIavg.value[1];
537  gAdcData.I.value[2] = gIavg.value[2];
538  }
539 
540 
541  // run the controller
542  CTRL_run(ctrlHandle,halHandle,&gAdcData,&gPwmData);
543 
544 
545  // write the PWM compare values
546  HAL_writePwmData(halHandle,&gPwmData);
547 
548 
549  // run the current ignore algorithm
550  {
551  uint16_t cmp1 = HAL_readPwmCmpA(halHandle,PWM_Number_1);
552  uint16_t cmp2 = HAL_readPwmCmpA(halHandle,PWM_Number_2);
553  uint16_t cmp3 = HAL_readPwmCmpA(halHandle,PWM_Number_3);
554  uint16_t cmpM1 = HAL_readPwmCmpAM(halHandle,PWM_Number_1);
555  uint16_t cmpM2 = HAL_readPwmCmpAM(halHandle,PWM_Number_2);
556  uint16_t cmpM3 = HAL_readPwmCmpAM(halHandle,PWM_Number_3);
557 
558  // run the current ignore algorithm
559  SVGENCURRENT_RunIgnoreShunt(svgencurrentHandle,cmp1,cmp2,cmp3,cmpM1,cmpM2,cmpM3);
560  }
561 
562  {
563  int16_t minwidth = SVGENCURRENT_getMinWidth(svgencurrentHandle);
564  SVGENCURRENT_IgnoreShunt_e ignoreShuntNextCycle = SVGENCURRENT_getIgnoreShunt(svgencurrentHandle);
565 
566  // Set trigger point in the middle of the low side pulse
567  HAL_setTrigger(halHandle,ignoreShuntNextCycle,minwidth,gCmpOffset);
568  }
569 
570  // setup the controller
571  CTRL_setup(ctrlHandle);
572 
573 
574  return;
575 } // end of mainISR() function
576 
577 
579 {
580  CTRL_Obj *obj = (CTRL_Obj *)handle;
581 
582  // get the speed estimate
583  gMotorVars.Speed_krpm = EST_getSpeed_krpm(obj->estHandle);
584 
585  // get the real time speed reference coming out of the speed trajectory generator
587 
588  // get the torque estimate
589  gMotorVars.Torque_Nm = USER_computeTorque_Nm(handle, gTorque_Flux_Iq_pu_to_Nm_sf, gTorque_Ls_Id_Iq_pu_to_Nm_sf);
590 
591  // get the magnetizing current
592  gMotorVars.MagnCurr_A = EST_getIdRated(obj->estHandle);
593 
594  // get the rotor resistance
595  gMotorVars.Rr_Ohm = EST_getRr_Ohm(obj->estHandle);
596 
597  // get the stator resistance
598  gMotorVars.Rs_Ohm = EST_getRs_Ohm(obj->estHandle);
599 
600  // get the stator inductance in the direct coordinate direction
601  gMotorVars.Lsd_H = EST_getLs_d_H(obj->estHandle);
602 
603  // get the stator inductance in the quadrature coordinate direction
604  gMotorVars.Lsq_H = EST_getLs_q_H(obj->estHandle);
605 
606  // get the flux in V/Hz in floating point
607  gMotorVars.Flux_VpHz = EST_getFlux_VpHz(obj->estHandle);
608 
609  // get the flux in Wb in fixed point
610  gMotorVars.Flux_Wb = USER_computeFlux(handle, gFlux_pu_to_Wb_sf);
611 
612  // get the controller state
613  gMotorVars.CtrlState = CTRL_getState(handle);
614 
615  // get the estimator state
616  gMotorVars.EstState = EST_getState(obj->estHandle);
617 
618  // read Vd and Vq vectors per units
619  gMotorVars.Vd = CTRL_getVd_out_pu(ctrlHandle);
620  gMotorVars.Vq = CTRL_getVq_out_pu(ctrlHandle);
621 
622  // calculate vector Vs in per units
623  gMotorVars.Vs = _IQsqrt(_IQmpy(gMotorVars.Vd, gMotorVars.Vd) + _IQmpy(gMotorVars.Vq, gMotorVars.Vq));
624 
625  // read Id and Iq vectors in amps
626  gMotorVars.Id_A = _IQmpy(CTRL_getId_in_pu(ctrlHandle), _IQ(USER_IQ_FULL_SCALE_CURRENT_A));
627  gMotorVars.Iq_A = _IQmpy(CTRL_getIq_in_pu(ctrlHandle), _IQ(USER_IQ_FULL_SCALE_CURRENT_A));
628 
629  // calculate vector Is in amps
630  gMotorVars.Is_A = _IQsqrt(_IQmpy(gMotorVars.Id_A, gMotorVars.Id_A) + _IQmpy(gMotorVars.Iq_A, gMotorVars.Iq_A));
631 
632  // Get the DC buss voltage
633  gMotorVars.VdcBus_kV = _IQmpy(gAdcData.dcBus,_IQ(USER_IQ_FULL_SCALE_VOLTAGE_V/1000.0));
634 
635  return;
636 } // end of updateGlobalVariables_motor() function
637 
638 
640 {
641  if((gMotorVars.CtrlState == CTRL_State_OnLine) && (gMotorVars.Flag_MotorIdentified == true) && (Flag_Latch_softwareUpdate == false))
642  {
643  // set the kp and ki speed values from the watch window
644  CTRL_setKp(handle,CTRL_Type_PID_spd,gMotorVars.Kp_spd);
645  CTRL_setKi(handle,CTRL_Type_PID_spd,gMotorVars.Ki_spd);
646 
647  // set the kp and ki current values for Id and Iq from the watch window
648  CTRL_setKp(handle,CTRL_Type_PID_Id,gMotorVars.Kp_Idq);
649  CTRL_setKi(handle,CTRL_Type_PID_Id,gMotorVars.Ki_Idq);
650  CTRL_setKp(handle,CTRL_Type_PID_Iq,gMotorVars.Kp_Idq);
651  CTRL_setKi(handle,CTRL_Type_PID_Iq,gMotorVars.Ki_Idq);
652  }
653 
654  return;
655 } // end of updateKpKiGains() function
656 
657 
659 // end of file
660 
661 
662 
float_t EST_getFlux_VpHz(EST_Handle handle)
#define USER_SYSTEM_FREQ_MHz
CLOCKS & TIMERS.
Definition: user.h:140
uint16_t gLEDcnt
Definition: proj_lab10a.c:99
void EST_setFlag_enableForceAngle(EST_Handle handle, const bool state)
_iq gTorque_Flux_Iq_pu_to_Nm_sf
Definition: proj_lab10a.c:141
#define MAX_ACCEL_KRPMPS_SF
Defines the speed acceleration scale factor.
Definition: float/main.h:37
void HAL_enableGlobalInts(HAL_Handle handle)
void HAL_enableAdcInts(HAL_Handle handle)
USER_ErrorCode_e UserErrorCode
Definition: main.h:150
bool Flag_MotorIdentified
Definition: main.h:138
void CTRL_run(CTRL_Handle handle, HAL_Handle halHandle, const HAL_AdcData_t *pAdcData, HAL_PwmData_t *pPwmData)
float_t Lsq_H
Definition: main.h:172
_iq USER_computeFlux_pu_to_VpHz_sf(void)
Computes the scale factor needed to convert from per unit to V/Hz.
#define _IQ(A)
_iq Iq_A
Definition: main.h:195
bool Flag_Run_Identify
Definition: main.h:137
_iq EST_getSpeed_krpm(EST_Handle handle)
static uint16_t HAL_readPwmCmpAM(HAL_Handle handle, const PWM_Number_e pwmNumber)
#define MATH_TWO_OVER_THREE
void USER_calcPIgains(CTRL_Handle handle)
Updates Id and Iq PI gains.
CTRL_State_e CtrlState
Definition: main.h:147
CTRL_Obj ctrl
Defines the CTRL object.
Definition: proj_lab10a.c:96
bool Flag_enablePowerWarp
Definition: main.h:144
static void HAL_writePwmData(HAL_Handle handle, HAL_PwmData_t *pPwmData)
void EST_setMaxCurrentSlope_pu(EST_Handle handle, const _iq maxCurrentSlope_pu)
_iq value[3]
#define NUM_MAIN_TICKS_FOR_GLOBAL_VARIABLE_UPDATE
Defines the number of main iterations before global variables are updated.
Definition: float/main.h:48
_iq CTRL_getId_in_pu(CTRL_Handle handle)
void HAL_writeDrvData(HAL_Handle handle, DRV_SPI_8301_Vars_t *Spi_8301_Vars)
bool CTRL_isError(CTRL_Handle handle)
uint_least16_t gCounter_updateGlobals
A counter that is denotes when to update the global variables.
Definition: proj_lab10a.c:68
void USER_softwareUpdate1p6(CTRL_Handle handle)
Recalculates Inductances with the correct Q Format.
static void HAL_acqAdcInt(HAL_Handle handle, const ADC_IntNumber_e intNumber)
_iq EST_get_pu_to_krpm_sf(EST_Handle handle)
#define LED_BLINK_FREQ_Hz
Definition: proj_lab10a.c:62
#define USER_IQ_FULL_SCALE_VOLTAGE_V
Defines full scale value for the IQ30 variable of Voltage inside the system.
Definition: user.h:88
bool Flag_Latch_softwareUpdate
Definition: proj_lab10a.c:70
void memCopy(uint16_t *srcStartAddr, uint16_t *srcEndAddr, uint16_t *dstAddr)
EST_Handle estHandle
MATH_vec3 gIavg
Definition: proj_lab10a.c:121
void CTRL_setKi(CTRL_Handle handle, const CTRL_Type_e ctrlType, const _iq Ki)
bool Flag_enableForceAngle
Definition: main.h:139
void CTRL_setFlag_enableCtrl(CTRL_Handle handle, const bool state)
void CTRL_setFlag_enableSpeedCtrl(CTRL_Handle handle, const bool state)
HAL_PwmData_t gPwmData
Defines the PWM data.
Definition: proj_lab10a.c:84
#define V_B_offset
Definition: user.h:133
long _iq
_iq gFlux_pu_to_Wb_sf
Definition: proj_lab10a.c:135
_iq OverModulation
Definition: main.h:161
interrupt void mainISR(void)
The main interrupt service (ISR) routine.
Definition: proj_lab10a.c:497
void USER_setParams(USER_Params *pUserParams)
Sets the user parameter values.
EST_State_e EstState
Definition: main.h:148
float_t Rs_Ohm
Definition: main.h:169
#define HAL_toggleLed
#define V_A_offset
ADC voltage offsets for A, B, and C phases.
Definition: user.h:132
#define I_A_offset
ADC current offsets for A, B, and C phases.
Definition: user.h:125
Defines the structures, global initialization, and functions used in MAIN.
void CTRL_setMaxAccel_pu(CTRL_Handle handle, const _iq maxAccel_pu)
SVGENCURRENT_Obj svgencurrent
Definition: proj_lab10a.c:115
_iq USER_computeFlux(CTRL_Handle handle, const _iq sf)
Computes Flux in Wb or V/Hz depending on the scale factor sent as parameter.
void HAL_setupFaults(HAL_Handle handle)
_iq Kp_Idq
Definition: main.h:185
float_t EST_getLs_d_H(EST_Handle handle)
void EST_setFlag_enableRsRecalc(EST_Handle handle, const bool state)
uint16_t gIavg_shift
Definition: proj_lab10a.c:122
_iq CTRL_getIq_in_pu(CTRL_Handle handle)
static void HAL_enablePwm(HAL_Handle handle)
void CTRL_setParams(CTRL_Handle handle, USER_Params *pUserParams)
#define _IQmpy(A, B)
USER_ErrorCode_e USER_getErrorCode(USER_Params *pUserParams)
Gets the error code in the user parameters.
void HAL_enableDrv(HAL_Handle handle)
_iq VdcBus_kV
Definition: main.h:192
_iq Torque_Nm
Definition: main.h:165
_iq SpeedRef_krpm
Definition: main.h:157
_iq IdRef_A
Definition: main.h:154
_iq Ki_spd
Definition: main.h:183
void HAL_setupDrvSpi(HAL_Handle handle, DRV_SPI_8301_Vars_t *Spi_8301_Vars)
float_t EST_getLs_q_H(EST_Handle handle)
bool CTRL_getFlag_enableUserMotorParams(CTRL_Handle handle)
void HAL_enableDebugInt(HAL_Handle handle)
CTRL_State_e CTRL_getState(CTRL_Handle handle)
#define I_C_offset
Definition: user.h:127
SVGENCURRENT_IgnoreShunt_e
float_t Flux_VpHz
Definition: main.h:173
_iq gTorque_Ls_Id_Iq_pu_to_Nm_sf
Definition: proj_lab10a.c:139
bool Flag_enableUserParams
Definition: main.h:142
EST_State_e EST_getState(EST_Handle handle)
bool Flag_enableOffsetcalc
Definition: main.h:143
#define I_B_offset
Definition: user.h:126
_iq CTRL_getKp(CTRL_Handle handle, const CTRL_Type_e ctrlType)
void CTRL_setup(CTRL_Handle handle)
MATH_vec3 V_bias
Definition: main.h:199
#define MOTOR_Vars_INIT
Initialization values of global variables.
Definition: main.h:76
_iq CTRL_getVd_out_pu(CTRL_Handle handle)
CTRL_State_e
#define V_C_offset
Definition: user.h:134
_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
float_t EST_getIdRated(EST_Handle handle)
_iq Is_A
Definition: main.h:196
_iq Ki_Idq
Definition: main.h:186
_iq gMaxCurrentSlope
Definition: proj_lab10a.c:88
USER_Params gUserParams
The user parameters.
Definition: proj_lab10a.c:82
float_t Rr_Ohm
Definition: main.h:168
float_t EST_getRs_Ohm(EST_Handle handle)
CTRL_Handle CTRL_initCtrl(const uint_least8_t estNumber, void *pMemory, const size_t numBytes)
void CTRL_setMaxVsMag_pu(CTRL_Handle handle, const _iq maxVsMag)
float_t Lsd_H
Definition: main.h:171
_iq CTRL_getId_ref_pu(CTRL_Handle handle)
void CTRL_setFlag_enablePowerWarp(CTRL_Handle handle, const bool state)
_iq gFlux_pu_to_VpHz_sf
Definition: proj_lab10a.c:137
#define USER_ISR_FREQ_Hz
Defines the Interrupt Service Routine (ISR) frequency, Hz.
Definition: user.h:173
SVGENCURRENT_Handle svgencurrentHandle
Definition: proj_lab10a.c:116
bool CTRL_updateState(CTRL_Handle handle)
_iq CTRL_getKi(CTRL_Handle handle, const CTRL_Type_e ctrlType)
static void HAL_disablePwm(HAL_Handle handle)
volatile MOTOR_Vars_t gMotorVars
Definition: proj_lab10a.c:101
bool EST_isMotorIdentified(EST_Handle handle)
_iq USER_computeTorque_Nm(CTRL_Handle handle, const _iq torque_Flux_sf, const _iq torque_Ls_sf)
Computes Torque in Nm.
void CTRL_setFlag_enableOffset(CTRL_Handle handle, const bool state)
HAL_Handle HAL_init(void *pMemory, const size_t numBytes)
HAL_Handle halHandle
The hal handle.
Definition: proj_lab10a.c:77
void CTRL_setFlag_enableDcBusComp(CTRL_Handle handle, const bool state)
#define _IQsqrt(A)
static uint16_t HAL_readPwmCmpA(HAL_Handle handle, const PWM_Number_e pwmNumber)
_iq EST_getMaxCurrentSlope_pu(EST_Handle handle)
void CTRL_setFlag_enableUserMotorParams(CTRL_Handle handle, const bool state)
_iq SpeedTraj_krpm
Definition: main.h:158
int16_t gCmpOffset
Definition: proj_lab10a.c:119
#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
static void HAL_readAdcData(HAL_Handle handle, HAL_AdcData_t *pAdcData)
HAL_Obj hal
static void HAL_setTrigger(HAL_Handle handle, const SVGENCURRENT_IgnoreShunt_e ignoreShunt, const int16_t minwidth, const int16_t cmpOffset)
static _iq HAL_getBias(HAL_Handle handle, const HAL_SensorType_e sensorType, uint_least8_t sensorNumber)
bool Flag_enableSys
Definition: main.h:136
_iq Id_A
Definition: main.h:194
void updateKpKiGains(CTRL_Handle handle)
Updates Kp and Ki gains in the controller object.
Definition: proj_lab10a.c:639
_iq MaxAccel_krpmps
Definition: main.h:159
_iq CTRL_getSpd_int_ref_pu(CTRL_Handle handle)
void CTRL_setId_ref_pu(CTRL_Handle handle, const _iq Id_ref_pu)
MATH_vec3 I_bias
Definition: main.h:198
bool Flag_enableRsRecalc
Definition: main.h:141
HAL_AdcData_t gAdcData
Defines the ADC data.
Definition: proj_lab10a.c:86
static void HAL_initIntVectorTable(HAL_Handle handle)
_iq Kp_spd
Definition: main.h:182
static void HAL_setBias(HAL_Handle handle, const HAL_SensorType_e sensorType, uint_least8_t sensorNumber, const _iq bias)
SVGENCURRENT_Handle SVGENCURRENT_init(void *pMemory, const size_t numBytes)
void updateGlobalVariables_motor(CTRL_Handle handle)
Updates the global motor variables.
Definition: proj_lab10a.c:578
void main(void)
Definition: proj_lab10a.c:146
static void HAL_updateAdcBias(HAL_Handle handle)
void CTRL_getVersion(CTRL_Handle handle, CTRL_Version *pVersion)
uint16_t minor
CTRL_Version CtrlVersion
Definition: main.h:152
float_t EST_getRr_Ohm(EST_Handle handle)
void HAL_setParams(HAL_Handle handle, const USER_Params *pUserParams)
void HAL_readDrvData(HAL_Handle handle, DRV_SPI_8301_Vars_t *Spi_8301_Vars)
void USER_checkForErrors(USER_Params *pUserParams)
Checks for errors in the user parameter values.
CTRL_Obj * controller_obj
Definition: proj_lab10c.c:84
_iq CTRL_getVq_out_pu(CTRL_Handle handle)
_iq Flux_Wb
Definition: main.h:164
GPIO_Number_e
_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...
void CTRL_setSpd_ref_krpm(CTRL_Handle handle, const _iq spd_ref_krpm)
_iq USER_computeFlux_pu_to_Wb_sf(void)
Computes the scale factor needed to convert from per unit to Wb.
void CTRL_setKp(CTRL_Handle handle, const CTRL_Type_e ctrlType, const _iq Kp)
float float_t
CTRL_Handle ctrlHandle
The controller handle.
Definition: proj_lab10a.c:72