I am working on interacting with a tactile robot and an eye tracker. So both have their own programming requirements, namely that the eye tracking software is python based and is the main language I program in. Our tactile robot has an API in C, so I had to write a shell in C, compile it as a DLL, and use ctypes in python to load functions.
I tested my DLL with MATLAB and everything works fine. However, something about my ctypes implementation in my python class does not give me the expected return value when I request the positional coordinates of the robot.
I will post the code here and a clear explanation of the problem below.
C source code for DLL shell:
#include <QHHeadersGLUT.h>
using namespace std;
class myHapClass{
public:
void InitOmni()
{
DeviceSpace* Omni = new DeviceSpace;
}
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;}
}
, 3 C ( ++), python/ctypes:
- myHap_new()
- myHapInit haptics
- myHapCoord double , int.
python:
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
python (self.pyMyHapObj = pyMyHap()) DLL. initOmni , 'getCoord' . , , getCoord, 1 ( 1, 1,0, , , , ).
MATLAB DLL, myHapInit myHapCoord, .
, python, ctypes, , myHapCoord DLL?
.
edit: Python version 2.3, ... .