Trouble with OnIputChange

Legacy support with Phidget21
Benjamin

Trouble with OnIputChange

Postby Benjamin » Wed Feb 10, 2010 5:21 am

Hi,
For a research work at university, we need to interface Phidget with Matlab.
We used the Phidget interface kit 0/16/16.

The outputs are following :
Output 0 : Motor ON – OFF
Output 1 : Motor Slow – Fast : it consists of a switch supplying the motor with 5 or 12 V

The inputs are followings :
Input (0) : Digital sensor that detect initial position.
Input (1) : Absolute position computed by digital sensor. There are 23 positions.

Our matlab version is R2007a
We used the “phidget21Matlab.h - 2.1.6.20100129” librabry version.

A first code was developped under VBA excel that works well.
We are now using a camera under matlab, and for practical reasons we want to use phidget under Matlab, too.

So, here is the code :

loadlibrary 'Phidget21' 'phidget21Matlab.h';

ptrIntKit = libpointer('int32Ptr', 0);
calllib('Phidget21', 'CPhidgetInterfaceKit_create', ptrIntKit);
IntKit = get(ptrIntKit, 'Value');

calllib('Phidget21', 'CPhidget_open', IntKit, 8555);

calllib('Phidget21', 'CPhidget_waitForAttachment', IntKit, 10000);

On = 1;
calllib('Phidget21', 'CPhidgetInterfaceKit_setOutputState',...
IntKit, 0, On);

Just to now, everything works well. And the motor is ON. There’s also no problem to stop it or to put in in Fast or Slow mode.

I want now to stop it at a determined position (initial or one of the 23 positions).
I so use the « set_OnInputChange_Handler » to call a simple function (StopMotor) that will stop the motor at right position.
But it does not work.

Here is my code :

PosIni = 0 ;
Comptr = 1 ; % following the input sensor employed
True = 1 ;
False = 0 ; %following the input state

FunctPtr = @StopMotor ;

strUser = 'UserFunct';
User = libpointer('voidPtr',[uint8(strUser)]);

calllib ('Phidget21', 'CPhidgetInterfaceKit_set_OnInputChange_Handler',...
IntKit, FunctPtr (IntKit, User, PosIni, True), User);

Iwrote it following the example :
int CPhidgetInterfaceKit_set_OnInputChange_Handler
(CphidgetInterfaceKitHandle phid,
int(*) (CphidgetInterfaceKitHandle phid, void *userPtr, int index, int InputState) fptr, void *userPtr )

Here is a simple function that detect the initial position and stop the motor.

function StopMotor (IntKit, User, PosIni, True)

if calllib ('Phidget21', 'CPhidgetInterfaceKit_getInputState',...
IntKit, PosIni, True) == 0

calllib('Phidget21', 'CPhidgetInterfaceKit_setOutputState',...
IntKit, 0, 0);

end

end

I think I’m on trouble with the definition of the “int (*fptr)” and the “void *userPtr”, and the transcription of the calllib OnInputChange under Matlab.

Thanks for any help you could provide me.

Benjamin

User avatar
Patrick
Lead Developer
Posts: 3051
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: Trouble with OnIputChange

Postby Patrick » Wed Feb 10, 2010 11:28 am

You're having trouble will the function callbacks? As far as I know, Matlab doesn't support callbacks, but I could be mistaken, now that I see you using function pointers...

I guess the main thing is that the callback function needs to be declared using the __stdcall calling convention (assuming that you're on Windows.)

-Patrick

Benjamin

Re: Trouble with OnIputChange

Postby Benjamin » Thu Feb 11, 2010 10:32 am

Dear Patrick,

Thanks for your prompt answer.

I'm still on trouble with "set_OnInputChange".

I think I was not clear. I need to detect and input change and then to call a function that will allow me to act on Output.

So, I put the Motor On using
calllib('Phidget21', 'CPhidgetInterfaceKit_setOutputState', IntKit, 1, 0);

After that, I wanna stop the motor after, by example, 8 input counts.

So I would have used "set_OnInputChange" to detect each input change, and so refer to a function and/or condition if :

if "input has been detected" and if "counterInput < 8" then
Let the motor on
counterInput= counterInput+1

if "input has been detected" and if "counterInput = 8" then
Stop motor

I've written the function that create the Interface handle, open it and put the motor On.
I've then try just to stop the motor using a call to another function placed in the same M-file, or in an other M-file :

if calllib ('Phidget21', 'CPhidgetInterfaceKit_getOutputState', IntKit, OnOff) == 1
calllib('Phidget21', 'CPhidgetInterfaceKit_setOutputState', IntKit, OnOff, Off);
end

But I encountered some troubles. As soon as I call the other function, it's as if the library had been unloaded and the variables deleted.

Do you see a suitable issue to my problem?
Do I absolutely need to use the callback functions (that matlab does not like) ? So I imagine they must be coded as a mex-files ?
Is it possible to use "set_OnInputChange" into matlab M-file ?

Thank you in advance for your help

Benjamin

User avatar
Patrick
Lead Developer
Posts: 3051
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: Trouble with OnIputChange

Postby Patrick » Thu Feb 11, 2010 11:17 am

What are you trying to do here:

if calllib ('Phidget21', 'CPhidgetInterfaceKit_getOutputState', IntKit, OnOff) == 1
calllib('Phidget21', 'CPhidgetInterfaceKit_setOutputState', IntKit, OnOff, Off);
end

?

CPhidgetInterfaceKit_getOutputState returns the output state in the OnOff variable (by reference). CPhidgetInterfaceKit_getOutputState will always return 0 unless an error has occurred.

-Patrick

Benjamin

Re: Trouble with OnIputChange

Postby Benjamin » Fri Feb 12, 2010 3:12 am

Dear Patrick,

Thanks for your answer. Sorry, but I make a mistake when I copy/paste the code. the right version is :

State = libpointer ('int32Ptr', 0);
calllib ('Phidget21', 'CPhidgetInterfaceKit_getOutputState', IntKit, OnOff, State)

if (get(State, 'Value') == 1)
calllib('Phidget21', 'CPhidgetInterfaceKit_setOutputState', IntKit, OnOff, Off);
end

Where OnOff refers to the output that control motor activation.

It was a small example to test call of function among the matlab Mfiles.
I've discovered that calling the Mfiles the one after the other, without refering them as functions, allows the user to employ phidget library and callib function. So it works now.

But I'm still on troubles with the "set_OnInputChange". What I really want to do, is to detect each input modification, which input changes, and so decide what to do. So I thought I could do something like :

MainMfile:

open library, handle, ...
set motor on,
if calllib ('Phidget21', 'CPhidgetInterfaceKit_set_OnInputChange_Handler',IntKit, ... ) == 0 ;
MfileAction
end


MfileAction: (little example)

if "input counter detected" and if "counterInput < 8" then
Let the motor on
counterInput= counterInput+1
end

if "input counter detected" and if "counterInput = 8" then
Stop motor
end

The Mfile Action and is not a problem, I just don't arrive to detect input change !

I fact, I especially do not arrive to write the code after "IntKit", in :
('Phidget21', 'CPhidgetInterfaceKit_set_OnInputChange_Handler',IntKit, ... ) ;

There's always a problem a problem of funtion or input function.
I asked me if I absolutely need to use the callback functions (that matlab does not like) ? and so I imagine they must be coded as a mex-files ?
Isn't it possible to use "set_OnInputChange" into matlab M-file, with an "if" condition, as I propose?

Thank you for all your answers.

Benjamin

User avatar
Patrick
Lead Developer
Posts: 3051
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: Trouble with OnIputChange

Postby Patrick » Fri Feb 12, 2010 11:03 am

I have never been able to get callbacks working in Matlab - and thus the library has to be used in a polled fashion - ie Poll InputState faster then you are expecting your inputs to change.

If you can get callbacks working, I'd be interested in knowing how you did it - just remember these are C callback, and they require the callback function to have a specific set of arguments and also it needs to be compiled using the __stdcall calling convention - if there isn't a way to specify this, then it's probably not going to work.

-Patrick


Return to “Matlab”

Who is online

Users browsing this forum: No registered users and 2 guests