AM263x Motor Control SDK  09.02.00
speedfr.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2024 Texas Instruments Incorporated
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the
14  * 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
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifndef SPEED_FR_H
34 #define SPEED_FR_H
35 
36 #ifdef __cplusplus
37 extern "C"
38 {
39 #endif
40 
52 #include <stdlib.h>
53 
54 #include "math_types.h"
55 #include "userParams.h"
56 
57 typedef struct _SPDFR_obj_
58 {
59  float32_t theta; // Input: reference set-point
60  float32_t thetaPrev; // Input: feedback
61  float32_t K2; // Parameter: proportional loop gain
62  float32_t K3; // Parameter: integral gain
63  float32_t scaleFreq; // Parameter:
64  float32_t speed_pu; // Output: frequency
65  float32_t speed_Hz; // Output: frequency
66 } SPDFR_Obj;
67 
70 typedef struct _SPDFR_obj_ *SPDFR_Handle;
71 
72 // ***************************************
73 // extern functions
74 // ***************************************
75 
76 //*****************************************************************************
77 //
82 //
83 //*****************************************************************************
84 SPDFR_Handle SPDFR_init(void *pMemory, const size_t numBytes);
85 
86 //*****************************************************************************
87 //
90 //
91 //*****************************************************************************
92 extern void SPDFR_reset(SPDFR_Handle handle);
93 
94 //*****************************************************************************
95 //
99 //
100 //*****************************************************************************
101 extern void SPDFR_setParams(SPDFR_Handle handle, const USER_Params *pUserParams);
102 
103 //*****************************************************************************
104 //
107 //
108 //*****************************************************************************
110 {
111  SPDFR_Obj *obj = (SPDFR_Obj *)handle;
112 
113  return(obj->speed_Hz);
114 }
115 
116 //*****************************************************************************
117 //
121 //
122 //*****************************************************************************
123 static __attribute__((always_inline))
124 void SPDFR_run(SPDFR_Handle handle, float32_t theta)
125 {
126  SPDFR_Obj *obj = (SPDFR_Obj *)handle;
127  float32_t error;
128 
129  obj->theta = theta * MATH_ONE_OVER_TWO_PI;
130 
131  // error cal
132  error = obj->theta - obj->thetaPrev;
133 
134  if(error < -0.5f)
135  {
136  error += 1.0f;
137  }
138  else if(error > 0.5f)
139  {
140  error -= 1.0f;
141  }
142 
143  // Update the electrical angle
144  obj->thetaPrev = obj->theta;
145 
146  // Low-pass filter
147  obj->speed_pu = (obj->K2 * obj->speed_pu) + (obj->K3 * error);
148 
149  // Saturate the output
150  obj->speed_pu = MATH_sat(obj->speed_pu, 1.0f, -1.0f);
151 
152  // Change motor speed for pu to rpm value
153  obj->speed_Hz = obj->scaleFreq * obj->speed_pu;
154 
155  return;
156 }
157 
158 //*****************************************************************************
159 //
160 // Close the Doxygen group.
162 //
163 //*****************************************************************************
164 
165 //*****************************************************************************
166 //
167 // Mark the end of the C bindings section for C++ compilers.
168 //
169 //*****************************************************************************
170 #ifdef __cplusplus
171 }
172 #endif
173 
174 #endif //end of SPEED_FR_H definition
SPDFR_Obj::thetaPrev
float32_t thetaPrev
Definition: speedfr.h:60
SPDFR_Obj::speed_Hz
float32_t speed_Hz
Definition: speedfr.h:65
SPDFR_Obj::K2
float32_t K2
Definition: speedfr.h:61
USER_Params
Defines a structure for the user parameters.
Definition: userParams.h:75
SPDFR_Obj::K3
float32_t K3
Definition: speedfr.h:62
SPDFR_Obj
Definition: speedfr.h:58
SPDFR_setParams
void SPDFR_setParams(SPDFR_Handle handle, const USER_Params *pUserParams)
Set the SPDFR controller.
SPDFR_Obj::theta
float32_t theta
Definition: speedfr.h:59
SPDFR_reset
void SPDFR_reset(SPDFR_Handle handle)
Set the SPDFR controller.
SPDFR_run
static void SPDFR_run(SPDFR_Handle handle, float32_t theta)
Set the SPDFR controller.
Definition: speedfr.h:124
SPDFR_Obj::speed_pu
float32_t speed_pu
Definition: speedfr.h:64
SPDFR_Obj::scaleFreq
float32_t scaleFreq
Definition: speedfr.h:63
userParams.h
Contains the public interface for the HAL and EST modules.
SPDFR_init
SPDFR_Handle SPDFR_init(void *pMemory, const size_t numBytes)
Set the SPDFR controller.
SPDFR_Handle
struct _SPDFR_obj_ * SPDFR_Handle
Defines the SPDFR handle.
Definition: speedfr.h:70
float32_t
float float32_t
Definition: dcl_common.h:58
SPDFR_getSpeedHz
static float32_t SPDFR_getSpeedHz(SPDFR_Handle handle)
Set the SPDFR controller.
Definition: speedfr.h:109