AM261x Motor Control SDK  10.02.00
dcl_tcm.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023-2025 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 #ifndef _DCL_TCM_H_
33 #define _DCL_TCM_H_
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
47 #include "dcl_fdlog.h"
48 #include "../dcl_common.h"
49 
52 typedef _DCL_VOLATILE struct dcl_tcm {
58  uint32_t mode;
59  uint32_t lead;
61 
64 #define TCM_DEFAULTS { FDLOG_DEFAULTS, FDLOG_DEFAULTS, FDLOG_DEFAULTS, 0.1f, -0.1f, 0, 10 }
65 
68 typedef enum dcl_tcm_states
69 {
71  TCM_IDLE = 1,
72  TCM_ARMED = 2,
76 
87 void DCL_initTCM(DCL_TCM *q, float32_t *addr, uint32_t size, uint32_t lead, float32_t tmin, float32_t tmax)
88 {
89  // assign and clear capture frame, and initialize index to end of lead frame
90  DCL_initLog(&(q->captFrame), addr, size);
91  DCL_clearLog(&(q->captFrame));
92  q->captFrame.dptr = q->captFrame.fptr + lead;
93 
94  // assign lead & monitor frame pointers
95  DCL_initLog(&(q->leadFrame), addr, lead);
96  DCL_initLog(&(q->moniFrame), q->captFrame.lptr - lead + 1, lead);
97 
98  // load remaining TCM vars
99  q->lead = lead;
100  q->trigMax = tmax;
101  q->trigMin = tmin;
102  q->mode = TCM_IDLE;
103 }
104 
111 {
112  DCL_clearLog(&(q->captFrame));
113  q->captFrame.dptr = q->captFrame.fptr + q->lead - 1;
114  DCL_resetLog(&(q->leadFrame));
115  DCL_resetLog(&(q->moniFrame));
116  q->mode = TCM_IDLE;
117 }
118 
126 uint16_t DCL_armTCM(DCL_TCM *q)
127 {
128  q->mode = (q->mode == TCM_IDLE) ? TCM_ARMED : TCM_IDLE;
129  return(q->mode);
130 }
131 
139 uint16_t DCL_runTCM(DCL_TCM *q, float32_t data)
140 {
141  switch(q->mode)
142  {
143  // TCM not initialized
144  case TCM_INVALID:
145  // idle: do nothing
146  case TCM_IDLE:
147  // complete: do nothing - results available in capture buffer
148  case TCM_COMPLETE:
149  default:
150  break;
151 
152  // armed: ready to begin capturing when either trigger threshold is crossed
153  case TCM_ARMED:
154  // check for trigger condition
155  if ((data > q->trigMax) || (data < q->trigMin))
156  {
157  // capture first data point & switch to capture mode
158  DCL_writeLog(&(q->captFrame), data);
159  DCL_writeLog(&(q->leadFrame), DCL_readLog(&(q->moniFrame)));
160  q->mode = TCM_CAPTURE;
161  }
162  else
163  {
164  // log data into monitor frame
165  DCL_writeLog(&(q->moniFrame), data);
166  }
167  break;
168 
169  // capture mode: acquiring data
170  case TCM_CAPTURE:
171  // check for full capture frame
172  if (q->captFrame.dptr > q->captFrame.fptr)
173  {
174  // write data into main frame
175  DCL_writeLog(&(q->captFrame), data);
176 
177  // check for full lead frame
178  if (q->leadFrame.dptr > q->leadFrame.fptr)
179  {
180  // un-wind monitor data into lead frame
181  DCL_writeLog(&(q->leadFrame), DCL_readLog(&(q->moniFrame)));
182  }
183  }
184  else
185  {
186  // TCM complete, capture frame spans full buffer
187  q->captFrame.fptr = q->leadFrame.fptr;
188  q->captFrame.dptr = q->captFrame.fptr;
189  q->mode = TCM_COMPLETE;
190  }
191  break;
192  }
193 
194  return(q->mode);
195 }
196 
205 {
206  float32_t tim = 0.0f;
207  float32_t rlt = 0.0f;
208  uint32_t size = DCL_getLogSize(elog);
209 
210  // set index pointer to buffer start
211  DCL_resetLog(elog);
212 
213  // accumulate ITAE data
214  while (size--)
215  {
216  rlt += fabsf(*(elog->dptr++)) * tim;
217  tim += prd;
218  }
219  DCL_resetLog(elog);
220 
221  return(rlt);
222 }
223 
231 {
232  float32_t rlt = 0.0f;
233  uint32_t size = DCL_getLogSize(elog);
234 
235  // set index pointer to buffer start
236  DCL_resetLog(elog);
237 
238  // accumulate IAE data
239  while (size--)
240  {
241  rlt += fabsf(*(elog->dptr++));
242  }
243  DCL_resetLog(elog);
244 
245  return(rlt);
246 }
247 
255 {
256  float32_t err;
257  float32_t rlt = 0.0f;
258  uint32_t size = DCL_getLogSize(elog);
259 
260  // set index pointers to buffer start
261  DCL_resetLog(elog);
262 
263  // accumulate IES data
264  while (size--)
265  {
266  err = *(elog->dptr++);
267  rlt += (err * err);
268  }
269 
270  // restore index pointer
271  DCL_resetLog(elog);
272 
273  return(rlt);
274 }
275 
278 #ifdef __cplusplus
279 }
280 #endif // extern "C"
281 
282 #endif // _DCL_TCM_H_
dcl_tcm::captFrame
DCL_FDLOG captFrame
Capture data frame.
Definition: dcl_tcm.h:55
TCM_COMPLETE
@ TCM_COMPLETE
Full data frame captured and available for read-out.
Definition: dcl_tcm.h:74
DCL_tcm_states
DCL_tcm_states
Enumerated TCM operating modes.
Definition: dcl_tcm.h:69
dcl_tcm::leadFrame
DCL_FDLOG leadFrame
Lead data frame.
Definition: dcl_tcm.h:54
dcl_tcm::trigMax
float32_t trigMax
Upper trigger threshold.
Definition: dcl_tcm.h:56
TCM_INVALID
@ TCM_INVALID
Buffer pointers not initialized.
Definition: dcl_tcm.h:70
dcl_tcm::trigMin
float32_t trigMin
Lower trigger threshold.
Definition: dcl_tcm.h:57
DCL_initTCM
_DCL_CODE_ACCESS void DCL_initTCM(DCL_TCM *q, float32_t *addr, uint32_t size, uint32_t lead, float32_t tmin, float32_t tmax)
Resets the TCM module: clears buffers and sets idle mode.
Definition: dcl_tcm.h:87
DCL_writeLog
_DCL_CODE_ACCESS float32_t DCL_writeLog(DCL_FDLOG *buf, float32_t data)
Writes a data point into the buffer and advances the indexing pointer, wrapping if necessary....
Definition: dcl_fdlog.h:182
DCL_resetLog
_DCL_CODE_ACCESS void DCL_resetLog(DCL_FDLOG *buf)
Resets the data index pointer to start of buffer.
Definition: dcl_fdlog.h:134
DCL_initLog
_DCL_CODE_ACCESS void DCL_initLog(DCL_FDLOG *buf, float32_t *addr, uint32_t size)
Assigns the buffer pointers to a memory block or array and sets the data index pointer to the first a...
Definition: dcl_fdlog.h:164
DCL_runIES
_DCL_CODE_ACCESS float32_t DCL_runIES(DCL_FDLOG *elog)
Computes IES performance index from a log of servo error.
Definition: dcl_tcm.h:254
DCL_runIAE
_DCL_CODE_ACCESS float32_t DCL_runIAE(DCL_FDLOG *elog)
Computes IAE performance index from a log of servo error.
Definition: dcl_tcm.h:230
_DCL_CODE_ACCESS
#define _DCL_CODE_ACCESS
Defines the scope of dcl functions.
Definition: dcl_common.h:63
DCL_getLogSize
#define DCL_getLogSize(buf)
Obtain the total size of buffer.
Definition: dcl_fdlog.h:91
DCL_readLog
_DCL_CODE_ACCESS float32_t DCL_readLog(DCL_FDLOG *buf)
Reads a data point from the buffer and then advances the indexing pointer, wrapping if necessary.
Definition: dcl_fdlog.h:203
dcl_tcm::moniFrame
DCL_FDLOG moniFrame
Monitor data frame.
Definition: dcl_tcm.h:53
dcl_tcm
Defines the TCM structure.
Definition: dcl_tcm.h:52
DCL_clearLog
#define DCL_clearLog(buf)
Clears the buffer contents by writing 0 to all elements and resets the data index pointer to the star...
Definition: dcl_fdlog.h:154
DCL_resetTCM
_DCL_CODE_ACCESS void DCL_resetTCM(DCL_TCM *q)
Resets the TCM module: clears all frame buffers and sets idle mode.
Definition: dcl_tcm.h:110
DCL_runTCM
_DCL_CODE_ACCESS uint16_t DCL_runTCM(DCL_TCM *q, float32_t data)
Runs the TCM module.
Definition: dcl_tcm.h:139
TCM_IDLE
@ TCM_IDLE
Memory initialized but module not armed.
Definition: dcl_tcm.h:71
DCL_armTCM
_DCL_CODE_ACCESS uint16_t DCL_armTCM(DCL_TCM *q)
Changes the TCM mode to "TCM_ARMED".
Definition: dcl_tcm.h:126
DCL_FDLOG
_DCL_VOLATILE struct dcl_fdlog DCL_FDLOG
TCM_CAPTURE
@ TCM_CAPTURE
Triggered: logging data into capture frame and un-winding monitor frame.
Definition: dcl_tcm.h:73
_DCL_VOLATILE
#define _DCL_VOLATILE
Defines volatile for DCL strctures Flags can be defined in dcl.h or user files before including DCL l...
Definition: dcl_common.h:75
DCL_TCM
_DCL_VOLATILE struct dcl_tcm DCL_TCM
dcl_fdlog.h
Defines a 32-bit floating-point data logger strcture and related functions.
float32_t
float float32_t
Definition: dcl_common.h:58
dcl_tcm::mode
uint32_t mode
Operating mode.
Definition: dcl_tcm.h:58
dcl_tcm::lead
uint32_t lead
Lead frame size in 32-bit words = trigger crossing index.
Definition: dcl_tcm.h:59
DCL_runITAE
_DCL_CODE_ACCESS float32_t DCL_runITAE(DCL_FDLOG *elog, float32_t prd)
Computes ITAE performance index from a log of servo error.
Definition: dcl_tcm.h:204
TCM_ARMED
@ TCM_ARMED
Armed: capturing monitor frame data and waiting for trigger.
Definition: dcl_tcm.h:72