Skip to content

Commit 1d40b3a

Browse files
committed
Added python bindings for setTargetPos and setTargetVel
1 parent b6e597d commit 1d40b3a

File tree

2 files changed

+75
-6
lines changed

2 files changed

+75
-6
lines changed

DMCC-py.c

+74-6
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,9 @@ dmcc_setPIDConstants(PyObject *self, PyObject *args)
269269
// anyways!
270270
static char szErrorMsg[80];
271271

272-
// DMCC.getQEI takes 6 arguments: board number, motor number, posOrVel, P, I, D
273-
if (!PyArg_ParseTuple(args, "IIIiii:getQEI", &nBoard, &nMotor, &posOrVel, &P, &I, &D)) {
272+
// DMCC.setPIDConstants takes 6 arguments: board number, motor number, posOrVel, P, I, D
273+
if (!PyArg_ParseTuple(args, "IIIiii:setPIDConstants", &nBoard, &nMotor,
274+
&posOrVel, &P, &I, &D)) {
274275
return NULL;
275276
}
276277
// validate the board number
@@ -290,30 +291,97 @@ dmcc_setPIDConstants(PyObject *self, PyObject *args)
290291

291292
if (posOrVel > 1) {
292293
sprintf(szErrorMsg, "posOrVal %d is invalid. posOrVal 0 or 1",
294+
posOrVel);
295+
PyErr_SetString(PyExc_IndexError,szErrorMsg);
296+
return NULL;
297+
}
298+
299+
if ((P > 32767) || (P < -32768)) {
300+
sprintf(szErrorMsg, "P=%d is invalid. P must be between -32768 and 32768",
301+
P);
302+
PyErr_SetString(PyExc_IndexError,szErrorMsg);
303+
return NULL;
304+
}
305+
306+
if ((I > 32767) || (I < -32768)) {
307+
sprintf(szErrorMsg, "I=%d is invalid. I must be between -32768 and 32768",
308+
I);
309+
PyErr_SetString(PyExc_IndexError,szErrorMsg);
310+
return NULL;
311+
}
312+
if ((D > 32767) || (D < -32768)) {
313+
sprintf(szErrorMsg, "D=%d is invalid. D must be between -32768 and 32768",
314+
D);
315+
PyErr_SetString(PyExc_IndexError,szErrorMsg);
316+
return NULL;
317+
}
318+
319+
int session;
320+
321+
session = DMCCstart(nBoard);
322+
setPIDConstants(session, nMotor, posOrVel, P, I, D);
323+
DMCCend(session);
324+
325+
return Py_BuildValue("i", 0);
326+
}
327+
328+
329+
static PyObject *
330+
dmcc_setTargetVel(PyObject *self, PyObject *args)
331+
{
332+
unsigned int nBoard;
333+
unsigned int nMotor;
334+
int nVel;
335+
336+
337+
// Make sure szErrorMsg is not on the stack
338+
// -- downside is that we could have concurrency issues with different
339+
// threads, but you know what, there should only be one error message
340+
// at a time. If you have it from multiple threads, your code is fubar'ed
341+
// anyways!
342+
static char szErrorMsg[80];
343+
344+
// DMCC.setTargetPos takes 3 arguments: board number, motor number, Position
345+
if (!PyArg_ParseTuple(args, "III:setTargetVel", &nBoard, &nMotor, &nVel)) {
346+
return NULL;
347+
}
348+
// validate the board number
349+
if (nBoard > 3) {
350+
sprintf(szErrorMsg, "Board number %d is invalid. Board number must be between 0 and 3.",
351+
nBoard);
352+
PyErr_SetString(PyExc_IndexError,szErrorMsg);
353+
return NULL;
354+
}
355+
// validate the motor number
356+
if ((nMotor < 1) || (nMotor > 2)) {
357+
sprintf(szErrorMsg, "Motor number %d is invalid. Motor number must be 1 or 2.",
293358
nMotor);
294359
PyErr_SetString(PyExc_IndexError,szErrorMsg);
295360
return NULL;
296361
}
297362

298-
/*
299363
int session;
300-
unsigned int nQEI;
301364

302365
session = DMCCstart(nBoard);
303-
nQEI = getQEI(session, nMotor);
366+
setTargetVel(session, nMotor, nVel);
304367
DMCCend(session);
305-
*/
368+
306369
return Py_BuildValue("i", 0);
307370
}
308371

309372

373+
374+
310375
static PyMethodDef
311376
module_functions[] = {
312377
{ "setMotor", dmcc_setMotor, METH_VARARGS, "Set motor (board, motorNum, power)" },
313378
{ "getMotorVoltage", dmcc_getMotorVoltage, METH_VARARGS, "Gets the Voltage (board)" },
314379
{ "getMotorVoltageInt", dmcc_getMotorVoltageInt, METH_VARARGS, "Gets the Voltage (board) as int" },
315380
{ "getQEI", dmcc_getQEI, METH_VARARGS, "Return the Quadrature Encoder value of the given (board, motor)" },
316381
{ "getQEIVel", dmcc_getQEIVel, METH_VARARGS, "Return the Quadrature Encoder Velocity of the given (board, motor)" },
382+
{ "setPIDConstants", dmcc_setPIDConstants, METH_VARARGS, "Set the PID constants (board, motor, posOrVel, P, I, D)" },
383+
{ "setTargetPos", dmcc_setTargetPos, METH_VARARGS, "Set position target and turn on the motor with PID" },
384+
{ "setTargetVel", dmcc_setTargetVel, METH_VARARGS, "Set velocity target and turn on the motor with PID" },
317385
{ NULL }
318386
};
319387

DMCC.c

+1
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,7 @@ int getTargetVel(int fd, unsigned int motor)
582582
// velocity - motor velocity
583583
void setTargetVel(int fd, unsigned int motor, int vel)
584584
{
585+
585586
short int vel16 = (short int) vel;
586587
unsigned char start;
587588

0 commit comments

Comments
 (0)