Log in

View Full Version : Java PWM getPosition() integer division bug?


FRC4ME
24-01-2010, 17:10
After much troubleshooting trying to figure out why Servo.get() was returning 0.0 for any servo position other than 1.0, I noticed the following code in edu.wpi.first.wpilibj.PWM:


public double getPosition() {
int value = getRaw();
if (value < getMinNegativePwm()) {
return 0.0;
} else if (value > getMaxPositivePwm()) {
return 1.0;
} else {
return (value - getMinNegativePwm()) / getFullRangeScaleFactor();
}
}


Notice the final return statement. value, getMinNegativePwm(), and getFullRangeScaleFactor() are all of type int. Would this not cause the integer division to truncate to 0 for any result less than 1? I checked the C++ version of the same function, and sure enough, the numerator and denominator of the fraction are cast to float before the division.

jhersh
25-01-2010, 03:21
After much troubleshooting trying to figure out why Servo.get() was returning 0.0 for any servo position other than 1.0, I noticed the following code in edu.wpi.first.wpilibj.PWM:


public double getPosition() {
int value = getRaw();
if (value < getMinNegativePwm()) {
return 0.0;
} else if (value > getMaxPositivePwm()) {
return 1.0;
} else {
return (value - getMinNegativePwm()) / getFullRangeScaleFactor();
}
}


Notice the final return statement. value, getMinNegativePwm(), and getFullRangeScaleFactor() are all of type int. Would this not cause the integer division to truncate to 0 for any result less than 1? I checked the C++ version of the same function, and sure enough, the numerator and denominator of the fraction are cast to float before the division.

Please post a bug report at: http://firstforge.wpi.edu/sf/tracker/do/listArtifacts/projects.wpilib/tracker.wpilib_java_bugs

Thanks,
-Joe

FRC4ME
28-01-2010, 23:16
Please post a bug report at: http://firstforge.wpi.edu/sf/tracker/do/listArtifacts/projects.wpilib/tracker.wpilib_java_bugs

Thanks,
-Joe

I posted the bug report. Did I do everything correctly? Am I supposed to assign the bug to anyone in particular?

http://firstforge.wpi.edu/sf/go/artf1156?nav=1&_pagenum=1&returnUrlKey=1264738567324

jhersh
29-01-2010, 00:56
Looks good to me. The right person isn't in the list. :( I'll make sure he hears about it.

greekgod8591
03-03-2010, 14:31
We've observed the same issue, hopefully first will put out a fix for this.