Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Java (http://www.chiefdelphi.com/forums/forumdisplay.php?f=184)
-   -   Code for Ultrasonic Sensor (http://www.chiefdelphi.com/forums/showthread.php?t=154747)

BetaBlues 04-02-2017 17:45

Code for Ultrasonic Sensor
 
We have our sensor set up as digital but we are having trouble understanding how to use it. Could someone give us a run down on how to use it? Thanks :D

MuskieProgramme 04-02-2017 19:40

Re: Code for Ultrasonic Sensor
 
In general, an ultrasonic sensor gives you a number. The number corresponds to distance. You can use that distance to (for example) calculate how far the robot needs to move, then move that distance.

For more specific instructions, it might be helpful if you specified which ultrasonic sensor you are using.

BetaBlues 06-02-2017 20:31

Re: Code for Ultrasonic Sensor
 
Quote:

Originally Posted by MuskieProgramme (Post 1640495)
In general, an ultrasonic sensor gives you a number. The number corresponds to distance. You can use that distance to (for example) calculate how far the robot needs to move, then move that distance.

For more specific instructions, it might be helpful if you specified which ultrasonic sensor you are using.

Our ultrasonic sensor is by Rockwell Automation: HRVL-MaxSonary-EZ MB1013

pblankenbaker 07-02-2017 07:23

Re: Code for Ultrasonic Sensor
 
I'm assuming that your sensor is a MaxBotix MB1013 and that the following is the data sheet for your sensor.

http://www.maxbotix.com/documents/HR..._Datasheet.pdf

Assuming that is your sensor, I would recommend that you start with pins 3, 6 and 7 on the sensor and connect them to an analog input on the roboRIO (refer to the data sheet to determine how they should be connected). The following class could then be used to display readings from the sensor on the SmartDashboard to help you determine how the voltage output from the sensor corresponds to distance.

You will need to determine the constant (VOLTS_TO_DIST) to convert the voltage to a real world distance (if you want to work in real world units instead of voltage).

Code:

package org.usfirst.frc.team868.robot.sensors;

import edu.wpi.first.wpilibj.AnalogInput;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;

public class DistanceSensor {
  // A MB1013 distance sensor - http://www.maxbotix.com/documents/HRLV-MaxSonar-EZ_Datasheet.pdf
  // (pins 3, 6 and 7 from sensor to analog input 0)
  private static final AnalogInput mb1013 = new AnalogInput(0);
 
  // TODO - You will need to determine how to convert voltage to distance
  // (use information from the data sheet, or your own measurements)
  private static final double VOLTS_TO_DIST = 1.0;

  public static double getVoltage() {
    return mb1013.getVoltage();
  }
 
  public static double getDistance() {
    return getVoltage() * VOLTS_TO_DIST;
  }
 
  public static void updateDashboard() {
    SmartDashboard.putNumber("Distance (volts)", getVoltage());
    SmartDashboard.putNumber("Distance (real)", getDistance());
  }
}

NOTE: You will need to periodically call the Distance.updateDashboard() command to see the values on the dashboard.

Coach Seb 07-02-2017 07:28

Re: Code for Ultrasonic Sensor
 
This is a great example, thank you!

in a command based situation, would you add this code as its own class, like a subsystem or would you add this to an existing class...?

pblankenbaker 07-02-2017 08:14

Re: Code for Ultrasonic Sensor
 
In a command based robot, I would be tempted to refactor the example into its own Subsystem.

However, this could be thought of as a special "read only" type of subsystems. Commands that made use of this subsystem would not need to require() it in their constructors as there isn't a problem if multiple commands take distance measurements at the same time. If two commands both want to get distance measurements, it isn't a problem like it is if two commands both want to manipulate drive motors.

Alternatively, I might be tempted to leave it as its own stand alone class (not make it a subsystem) and just use it as is.

rich2202 07-02-2017 10:19

Re: Code for Ultrasonic Sensor
 
Quote:

Originally Posted by BetaBlues (Post 1640450)
We have our sensor set up as digital...


Quote:

Originally Posted by pblankenbaker (Post 1641503)
connect them to an analog input

Just want to make sure you saw this.

You can do digital via RS232, but it is much easier to do analog.


All times are GMT -5. The time now is 15:35.

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