2020 FIRST Robotics Teams Receive a MaxBotix Ultrasonic Sensor

2020 First Robotics Teams – Welcome!

MB1013

FRC competitors, once again we are supporting your work in the field of robotics by giving each team a free MB1013 MaxSonar Sensor from our HRLV-MaxSonar-EZ line. This small and lightweight sensor is designed for easy integration into your project, and it is a great option for autonomous robot navigation.

Take a look at our data sheet for more information on your sensor’s specs! 

 

How Ultrasonics work

All of our ultrasonic rangefinders will measure distance by tracking the time-of-flight of a sound wave. The diagram below shows how the sensor sends and receives the sound waves. Once the sensor emits a sound wave, it tracks how long it takes for the sound to reflect off of a surface and travel back to the sensor. The sensor then uses the known speed-of-sound to turn the time-of-flight into a range reading.

how ultrasonic sensors work

 

Using Your Ultrasonic Sensor with your NI roboRIO

 

1. HOOK UP AND USE YOUR CONTROLLER WITH ONE SENSOR

Our sensors can easily be connected and powered by the analog inputs on your NI roboRIO. Do note that if you choose to use multiple sensors you will have to connect the trigger pin of the sensor to a digital output on the NI roboRIO. It is recommended to do multitasking such as creating a new thread to trigger the sensor while your main robot code is still running.

Single Sensor Setup
MaxBotix roboRIO

Please note that the WPILib contains an example for using the analog voltage output of an ultrasonic sensor to control the movement of your robot. This example follows.

 

Java

/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-2018 FIRST. All Rights Reserved.                        */
/* Open Source Software - may be modified and shared by FRC teams. The code   */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project.                                                               */
/*----------------------------------------------------------------------------*/

package frc.robot;

import edu.wpi.first.wpilibj.AnalogInput;
import edu.wpi.first.wpilibj.PWMVictorSPX;
import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj.drive.DifferentialDrive;

/**
 * This is a sample program demonstrating how to use an ultrasonic sensor and
 * proportional control to maintain a set distance from an object.
 */

public class Robot extends TimedRobot {
  // distance in inches the robot wants to stay from an object
  private static final double kHoldDistance = 12.0;

  // factor to convert sensor values to a distance in inches
  private static final double kValueToInches = 0.125;

  // proportional speed constant
  private static final double kP = 0.05;

  private static final int kLeftMotorPort = 0;
  private static final int kRightMotorPort = 1;
  private static final int kUltrasonicPort = 0;

  private final AnalogInput m_ultrasonic = new AnalogInput(kUltrasonicPort);
  private final DifferentialDrive m_robotDrive
      = new DifferentialDrive(new PWMVictorSPX(kLeftMotorPort),
      new PWMVictorSPX(kRightMotorPort));

  /**
   * Tells the robot to drive to a set distance (in inches) from an object
   * using proportional control.
   */
  @Override
  public void teleopPeriodic() {
    // sensor returns a value from 0-4095 that is scaled to inches
    double currentDistance = m_ultrasonic.getValue() * kValueToInches;

    // convert distance error to a motor speed
    double currentSpeed = (kHoldDistance - currentDistance) * kP;

    // drive robot
    m_robotDrive.arcadeDrive(currentSpeed, 0);
  }
}

CPP

/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-2018 FIRST. All Rights Reserved.                        */
/* Open Source Software - may be modified and shared by FRC teams. The code   */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project.                                                               */
/*----------------------------------------------------------------------------*/

#include <frc/AnalogInput.h>
#include <frc/PWMVictorSPX.h>
#include <frc/TimedRobot.h>
#include <frc/drive/DifferentialDrive.h>

/**
 * This is a sample program demonstrating how to use an ultrasonic sensor and
 * proportional control to maintain a set distance from an object.
 */
class Robot : public frc::TimedRobot {
 public:
  /**
   * Tells the robot to drive to a set distance (in inches) from an object using
   * proportional control.
   */
  void TeleopPeriodic() override {
    // Sensor returns a value from 0-4095 that is scaled to inches
    double currentDistance = m_ultrasonic.GetValue() * kValueToInches;
    // Convert distance error to a motor speed
    double currentSpeed = (kHoldDistance - currentDistance) * kP;
    // Drive robot
    m_robotDrive.ArcadeDrive(currentSpeed, 0);
  }

 private:
  // Distance in inches the robot wants to stay from an object
  static constexpr int kHoldDistance = 12;

  // Factor to convert sensor values to a distance in inches
  static constexpr double kValueToInches = 0.125;

  // Proportional speed constant
  static constexpr double kP = 0.05;

  static constexpr int kLeftMotorPort = 0;
  static constexpr int kRightMotorPort = 1;
  static constexpr int kUltrasonicPort = 0;

  frc::AnalogInput m_ultrasonic{kUltrasonicPort};

  frc::PWMVictorSPX m_left{kLeftMotorPort};
  frc::PWMVictorSPX m_right{kRightMotorPort};
  frc::DifferentialDrive m_robotDrive{m_left, m_right};
};

#ifndef RUNNING_FRC_TESTS
int main() { return frc::StartRobot<Robot>(); }
#endif

 

 

How This Helps You

Once you have your ultrasonic sensor hooked up, you can write code for a multitude of tasks that are extremely useful for the autonomous section of the contest. Your robot can use distance measurements to navigate around obstacles and other robots. Use range information to start routines in code when you approach a target. This data can make your robot perform better and more quickly even during the remote-controlled section of the contest.

Even if you are using Lidar sensors. Ultrasonic sensors are a great addition. Only ultrasonic sensors can reliably detect certain surfaces such as glass. Transparency and color of objects have no effect on if an ultrasonic sensor can see an object. This is especially useful if you don’t know what kind of environment your robot will be operating in.

 

Q&A

This section will be questions coming from FIRST Robotics Teams in order to assist you with troubleshooting your sensor.

Question:

When we were outputting the value of the current distance in inches, the values we were
getting were incorrect.  When the sensor was in the range of 0-24 inches and
100-190 inches, the sensor was accurate.  However, between 24-100 inches,
the value that we were getting was 28 inches except occasionally we would
get the accurate value.  The values we were getting were oscillating wildly,
while the robot was in the range of 60-80 inches, from the actual value to
28 inches.  I copied the code that was on the website, so I do not believe
that it is a coding issue.  We are unsure of what the issue may be since it
seemed to be working a number of times.  Any help would be greatly
appreciated.

Answer: 

First, please check for an acoustical noise source.

Next, verify you have a clean power supply.  If you don’t, we offer a power supply filter as a solution.

Finally, verify you’re not detecting off axis targets.  You can read more about that here.

 

Need More Help?

FIRST contestants, we proudly support all teams in this year’s 2020 FIRST Robotics Competition. If you find yourself in need of more information about your MaxSonar sensor, we at MaxBotix are more than happy to help you. Please call us or email our technical support team at [email protected].

 

Need assistance with more sensors?  We provide educational discounts.  Contact our sales team to for more information at [email protected]

Do NOT follow this link or you will be banned from the site!