How to get distance from mb1013

i have problem with the ultrasonic sensor. we are using the mb1013 module and i have no idea how to get the distance. i’ve tried .getValue() but i don’t know what does the output number meant. i’ve tried running the robot and put a barrier in front of the sensor, but the numbers on the shuffle board don’t change. how can i get the actual distance? i found some documentation said that i have to multiple by the voltage scaler?

package frc.robot;

import com.ctre.phoenix.motorcontrol.can.WPI_TalonSRX;
import edu.wpi.first.wpilibj.AnalogInput;
import edu.wpi.first.wpilibj.AnalogOutput;
import edu.wpi.first.wpilibj.RobotController;
import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj.Ultrasonic;
import edu.wpi.first.wpilibj.smartdashboard.SendableChooser;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;

public class Robot extends TimedRobot {
  private static final String kDefaultAuto = "Default";
  private static final String kCustomAuto = "My Auto";
  private String m_autoSelected;
  private final SendableChooser<String> m_chooser = new SendableChooser<>();
  

  public final int rearUltrasonicPort = 0;
  double voltageScaler = 1.0;

  AnalogInput rearUltrasonic = new AnalogInput(rearUltrasonicPort);
  
  @Override
  public void robotInit() {
    m_chooser.setDefaultOption("Default Auto", kDefaultAuto);
    m_chooser.addOption("My Auto", kCustomAuto);
    SmartDashboard.putData("Auto choices", m_chooser);
  }

  @Override
  public void robotPeriodic() {
    SmartDashboard.putNumber("rearUltrasonic channel", rearUltrasonic.getChannel());
    SmartDashboard.putNumber("rearUltrasonic voltage", rearUltrasonic.getVoltage());
    SmartDashboard.putNumber("rearUltrasonic value", rearUltrasonic.getValue());
    SmartDashboard.putNumber("rearUltrasonic average voltage", rearUltrasonic.getAverageVoltage());
    SmartDashboard.putNumber("rearUltrasonic average value", rearUltrasonic.getAverageValue());
  }

Maxbotix put together a guide on their site specifically for using this sensor in FRC:

2 Likes

The code is pretty simple, so if you’re not seeing the value change, double check your wiring. Make sure you are using analog input 0, or change the constant in the code to match the one you are using. You can use a multimeter to measure the voltage between the signal and ground pins, you should see this change when you move the sensor.

thank you for responding. i get it

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.