-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathDMCC-py.c
More file actions
433 lines (370 loc) · 13.8 KB
/
DMCC-py.c
File metadata and controls
433 lines (370 loc) · 13.8 KB
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//
// DMCC-py.c - DMCC python interface
//
#include "Python.h"
#include "DMCC.h"
static PyObject *
dmcc_setMotor(PyObject *self, PyObject *args)
{
int nBoard;
int nMotor;
int nPower;
// Make sure szErrorMsg is not on the stack
// -- downside is that we could have concurrency issues with different
// threads, but you know what, there should only be one error message
// at a time. If you have it from multiple threads, your code is fubar'ed
// anyways!
static char szErrorMsg[80];
// DMCC.setMotor takes 3 arguments: board number, motor number, power
if (!PyArg_ParseTuple(args, "iii:setMotor", &nBoard, &nMotor, &nPower)) {
return NULL;
}
// validate the board number
if ((nBoard < 0) || (nBoard > 3)) {
sprintf(szErrorMsg, "Board number %d is invalid. Board number must be between 0 and 3.",
nBoard);
PyErr_SetString(PyExc_IndexError,szErrorMsg);
return NULL;
}
// validate the motor number
if ((nMotor < 1) || (nMotor > 2)) {
sprintf(szErrorMsg, "Motor number %d is invalid. Motor number must be between 1 and 2.",
nMotor);
PyErr_SetString(PyExc_IndexError,szErrorMsg);
return NULL;
}
// validate the power
if ((nPower < -10000) || (nPower > 10000)) {
sprintf(szErrorMsg, "Power %d is invalid. Power must be between -10000 and 10000.",
nPower);
PyErr_SetString(PyExc_IndexError,szErrorMsg);
return NULL;
}
int session;
session = DMCCstart(nBoard);
setMotorPower(session, nMotor, nPower);
DMCCend(session);
return Py_BuildValue("i", 0);
}
static PyObject *
dmcc_getMotorVoltageInt(PyObject *self, PyObject *args)
{
int nBoard;
// Make sure szErrorMsg is not on the stack
// -- downside is that we could have concurrency issues with different
// threads, but you know what, there should only be one error message
// at a time. If you have it from multiple threads, your code is fubar'ed
// anyways!
static char szErrorMsg[80];
// DMCC.getMotorVoltageInt takes 1 argument: board number - MotorVoltage is
// actually the voltage at middle connector on the board
if (!PyArg_ParseTuple(args, "i:getMotorVoltage", &nBoard)) {
return NULL;
}
// validate the board number
if ((nBoard < 0) || (nBoard > 3)) {
sprintf(szErrorMsg, "Board number %d is invalid. Board number must be between 0 and 3.",
nBoard);
PyErr_SetString(PyExc_IndexError,szErrorMsg);
return NULL;
}
int session;
unsigned int voltage;
session = DMCCstart(nBoard);
voltage = getMotorVoltage(session);
DMCCend(session);
return Py_BuildValue("i", voltage);
}
static PyObject *
dmcc_getMotorVoltage(PyObject *self, PyObject *args)
{
int nBoard;
// Make sure szErrorMsg is not on the stack
// -- downside is that we could have concurrency issues with different
// threads, but you know what, there should only be one error message
// at a time. If you have it from multiple threads, your code is fubar'ed
// anyways!
static char szErrorMsg[80];
// DMCC.getMotorVoltage takes 1 argument: board number - MotorVoltage is
// actually the voltage at middle connector on the board
if (!PyArg_ParseTuple(args, "i:getMotorVoltage", &nBoard)) {
return NULL;
}
// validate the board number
if ((nBoard < 0) || (nBoard > 3)) {
sprintf(szErrorMsg, "Board number %d is invalid. Board number must be between 0 and 3.",
nBoard);
PyErr_SetString(PyExc_IndexError,szErrorMsg);
return NULL;
}
int session;
unsigned int voltage;
double fVoltage;
session = DMCCstart(nBoard);
voltage = getMotorVoltage(session);
DMCCend(session);
fVoltage = voltage * 1.0 / 1000.0;
return Py_BuildValue("d", fVoltage);
}
static PyObject *
dmcc_getMotorCurrent(PyObject *self, PyObject *args)
{
int nBoard;
int nMotor;
// Make sure szErrorMsg is not on the stack
// -- downside is that we could have concurrency issues with different
// threads, but you know what, there should only be one error message
// at a time. If you have it from multiple threads, your code is fubar'ed
// anyways!
static char szErrorMsg[80];
// DMCC.getMotorCurrent takes 2 arguments: board number, motor number
if (!PyArg_ParseTuple(args, "ii:getQEI", &nBoard, &nMotor)) {
return NULL;
}
// validate the board number
if ((nBoard < 0) || (nBoard > 3)) {
sprintf(szErrorMsg, "Board number %d is invalid. Board number must be between 0 and 3.",
nBoard);
PyErr_SetString(PyExc_IndexError,szErrorMsg);
return NULL;
}
// validate the motor number
if ((nMotor < 1) || (nMotor > 2)) {
sprintf(szErrorMsg, "Motor number %d is invalid. Motor number must be 1 or 2.",
nMotor);
PyErr_SetString(PyExc_IndexError,szErrorMsg);
return NULL;
}
int session;
unsigned int current;
session = DMCCstart(nBoard);
current = getMotorCurrent(session,nMotor);
DMCCend(session);
return Py_BuildValue("i", current);
}
static PyObject *
dmcc_getQEI(PyObject *self, PyObject *args)
{
int nBoard;
int nMotor;
// Make sure szErrorMsg is not on the stack
// -- downside is that we could have concurrency issues with different
// threads, but you know what, there should only be one error message
// at a time. If you have it from multiple threads, your code is fubar'ed
// anyways!
static char szErrorMsg[80];
// DMCC.getQEI takes 2 arguments: board number, motor number
if (!PyArg_ParseTuple(args, "ii:getQEI", &nBoard, &nMotor)) {
return NULL;
}
// validate the board number
if ((nBoard < 0) || (nBoard > 3)) {
sprintf(szErrorMsg, "Board number %d is invalid. Board number must be between 0 and 3.",
nBoard);
PyErr_SetString(PyExc_IndexError,szErrorMsg);
return NULL;
}
// validate the motor number
if ((nMotor < 1) || (nMotor > 2)) {
sprintf(szErrorMsg, "Motor number %d is invalid. Motor number must be 1 or 2.",
nMotor);
PyErr_SetString(PyExc_IndexError,szErrorMsg);
return NULL;
}
int session;
unsigned int nQEI;
session = DMCCstart(nBoard);
nQEI = getQEI(session, nMotor);
DMCCend(session);
return Py_BuildValue("i", nQEI);
}
static PyObject *
dmcc_getQEIVel(PyObject *self, PyObject *args)
{
int nBoard;
int nMotor;
// Make sure szErrorMsg is not on the stack
// -- downside is that we could have concurrency issues with different
// threads, but you know what, there should only be one error message
// at a time. If you have it from multiple threads, your code is fubar'ed
// anyways!
static char szErrorMsg[80];
// DMCC.getQEIVel takes 2 arguments: board number, motor number
if (!PyArg_ParseTuple(args, "ii:getQEI", &nBoard, &nMotor)) {
return NULL;
}
// validate the board number
if ((nBoard < 0) || (nBoard > 3)) {
sprintf(szErrorMsg, "Board number %d is invalid. Board number must be between 0 and 3.",
nBoard);
PyErr_SetString(PyExc_IndexError,szErrorMsg);
return NULL;
}
// validate the motor number
if ((nMotor < 1) || (nMotor > 2)) {
sprintf(szErrorMsg, "Motor number %d is invalid. Motor number must be 1 or 2.",
nMotor);
PyErr_SetString(PyExc_IndexError,szErrorMsg);
return NULL;
}
int session;
int vel;
session = DMCCstart(nBoard);
vel = getQEIVel(session, nMotor);
DMCCend(session);
return Py_BuildValue("i", vel);
}
static PyObject *
dmcc_setTargetPos(PyObject *self, PyObject *args)
{
unsigned int nBoard;
unsigned int nMotor;
unsigned int nPosition;
// Make sure szErrorMsg is not on the stack
// -- downside is that we could have concurrency issues with different
// threads, but you know what, there should only be one error message
// at a time. If you have it from multiple threads, your code is fubar'ed
// anyways!
static char szErrorMsg[80];
// DMCC.setTargetPos takes 3 arguments: board number, motor number, Position
if (!PyArg_ParseTuple(args, "III:setMotor", &nBoard, &nMotor, &nPosition)) {
return NULL;
}
// validate the board number
if (nBoard > 3) {
sprintf(szErrorMsg, "Board number %d is invalid. Board number must be between 0 and 3.",
nBoard);
PyErr_SetString(PyExc_IndexError,szErrorMsg);
return NULL;
}
// validate the motor number
if ((nMotor < 1) || (nMotor > 2)) {
sprintf(szErrorMsg, "Motor number %d is invalid. Motor number must be 1 or 2.",
nMotor);
PyErr_SetString(PyExc_IndexError,szErrorMsg);
return NULL;
}
int session;
session = DMCCstart(nBoard);
setTargetPos(session, nMotor, nPosition);
DMCCend(session);
return Py_BuildValue("i", 0);
}
static PyObject *
dmcc_setPIDConstants(PyObject *self, PyObject *args)
{
unsigned int nBoard;
unsigned int nMotor;
unsigned int posOrVel;
int P;
int I;
int D;
// Make sure szErrorMsg is not on the stack
// -- downside is that we could have concurrency issues with different
// threads, but you know what, there should only be one error message
// at a time. If you have it from multiple threads, your code is fubar'ed
// anyways!
static char szErrorMsg[80];
// DMCC.setPIDConstants takes 6 arguments: board number, motor number, posOrVel, P, I, D
if (!PyArg_ParseTuple(args, "IIIiii:setPIDConstants", &nBoard, &nMotor,
&posOrVel, &P, &I, &D)) {
return NULL;
}
// validate the board number
if (nBoard > 3) {
sprintf(szErrorMsg, "Board number %d is invalid. Board number must be between 0 and 3.",
nBoard);
PyErr_SetString(PyExc_IndexError,szErrorMsg);
return NULL;
}
// validate the motor number
if ((nMotor < 1) || (nMotor > 2)) {
sprintf(szErrorMsg, "Motor number %d is invalid. Motor number must be 1 or 2.",
nMotor);
PyErr_SetString(PyExc_IndexError,szErrorMsg);
return NULL;
}
if (posOrVel > 1) {
sprintf(szErrorMsg, "posOrVal %d is invalid. posOrVal 0 or 1",
posOrVel);
PyErr_SetString(PyExc_IndexError,szErrorMsg);
return NULL;
}
if ((P > 32767) || (P < -32768)) {
sprintf(szErrorMsg, "P=%d is invalid. P must be between -32768 and 32768",
P);
PyErr_SetString(PyExc_IndexError,szErrorMsg);
return NULL;
}
if ((I > 32767) || (I < -32768)) {
sprintf(szErrorMsg, "I=%d is invalid. I must be between -32768 and 32768",
I);
PyErr_SetString(PyExc_IndexError,szErrorMsg);
return NULL;
}
if ((D > 32767) || (D < -32768)) {
sprintf(szErrorMsg, "D=%d is invalid. D must be between -32768 and 32768",
D);
PyErr_SetString(PyExc_IndexError,szErrorMsg);
return NULL;
}
int session;
session = DMCCstart(nBoard);
setPIDConstants(session, nMotor, posOrVel, P, I, D);
DMCCend(session);
return Py_BuildValue("i", 0);
}
static PyObject *
dmcc_setTargetVel(PyObject *self, PyObject *args)
{
unsigned int nBoard;
unsigned int nMotor;
int nVel;
// Make sure szErrorMsg is not on the stack
// -- downside is that we could have concurrency issues with different
// threads, but you know what, there should only be one error message
// at a time. If you have it from multiple threads, your code is fubar'ed
// anyways!
static char szErrorMsg[80];
// DMCC.setTargetPos takes 3 arguments: board number, motor number, Position
if (!PyArg_ParseTuple(args, "III:setTargetVel", &nBoard, &nMotor, &nVel)) {
return NULL;
}
// validate the board number
if (nBoard > 3) {
sprintf(szErrorMsg, "Board number %d is invalid. Board number must be between 0 and 3.",
nBoard);
PyErr_SetString(PyExc_IndexError,szErrorMsg);
return NULL;
}
// validate the motor number
if ((nMotor < 1) || (nMotor > 2)) {
sprintf(szErrorMsg, "Motor number %d is invalid. Motor number must be 1 or 2.",
nMotor);
PyErr_SetString(PyExc_IndexError,szErrorMsg);
return NULL;
}
int session;
session = DMCCstart(nBoard);
setTargetVel(session, nMotor, nVel);
DMCCend(session);
return Py_BuildValue("i", 0);
}
static PyMethodDef
module_functions[] = {
{ "setMotor", dmcc_setMotor, METH_VARARGS, "Set motor (board, motorNum, power)" },
{ "getMotorVoltage", dmcc_getMotorVoltage, METH_VARARGS, "Gets the Voltage (board)" },
{ "getMotorVoltageInt", dmcc_getMotorVoltageInt, METH_VARARGS, "Gets the Voltage (board) as int" },
{ "getMotorCurrent", dmcc_getMotorCurrent, METH_VARARGS, "Gets the current on a motor" },
{ "getQEI", dmcc_getQEI, METH_VARARGS, "Return the Quadrature Encoder value of the given (board, motor)" },
{ "getQEIVel", dmcc_getQEIVel, METH_VARARGS, "Return the Quadrature Encoder Velocity of the given (board, motor)" },
{ "setPIDConstants", dmcc_setPIDConstants, METH_VARARGS, "Set the PID constants (board, motor, posOrVel, P, I, D)" },
{ "setTargetPos", dmcc_setTargetPos, METH_VARARGS, "Set position target and turn on the motor with PID" },
{ "setTargetVel", dmcc_setTargetVel, METH_VARARGS, "Set velocity target and turn on the motor with PID" },
{ NULL }
};
void
initDMCC(void)
{
Py_InitModule3("DMCC", module_functions, "DMCC module by Exadler");
}