AM263x Motor Control SDK  09.02.00
dcl_tcm.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 _DCL_TCM_H_
34 #define _DCL_TCM_H_
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
48 #include <math.h>
49 #include "dcl_fdlog.h"
50 #include "../dcl_common.h"
51 
54 typedef _DCL_VOLATILE struct dcl_tcm {
60  uint32_t mode;
61  uint32_t lead;
63 
66 #define TCM_DEFAULTS { FDLOG_DEFAULTS, FDLOG_DEFAULTS, FDLOG_DEFAULTS, 0.1, -0.1, 0, 10 }
67 
70 typedef enum dcl_tcm_states
71 {
73  TCM_IDLE = 1,
74  TCM_ARMED = 2,
78 
89 void DCL_initTCM(DCL_TCM *q, float32_t *addr, uint32_t size, uint32_t lead, float32_t tmin, float32_t tmax)
90 {
91  // assign and clear capture frame, and initialize index to end of lead frame
92  DCL_initLog(&(q->captFrame), addr, size);
93  DCL_clearLog(&(q->captFrame));
94  q->captFrame.dptr = q->captFrame.fptr + lead;
95 
96  // assign lead & monitor frame pointers
97  DCL_initLog(&(q->leadFrame), addr, lead);
98  DCL_initLog(&(q->moniFrame), q->captFrame.lptr - lead + 1, lead);
99 
100  // load remaining TCM vars
101  q->lead = lead;
102  q->trigMax = tmax;
103  q->trigMin = tmin;
104  q->mode = TCM_IDLE;
105 }
106 
113 {
114  DCL_clearLog(&(q->captFrame));
115  q->captFrame.dptr = q->captFrame.fptr + q->lead - 1;
116  DCL_resetLog(&(q->leadFrame));
117  DCL_resetLog(&(q->moniFrame));
118  q->mode = TCM_IDLE;
119 }
120 
128 uint16_t DCL_armTCM(DCL_TCM *q)
129 {
130  q->mode = (q->mode == TCM_IDLE) ? TCM_ARMED : TCM_IDLE;
131  return(q->mode);
132 }
133 
141 uint16_t DCL_runTCM(DCL_TCM *q, float32_t data)
142 {
143  switch(q->mode)
144  {
145  // TCM not initialized
146  case TCM_INVALID:
147  // idle: do nothing
148  case TCM_IDLE:
149  // complete: do nothing - results available in capture buffer
150  case TCM_COMPLETE:
151  default:
152  break;
153 
154  // armed: ready to begin capturing when either trigger threshold is crossed
155  case TCM_ARMED:
156  // check for trigger condition
157  if ((data > q->trigMax) || (data < q->trigMin))
158  {
159  // capture first data point & switch to capture mode
160  DCL_writeLog(&(q->captFrame), data);
161  DCL_writeLog(&(q->leadFrame), DCL_readLog(&(q->moniFrame)));
162  q->mode = TCM_CAPTURE;
163  }
164  else
165  {
166  // log data into monitor frame
167  DCL_writeLog(&(q->moniFrame), data);
168  }
169  break;
170 
171  // capture mode: acquiring data
172  case TCM_CAPTURE:
173  // check for full capture frame
174  if (q->captFrame.dptr > q->captFrame.fptr)
175  {
176  // write data into main frame
177  DCL_writeLog(&(q->captFrame), data);
178 
179  // check for full lead frame
180  if (q->leadFrame.dptr > q->leadFrame.fptr)
181  {
182  // un-wind monitor data into lead frame
183  DCL_writeLog(&(q->leadFrame), DCL_readLog(&(q->moniFrame)));
184  }
185  }
186  else
187  {
188  // TCM complete, capture frame spans full buffer
189  q->captFrame.fptr = q->leadFrame.fptr;
190  q->captFrame.dptr = q->captFrame.fptr;
191  q->mode = TCM_COMPLETE;
192  }
193  break;
194  }
195 
196  return(q->mode);
197 }
198 
207 {
208  float32_t tim = 0.0f;
209  float32_t rlt = 0.0f;
210  uint32_t size = DCL_getLogSize(elog);
211 
212  // set index pointer to buffer start
213  DCL_resetLog(elog);
214 
215  // accumulate ITAE data
216  while (size--)
217  {
218  rlt += fabs(*(elog->dptr++)) * tim;
219  tim += prd;
220  }
221  DCL_resetLog(elog);
222 
223  return(rlt);
224 }
225 
233 {
234  float32_t rlt = 0.0f;
235  uint32_t size = DCL_getLogSize(elog);
236 
237  // set index pointer to buffer start
238  DCL_resetLog(elog);
239 
240  // accumulate IAE data
241  while (size--)
242  {
243  rlt += fabs(*(elog->dptr++));
244  }
245  DCL_resetLog(elog);
246 
247  return(rlt);
248 }
249 
257 {
258  float32_t err;
259  float32_t rlt = 0.0f;
260  uint32_t size = DCL_getLogSize(elog);
261 
262  // set index pointers to buffer start
263  DCL_resetLog(elog);
264 
265  // accumulate IES data
266  while (size--)
267  {
268  err = *(elog->dptr++);
269  rlt += (err * err);
270  }
271 
272  // restore index pointer
273  DCL_resetLog(elog);
274 
275  return(rlt);
276 }
277 
280 #ifdef __cplusplus
281 }
282 #endif // extern "C"
283 
284 #endif // _DCL_TCM_H_
dcl_tcm::captFrame
DCL_FDLOG captFrame
Capture data frame.
Definition: dcl_tcm.h:57
TCM_COMPLETE
@ TCM_COMPLETE
Full data frame captured and available for read-out.
Definition: dcl_tcm.h:76
DCL_tcm_states
DCL_tcm_states
Enumerated TCM operating modes.
Definition: dcl_tcm.h:71
dcl_tcm::leadFrame
DCL_FDLOG leadFrame
Lead data frame.
Definition: dcl_tcm.h:56
dcl_tcm::trigMax
float32_t trigMax
Upper trigger threshold.
Definition: dcl_tcm.h:58
TCM_INVALID
@ TCM_INVALID
Buffer pointers not initialized.
Definition: dcl_tcm.h:72
dcl_tcm::trigMin
float32_t trigMin
Lower trigger threshold.
Definition: dcl_tcm.h:59
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:89
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:183
DCL_resetLog
_DCL_CODE_ACCESS void DCL_resetLog(DCL_FDLOG *buf)
Resets the data index pointer to start of buffer.
Definition: dcl_fdlog.h:135
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:165
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:256
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:232
_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:92
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:204
dcl_tcm::moniFrame
DCL_FDLOG moniFrame
Monitor data frame.
Definition: dcl_tcm.h:55
dcl_tcm
Defines the TCM structure.
Definition: dcl_tcm.h:54
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:155
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:112
DCL_runTCM
_DCL_CODE_ACCESS uint16_t DCL_runTCM(DCL_TCM *q, float32_t data)
Runs the TCM module.
Definition: dcl_tcm.h:141
TCM_IDLE
@ TCM_IDLE
Memory initialized but module not armed.
Definition: dcl_tcm.h:73
TCM
_DCL_VOLATILE struct dcl_tcm TCM
DCL_armTCM
_DCL_CODE_ACCESS uint16_t DCL_armTCM(DCL_TCM *q)
Changes the TCM mode to "TCM_ARMED".
Definition: dcl_tcm.h:128
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:75
_DCL_VOLATILE
#define _DCL_VOLATILE
Defines volatile for DCL strctures.
Definition: dcl_common.h:79
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:60
dcl_tcm::lead
uint32_t lead
Lead frame size in 32-bit words = trigger crossing index.
Definition: dcl_tcm.h:61
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:206
TCM_ARMED
@ TCM_ARMED
Armed: capturing monitor frame data and waiting for trigger.
Definition: dcl_tcm.h:74