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
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
Iâm assuming that your sensor is a MaxBotix MB1013 and that the following is the data sheet for your sensor.
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).
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.
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�
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.
Just want to make sure you saw this.
You can do digital via RS232, but it is much easier to do analog.
Allrighty⌠today is a good day !!! nice whne we can finally get something goingâŚ
So we created a new RangeSensor_Subsystem class and here is the codeâŚ
public class RangeSensor_Subsystem extends Subsystem {
@Override
protected void initDefaultCommand() {
// TODO Auto-generated method stub
}
// 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());
}
}
The value where not showing on the smartdashboard so i copy the line to the Robot.java like this and i can see the value on the SMartDashboardâŚ
public void teleopPeriodic() {
oi.updateSmart();
Scheduler.getInstance().run();
SmartDashboard.putBoolean("Micro Switch", GearGobbler_Subsystem.isGearPresent());
SmartDashboard.putBoolean("Prox Switch", GearGobbler_Subsystem.isGearPresent2());
SmartDashboard.putNumber("Distance (volts)", RangeSensor_Subsystem.getVoltage());
SmartDashboard.putNumber("Distance (real)", RangeSensor_Subsystem.getDistance());
}
So while i understand this is currently displaying what i want⌠do i need ot worry that there will be instance where it will not because it is in the TeleopPeriodic class?
Iâm not sure if this is what youâre asking:
You didnât declare anything in TelopPeriodic(), so things wonât âgo awayâ when it terminates.
As an aside, note that by making mb1013 and the methods static, you will be unable to use this code to access a second ultrasonic sensor. This is because that object and those methods belong to the class, not the object. If you wanted to be able to do this, you would remove static from these declarations, write an explicit constructor method, and send the port number (currently hardwired to zero) to the constructor method to use in the call to new AnalogInput().