Go to Post Everyone needs to start working with what they are given, and start appreciating FIRST, not complaining about what they think FIRST is doing wrong. - Marygrace [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
 
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 11-02-2010, 15:38
mobilegamer999 mobilegamer999 is offline
Registered User
FRC #3357 (Comets)
Team Role: Programmer
 
Join Date: Feb 2008
Rookie Year: 2008
Location: Michigan
Posts: 124
mobilegamer999 is a jewel in the roughmobilegamer999 is a jewel in the roughmobilegamer999 is a jewel in the rough
Java Maxbotix Ultrasonic Sensor Code

This is code for all of you using the maxbotix EZ1 sensor with java.
This code is a direct port from last years c++ to java for this year, its not 100% tested yet so if any of you have problems with it just post about it.
Code:
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.
    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
     */

    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
     */

    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;
    }
}
Attached Files
File Type: zip MaxbotixUltrasonic.zip (980 Bytes, 96 views)
__________________
  #2   Spotlight this post!  
Unread 05-02-2012, 16:09
jcrosby jcrosby is offline
Registered User
FRC #0172
 
Join Date: Jan 2012
Location: Team 172 (Maine)
Posts: 1
jcrosby is an unknown quantity at this point
Thank you, this will save us some time! BTW: Have you tried to use the serial communications on the new unit?
Closed Thread


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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Ultrasonic sensor scorpions980 Electrical 3 03-02-2010 00:32
MaxBotix Sonar Sensor - Help please Needlerp Sensors 6 21-09-2009 13:01
Maxbotix Ultrasonic Sensor Help... manderson5192 Programming 9 22-02-2008 14:25
Parallax and Maxbotix ultrasonic sensors Lucretius Programming 5 20-02-2008 14:34
Maxbotix Sonar Sensor DonRotolo Electrical 17 04-02-2007 12:27


All times are GMT -5. The time now is 22:23.

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