Page 1 of 1

Phidget Encoder where to start? [beginner]

Posted: Tue May 26, 2015 4:26 am
by autofasurer
Hi all,

I'm trying to use a Phidget Motor Control in an openFrameworks program, but before getting to that point I've been trying to get some control over it by studying and changing the 'MotorControl-simple' example.
I've managed to implement the onEncoderPositionChange en onEncoderPositionUpdate handlers into the example, but I'm having trouble getting CPhidgetMotorControl_getEncoderPosition / setEncoderPosition to work. :(
I'm very new at all this and it's probably safe to say I have no idea how to get it to work.
I want to use these functions since I want to call the getEncoderPosition in my update function so I can use the current encoder position in the draw function of my program.

Any help would be appreciated...

Re: Phidget Encoder where to start? [beginner]

Posted: Tue May 26, 2015 7:12 am
by mparadis
Can you describe what is not working (what are you trying to do, what are you expecting to have happen, and what is actually happening)?

Re: Phidget Encoder where to start? [beginner]

Posted: Tue May 26, 2015 7:55 am
by autofasurer
I guess the problem is that I don't understand how to properly call this function. I managed with the event handlers because I could copy how to do that from the example.
This is different, and my knowledge of C is so very limited.
(I was working with the controller in max/msp, but now it is decided to work with openFrameworks, so all of a sudden I have a lot on my plate, being new to also that)

I ask the position and print it like this:

Code: Select all

 CPhidgetMotorControl_getEncoderPosition(motoControl, encIndex, &position);
    printf("encIndex %d at position %d\n", encIndex, position);

But that seems to only give me a relative position change and not an absolute one.
In max/msp the position counted up or donw continuously, depending on the rotation. Could it be that this functionality was implemented in the max object itself and is not a part of the motor controller's built-in functionality? Meaning I would need to call getposition, and increment an variable myself with this relative position?


Re: Phidget Encoder where to start? [beginner]

Posted: Tue May 26, 2015 10:23 am
by mparadis
getPosition should always be relative. When the motor controller first starts up, the position the motor is in is treated as zero. If you want position to correspond to an absolute position for the motor, then you need to move the motor to a known location when your program starts (for example, with a limit switch) and then set the position to zero.

Or, if you wanted to measure the number of rotations the motor has made in a certain period of time, you would get the position before and the position after, and calculate the difference.

Re: Phidget Encoder where to start? [beginner]

Posted: Tue May 26, 2015 11:38 am
by autofasurer
So what you're saying is that in the phidget there's no counter going up or down once it's initiated?

Just to make sure I understand the workings:

I understand that when the device is created the position is 0. When using event driven code I get a time and a position, which I believe (please correct me if I'm wrong) means the time passed between 2 events and the change in position that occured in that period.

But when I want to use logic code, so only polling the encoder when needed, there is no position running up or down when the motor is turning?

Basically (while the motor is running):
- check the encoder position (let's say 360 steps per turn)
- update variable with new position (starting at 0)
- draw something with new position as rotation

where at the next update (say 1 full turn later) the encoder position will have increased and be 360, then 720, 1080, 1440,...

apologies if this is unclear...

Re: Phidget Encoder where to start? [beginner]

Posted: Tue May 26, 2015 12:31 pm
by mparadis
getPosition should behave as you are expecting it to; it should count up or down on its own. Can you paste your code? It's possible you're trying to read the return value of getPosition instead of the position reference parameter.

Re: Phidget Encoder where to start? [beginner]

Posted: Wed May 27, 2015 2:23 am
by autofasurer
It is very possible I'm doing a lot of things wrong, I'm in over my head and so far I'm just messing with the example! :oops:

Code: Select all

int display_properties(CPhidgetMotorControlHandle phid)
   int serialNo, version, numInputs, numMotors, position, encIndex;
    const char* ptr;
   CPhidget_getDeviceType((CPhidgetHandle)phid, &ptr);
   CPhidget_getSerialNumber((CPhidgetHandle)phid, &serialNo);
   CPhidget_getDeviceVersion((CPhidgetHandle)phid, &version);
    CPhidgetMotorControl_getEncoderPosition(phid, encIndex, &position);
    printf("Encoder index: %d\n", encIndex);
    printf("Encoder position: %d\n",position);
    return 0;

This outputs:
Encoder index: 1
Encoder position: 0

Then in motorcontrol_simple() I try to set and get the position:

Code: Select all

int motorcontrol_simple()
   int result, position, encIndex;
   const char *err;
//set and read motor control encoder data
    CPhidgetMotorControl_setEncoderPosition(motoControl, 1, 100.00);
    CPhidgetMotorControl_getEncoderPosition(motoControl, 1, &position);
    printf("Position: %d\n", position);

This still outputs:
Position: 0

Re: Phidget Encoder where to start? [beginner]

Posted: Wed May 27, 2015 9:18 am
by mparadis
Don't sweat it, we all have to start from nothing when it comes to programming.

I think the problem is that you're trying to access index '1', but indexes in programming start counting at '0'. In more sophisticated programming languages, you would have gotten an error telling you that the index is out of bounds, but in this case with C/C++ you won't know if setPosition/getPosition are failing unless you look at the return value of the function.

Re: Phidget Encoder where to start? [beginner]

Posted: Wed May 27, 2015 10:23 am
by autofasurer
It makes more sense now!
Except the values are signed inversely. Setting it to 100 gives a -100 result.
I can live with that but if there's a reason for it I'd be interested to know!

Thank you very much!

Re: Phidget Encoder where to start? [beginner]

Posted: Sat May 30, 2015 7:21 am
by autofasurer