instaspin_foc
proj_lab20.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 // due to memory limitations, when building for F2802xF run mainISR from Flash
53 #if defined(FLASH) && !defined(F2802xF)
54 #pragma CODE_SECTION(mainISR,"ramfuncs");
55 #endif
56 
57 // Include header files used in the main function
58 
59 
60 // **************************************************************************
61 // the defines
62 
63 
64 #define LED_BLINK_FREQ_Hz 5
65 
66 
67 // **************************************************************************
68 // the globals
69 
70 uint_least16_t gCounter_updateGlobals = 0;
71 
73 
77 
80 
83 
85 
88 
91 
94 
95 #ifdef F2802xF
96 #pragma DATA_SECTION(halHandle,"rom_accessed_data");
97 #endif
99 
100 #ifdef F2802xF
101 #pragma DATA_SECTION(gUserParams,"rom_accessed_data");
102 #endif
104 
105 HAL_PwmData_t gPwmData = {_IQ(0.0), _IQ(0.0), _IQ(0.0)};
106 
108 
110 
111 #ifdef FAST_ROM_V1p6
113 #else
114 #ifdef F2802xF
115 #pragma DATA_SECTION(ctrl,"rom_accessed_data");
116 #endif
117 CTRL_Obj ctrl; //v1p7 format
118 #endif
119 
120 uint16_t gLEDcnt = 0;
121 
123 
124 #ifdef FLASH
125 // Used for running BackGround in flash, and ISR in RAM
126 extern uint16_t *RamfuncsLoadStart, *RamfuncsLoadEnd, *RamfuncsRunStart;
127 
128 #ifdef F2802xF
129 extern uint16_t *econst_start, *econst_end, *econst_ram_load;
130 extern uint16_t *switch_start, *switch_end, *switch_ram_load;
131 #endif
132 
133 #endif
134 
135 
136 #ifdef DRV8301_SPI
137 // Watch window interface to the 8301 SPI
138 DRV_SPI_8301_Vars_t gDrvSpi8301Vars;
139 #endif
140 
141 #ifdef DRV8305_SPI
142 // Watch window interface to the 8305 SPI
143 DRV_SPI_8305_Vars_t gDrvSpi8305Vars;
144 #endif
145 
147 
149 
151 
153 
154 // **************************************************************************
155 // the functions
156 
157 void main(void)
158 {
159  uint_least8_t estNumber = 0;
160 
161 #ifdef FAST_ROM_V1p6
162  uint_least8_t ctrlNumber = 0;
163 #endif
164 
165  // Only used if running from FLASH
166  // Note that the variable FLASH is defined by the project
167  #ifdef FLASH
168  // Copy time critical code and Flash setup code to RAM
169  // The RamfuncsLoadStart, RamfuncsLoadEnd, and RamfuncsRunStart
170  // symbols are created by the linker. Refer to the linker files.
171  memCopy((uint16_t *)&RamfuncsLoadStart,(uint16_t *)&RamfuncsLoadEnd,(uint16_t *)&RamfuncsRunStart);
172 
173  #ifdef F2802xF
174  //copy .econst to unsecure RAM
175  if(*econst_end - *econst_start)
176  {
177  memCopy((uint16_t *)&econst_start,(uint16_t *)&econst_end,(uint16_t *)&econst_ram_load);
178  }
179 
180  //copy .switch ot unsecure RAM
181  if(*switch_end - *switch_start)
182  {
183  memCopy((uint16_t *)&switch_start,(uint16_t *)&switch_end,(uint16_t *)&switch_ram_load);
184  }
185  #endif
186 
187  #endif
188 
189  // initialize the hardware abstraction layer
190  halHandle = HAL_init(&hal,sizeof(hal));
191 
192 
193  // check for errors in user parameters
194  USER_checkForErrors(&gUserParams);
195 
196 
197  // store user parameter error in global variable
198  gMotorVars.UserErrorCode = USER_getErrorCode(&gUserParams);
199 
200 
201  // do not allow code execution if there is a user parameter error
202  if(gMotorVars.UserErrorCode != USER_ErrorCode_NoError)
203  {
204  for(;;)
205  {
206  gMotorVars.Flag_enableSys = false;
207  }
208  }
209 
210 
211  // initialize the user parameters
212  USER_setParams(&gUserParams);
213 
214 
215  // set the hardware abstraction layer parameters
216  HAL_setParams(halHandle,&gUserParams);
217 
218 
219  // initialize the controller
220 #ifdef FAST_ROM_V1p6
221  ctrlHandle = CTRL_initCtrl(ctrlNumber, estNumber); //v1p6 format (06xF and 06xM devices)
222  controller_obj = (CTRL_Obj *)ctrlHandle;
223 #else
224  ctrlHandle = CTRL_initCtrl(estNumber,&ctrl,sizeof(ctrl)); //v1p7 format default
225 #endif
226 
227 
228  {
229  CTRL_Version version;
230 
231  // get the version number
232  CTRL_getVersion(ctrlHandle,&version);
233 
234  gMotorVars.CtrlVersion = version;
235  }
236 
237 
238  // set the default controller parameters
239  CTRL_setParams(ctrlHandle,&gUserParams);
240 
241 
242  // initialize the Clarke modules
243  clarkeHandle_I = CLARKE_init(&clarke_I,sizeof(clarke_I));
244  clarkeHandle_V = CLARKE_init(&clarke_V,sizeof(clarke_V));
245 
246 
247  // set the number of current sensors
248  setupClarke_I(clarkeHandle_I,gUserParams.numCurrentSensors);
249 
250 
251  // set the number of voltage sensors
252  setupClarke_V(clarkeHandle_V,gUserParams.numVoltageSensors);
253 
254 
255 #ifdef FAST_ROM_V1p6
256  estHandle = controller_obj->estHandle;
257 #else
258  estHandle = ctrl.estHandle;
259 #endif
260 
261 
262  // initialize the inverse Park module
263  iparkHandle = IPARK_init(&ipark,sizeof(ipark));
264 
265 
266  // initialize the Park module
267  parkHandle = PARK_init(&park,sizeof(park));
268 
269 
270  // initialize the space vector generator module
271  svgenHandle = SVGEN_init(&svgen,sizeof(svgen));
272 
273 
274  // setup faults
275  HAL_setupFaults(halHandle);
276 
277 
278  // initialize the interrupt vector table
279  HAL_initIntVectorTable(halHandle);
280 
281 
282  // enable the ADC interrupts
283  HAL_enableAdcInts(halHandle);
284 
285 
286  // enable global interrupts
287  HAL_enableGlobalInts(halHandle);
288 
289 
290  // enable debug interrupts
291  HAL_enableDebugInt(halHandle);
292 
293 
294  // disable the PWM
295  HAL_disablePwm(halHandle);
296 
297 
298 #ifdef DRV8301_SPI
299  // turn on the DRV8301 if present
300  HAL_enableDrv(halHandle);
301  // initialize the DRV8301 interface
302  HAL_setupDrvSpi(halHandle,&gDrvSpi8301Vars);
303 #endif
304 
305 
306 #ifdef DRV8305_SPI
307  // turn on the DRV8305 if present
308  HAL_enableDrv(halHandle);
309  // initialize the DRV8305 interface
310  HAL_setupDrvSpi(halHandle,&gDrvSpi8305Vars);
311 #endif
312 
313 
314  // enable DC bus compensation
315  CTRL_setFlag_enableDcBusComp(ctrlHandle, true);
316 
317 
318  // compute scaling factors for flux and torque calculations
323 
324 
325  for(;;)
326  {
327  // Waiting for enable system flag to be set
328  while(!(gMotorVars.Flag_enableSys));
329 
330  // Enable the Library internal PI. Iq is referenced by the speed PI now
331  CTRL_setFlag_enableSpeedCtrl(ctrlHandle, true);
332 
333  // loop while the enable system flag is true
334  while(gMotorVars.Flag_enableSys)
335  {
336  CTRL_Obj *obj = (CTRL_Obj *)ctrlHandle;
337 
338  // increment counters
340 
341  // enable/disable the use of motor parameters being loaded from user.h
343 
344  // enable/disable Rs recalibration during motor startup
346 
347  // enable/disable automatic calculation of bias values
348  CTRL_setFlag_enableOffset(ctrlHandle,gMotorVars.Flag_enableOffsetcalc);
349 
350 
351  if(CTRL_isError(ctrlHandle))
352  {
353  // set the enable controller flag to false
354  CTRL_setFlag_enableCtrl(ctrlHandle,false);
355 
356  // set the enable system flag to false
357  gMotorVars.Flag_enableSys = false;
358 
359  // disable the PWM
360  HAL_disablePwm(halHandle);
361  }
362  else
363  {
364  // update the controller state
365  bool flag_ctrlStateChanged = CTRL_updateState(ctrlHandle);
366 
367  // enable or disable the control
368  CTRL_setFlag_enableCtrl(ctrlHandle, gMotorVars.Flag_Run_Identify);
369 
370  if(flag_ctrlStateChanged)
371  {
372  CTRL_State_e ctrlState = CTRL_getState(ctrlHandle);
373 
374  if(ctrlState == CTRL_State_OffLine)
375  {
376  // enable the PWM
377  HAL_enablePwm(halHandle);
378  }
379  else if(ctrlState == CTRL_State_OnLine)
380  {
381  if(gMotorVars.Flag_enableOffsetcalc == true)
382  {
383  uint_least16_t cnt;
384 
385  // update the ADC bias values
386  HAL_updateAdcBias(halHandle);
387 
388  // record the current bias
389  for(cnt=0;cnt<3;cnt++)
390  gAdcBiasI.value[cnt] = HAL_getBias(halHandle,HAL_SensorType_Current,cnt);
391 
392  // record the voltage bias
393  for(cnt=0;cnt<3;cnt++)
394  gAdcBiasV.value[cnt] = HAL_getBias(halHandle,HAL_SensorType_Voltage,cnt);
395 
396  gMotorVars.Flag_enableOffsetcalc = false;
397  }
398  else
399  {
400  uint_least16_t cnt;
401 
402  // set the current bias
403  for(cnt=0;cnt<3;cnt++)
404  HAL_setBias(halHandle,HAL_SensorType_Current,cnt,gAdcBiasI.value[cnt]);
405 
406  // set the voltage bias
407  for(cnt=0;cnt<3;cnt++)
408  HAL_setBias(halHandle,HAL_SensorType_Voltage,cnt,gAdcBiasV.value[cnt]);
409  }
410 
411  // enable the PWM
412  HAL_enablePwm(halHandle);
413  }
414  else if(ctrlState == CTRL_State_Idle)
415  {
416  // disable the PWM
417  HAL_disablePwm(halHandle);
418  gMotorVars.Flag_Run_Identify = false;
419  }
420 
421  if((CTRL_getFlag_enableUserMotorParams(ctrlHandle) == true) &&
422  (ctrlState > CTRL_State_Idle) &&
423  (gMotorVars.CtrlVersion.minor == 6))
424  {
425  // call this function to fix 1p6
426  USER_softwareUpdate1p6(ctrlHandle);
427  }
428  }
429  }
430 
431 
433  {
434  // set the current ramp
436  gMotorVars.Flag_MotorIdentified = true;
437 
438  // set the speed reference
439  CTRL_setSpd_ref_krpm(ctrlHandle,gMotorVars.SpeedRef_krpm);
440 
441  // set the speed acceleration
444  {
446 
447  USER_calcPIgains(ctrlHandle);
448 
449  // initialize the watch window kp and ki current values with pre-calculated values
450  gMotorVars.Kp_Idq = CTRL_getKp(ctrlHandle,CTRL_Type_PID_Id);
451  gMotorVars.Ki_Idq = CTRL_getKi(ctrlHandle,CTRL_Type_PID_Id);
452 
453  // initialize the watch window kp and ki values with pre-calculated values
454  gMotorVars.Kp_spd = CTRL_getKp(ctrlHandle,CTRL_Type_PID_spd);
455  gMotorVars.Ki_spd = CTRL_getKi(ctrlHandle,CTRL_Type_PID_spd);
456  }
457 
458  }
459  else
460  {
462 
463  // the estimator sets the maximum current slope during identification
465  }
466 
467 
468  // when appropriate, update the global variables
470  {
471  // reset the counter
473 
474  updateGlobalVariables_motor(ctrlHandle);
475  }
476 
477 
478  // update Kp and Ki gains
479  updateKpKiGains(ctrlHandle);
480 
481  // enable/disable the forced angle
483 
484  // enable or disable power warp
485  CTRL_setFlag_enablePowerWarp(ctrlHandle,gMotorVars.Flag_enablePowerWarp);
486 
487 #ifdef DRV8301_SPI
488  HAL_writeDrvData(halHandle,&gDrvSpi8301Vars);
489 
490  HAL_readDrvData(halHandle,&gDrvSpi8301Vars);
491 #endif
492 #ifdef DRV8305_SPI
493  HAL_writeDrvData(halHandle,&gDrvSpi8305Vars);
494 
495  HAL_readDrvData(halHandle,&gDrvSpi8305Vars);
496 #endif
497  } // end of while(gFlag_enableSys) loop
498 
499 
500  // disable the PWM
501  HAL_disablePwm(halHandle);
502 
503  // set the default controller parameters (Reset the control to re-identify the motor)
504  CTRL_setParams(ctrlHandle,&gUserParams);
505  gMotorVars.Flag_Run_Identify = false;
506 
507  } // end of for(;;) loop
508 
509 } // end of main() function
510 
511 
512 interrupt void mainISR(void)
513 {
514  _iq angle_pu = 0;
515  _iq speed_pu;
516  _iq speed_ref_pu = TRAJ_getIntValue(((CTRL_Obj *)ctrlHandle)->trajHandle_spd);
517  _iq speed_outMax_pu = TRAJ_getIntValue(((CTRL_Obj *)ctrlHandle)->trajHandle_spdMax);
518 
519  MATH_vec2 Iab_pu;
520  MATH_vec2 Vab_pu;
521  MATH_vec2 Vdq_out_pu;
522  MATH_vec2 Vab_out_pu;
523  MATH_vec2 phasor;
524 
525 
526  // toggle status LED
527  if(gLEDcnt++ > (uint_least32_t)(USER_ISR_FREQ_Hz / LED_BLINK_FREQ_Hz))
528  {
529  HAL_toggleLed(halHandle,(GPIO_Number_e)HAL_Gpio_LED2);
530  gLEDcnt = 0;
531  }
532 
533 
534  // acknowledge the ADC interrupt
535  HAL_acqAdcInt(halHandle,ADC_IntNumber_1);
536 
537 
538  // convert the ADC data
539  HAL_readAdcData(halHandle,&gAdcData);
540 
541  {
542  uint_least16_t count_isr = CTRL_getCount_isr(ctrlHandle);
543  uint_least16_t numIsrTicksPerCtrlTick = CTRL_getNumIsrTicksPerCtrlTick(ctrlHandle);
544 
545  // if needed, run the controller
546  if(count_isr >= numIsrTicksPerCtrlTick)
547  {
548  CTRL_State_e ctrlState = CTRL_getState(ctrlHandle);
549 
550  bool flag_enableSpeedCtrl;
551  bool flag_enableCurrentCtrl;
552 
553  MATH_vec2 Idq_offset_pu;
554  MATH_vec2 Vdq_offset_pu;
555 
556 
557  // reset the isr count
558  CTRL_resetCounter_isr(ctrlHandle);
559 
560  // run Clarke transform on current
561  CLARKE_run(clarkeHandle_I,&gAdcData.I,&Iab_pu);
562 
563  // run Clarke transform on voltage
564  CLARKE_run(clarkeHandle_V,&gAdcData.V,&Vab_pu);
565 
566  {
567  // run the estimator
568  EST_run(estHandle, \
569  &Iab_pu, \
570  &Vab_pu, \
571  gAdcData.dcBus, \
572  speed_ref_pu);
573 
574  flag_enableSpeedCtrl = EST_doSpeedCtrl(estHandle);
575  flag_enableCurrentCtrl = EST_doCurrentCtrl(estHandle);
576  }
577 
578  // generate the motor electrical angle
579  angle_pu = EST_getAngle_pu(estHandle);
580  speed_pu = EST_getFm_pu(estHandle);
581 
582  // compute the sin/cos phasor
583  CTRL_computePhasor(angle_pu,&phasor);
584 
585  // set the phasor in the Park transform
586  PARK_setPhasor(parkHandle,&phasor);
587 
588  // run the Park transform
589  PARK_run(parkHandle,&Iab_pu,CTRL_getIdq_in_addr(ctrlHandle));
590 
591  // set the offset based on the Id trajectory
592  Idq_offset_pu.value[0] = TRAJ_getIntValue(((CTRL_Obj *)ctrlHandle)->trajHandle_Id);
593  Idq_offset_pu.value[1] = _IQ(0.0);
594 
595  Vdq_offset_pu.value[0] = 0;
596  Vdq_offset_pu.value[1] = 0;
597 
598  CTRL_setup_user(ctrlHandle,
599  angle_pu,
600  speed_ref_pu,
601  speed_pu,
602  speed_outMax_pu,
603  &Idq_offset_pu,
604  &Vdq_offset_pu,
605  flag_enableSpeedCtrl,
606  flag_enableCurrentCtrl);
607 
608  // run the appropriate controller
609  if(ctrlState == CTRL_State_OnLine)
610  {
611  // run the online controller
612  CTRL_runPiOnly(ctrlHandle); //,&gAdcData,&gPwmData);
613 
614  // get the controller output
615  CTRL_getVdq_out_pu(ctrlHandle,&Vdq_out_pu);
616 
617  // set the phasor in the inverse Park transform
618  IPARK_setPhasor(iparkHandle,&phasor);
619 
620  // run the inverse Park module
621  IPARK_run(iparkHandle,&Vdq_out_pu,&Vab_out_pu);
622 
623  // run the space Vector Generator (SVGEN) module
624  SVGEN_run(svgenHandle,&Vab_out_pu,&(gPwmData.Tabc));
625  }
626  else if(ctrlState == CTRL_State_OffLine)
627  {
628  // run the offline controller
629  CTRL_runOffLine(ctrlHandle,halHandle,&gAdcData,&gPwmData);
630  }
631  }
632  else
633  {
634  // increment the isr count
635  CTRL_incrCounter_isr(ctrlHandle);
636  }
637  }
638 
639  // write the PWM compare values
640  HAL_writePwmData(halHandle,&gPwmData);
641 
642 
643  return;
644 } // end of mainISR() function
645 
646 
647 void setupClarke_I(CLARKE_Handle handle,const uint_least8_t numCurrentSensors)
648 {
649  _iq alpha_sf,beta_sf;
650 
651 
652  // initialize the Clarke transform module for current
653  if(numCurrentSensors == 3)
654  {
655  alpha_sf = _IQ(MATH_ONE_OVER_THREE);
656  beta_sf = _IQ(MATH_ONE_OVER_SQRT_THREE);
657  }
658  else if(numCurrentSensors == 2)
659  {
660  alpha_sf = _IQ(1.0);
661  beta_sf = _IQ(MATH_ONE_OVER_SQRT_THREE);
662  }
663  else
664  {
665  alpha_sf = _IQ(0.0);
666  beta_sf = _IQ(0.0);
667  }
668 
669  // set the parameters
670  CLARKE_setScaleFactors(handle,alpha_sf,beta_sf);
671  CLARKE_setNumSensors(handle,numCurrentSensors);
672 
673  return;
674 } // end of setupClarke_I() function
675 
676 
677 void setupClarke_V(CLARKE_Handle handle,const uint_least8_t numVoltageSensors)
678 {
679  _iq alpha_sf,beta_sf;
680 
681 
682  // initialize the Clarke transform module for voltage
683  if(numVoltageSensors == 3)
684  {
685  alpha_sf = _IQ(MATH_ONE_OVER_THREE);
686  beta_sf = _IQ(MATH_ONE_OVER_SQRT_THREE);
687  }
688  else
689  {
690  alpha_sf = _IQ(0.0);
691  beta_sf = _IQ(0.0);
692  }
693 
694  // set the parameters
695  CLARKE_setScaleFactors(handle,alpha_sf,beta_sf);
696  CLARKE_setNumSensors(handle,numVoltageSensors);
697 
698  return;
699 } // end of setupClarke_V() function
700 
701 
703 {
704  CTRL_Obj *obj = (CTRL_Obj *)handle;
705 
706  // get the speed estimate
707  gMotorVars.Speed_krpm = EST_getSpeed_krpm(obj->estHandle);
708 
709  // get the real time speed reference coming out of the speed trajectory generator
711 
712  // get the torque estimate
714 
715  // get the magnetizing current
716  gMotorVars.MagnCurr_A = EST_getIdRated(obj->estHandle);
717 
718  // get the rotor resistance
719  gMotorVars.Rr_Ohm = EST_getRr_Ohm(obj->estHandle);
720 
721  // get the stator resistance
722  gMotorVars.Rs_Ohm = EST_getRs_Ohm(obj->estHandle);
723 
724  // get the stator inductance in the direct coordinate direction
725  gMotorVars.Lsd_H = EST_getLs_d_H(obj->estHandle);
726 
727  // get the stator inductance in the quadrature coordinate direction
728  gMotorVars.Lsq_H = EST_getLs_q_H(obj->estHandle);
729 
730  // get the flux in V/Hz in floating point
731  gMotorVars.Flux_VpHz = EST_getFlux_VpHz(obj->estHandle);
732 
733  // get the flux in Wb in fixed point
734  gMotorVars.Flux_Wb = USER_computeFlux(handle, gFlux_pu_to_Wb_sf);
735 
736  // get the controller state
737  gMotorVars.CtrlState = CTRL_getState(handle);
738 
739  // get the estimator state
740  gMotorVars.EstState = EST_getState(obj->estHandle);
741 
742  // Get the DC buss voltage
743  gMotorVars.VdcBus_kV = _IQmpy(gAdcData.dcBus,_IQ(USER_IQ_FULL_SCALE_VOLTAGE_V/1000.0));
744 
745  return;
746 } // end of updateGlobalVariables_motor() function
747 
748 
750 {
751  if((gMotorVars.CtrlState == CTRL_State_OnLine) && (gMotorVars.Flag_MotorIdentified == true) && (Flag_Latch_softwareUpdate == false))
752  {
753  // set the kp and ki speed values from the watch window
754  CTRL_setKp(handle,CTRL_Type_PID_spd,gMotorVars.Kp_spd);
755  CTRL_setKi(handle,CTRL_Type_PID_spd,gMotorVars.Ki_spd);
756 
757  // set the kp and ki current values for Id and Iq from the watch window
758  CTRL_setKp(handle,CTRL_Type_PID_Id,gMotorVars.Kp_Idq);
759  CTRL_setKi(handle,CTRL_Type_PID_Id,gMotorVars.Ki_Idq);
760  CTRL_setKp(handle,CTRL_Type_PID_Iq,gMotorVars.Kp_Idq);
761  CTRL_setKi(handle,CTRL_Type_PID_Iq,gMotorVars.Ki_Idq);
762  }
763 
764  return;
765 } // end of updateKpKiGains() function
766 
767 
769 // end of file
770 
771 
772 
float_t EST_getFlux_VpHz(EST_Handle handle)
void EST_setFlag_enableForceAngle(EST_Handle handle, const bool state)
CTRL_Obj ctrl
Defines the CTRL object.
Definition: proj_lab20.c:117
IPARK_Handle iparkHandle
the handle for the inverse Park transform
Definition: proj_lab20.c:86
IPARK_Handle IPARK_init(void *pMemory, const size_t numBytes)
void main(void)
Definition: fast_obs_im.c:119
void CTRL_incrCounter_isr(CTRL_Handle handle)
#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 updateGlobalVariables_motor(EST_Handle estHandle)
Updates the global motor variables.
Definition: fast_obs_im.c:491
float_t Lsq_H
Definition: main.h:172
uint_least16_t gCounter_updateGlobals
A counter that is denotes when to update the global variables.
Definition: proj_lab20.c:70
_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 gFlux_pu_to_VpHz_sf
Definition: proj_lab20.c:148
bool Flag_Run_Identify
Definition: main.h:137
static void PARK_setPhasor(PARK_Handle handle, const MATH_vec2 *pPhasor)
_iq EST_getSpeed_krpm(EST_Handle handle)
_iq gTorque_Flux_Iq_pu_to_Nm_sf
Definition: proj_lab20.c:152
CLARKE_Obj clarke_V
the voltage Clarke transform object
Definition: proj_lab20.c:82
static _iq TRAJ_getIntValue(TRAJ_Handle handle)
void CTRL_getVdq_out_pu(CTRL_Handle handle, MATH_vec2 *pVdq_out_pu)
void USER_calcPIgains(CTRL_Handle handle)
Updates Id and Iq PI gains.
CTRL_State_e CtrlState
Definition: main.h:147
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]
interrupt void mainISR(void)
The main interrupt service (ISR) routine.
Definition: proj_lab20.c:512
#define NUM_MAIN_TICKS_FOR_GLOBAL_VARIABLE_UPDATE
Defines the number of main iterations before global variables are updated.
Definition: float/main.h:48
CLARKE_Handle clarkeHandle_V
the handle for the voltage Clarke transform
Definition: proj_lab20.c:81
struct _EST_Obj_ * EST_Handle
void HAL_writeDrvData(HAL_Handle handle, DRV_SPI_8301_Vars_t *Spi_8301_Vars)
bool CTRL_isError(CTRL_Handle handle)
uint_least16_t CTRL_getNumIsrTicksPerCtrlTick(CTRL_Handle handle)
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)
TRAJ_Handle trajHandle_spd
the handle for the speed trajectory
#define USER_IQ_FULL_SCALE_VOLTAGE_V
Defines full scale value for the IQ30 variable of Voltage inside the system.
Definition: user.h:88
uint_least8_t numCurrentSensors
void memCopy(uint16_t *srcStartAddr, uint16_t *srcEndAddr, uint16_t *dstAddr)
EST_Handle estHandle
void CTRL_setKi(CTRL_Handle handle, const CTRL_Type_e ctrlType, const _iq Ki)
HAL_AdcData_t gAdcData
Defines the ADC data.
Definition: proj_lab20.c:107
bool Flag_enableForceAngle
Definition: main.h:139
static void SVGEN_run(SVGEN_Handle handle, const MATH_vec2 *pVab, MATH_vec3 *pT)
void CTRL_setFlag_enableCtrl(CTRL_Handle handle, const bool state)
void CTRL_setFlag_enableSpeedCtrl(CTRL_Handle handle, const bool state)
long _iq
MATH_vec2 * CTRL_getIdq_in_addr(CTRL_Handle handle)
#define MATH_ONE_OVER_THREE
void USER_setParams(USER_Params *pUserParams)
Sets the user parameter values.
uint_least16_t CTRL_getCount_isr(CTRL_Handle handle)
EST_State_e EstState
Definition: main.h:148
float_t Rs_Ohm
Definition: main.h:169
#define HAL_toggleLed
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.
void CTRL_setMaxAccel_pu(CTRL_Handle handle, const _iq maxAccel_pu)
_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)
void setupClarke_V(CLARKE_Handle handle, const uint_least8_t numVoltageSensors)
Sets the number of voltage sensors.
Definition: proj_lab20.c:677
MATH_vec3 Tabc
static void HAL_enablePwm(HAL_Handle handle)
void CTRL_setParams(CTRL_Handle handle, USER_Params *pUserParams)
#define _IQmpy(A, B)
volatile MOTOR_Vars_t gMotorVars
Definition: proj_lab20.c:122
USER_ErrorCode_e USER_getErrorCode(USER_Params *pUserParams)
Gets the error code in the user parameters.
void HAL_enableDrv(HAL_Handle handle)
void CTRL_resetCounter_isr(CTRL_Handle handle)
TRAJ_Handle trajHandle_Id
the handle for the Id trajectory
Definition: fast_obs_im.c:102
_iq value[2]
_iq VdcBus_kV
Definition: main.h:192
bool EST_doSpeedCtrl(EST_Handle handle)
_iq Torque_Nm
Definition: main.h:165
_iq SpeedRef_krpm
Definition: main.h:157
_iq Ki_spd
Definition: main.h:183
void HAL_setupDrvSpi(HAL_Handle handle, DRV_SPI_8301_Vars_t *Spi_8301_Vars)
uint_least8_t numVoltageSensors
float_t EST_getLs_q_H(EST_Handle handle)
bool CTRL_getFlag_enableUserMotorParams(CTRL_Handle handle)
void HAL_enableDebugInt(HAL_Handle handle)
CLARKE_Handle CLARKE_init(void *pMemory, const size_t numBytes)
PARK_Obj park
the Park transform object
Definition: proj_lab20.c:90
void setupClarke_I(CLARKE_Handle handle, const uint_least8_t numCurrentSensors)
Sets the number of current sensors.
Definition: proj_lab20.c:647
CTRL_State_e CTRL_getState(CTRL_Handle handle)
float_t Flux_VpHz
Definition: main.h:173
static void IPARK_run(IPARK_Handle handle, const MATH_vec2 *pInVec, MATH_vec2 *pOutVec)
bool Flag_enableUserParams
Definition: main.h:142
MATH_vec3 gAdcBiasI
Definition: proj_lab20.c:74
EST_State_e EST_getState(EST_Handle handle)
bool Flag_enableOffsetcalc
Definition: main.h:143
PARK_Handle parkHandle
the handle for the Park object
Definition: proj_lab20.c:89
uint16_t gLEDcnt
Definition: proj_lab20.c:120
_iq EST_getFm_pu(EST_Handle handle)
_iq CTRL_getKp(CTRL_Handle handle, const CTRL_Type_e ctrlType)
#define MOTOR_Vars_INIT
Initialization values of global variables.
Definition: main.h:76
CTRL_State_e
bool Flag_Latch_softwareUpdate
Definition: proj_lab20.c:72
_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 Ki_Idq
Definition: main.h:186
void CTRL_runOffLine(CTRL_Handle handle, HAL_Handle halHandle, const HAL_AdcData_t *pAdcData, HAL_PwmData_t *pPwmData)
_iq gTorque_Ls_Id_Iq_pu_to_Nm_sf
Definition: proj_lab20.c:150
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)
CTRL_Handle CTRL_initCtrl(const uint_least8_t estNumber, void *pMemory, const size_t numBytes)
float_t Lsd_H
Definition: main.h:171
bool EST_doCurrentCtrl(EST_Handle handle)
void CTRL_setup_user(CTRL_Handle handle, const _iq angle_pu, const _iq speed_ref_pu, const _iq speed_fb_pu, const _iq speed_outMax_pu, const MATH_vec2 *pIdq_offset_pu, const MATH_vec2 *pVdq_offset_pu, const bool flag_enableSpeedCtrl, const bool flag_enableCurrentCtrl)
HAL_Handle halHandle
The hal handle.
Definition: proj_lab20.c:98
void CTRL_setFlag_enablePowerWarp(CTRL_Handle handle, const bool state)
#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)
bool CTRL_updateState(CTRL_Handle handle)
_iq EST_getAngle_pu(EST_Handle handle)
_iq CTRL_getKi(CTRL_Handle handle, const CTRL_Type_e ctrlType)
static void HAL_disablePwm(HAL_Handle handle)
void CTRL_runPiOnly(CTRL_Handle handle)
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)
static void CLARKE_run(CLARKE_Handle handle, const MATH_vec3 *pInVec, MATH_vec2 *pOutVec)
static void PARK_run(PARK_Handle handle, const MATH_vec2 *pInVec, MATH_vec2 *pOutVec)
MATH_vec3 gAdcBiasV
Definition: proj_lab20.c:75
void CTRL_setFlag_enableDcBusComp(CTRL_Handle handle, const bool state)
HAL_PwmData_t gPwmData
Defines the PWM data.
Definition: proj_lab20.c:105
_iq gFlux_pu_to_Wb_sf
Definition: proj_lab20.c:146
CTRL_Handle ctrlHandle
The controller handle.
Definition: proj_lab20.c:76
SVGEN_Obj svgen
the space vector generator object
Definition: proj_lab20.c:93
_iq gMaxCurrentSlope
Definition: proj_lab20.c:109
_iq EST_getMaxCurrentSlope_pu(EST_Handle handle)
void CTRL_setFlag_enableUserMotorParams(CTRL_Handle handle, const bool state)
_iq SpeedTraj_krpm
Definition: main.h:158
float_t MagnCurr_A
Definition: main.h:167
static void HAL_readAdcData(HAL_Handle handle, HAL_AdcData_t *pAdcData)
HAL_Obj hal
void updateKpKiGains(CTRL_Handle handle)
Updates Kp and Ki gains in the controller object.
Definition: proj_lab20.c:749
static _iq HAL_getBias(HAL_Handle handle, const HAL_SensorType_e sensorType, uint_least8_t sensorNumber)
bool Flag_enableSys
Definition: main.h:136
_iq MaxAccel_krpmps
Definition: main.h:159
_iq CTRL_getSpd_int_ref_pu(CTRL_Handle handle)
#define MATH_ONE_OVER_SQRT_THREE
bool Flag_enableRsRecalc
Definition: main.h:141
CLARKE_Obj clarke_I
the current Clarke transform object
Definition: proj_lab20.c:79
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)
static void HAL_updateAdcBias(HAL_Handle handle)
PARK_Handle PARK_init(void *pMemory, const size_t numBytes)
void CTRL_getVersion(CTRL_Handle handle, CTRL_Version *pVersion)
uint16_t minor
SVGEN_Handle svgenHandle
the handle for the space vector generator
Definition: proj_lab20.c:92
CTRL_Version CtrlVersion
Definition: main.h:152
float_t EST_getRr_Ohm(EST_Handle handle)
void CTRL_computePhasor(const _iq angle_pu, MATH_vec2 *pPhasor)
void HAL_setParams(HAL_Handle handle, const USER_Params *pUserParams)
CLARKE_Handle clarkeHandle_I
the handle for the current Clarke transform
Definition: proj_lab20.c:78
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
USER_Params gUserParams
The user parameters.
Definition: proj_lab20.c:103
SVGEN_Handle SVGEN_init(void *pMemory, const size_t numBytes)
_iq Flux_Wb
Definition: main.h:164
EST_Handle estHandle
the handle for the estimator
Definition: proj_lab20.c:84
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...
IPARK_Obj ipark
the inverse Park transform object
Definition: proj_lab20.c:87
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.
#define LED_BLINK_FREQ_Hz
Definition: proj_lab20.c:64
void CTRL_setKp(CTRL_Handle handle, const CTRL_Type_e ctrlType, const _iq Kp)
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)