AM263x Motor Control SDK  09.02.00
speedcalc.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_CALC_H
34 #define SPEED_CALC_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 _SPDCALC_obj_
58 {
59  float32_t ref; // Input: reference set-point
60  float32_t fbk; // Input: feedback
61  float32_t err; // Error
62  float32_t out; // Output: controller output
63  float32_t Kp; // Parameter: proportional loop gain
64  float32_t Ki; // Parameter: integral gain
65  float32_t Umax; // Parameter: upper saturation limit
66  float32_t Umin; // Parameter: lower saturation limit
67  float32_t Up; // Data: proportional term
68  float32_t Ui; // Data: integral term
69  float32_t speed_Hz; // Output freq
70  float32_t thetaDelta; // Parameter: theta maximum
71 } SPDCALC_Obj;
72 
75 typedef struct _SPDCALC_obj_ *SPDCALC_Handle;
76 
77 // ***************************************
78 // extern functions
79 // ***************************************
80 
81 //*****************************************************************************
82 //
87 
88 //*****************************************************************************
89 SPDCALC_Handle SPDCALC_init(void *pMemory, const size_t numBytes);
90 
91 //*****************************************************************************
92 //
96 //*****************************************************************************
97 extern void SPDCALC_reset(SPDCALC_Handle handle);
98 
99 //*****************************************************************************
100 //
105 //*****************************************************************************
106 extern void SPDCALC_setParams(SPDCALC_Handle handle, const USER_Params *pUserParams);
107 
108 //*****************************************************************************
109 //
113 //*****************************************************************************
115 {
116  SPDCALC_Obj *obj = (SPDCALC_Obj *)handle;
117 
118  return(obj->speed_Hz);
119 }
120 
121 //*****************************************************************************
122 //
127 //*****************************************************************************
128 static __attribute__((always_inline))
130 {
131  SPDCALC_Obj *obj = (SPDCALC_Obj *)handle;
132 
133  obj->ref = theta;
134 
135  // error cal
136  obj->err = obj->ref - obj->fbk;
137 
138  // roll in the error
139  if(obj->err >= MATH_PI)
140  {
141  obj->err = obj->err - MATH_TWO_PI;
142  }
143  else if(obj->err <= -MATH_PI)
144  {
145  obj->err = obj->err + MATH_TWO_PI;
146  }
147 
148  // P and I control
149  obj->Up = obj->Kp * obj->err; // P control
150  obj->Ui += obj->Ki * obj->err; // I control
151  obj->Ui = MATH_sat(obj->Ui, obj->Umax, obj->Umin);
152 
153  // control output
154  obj->out = obj->Up + obj->Ui;
155  obj->out = MATH_sat(obj->out, obj->Umax, obj->Umin); // rad/s
156 
157  obj->speed_Hz = obj->out * MATH_ONE_OVER_TWO_PI;
158 
159  // Latest angle feedback estimation --> ( Fbk = integral of speed )
160  obj->fbk = obj->fbk + obj->out * obj->thetaDelta;
161 
162  // roll "Fbk" within -pi to pi
163  if(obj->fbk >= MATH_PI)
164  {
165  obj->fbk = obj->fbk - MATH_TWO_PI;
166  }
167  else if(obj->fbk <= -MATH_PI)
168  {
169  obj->fbk = obj->fbk + MATH_TWO_PI;
170  }
171 
172  return;
173 }
174 
175 //*****************************************************************************
176 //
177 // Close the Doxygen group.
179 //
180 //*****************************************************************************
181 
182 //*****************************************************************************
183 //
184 // Mark the end of the C bindings section for C++ compilers.
185 //
186 //*****************************************************************************
187 #ifdef __cplusplus
188 }
189 #endif
190 
191 #endif //end of SPEED_CALC_H definition
SPDCALC_Obj::Ui
float32_t Ui
Definition: speedcalc.h:68
SPDCALC_Obj::Kp
float32_t Kp
Definition: speedcalc.h:63
SPDCALC_Obj
Definition: speedcalc.h:58
SPDCALC_setParams
void SPDCALC_setParams(SPDCALC_Handle handle, const USER_Params *pUserParams)
Set the SPDCALC controller.
SPDCALC_getSpeedHz
static float32_t SPDCALC_getSpeedHz(SPDCALC_Handle handle)
Set the SPDCALC controller.
Definition: speedcalc.h:114
SPDCALC_Obj::Umin
float32_t Umin
Definition: speedcalc.h:66
SPDCALC_Obj::speed_Hz
float32_t speed_Hz
Definition: speedcalc.h:69
SPDCALC_Obj::Up
float32_t Up
Definition: speedcalc.h:67
USER_Params
Defines a structure for the user parameters.
Definition: userParams.h:75
SPDCALC_Obj::Umax
float32_t Umax
Definition: speedcalc.h:65
SPDCALC_Obj::out
float32_t out
Definition: speedcalc.h:62
SPDCALC_reset
void SPDCALC_reset(SPDCALC_Handle handle)
Set the SPDCALC controller.
SPDCALC_Obj::err
float32_t err
Definition: speedcalc.h:61
SPDCALC_run
static void SPDCALC_run(SPDCALC_Handle handle, float32_t theta)
Set the SPDCALC controller.
Definition: speedcalc.h:129
SPDCALC_Obj::Ki
float32_t Ki
Definition: speedcalc.h:64
SPDCALC_Obj::ref
float32_t ref
Definition: speedcalc.h:59
SPDCALC_Handle
struct _SPDCALC_obj_ * SPDCALC_Handle
Defines the ESMO handle.
Definition: speedcalc.h:75
SPDCALC_init
SPDCALC_Handle SPDCALC_init(void *pMemory, const size_t numBytes)
Set the SPDCALC controller.
SPDCALC_Obj::thetaDelta
float32_t thetaDelta
Definition: speedcalc.h:70
userParams.h
Contains the public interface for the HAL and EST modules.
SPDCALC_Obj::fbk
float32_t fbk
Definition: speedcalc.h:60
float32_t
float float32_t
Definition: dcl_common.h:58