Go to Post FIRST doesn't end after your last year within a team. It's never going to end. - Tetraman [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 09-02-2014, 00:53
tuXguy15's Avatar
tuXguy15 tuXguy15 is offline
Team Mentor
AKA: Devin Kolarac
FRC #2559 (Normality Zero)
 
Join Date: Apr 2012
Rookie Year: 2012
Location: Harrisburg, PA
Posts: 127
tuXguy15 is an unknown quantity at this point
Maxbotix analog programming

Does anyone have any example code of getting the distance to something and driving that distance with a macho tis range finder?
__________________
Reply With Quote
  #2   Spotlight this post!  
Unread 11-02-2014, 02:26
irvingc irvingc is offline
Registered User
FRC #0948 (Newport Robotics Group)
Team Role: Leadership
 
Join Date: Jan 2014
Rookie Year: 2011
Location: Bellevue, WA
Posts: 31
irvingc is on a distinguished road
Re: Maxbotix analog programming

What you're asking is actually not so simple! Just getting the distance reading from the sensor can be somewhat complicated, depending on the interface. Here's some code I dug up from the last time we used an ultrasonic sensor that connected to the I2C bus (this was before we switched to the command-based framework):
Code:
import edu.wpi.first.wpilibj.*;
import java.util.TimerTask;

public class NRGUltrasonic
{
    public static final boolean USING_ULTRASONIC = false;
    private Timer timer;
    private java.util.Timer controlLoop;
    private DigitalModule digMod;
    private I2C i2c;
    private static final int ULTRASONIC_SLOT = 4;
    private static final int ULTRASONIC_ADDRESS = 0xE0;
    private static final int ECHO_ADDRESS_HIGH = 2;
    private static final int ECHO_ADDRESS_LOW = 3;
    private int dist;
    private double period;
    private boolean writeFail;

    private class UltrasonicTask extends TimerTask
    {
	private NRGUltrasonic u;

	public UltrasonicTask(NRGUltrasonic ultrasonic)
	{
	    if (ultrasonic == null)
	    {
		throw new NullPointerException("Given Ultrasonic was null");
	    }
	    this.u = ultrasonic;
	}

	public void run()
	{
	    u.setDist();
	}
    }

    public NRGUltrasonic()
    {
	dist = 0;
	writeFail = false;
	if(USING_ULTRASONIC)
	{
	    digMod = DigitalModule.getInstance(ULTRASONIC_SLOT);
	    i2c = digMod.getI2C(ULTRASONIC_ADDRESS);
	    timer = new Timer();
	    controlLoop = new java.util.Timer();
	    if (i2c.addressOnly())
	    {
		NRGLCD.println4("ERR: No Ultrasonic!");
		Debug.println(Debug.FATAL_EXCEPTIONS, "ERROR: Could not find Ultrasonic.");
	    }
	    //Range: Three meters.
	    i2c.write(0x02, 0x46);
	    //Analogue Gain: 317
	    i2c.write(0x01, 0x18);
	    period = .05;
	    setDist();
	}
    }

    /**
     * Ends the control loop.
     */
    public void free()
    {
	controlLoop.cancel();
	controlLoop = null;
    }

    public int getDist()
    {
	return dist;
    }

    public boolean getWriteFail()
    {
	return writeFail;
    }

    //gets distance
    private void setDist()
    {
	byte high, low;

	try
	{
	    //setting range to centimeters
	    if (i2c.write(0x00, 0x51))
	    {
		writeFail = true;
		Debug.println(Debug.ULTRASONIC, "Attemted write to ultrasonic, failed.");
	    }
	    else
	    {
		writeFail = false;
		Debug.println(Debug.ULTRASONIC, "Wrote to ultrasonic, began rangefinding in centimeters.");
		timer.reset();
		timer.start();

		try
		{
		    boolean done = false;
		    Timer.delay(.065);
		    for (int i = 0; i < 10; i++)
		    {
			byte[] buffer = new byte[1];
			if (i2c.read(ECHO_ADDRESS_HIGH, 1, buffer))
			{
			    //Debug.println(Debug.ULTRASONIC, "Attempted read to ultrasonic high, failed.");
			}
			else
			{
			    high = buffer[0];
			    if (i2c.read(ECHO_ADDRESS_LOW, 1, buffer))
			    {
				//Debug.println(Debug.ULTRASONIC, "Attempted read to ultrasonic low, failed.");
			    }
			    else
			    {
				low = buffer[0];
				dist = high * 256 + ((int) low & 0xFF);
				Debug.println(Debug.ULTRASONIC, "Try: " + i + " Time: " + timer.get() + " High: " + high + " Low: " + low + " Dist: " + dist);
				done = true;
			    }
			}
			if (done)
			    break;
			//Debug.println(Debug.ULTRASONIC, "Try "+i+" failed.");
			if (i == 9)
			    Debug.println(Debug.ULTRASONIC, "TRIED 10 TIMES!");
			Timer.delay(.02);
		    }
		}
		catch (Exception e)
		{
		    Debug.println(Debug.FATAL_EXCEPTIONS, "Attempt to read ultrasonic caused a fatal exception.");
		}
	    }
	}
	catch (Exception e)
	{
	    Debug.println(Debug.FATAL_EXCEPTIONS, "Attempt to write to ultrasonic caused a fatal exception");
	}
	//Schedule next loop.
	controlLoop.schedule(new UltrasonicTask(this), (long) (period * 1000));

    }
}
And even after you know your distance, it's really not so easy to just drive directly at it. What type of drive train are you running? If it's a tank drive - does your robot drive straight if you just feed the same power to left and right motors?
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 22:39.

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