Encoder can't be read on MotorControl Phidget

Supporting Java SE version 7 and up
anewsome
Phidgeteer!
Posts: 54
Joined: Thu Jul 07, 2011 1:43 pm
Contact:

Encoder can't be read on MotorControl Phidget

Postby anewsome » Sun Oct 23, 2011 7:12 pm

The phidget control panel in the System Prefs seems to work fine. When I open the 1065 MotorControl from the control panel, the onboard encoder seems to work fine. It appears to read the values from the encoder as it should.

However, when I try the Java example for the MotorControl, the encoder value in the GUI always reads 0 and flickers to -1 when I rotate the encoder. The encoder value is never shown, only the 0 and -1 from what I can tell.

I've looked at the sample code provided by Phidgets and it looks like there are

MCEncoderPositionChangeListener
and
MCEncoderPositionUpdateListener

and the "update" one is commented out. I thought maybe it needed to used instead of the "change" one. After switching the code to use the update listener instead of the change listener, the results are the same. The encoder value is not shown.

I've tried making my own small Java program and I'm not able to get the encoder value in their either. I've looked at the Javadoc and it seems I'm doing everything right.

Has anyone had any luck reading the encoder value in Java? Again, the encoder is connected correctly, the values read fine in the control panel application.

User avatar
burley
Human-Cyborg Relations
Posts: 441
Joined: Tue Sep 27, 2011 2:37 pm
Location: Calgary
Contact:

Re: Encoder can't be read on MotorControl Phidget

Postby burley » Mon Oct 24, 2011 9:37 am

It sounds like you are looking at the current encoder change. It is 0 most of the time meaning the encoder is not changing position, and when it flickers to -1 it means you have spun the encoder by 1 notch in the negative direction since it last checked.

From what I can see the Java version of the code does not have a display for the total encoder position. To add this you would simply have to have an extra bit of logic that accumulates all of the encoder change values.
Brian Burley
403-282-7335 ext. 6003
support@phidgets.com

anewsome
Phidgeteer!
Posts: 54
Joined: Thu Jul 07, 2011 1:43 pm
Contact:

Re: Encoder can't be read on MotorControl Phidget

Postby anewsome » Mon Oct 24, 2011 11:58 am

Hi Burley. Thanks for the reply. I did figure this one out somewhat, after more experimentation.

As it turns out, the encoder position data is not in the event at all.

getValue();

is only giving me the changes since the last event, not the absolute position.

I tried a few things to get the absolute position. I made a global variable and then use getValue to add and subtract values from the global variable. This seems to work I suppose, not sure how accurate this would be since there is the possibility of "missing events" at runtime for various reasons.

I also tried doing this in the event listener:

try {
MotorControlPhidget motor = (MotorControlPhidget).event.getSource();
int position = motor.getEncoderPosition();
positionField.setText(Integer.toString(position);
} catch (PhidgetException ex) {
System.out.println(ex.getDescription());
}

Which DOES work, and does give me the absolute position from inside the event listener. I have no idea if the global variable is better or more reliable, I worry that the second version would be too slow, creating a motor control phidget instance on every change event.

Any advice on the best way to do this?

User avatar
burley
Human-Cyborg Relations
Posts: 441
Joined: Tue Sep 27, 2011 2:37 pm
Location: Calgary
Contact:

Re: Encoder can't be read on MotorControl Phidget

Postby burley » Mon Oct 24, 2011 2:54 pm

I would think that using a global variable is better. The likelihood of missing an event is pretty low.
Brian Burley
403-282-7335 ext. 6003
support@phidgets.com

erik
King of the Lab
Posts: 476
Joined: Fri Mar 06, 2009 12:42 pm
Location: Calgary, Canada
Contact:

Re: Encoder can't be read on MotorControl Phidget

Postby erik » Mon Oct 24, 2011 3:56 pm

Instead of having position as the global variable, you can also make MotorControlPhidget as the global variable, and that way you won't have to keep recreating the object inside the event handler. I'm not so fluent in Java, but it does work in C#.

If you have more than one encoder in your system, you can either have separate events for each one, or you can compare/switch on the event.getSource with either encoders.

anewsome
Phidgeteer!
Posts: 54
Joined: Thu Jul 07, 2011 1:43 pm
Contact:

Re: Encoder can't be read on MotorControl Phidget

Postby anewsome » Mon Oct 24, 2011 9:00 pm

erik wrote:Instead of having position as the global variable, you can also make MotorControlPhidget as the global variable, and that way you won't have to keep recreating the object inside the event handler. I'm not so fluent in Java, but it does work in C#.

If you have more than one encoder in your system, you can either have separate events for each one, or you can compare/switch on the event.getSource with either encoders.


Thanks for this advice Erik. This seems like it would be better, and I wouldn't have to create the MotorControlPhidget on every event. I'll think about how to code this.


Return to “Java”

Who is online

Users browsing this forum: No registered users and 1 guest