speedcalc.h
Go to the documentation of this file.
1 //#############################################################################
2 // $TI Release: MotorControl SDK v1.00.00.00 $
3 // $Release Date: Mon Mar 11 18:37:40 CDT 2019 $
4 // $Copyright:
5 // Copyright (C) 2017-2018 Texas Instruments Incorporated - http://www.ti.com/
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
11 // Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 //
14 // Redistributions in binary form must reproduce the above copyright
15 // notice, this list of conditions and the following disclaimer in the
16 // documentation and/or other materials provided with the
17 // distribution.
18 //
19 // Neither the name of Texas Instruments Incorporated nor the names of
20 // its contributors may be used to endorse or promote products derived
21 // from this software without specific prior written permission.
22 //
23 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 // $
35 //#############################################################################
36 
37 #ifndef SPEED_CALC_H
38 #define SPEED_CALC_H
39 
44 
45 //*****************************************************************************
46 //
47 // If building with a C++ compiler, make all of the definitions in this header
48 // have a C binding.
49 //
50 //*****************************************************************************
51 #ifdef __cplusplus
52 extern "C"
53 {
54 #endif
55 
56 //*****************************************************************************
57 //
60 //
61 //*****************************************************************************
62 
63 // the includes
64 #ifdef __TMS320C28XX_CLA__
65 #include "libraries/math/src/float/CLAmath.h"
66 #else
67 #include <math.h>
68 #endif
69 
71 
72 //modules
73 #include "userParams.h"
74 
75 
76 typedef struct _SPDCALC_obj_
77 {
78  float32_t ref; // Input: reference set-point
79  float32_t fbk; // Input: feedback
80  float32_t err; // Error
81  float32_t out; // Output: controller output
82  float32_t Kp; // Parameter: proportional loop gain
83  float32_t Ki; // Parameter: integral gain
84  float32_t Umax; // Parameter: upper saturation limit
85  float32_t Umin; // Parameter: lower saturation limit
86  float32_t Up; // Data: proportional term
87  float32_t Ui; // Data: integral term
88  float32_t speed_Hz; // Output freq
89  float32_t thetaDelta; // Parameter: theta maximum
90 } SPDCALC_Obj;
91 
94 typedef struct _SPDCALC_obj_ *SPDCALC_Handle;
95 
96 // ***************************************
97 // extern functions
98 // ***************************************
101 SPDCALC_Handle SPDCALC_init(void *pMemory, const size_t numBytes);
102 
105 void SPDCALC_reset(SPDCALC_Handle handle);
106 
109 void SPDCALC_setParams(SPDCALC_Handle handle, const USER_Params *pUserParams);
110 
114 {
115  SPDCALC_Obj *obj = (SPDCALC_Obj *)handle;
116 
117  return(obj->speed_Hz);
118 }
119 
122 static inline void SPDCALC_run(SPDCALC_Handle handle, float32_t theta)
123 {
124  SPDCALC_Obj *obj = (SPDCALC_Obj *)handle;
125 
126  obj->ref = theta;
127 
128  // error cal
129  obj->err = obj->ref - obj->fbk;
130 
131  // roll in the error
132  if(obj->err >= MATH_PI)
133  {
134  obj->err = obj->err - MATH_TWO_PI;
135  }
136  else if(obj->err <= -MATH_PI)
137  {
138  obj->err = obj->err + MATH_TWO_PI;
139  }
140 
141  // P and I control
142  obj->Up = obj->Kp * obj->err; // P control
143  obj->Ui += obj->Ki * obj->err; // I control
144  obj->Ui = __fsat(obj->Ui, obj->Umax, obj->Umin);
145 
146  // control output
147  obj->out = obj->Up + obj->Ui;
148  obj->out = __fsat(obj->out, obj->Umax, obj->Umin); // rad/s
149 
150  obj->speed_Hz = obj->out * MATH_ONE_OVER_TWO_PI;
151 
152  // Latest angle feedback estimation --> ( Fbk = integral of speed )
153  obj->fbk = obj->fbk + obj->out * obj->thetaDelta;
154 
155  // roll "Fbk" within -pi to pi
156  if(obj->fbk >= MATH_PI)
157  {
158  obj->fbk = obj->fbk - MATH_TWO_PI;
159  }
160  else if(obj->fbk <= -MATH_PI)
161  {
162  obj->fbk = obj->fbk + MATH_TWO_PI;
163  }
164 
165  return;
166 }
167 
168 //*****************************************************************************
169 //
170 // Close the Doxygen group.
172 //
173 //*****************************************************************************
174 
175 //*****************************************************************************
176 //
177 // Mark the end of the C bindings section for C++ compilers.
178 //
179 //*****************************************************************************
180 #ifdef __cplusplus
181 }
182 #endif
183 
184 #endif //end of SPEED_CALC_H definition
_SPDCALC_obj_::speed_Hz
float32_t speed_Hz
Definition: speedcalc.h:88
SPDCALC_Obj
struct _SPDCALC_obj_ SPDCALC_Obj
_SPDCALC_obj_::Kp
float32_t Kp
Definition: speedcalc.h:82
float32_t
float float32_t
Definition: sfra_f32.h:42
MATH_ONE_OVER_TWO_PI
#define MATH_ONE_OVER_TWO_PI
Defines 1/(2*pi)
Definition: math.h:133
MATH_TWO_PI
#define MATH_TWO_PI
Defines 2*pi.
Definition: math.h:154
_SPDCALC_obj_::thetaDelta
float32_t thetaDelta
Definition: speedcalc.h:89
SPDCALC_init
SPDCALC_Handle SPDCALC_init(void *pMemory, const size_t numBytes)
Set the SPDCALC controller.
MATH_PI
#define MATH_PI
Defines pi.
Definition: math.h:140
_SPDCALC_obj_::Ki
float32_t Ki
Definition: speedcalc.h:83
SPDCALC_Handle
struct _SPDCALC_obj_ * SPDCALC_Handle
Defines the ESMO handle.
Definition: speedcalc.h:94
SPDCALC_getSpeedHz
static float32_t SPDCALC_getSpeedHz(SPDCALC_Handle handle)
Set the SPDCALC controller.
Definition: speedcalc.h:113
_SPDCALC_obj_::Umin
float32_t Umin
Definition: speedcalc.h:85
math.h
SPDCALC_run
static void SPDCALC_run(SPDCALC_Handle handle, float32_t theta)
Set the SPDCALC controller.
Definition: speedcalc.h:122
_SPDCALC_obj_::err
float32_t err
Definition: speedcalc.h:80
_SPDCALC_obj_::ref
float32_t ref
Definition: speedcalc.h:78
_SPDCALC_obj_::Umax
float32_t Umax
Definition: speedcalc.h:84
_SPDCALC_obj_::Ui
float32_t Ui
Definition: speedcalc.h:87
_SPDCALC_obj_::Up
float32_t Up
Definition: speedcalc.h:86
SPDCALC_setParams
void SPDCALC_setParams(SPDCALC_Handle handle, const USER_Params *pUserParams)
Set the SPDCALC controller.
_SPDCALC_obj_::fbk
float32_t fbk
Definition: speedcalc.h:79
_SPDCALC_obj_
Definition: speedcalc.h:76
_SPDCALC_obj_::out
float32_t out
Definition: speedcalc.h:81
SPDCALC_reset
void SPDCALC_reset(SPDCALC_Handle handle)
Set the SPDCALC controller.
_USER_Params_
Defines a structure for the user parameters.
Definition: include/userParams.h:98

Copyright 2023, Texas Instruments Incorporated