Go to Post The biggest thing is you don't have to make your robot like another teams. Come up with something unique and go from there. RAISE THE BAR..... - camtunkpa [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 30-01-2014, 19:36
Abhi_4505 Abhi_4505 is offline
Registered User
FRC #4505
 
Join Date: Jan 2014
Location: Maryland
Posts: 6
Abhi_4505 is an unknown quantity at this point
Maxbotix EZ1 MB1000 ultrasonic sensor with Java - Analog Input Help?

Our team (4505) is trying to use the Maxbotix MB1000 ultrasonic sensor on our bot, are there any suggestions for how to go about doing this by using an Analog Input? Any working example code? Below is the code we have now, is it a good starting point or should we start from scratch?

Source: http://www.chiefdelphi.com/forums/sh...ad.php?t=82409

package edu.wpi.first.wpilibj.samples;

import edu.wpi.first.wpilibj.*;

public class MaxbotixUltrasonic extends SensorBase {

private final double IN_TO_CM_CONVERSION = 2.54;
private boolean use_units; //Are we using units or just returning voltage?
private double min_voltage; //Minimum voltage the ultrasonic sensor can return
private double voltage_range; //The range of the voltages returned by the sensor (maximum - minimum)
private double min_distance; //Minimum distance the ultrasonic sensor can return in inches
private double distance_range;//The range of the distances returned by this class in inches (maximum - minimum)
AnalogChannel channel;
//constructor
public MaxbotixUltrasonic(int _channel) {
channel = new AnalogChannel(_channel);
//default values
use_units = true;
min_voltage = .5;
voltage_range = 5.0 - min_voltage;
min_distance = 3.0;
distance_range = 60.0 - min_distance;
}
//constructor
public MaxbotixUltrasonic(int _channel, boolean _use_units, double _min_voltage,
double _max_voltage, double _min_distance, double _max_distance) {
channel = new AnalogChannel(_channel);
//only use unit-specific variables if we're using units
if (_use_units) {
use_units = true;
min_voltage = _min_voltage;
voltage_range = _max_voltage - _min_voltage;
min_distance = _min_distance;
distance_range = _max_distance - _min_distance;
}
}
// Just get the voltage.
public double GetVoltage() {
return channel.getVoltage();
}
/* GetRangeInInches
* Returns the range in inches
* Returns -1.0 if units are not being used
* Returns -2.0 if the voltage is below the minimum voltage
*/

public double GetRangeInInches() {
double range;
//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();
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;
return range;
}
/* GetRangeInCM
* Returns the range in centimeters
* Returns -1.0 if units are not being used
* Returns -2.0 if the voltage is below the minimum voltage
*/

public double GetRangeInCM() {
double range;
//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();
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;
}
}
Reply With Quote
  #2   Spotlight this post!  
Unread 31-01-2014, 05:27
pblankenbaker pblankenbaker is offline
Registered User
FRC #0868
 
Join Date: Feb 2012
Location: Carmel, IN, USA
Posts: 102
pblankenbaker is a glorious beacon of lightpblankenbaker is a glorious beacon of lightpblankenbaker is a glorious beacon of lightpblankenbaker is a glorious beacon of lightpblankenbaker is a glorious beacon of light
Re: Maxbotix EZ1 MB1000 ultrasonic sensor with Java - Analog Input Help?

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
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 11:00.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi