3

I'm in the process of working on interfacing a haptic robot and eye tracker. So, both come with their own programming requirements, namely that the eye tracker software is based in python and is the main language in which I'm programming. Our haptic robot has an API in C, so I've had to write a wrapper in C, compile it as a DLL, and use ctypes in python to load the functions.

I've tested my DLL with MATLAB, and everything works just fine. However, something about my implementation of ctypes in my python class is not giving me the expected return value when i query the robot's positional coordinates.

I'll post the code here, and a clearer explanation of the problem at the bottom.

C source code for DLL wrapper:

#include <QHHeadersGLUT.h>
using namespace std;
class myHapClass{
public:
void InitOmni()
{
    DeviceSpace* Omni = new DeviceSpace; //Find a the default phantom
}
double GetCoord(int index)
{
    HDdouble hapPos[3];
    hdGetDoublev(HD_CURRENT_POSITION,hapPos);
    return hapPos[index];
}
};

extern "C" {
int index = 1;
__declspec(dllexport) myHapClass* myHap_new(){ return new myHapClass();}
__declspec(dllexport) void myHapInit(myHapClass* myHapObj){ myHapObj->InitOmni();}
__declspec(dllexport) double myHapCoord(myHapClass* myHapObj){ double nowCoord = myHapObj->GetCoord(index); return nowCoord;}
}

The theory for this code is simply to have 3 available C (not C++) calls that will be compatible with python/ctypes:

  1. myHap_new() returns a pointer to an instance of the class
  2. myHapInit initializes the haptics device
  3. myHapCoord returns a double for the current position, of the axis referenced by int Index.

The python class follows here:

import sreb
from ctypes import cdll
lib = cdll.LoadLibrary('C:\Documents and Settings\EyeLink\My Documents\Visual Studio 2010\Projects\myHapDLLSolution\Debug\myHapDLL.dll')

class CustomClassTemplate(sreb.EBObject):
def __init__(self):
    sreb.EBObject.__init__(self)
    self.pyMyHapObj = pyMyHap()
    self.coordval=2.0

def setCoordval(self,c):
    self.coordval = c
    pass

def getCoordval(self):
    return self.coordval

def initOmni(self):
    self.pyMyHapObj.pyHapInit()
    pass

def getCoord(self):
    self.coordval = self.pyMyHapObj.pyHapCoord()
    return self.coordval

class pyMyHap(object):
def __init__(self):
    self.obj = lib.myHap_new()
    self.coord = 1.0

def pyHapInit(self):
        lib.myHapInit(self.obj)

    def pyHapCoord(self):
    self.coord = lib.myHapCoord(self.obj)
    return self.coord

The theory of the python custom class is to instantiate an object (self.pyMyHapObj = pyMyHap()) of the loaded DLL class. Making a call to the function 'initOmni' successfully initializes the robot, however a call to 'getCoord' does not return the expected value. In fact, the result I get from 'getCoord' is 1 (and it is listed as 1, not 1.0, so I think it is returning an integer, not a double as it should).

In MATLAB, I have use the DLL library, and both the myHapInit and myHapCoord functions work, and I can initialize the robot and query the position coordinates successfully.

So what is it about my python class that is causing ctypes to not have the proper value returned from myHapCoord from my DLL?

Any help would be appreciated. Thanks

edit: Python version 2.3, if it matters... I'm stuck to that version.

1 Answer 1

5

Return values default to int. Use something like:

lib.myHapCoord.restype = ctypes.c_double

before calling the function to interpret the return value properly.

3
  • hmmm. that would make sense, because matlab requires the DLL's header file from which it gets argument types. Nothing in python/ctypes asked for the header, so that'd make sense. Was hoping that streaming the output to a variable declared as double would automatically cast it to the right type, but I don't know much about python so I guess not.. will try first thing tomorrow when i'm in the lab. Thanks!
    – brneuro
    May 11, 2012 at 3:06
  • You're welcome! See docs.python.org/library/ctypes.html#return-types for details. Remember to accept the answer if it is useful to you :^) May 11, 2012 at 3:48
  • worked out great, thanks. I had browsed some ctypes tutorials but they are quite extensive and I missed this. The other useful and related info was finding .argtypes = ... for defining the input argument types. Also that I then had to include something like "from ctypes import c_double". thanks again!
    – brneuro
    May 11, 2012 at 18:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.