Your implementation looks reasonable to me. My only suggestion is that you determine your constant values (min_volts, voltage_range, min_distance, distance_range) empirically.
Try adding the following method:
Code:
public void updateDashboard() {
double dist = GetRangeInCM();
SmartDashboard.putNumber("Matbotix Volts", lastReading);
SmartDashboard.putNumber("Matbotix Dist", dist);
}
//
// Slight modification to your GetRangeInCM() method to save last
// voltage reading
//
public double GetRangeInCM() {
//if we're not using units, return -1, a range that will most likely never be returned
if (!use_units) {
return -1.0;
}
range = channel.getVoltage();
// *NEW* save last voltage reading in object so we can look at
// the value read (if desired) when reporting distance
lastVoltageReading = range;
if (range < min_voltage) {
return -2.0;
}
//first, normalize the voltage
range = (range - min_voltage) / voltage_range;
//next, denormalize to the unit range
range = (range * distance_range) + min_distance;
//finally, convert to centimeters
range *= IN_TO_CM_CONVERSION;
return range;
}
- Update your main robot code to either periodically call the new updateDashboard() method, or call it when a button is pressed.
- Turn on your robot and make sure that there is nothing too close to the Maxbotix sensor when powering up (I think they like to have a clear field of view of a couple of feet for the first couple of seconds when the calibrate during power up).
- Have some students label and place masking tape at fixed intervals in front of the robot (maybe every 50 cm or 2 feet).
- Place the robot so that the front of the Maxbotix sensor lines up with the zero marker of your tape strips.
- Get a good sized flat object (maybe a small table on its side) that can serve as the object to bounce the signal off of.
- Record voltage and distance in a spread sheet as you move the flat object to each piece of tape.
- Determine the minimum distance and maximum distance at which your voltage readings stop changing.
At this point you have data collected and you know that there is a linear relationship between voltage readings and distance. You should be able to determine your constants by finding the best fit line using the data samples that were within range.
Update your code with the new constants (or verify that they match your original values), and then go back and repeat the tests to verify that the robot reports the expected distances at each piece of tape.
Hope that helps,
Paul