@@ -269,8 +269,9 @@ dmcc_setPIDConstants(PyObject *self, PyObject *args)
269
269
// anyways!
270
270
static char szErrorMsg [80 ];
271
271
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 )) {
274
275
return NULL ;
275
276
}
276
277
// validate the board number
@@ -290,30 +291,97 @@ dmcc_setPIDConstants(PyObject *self, PyObject *args)
290
291
291
292
if (posOrVel > 1 ) {
292
293
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." ,
293
358
nMotor );
294
359
PyErr_SetString (PyExc_IndexError ,szErrorMsg );
295
360
return NULL ;
296
361
}
297
362
298
- /*
299
363
int session ;
300
- unsigned int nQEI;
301
364
302
365
session = DMCCstart (nBoard );
303
- nQEI = getQEI (session, nMotor);
366
+ setTargetVel (session , nMotor , nVel );
304
367
DMCCend (session );
305
- */
368
+
306
369
return Py_BuildValue ("i" , 0 );
307
370
}
308
371
309
372
373
+
374
+
310
375
static PyMethodDef
311
376
module_functions [] = {
312
377
{ "setMotor" , dmcc_setMotor , METH_VARARGS , "Set motor (board, motorNum, power)" },
313
378
{ "getMotorVoltage" , dmcc_getMotorVoltage , METH_VARARGS , "Gets the Voltage (board)" },
314
379
{ "getMotorVoltageInt" , dmcc_getMotorVoltageInt , METH_VARARGS , "Gets the Voltage (board) as int" },
315
380
{ "getQEI" , dmcc_getQEI , METH_VARARGS , "Return the Quadrature Encoder value of the given (board, motor)" },
316
381
{ "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" },
317
385
{ NULL }
318
386
};
319
387
0 commit comments