1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
/*!
********************************************************************
* Description: tcq.c
*\brief queue handling functions for trajectory planner
* These following functions implement the motion queue that
* is fed by tpAddLine/tpAddCircle and consumed by tpRunCycle.
* They have been fully working for a long time and a wise programmer
* won't mess with them.
*
*\author Derived from a work by Fred Proctor & Will Shackleford
*\author rewritten by Chris Radek
*
* License: GPL Version 2
* System: Linux
*
* Copyright (c) 2004 All rights reserved.
*
********************************************************************/
#include "tcq.h"
#include <stddef.h>
/** Return 0 if queue is valid, -1 if not */
static inline int tcqCheck(TC_QUEUE_STRUCT const * const tcq)
{
if ((0 == tcq) || (0 == tcq->queue))
{
return -1;
}
return 0;
}
/*! tcqCreate() function
*
* \brief Creates a new queue for TC elements.
*
* This function creates a new queue for TC elements.
* It gets called by tpCreate()
*
* @param tcq pointer to the new TC_QUEUE_STRUCT
* @param _size size of the new queue
* @param tcSpace holds the space allocated for the new queue, allocated in motion.c
*
* @return int returns success or failure
*/
int tcqCreate(TC_QUEUE_STRUCT * const tcq, int _size, TC_STRUCT * const tcSpace)
{
if (_size <= 0 || 0 == tcq) {
return -1;
} else {
tcq->queue = tcSpace;
tcq->size = _size;
tcq->_len = 0;
tcq->start = tcq->end = 0;
tcq->allFull = 0;
if (0 == tcq->queue) {
return -1;
}
return 0;
}
}
/*! tcqDelete() function
*
* \brief Deletes a queue holding TC elements.
*
* This function creates deletes a queue. It doesn't free the space
* only throws the pointer away.
* It gets called by tpDelete()
* \todo FIXME, it seems tpDelete() is gone, and this function isn't used.
*
* @param tcq pointer to the TC_QUEUE_STRUCT
*
* @return int returns success
*/
int tcqDelete(TC_QUEUE_STRUCT * const tcq)
{
if (!tcqCheck(tcq)) {
/* free(tcq->queue); */
tcq->queue = 0;
}
return 0;
}
/*! tcqInit() function
*
* \brief Initializes a queue with TC elements.
*
* This function initializes a queue with TC elements.
* It gets called by tpClear() and
* by tpRunCycle() when we are aborting
*
* @param tcq pointer to the TC_QUEUE_STRUCT
*
* @return int returns success or failure (if no tcq found)
*/
int tcqInit(TC_QUEUE_STRUCT * const tcq)
{
if (tcqCheck(tcq)) return -1;
tcq->_len = 0;
tcq->start = tcq->end = 0;
tcq->allFull = 0;
return 0;
}
/*! tcqPut() function
*
* \brief puts a TC element at the end of the queue
*
* This function adds a tc element at the end of the queue.
* It gets called by tpAddLine() and tpAddCircle()
*
* @param tcq pointer to the new TC_QUEUE_STRUCT
* @param tc the new TC element to be added
*
* @return int returns success or failure
*/
int tcqPut(TC_QUEUE_STRUCT * const tcq, TC_STRUCT const * const tc)
{
/* check for initialized */
if (tcqCheck(tcq)) return -1;
/* check for allFull, so we don't overflow the queue */
if (tcq->allFull) {
return -1;
}
/* add it */
tcq->queue[tcq->end] = *tc;
tcq->_len++;
/* update end ptr, modulo size of queue */
tcq->end = (tcq->end + 1) % tcq->size;
/* set allFull flag if we're really full */
if (tcq->end == tcq->start) {
tcq->allFull = 1;
}
return 0;
}
/*! tcqPopBack() function
*
* \brief removes the newest TC element (converse of tcqRemove)
*
* @param tcq pointer to the TC_QUEUE_STRUCT
*
* @return int returns success or failure
*/
int tcqPopBack(TC_QUEUE_STRUCT * const tcq)
{
/* check for initialized */
if (tcqCheck(tcq)) return -1;
/* Too short to pop! */
if (tcq->_len < 1) {
return -1;
}
int n = tcq->end - 1 + tcq->size;
tcq->end = n % tcq->size;
tcq->_len--;
return 0;
}
/*! tcqRemove() function
*
* \brief removes n items from the queue
*
* This function removes the first n items from the queue,
* after checking that they can be removed
* (queue initialized, queue not empty, enough elements in it)
* Function gets called by tpRunCycle() with n=1
* \todo FIXME: Optimize the code to remove only 1 element, might speed it up
*
* @param tcq pointer to the new TC_QUEUE_STRUCT
* @param n the number of TC elements to be removed
*
* @return int returns success or failure
*/
int tcqRemove(TC_QUEUE_STRUCT * const tcq, int n)
{
if (n <= 0) {
return 0; /* okay to remove 0 or fewer */
}
if (tcqCheck(tcq) || ((tcq->start == tcq->end) && !tcq->allFull) ||
(n > tcq->_len)) { /* too many requested */
return -1;
}
/* update start ptr and reset allFull flag and len */
tcq->start = (tcq->start + n) % tcq->size;
tcq->allFull = 0;
tcq->_len -= n;
return 0;
}
/*! tcqLen() function
*
* \brief returns the number of elements in the queue
*
* Function gets called by tpSetVScale(), tpAddLine(), tpAddCircle()
*
* @param tcq pointer to the TC_QUEUE_STRUCT
*
* @return int returns number of elements
*/
int tcqLen(TC_QUEUE_STRUCT const * const tcq)
{
if (tcqCheck(tcq)) return -1;
return tcq->_len;
}
/*! tcqItem() function
*
* \brief gets the n-th TC element in the queue, without removing it
*
* Function gets called by tpSetVScale(), tpRunCycle(), tpIsPaused()
*
* @param tcq pointer to the TC_QUEUE_STRUCT
*
* @return TC_STRUCT returns the TC elements
*/
TC_STRUCT * tcqItem(TC_QUEUE_STRUCT const * const tcq, int n)
{
if (tcqCheck(tcq) || (n < 0) || (n >= tcq->_len)) return NULL;
return &(tcq->queue[(tcq->start + n) % tcq->size]);
}
/*!
* \def TC_QUEUE_MARGIN
* sets up a margin at the end of the queue, to reduce effects of race conditions
*/
#define TC_QUEUE_MARGIN 20
/*! tcqFull() function
*
* \brief get the full status of the queue
* Function returns full if the count is closer to the end of the queue than TC_QUEUE_MARGIN
*
* Function called by update_status() in control.c
*
* @param tcq pointer to the TC_QUEUE_STRUCT
*
* @return int returns status (0==not full, 1==full)
*/
int tcqFull(TC_QUEUE_STRUCT const * const tcq)
{
if (tcqCheck(tcq)) {
return 1; /* null queue is full, for safety */
}
/* call the queue full if the length is into the margin, so reduce the
effect of a race condition where the appending process may not see the
full status immediately and send another motion */
if (tcq->size <= TC_QUEUE_MARGIN) {
/* no margin available, so full means really all full */
return tcq->allFull;
}
if (tcq->_len >= tcq->size - TC_QUEUE_MARGIN) {
/* we're into the margin, so call it full */
return 1;
}
/* we're not into the margin */
return 0;
}
/*! tcqLast() function
*
* \brief gets the last TC element in the queue, without removing it
*
*
* @param tcq pointer to the TC_QUEUE_STRUCT
*
* @return TC_STRUCT returns the TC element
*/
TC_STRUCT *tcqLast(TC_QUEUE_STRUCT const * const tcq)
{
if (tcqCheck(tcq)) {
return NULL;
}
if (tcq->_len == 0) {
return NULL;
}
//Fix for negative modulus error
int n = tcq->end-1 + tcq->size;
return &(tcq->queue[n % tcq->size]);
}
|