instaspin_foc
proj_lab09a.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 
75 
77 
78 HAL_PwmData_t gPwmData = {_IQ(0.0), _IQ(0.0), _IQ(0.0)};
79 
81 
83 
84 #ifdef FAST_ROM_V1p6
86 #else
87 CTRL_Obj ctrl; //v1p7 format
88 #endif
89 
90 uint16_t gLEDcnt = 0;
91 
93 
94 #ifdef FLASH
95 // Used for running BackGround in flash, and ISR in RAM
96 extern uint16_t *RamfuncsLoadStart, *RamfuncsLoadEnd, *RamfuncsRunStart;
97 #endif
100 
102 
103 
104 #ifdef DRV8301_SPI
105 // Watch window interface to the 8301 SPI
106 DRV_SPI_8301_Vars_t gDrvSpi8301Vars;
107 #endif
108 
110 
112 
114 
116 
117 // **************************************************************************
118 // the functions
119 
120 void main(void)
121 {
122  uint_least8_t estNumber = 0;
123 
124 #ifdef FAST_ROM_V1p6
125  uint_least8_t ctrlNumber = 0;
126 #endif
127 
128  // Only used if running from FLASH
129  // Note that the variable FLASH is defined by the project
130  #ifdef FLASH
131  // Copy time critical code and Flash setup code to RAM
132  // The RamfuncsLoadStart, RamfuncsLoadEnd, and RamfuncsRunStart
133  // symbols are created by the linker. Refer to the linker files.
134  memCopy((uint16_t *)&RamfuncsLoadStart,(uint16_t *)&RamfuncsLoadEnd,(uint16_t *)&RamfuncsRunStart);
135  #endif
136 
137  // initialize the hardware abstraction layer
138  halHandle = HAL_init(&hal,sizeof(hal));
139 
140 
141  // check for errors in user parameters
142  USER_checkForErrors(&gUserParams);
143 
144 
145  // store user parameter error in global variable
146  gMotorVars.UserErrorCode = USER_getErrorCode(&gUserParams);
147 
148 
149  // do not allow code execution if there is a user parameter error
150  if(gMotorVars.UserErrorCode != USER_ErrorCode_NoError)
151  {
152  for(;;)
153  {
154  gMotorVars.Flag_enableSys = false;
155  }
156  }
157 
158 
159  // initialize the user parameters
160  USER_setParams(&gUserParams);
161 
162 
163  // set the hardware abstraction layer parameters
164  HAL_setParams(halHandle,&gUserParams);
165 
166 
167  // initialize the controller
168 #ifdef FAST_ROM_V1p6
169  ctrlHandle = CTRL_initCtrl(ctrlNumber, estNumber); //v1p6 format (06xF and 06xM devices)
170  controller_obj = (CTRL_Obj *)ctrlHandle;
171 #else
172  ctrlHandle = CTRL_initCtrl(estNumber,&ctrl,sizeof(ctrl)); //v1p7 format default
173 #endif
174 
175 
176  {
177  CTRL_Version version;
178 
179  // get the version number
180  CTRL_getVersion(ctrlHandle,&version);
181 
182  gMotorVars.CtrlVersion = version;
183  }
184 
185 
186  // set the default controller parameters
187  CTRL_setParams(ctrlHandle,&gUserParams);
188 
189 
190  // Initialize field weakening
191  fwHandle = FW_init(&fw,sizeof(fw));
192 
193 
194  // Disable field weakening
195  FW_setFlag_enableFw(fwHandle, false);
196 
197 
198  // Clear field weakening counter
199  FW_clearCounter(fwHandle);
200 
201 
202  // Set the number of ISR per field weakening ticks
204 
205 
206  // Set the deltas of field weakening
208 
209 
210  // Set initial output of field weakening to zero
211  FW_setOutput(fwHandle, _IQ(0.0));
212 
213 
214  // Set the field weakening controller limits
216 
217 
218  // setup faults
219  HAL_setupFaults(halHandle);
220 
221 
222  // initialize the interrupt vector table
223  HAL_initIntVectorTable(halHandle);
224 
225 
226  // enable the ADC interrupts
227  HAL_enableAdcInts(halHandle);
228 
229 
230  // enable global interrupts
231  HAL_enableGlobalInts(halHandle);
232 
233 
234  // enable debug interrupts
235  HAL_enableDebugInt(halHandle);
236 
237 
238  // disable the PWM
239  HAL_disablePwm(halHandle);
240 
241 
242 #ifdef DRV8301_SPI
243  // turn on the DRV8301 if present
244  HAL_enableDrv(halHandle);
245  // initialize the DRV8301 interface
246  HAL_setupDrvSpi(halHandle,&gDrvSpi8301Vars);
247 #endif
248 
249 
250  // enable DC bus compensation
251  CTRL_setFlag_enableDcBusComp(ctrlHandle, true);
252 
253 
254  // compute scaling factors for flux and torque calculations
259 
260 
261  for(;;)
262  {
263  // Waiting for enable system flag to be set
264  while(!(gMotorVars.Flag_enableSys));
265 
267 
268  // Enable the Library internal PI. Iq is referenced by the speed PI now
269  CTRL_setFlag_enableSpeedCtrl(ctrlHandle, true);
270 
271  // loop while the enable system flag is true
272  while(gMotorVars.Flag_enableSys)
273  {
274  CTRL_Obj *obj = (CTRL_Obj *)ctrlHandle;
275 
276  // increment counters
278 
279  // enable/disable the use of motor parameters being loaded from user.h
281 
282  // enable/disable Rs recalibration during motor startup
284 
285  // enable/disable automatic calculation of bias values
286  CTRL_setFlag_enableOffset(ctrlHandle,gMotorVars.Flag_enableOffsetcalc);
287 
288 
289  if(CTRL_isError(ctrlHandle))
290  {
291  // set the enable controller flag to false
292  CTRL_setFlag_enableCtrl(ctrlHandle,false);
293 
294  // set the enable system flag to false
295  gMotorVars.Flag_enableSys = false;
296 
297  // disable the PWM
298  HAL_disablePwm(halHandle);
299  }
300  else
301  {
302  // update the controller state
303  bool flag_ctrlStateChanged = CTRL_updateState(ctrlHandle);
304 
305  // enable or disable the control
306  CTRL_setFlag_enableCtrl(ctrlHandle, gMotorVars.Flag_Run_Identify);
307 
308  if(flag_ctrlStateChanged)
309  {
310  CTRL_State_e ctrlState = CTRL_getState(ctrlHandle);
311 
312  if(ctrlState == CTRL_State_OffLine)
313  {
314  // enable the PWM
315  HAL_enablePwm(halHandle);
316  }
317  else if(ctrlState == CTRL_State_OnLine)
318  {
319  if(gMotorVars.Flag_enableOffsetcalc == true)
320  {
321  // update the ADC bias values
322  HAL_updateAdcBias(halHandle);
323  }
324  else
325  {
326  // set the current bias
327  HAL_setBias(halHandle,HAL_SensorType_Current,0,_IQ(I_A_offset));
328  HAL_setBias(halHandle,HAL_SensorType_Current,1,_IQ(I_B_offset));
329  HAL_setBias(halHandle,HAL_SensorType_Current,2,_IQ(I_C_offset));
330 
331  // set the voltage bias
332  HAL_setBias(halHandle,HAL_SensorType_Voltage,0,_IQ(V_A_offset));
333  HAL_setBias(halHandle,HAL_SensorType_Voltage,1,_IQ(V_B_offset));
334  HAL_setBias(halHandle,HAL_SensorType_Voltage,2,_IQ(V_C_offset));
335  }
336 
337  // Return the bias value for currents
338  gMotorVars.I_bias.value[0] = HAL_getBias(halHandle,HAL_SensorType_Current,0);
339  gMotorVars.I_bias.value[1] = HAL_getBias(halHandle,HAL_SensorType_Current,1);
340  gMotorVars.I_bias.value[2] = HAL_getBias(halHandle,HAL_SensorType_Current,2);
341 
342  // Return the bias value for voltages
343  gMotorVars.V_bias.value[0] = HAL_getBias(halHandle,HAL_SensorType_Voltage,0);
344  gMotorVars.V_bias.value[1] = HAL_getBias(halHandle,HAL_SensorType_Voltage,1);
345  gMotorVars.V_bias.value[2] = HAL_getBias(halHandle,HAL_SensorType_Voltage,2);
346 
347  // enable the PWM
348  HAL_enablePwm(halHandle);
349  }
350  else if(ctrlState == CTRL_State_Idle)
351  {
352  // disable the PWM
353  HAL_disablePwm(halHandle);
354  gMotorVars.Flag_Run_Identify = false;
355  }
356 
357  if((CTRL_getFlag_enableUserMotorParams(ctrlHandle) == true) &&
358  (ctrlState > CTRL_State_Idle) &&
359  (gMotorVars.CtrlVersion.minor == 6))
360  {
361  // call this function to fix 1p6
362  USER_softwareUpdate1p6(ctrlHandle);
363  }
364 
365  }
366  }
367 
368 
370  {
371  _iq Is_Max_squared_pu = _IQ((USER_MOTOR_MAX_CURRENT*USER_MOTOR_MAX_CURRENT)/ \
373  _iq Id_squared_pu = _IQmpy(CTRL_getId_ref_pu(ctrlHandle),CTRL_getId_ref_pu(ctrlHandle));
374 
375  // Take into consideration that Iq^2+Id^2 = Is^2
376  Iq_Max_pu = _IQsqrt(Is_Max_squared_pu-Id_squared_pu);
377 
378  //Set new max trajectory
379  CTRL_setSpdMax(ctrlHandle, Iq_Max_pu);
380 
381  // set the current ramp
383  gMotorVars.Flag_MotorIdentified = true;
384 
385  // set the speed reference
386  CTRL_setSpd_ref_krpm(ctrlHandle,gMotorVars.SpeedRef_krpm);
387 
388  // set the speed acceleration
390 
392  {
394 
395  USER_calcPIgains(ctrlHandle);
396 
397  // initialize the watch window kp and ki current values with pre-calculated values
398  gMotorVars.Kp_Idq = CTRL_getKp(ctrlHandle,CTRL_Type_PID_Id);
399  gMotorVars.Ki_Idq = CTRL_getKi(ctrlHandle,CTRL_Type_PID_Id);
400  }
401 
402  }
403  else
404  {
406 
407  // initialize the watch window kp and ki values with pre-calculated values
408  gMotorVars.Kp_spd = CTRL_getKp(ctrlHandle,CTRL_Type_PID_spd);
409  gMotorVars.Ki_spd = CTRL_getKi(ctrlHandle,CTRL_Type_PID_spd);
410 
411 
412  // the estimator sets the maximum current slope during identification
414  }
415 
416 
417  // when appropriate, update the global variables
419  {
420  // reset the counter
422 
423  updateGlobalVariables_motor(ctrlHandle);
424  }
425 
426 
427  // update Kp and Ki gains
428  updateKpKiGains(ctrlHandle);
429 
430  // set field weakening enable flag depending on user's input
431  FW_setFlag_enableFw(fwHandle,gMotorVars.Flag_enableFieldWeakening);
432 
433  // enable/disable the forced angle
435 
436  // enable or disable power warp
437  CTRL_setFlag_enablePowerWarp(ctrlHandle,gMotorVars.Flag_enablePowerWarp);
438 
439 #ifdef DRV8301_SPI
440  HAL_writeDrvData(halHandle,&gDrvSpi8301Vars);
441 
442  HAL_readDrvData(halHandle,&gDrvSpi8301Vars);
443 #endif
444 
445  } // end of while(gFlag_enableSys) loop
446 
447 
448  // disable the PWM
449  HAL_disablePwm(halHandle);
450 
451  // set the default controller parameters (Reset the control to re-identify the motor)
452  CTRL_setParams(ctrlHandle,&gUserParams);
453  gMotorVars.Flag_Run_Identify = false;
454 
455  } // end of for(;;) loop
456 
457 } // end of main() function
458 
459 
460 interrupt void mainISR(void)
461 {
462  // toggle status LED
463  if(gLEDcnt++ > (uint_least32_t)(USER_ISR_FREQ_Hz / LED_BLINK_FREQ_Hz))
464  {
465  HAL_toggleLed(halHandle,(GPIO_Number_e)HAL_Gpio_LED2);
466  gLEDcnt = 0;
467  }
468 
469 
470  // acknowledge the ADC interrupt
471  HAL_acqAdcInt(halHandle,ADC_IntNumber_1);
472 
473 
474  // convert the ADC data
475  HAL_readAdcData(halHandle,&gAdcData);
476 
477 
478  // run the controller
479  CTRL_run(ctrlHandle,halHandle,&gAdcData,&gPwmData);
480 
481 
482  // write the PWM compare values
483  HAL_writePwmData(halHandle,&gPwmData);
484 
485 
486  if(FW_getFlag_enableFw(fwHandle) == true)
487  {
488  FW_incCounter(fwHandle);
489 
490  if(FW_getCounter(fwHandle) > FW_getNumIsrTicksPerFwTick(fwHandle))
491  {
492  _iq refValue;
493  _iq fbackValue;
494  _iq output;
495 
496  FW_clearCounter(fwHandle);
497 
498  refValue = gMotorVars.VsRef;
499 
500  fbackValue = gMotorVars.Vs;
501 
502  FW_run(fwHandle, refValue, fbackValue, &output);
503 
504  CTRL_setId_ref_pu(ctrlHandle, output);
505 
507  }
508  }
509  else
510  {
511  CTRL_setId_ref_pu(ctrlHandle, _IQmpy(gMotorVars.IdRef_A, _IQ(1.0/USER_IQ_FULL_SCALE_CURRENT_A)));
512  }
513 
514  // setup the controller
515  CTRL_setup(ctrlHandle);
516 
517 
518  return;
519 } // end of mainISR() function
520 
521 
523 {
524  CTRL_Obj *obj = (CTRL_Obj *)handle;
525  int32_t tmp;
526 
527  // get the speed estimate
528  gMotorVars.Speed_krpm = EST_getSpeed_krpm(obj->estHandle);
529 
530  // get the real time speed reference coming out of the speed trajectory generator
532 
533  // get the torque estimate
535 
536  // when calling EST_ functions that return a float, and fpu32 is enabled, an integer is needed as a return
537  // so that the compiler reads the returned value from the accumulator instead of fpu32 registers
538  // get the magnetizing current
539  tmp = EST_getIdRated(obj->estHandle);
540  gMotorVars.MagnCurr_A = *((float_t *)&tmp);
541 
542  // get the rotor resistance
543  tmp = EST_getRr_Ohm(obj->estHandle);
544  gMotorVars.Rr_Ohm = *((float_t *)&tmp);
545 
546  // get the stator resistance
547  tmp = EST_getRs_Ohm(obj->estHandle);
548  gMotorVars.Rs_Ohm = *((float_t *)&tmp);
549 
550  // get the stator inductance in the direct coordinate direction
551  tmp = EST_getLs_d_H(obj->estHandle);
552  gMotorVars.Lsd_H = *((float_t *)&tmp);
553 
554  // get the stator inductance in the quadrature coordinate direction
555  tmp = EST_getLs_q_H(obj->estHandle);
556  gMotorVars.Lsq_H = *((float_t *)&tmp);
557 
558  // get the flux in V/Hz in floating point
559  tmp = EST_getFlux_VpHz(obj->estHandle);
560  gMotorVars.Flux_VpHz = *((float_t *)&tmp);
561 
562  // get the flux in Wb in fixed point
563  gMotorVars.Flux_Wb = USER_computeFlux(handle, gFlux_pu_to_Wb_sf);
564 
565  // get the controller state
566  gMotorVars.CtrlState = CTRL_getState(handle);
567 
568  // get the estimator state
569  gMotorVars.EstState = EST_getState(obj->estHandle);
570 
571  // read Vd and Vq vectors per units
572  gMotorVars.Vd = CTRL_getVd_out_pu(ctrlHandle);
573  gMotorVars.Vq = CTRL_getVq_out_pu(ctrlHandle);
574 
575  // calculate vector Vs in per units
576  gMotorVars.Vs = _IQsqrt(_IQmpy(gMotorVars.Vd, gMotorVars.Vd) + _IQmpy(gMotorVars.Vq, gMotorVars.Vq));
577 
578  // read Id and Iq vectors in amps
579  gMotorVars.Id_A = _IQmpy(CTRL_getId_in_pu(ctrlHandle), _IQ(USER_IQ_FULL_SCALE_CURRENT_A));
580  gMotorVars.Iq_A = _IQmpy(CTRL_getIq_in_pu(ctrlHandle), _IQ(USER_IQ_FULL_SCALE_CURRENT_A));
581 
582  // calculate vector Is in amps
583  gMotorVars.Is_A = _IQsqrt(_IQmpy(gMotorVars.Id_A, gMotorVars.Id_A) + _IQmpy(gMotorVars.Iq_A, gMotorVars.Iq_A));
584 
585  // Get the DC buss voltage
586  gMotorVars.VdcBus_kV = _IQmpy(gAdcData.dcBus,_IQ(USER_IQ_FULL_SCALE_VOLTAGE_V/1000.0));
587 
588  return;
589 } // end of updateGlobalVariables_motor() function
590 
591 
593 {
594  if((gMotorVars.CtrlState == CTRL_State_OnLine) && (gMotorVars.Flag_MotorIdentified == true) && (Flag_Latch_softwareUpdate == false))
595  {
596  // set the kp and ki speed values from the watch window
597  CTRL_setKp(handle,CTRL_Type_PID_spd,gMotorVars.Kp_spd);
598  CTRL_setKi(handle,CTRL_Type_PID_spd,gMotorVars.Ki_spd);
599 
600  // set the kp and ki current values for Id and Iq from the watch window
601  CTRL_setKp(handle,CTRL_Type_PID_Id,gMotorVars.Kp_Idq);
602  CTRL_setKi(handle,CTRL_Type_PID_Id,gMotorVars.Ki_Idq);
603  CTRL_setKp(handle,CTRL_Type_PID_Iq,gMotorVars.Kp_Idq);
604  CTRL_setKi(handle,CTRL_Type_PID_Iq,gMotorVars.Ki_Idq);
605  }
606 
607  return;
608 } // end of updateKpKiGains() function
609 
610 
612 // end of file
613 
614 
615 
float_t EST_getFlux_VpHz(EST_Handle handle)
void EST_setFlag_enableForceAngle(EST_Handle handle, const bool state)
#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
#define USER_MAX_NEGATIVE_ID_REF_CURRENT_A
LIMITS.
Definition: user.h:239
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
#define FW_INC_DELTA
bool Flag_Run_Identify
Definition: main.h:137
_iq gTorque_Flux_Iq_pu_to_Nm_sf
Definition: proj_lab09a.c:115
_iq EST_getSpeed_krpm(EST_Handle handle)
#define FW_DEC_DELTA
FW_Handle FW_init(void *pMemory, const size_t numBytes)
void USER_calcPIgains(CTRL_Handle handle)
Updates Id and Iq PI gains.
static bool FW_getFlag_enableFw(FW_Handle fwHandle)
CTRL_State_e CtrlState
Definition: main.h:147
static uint32_t FW_getNumIsrTicksPerFwTick(FW_Handle fwHandle)
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)
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)
bool Flag_Latch_softwareUpdate
Definition: proj_lab09a.c:70
void USER_softwareUpdate1p6(CTRL_Handle handle)
Recalculates Inductances with the correct Q Format.
_iq gMaxCurrentSlope
Definition: proj_lab09a.c:82
_iq gFlux_pu_to_Wb_sf
Definition: proj_lab09a.c:109
static void HAL_acqAdcInt(HAL_Handle handle, const ADC_IntNumber_e intNumber)
_iq EST_get_pu_to_krpm_sf(EST_Handle handle)
#define USER_IQ_FULL_SCALE_VOLTAGE_V
Defines full scale value for the IQ30 variable of Voltage inside the system.
Definition: user.h:88
CTRL_Obj ctrl
Defines the CTRL object.
Definition: proj_lab09a.c:87
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 CTRL_setKi(CTRL_Handle handle, const CTRL_Type_e ctrlType, const _iq Ki)
bool Flag_enableForceAngle
Definition: main.h:139
interrupt void mainISR(void)
The main interrupt service (ISR) routine.
Definition: proj_lab09a.c:460
void CTRL_setFlag_enableCtrl(CTRL_Handle handle, const bool state)
void CTRL_setFlag_enableSpeedCtrl(CTRL_Handle handle, const bool state)
USER_Params gUserParams
The user parameters.
Definition: proj_lab09a.c:76
#define V_B_offset
Definition: user.h:133
long _iq
FW_Obj fw
Definition: proj_lab09a.c:98
void main(void)
Definition: proj_lab09a.c:120
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)
_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
uint_least16_t gCounter_updateGlobals
A counter that is denotes when to update the global variables.
Definition: proj_lab09a.c:68
float_t EST_getLs_d_H(EST_Handle handle)
void EST_setFlag_enableRsRecalc(EST_Handle handle, const bool state)
_iq gFlux_pu_to_VpHz_sf
Definition: proj_lab09a.c:111
_iq CTRL_getIq_in_pu(CTRL_Handle handle)
uint16_t gLEDcnt
Definition: proj_lab09a.c:90
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
CTRL_Handle ctrlHandle
The controller handle.
Definition: proj_lab09a.c:72
_iq Torque_Nm
Definition: main.h:165
HAL_Handle halHandle
The hal handle.
Definition: proj_lab09a.c:74
_iq SpeedRef_krpm
Definition: main.h:157
_iq IdRef_A
Definition: main.h:154
_iq VsRef
Definition: main.h:191
_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)
static void FW_incCounter(FW_Handle fwHandle)
CTRL_State_e CTRL_getState(CTRL_Handle handle)
#define I_C_offset
Definition: user.h:127
float_t Flux_VpHz
Definition: main.h:173
bool Flag_enableUserParams
Definition: main.h:142
#define FW_NUM_ISR_TICKS_PER_CTRL_TICK
EST_State_e EST_getState(EST_Handle handle)
bool Flag_enableOffsetcalc
Definition: main.h:143
HAL_PwmData_t gPwmData
Defines the PWM data.
Definition: proj_lab09a.c:78
#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
void CTRL_setSpdMax(CTRL_Handle handle, const _iq spdMax)
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
_iq CTRL_getId_ref_pu(CTRL_Handle handle)
void CTRL_setFlag_enablePowerWarp(CTRL_Handle handle, const bool state)
HAL_AdcData_t gAdcData
Defines the ADC data.
Definition: proj_lab09a.c:80
static void FW_setFlag_enableFw(FW_Handle fwHandle, const bool state)
#define USER_ISR_FREQ_Hz
Defines the Interrupt Service Routine (ISR) frequency, Hz.
Definition: user.h:173
static void FW_clearCounter(FW_Handle fwHandle)
bool CTRL_updateState(CTRL_Handle handle)
_iq CTRL_getKi(CTRL_Handle handle, const CTRL_Type_e ctrlType)
static void HAL_disablePwm(HAL_Handle handle)
static void FW_setDeltas(FW_Handle fwHandle, const _iq delta_inc, const _iq delta_dec)
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)
void updateKpKiGains(CTRL_Handle handle)
Updates Kp and Ki gains in the controller object.
Definition: proj_lab09a.c:592
_iq gTorque_Ls_Id_Iq_pu_to_Nm_sf
Definition: proj_lab09a.c:113
_iq Iq_Max_pu
Definition: proj_lab09a.c:101
volatile MOTOR_Vars_t gMotorVars
Definition: proj_lab09a.c:92
void CTRL_setFlag_enableDcBusComp(CTRL_Handle handle, const bool state)
#define _IQsqrt(A)
_iq EST_getMaxCurrentSlope_pu(EST_Handle handle)
void CTRL_setFlag_enableUserMotorParams(CTRL_Handle handle, const bool state)
_iq SpeedTraj_krpm
Definition: main.h:158
#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 _iq HAL_getBias(HAL_Handle handle, const HAL_SensorType_e sensorType, uint_least8_t sensorNumber)
bool Flag_enableSys
Definition: main.h:136
void updateGlobalVariables_motor(CTRL_Handle handle)
Updates the global motor variables.
Definition: proj_lab09a.c:522
_iq Id_A
Definition: main.h:194
_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)
static uint32_t FW_getCounter(FW_Handle fwHandle)
MATH_vec3 I_bias
Definition: main.h:198
bool Flag_enableRsRecalc
Definition: main.h:141
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 FW_setNumIsrTicksPerFwTick(FW_Handle fwHandle, const uint32_t numIsrTicksPerFwTick)
static void HAL_updateAdcBias(HAL_Handle handle)
void CTRL_getVersion(CTRL_Handle handle, CTRL_Version *pVersion)
uint16_t minor
FW_Handle fwHandle
Definition: proj_lab09a.c:99
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)
#define LED_BLINK_FREQ_Hz
Definition: proj_lab09a.c:62
void HAL_readDrvData(HAL_Handle handle, DRV_SPI_8301_Vars_t *Spi_8301_Vars)
bool Flag_enableFieldWeakening
Definition: main.h:140
#define USER_MOTOR_MAX_CURRENT
Definition: user.h:407
static void FW_setMinMax(FW_Handle fwHandle, const _iq outMin, const _iq outMax)
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