VEX Ultrasonic Sensor

I have been trying to use this ultrasonic sensor for our robot but it never works. I am wondering if any one could help me out with the code to run this sensor using Ultrasonic Output in DIO “0” and Ultrasonic Input in DIO “1”.

You may want to take a look at the WPILib Java documentation on Ultrasonic sensors. I personally haven’t used them, but it looks like (based off of the Javadocs) that you code should look similiar to this:

//Setting up the sensor
Ultrasonic ultra = new Ultrasonic(0, 1); //Make a new Ultrasonic Sensor on "pingChannel" 0 and "echoChannel" 1.
ultra.setAutomaticMode(true); //If you don't want to have to call whenever you want to check for values
ultra.setEnabled(true); //Enable the Ultrasonic sensor

//Whenever you want to read values:
double millis = ultra.getRangeMM(); //Get distance in millimeters
double inches = ultra.getRangeInches(); //Get distance in inches

Ok thanks I will try that I was not always having the Enable command.

Because you seem to be good at code and we are a 2 year team and have never used java. what would the code be like the track the totes with a HD Microsoft Webcam and make the robot center up with the totes and drive up to them till the ultrasonic sensor says like 6 inches.

I can’t help you very much with the vision tracking part of the code due to never EVER working with the darn stuff, but I can help with the 6 inch spacing part.

//After you've found the stack and lined up
double kP = .2
double minSpeed = 0.3;
double maxSpeed = 0.5;
distance = ultra.getRangeInches();
if (distance != 6) {
     while (true) {
          distance = 6 - ultra.getRangeInches(); //Update the distance from our goal (6 inches).
          double p = distance * kP; //Calculate p, which is the speed we want to move at.
          //Making sure p is within maxSpeed and minSpeed and correcting it if it isn't.
          if (Math.abs(p) > maxSpeed) {
               p = maxSpeed * (Math.abs(p)/p); //p equals maxSpeed multiplied by the sign of p
          } else if (Math.abs(p) < minSpeed) {
               p = 0; //We're moving too slow, so we're saying we've stopped and hit our goal.

          //Set our motors to p.

         //If we're not moving, we've made it to our goal and want to exit this loop.
          if (p == 0) {

Now let me explain what this is and how it works. This is a very simple P loop. How P loops work is a speed is calculated based off of how far you are away from your target. The farther you are away, the faster you move. The speed is calculated by multiplying distance by kP, which you’ll need to fine-tune the value of to get it working well. The lower kP is, the more speed scaling you’ll see (i.e. the robot slowing down much more as it approaches 6 inches). You’ll also want to adjust maxSpeed and minSpeed. maxSpeed is the fastest the robot is allowed to go, and minSpeed is how slow the robot can be going before we say we’re arrived at our destination.

This may be all you need, or you may need to expand on this and make it a PI, PD, or PID loop. If you have any issues with this, feel free to ask. I’m happy to help :slight_smile:

Here is my current code that is not working for some reason.

package org.usfirst.frc.team4076.robot;

import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.SampleRobot;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.Ultrasonic;

public class Robot extends SampleRobot {

    RobotDrive myRobot;
    Joystick stick;
    Ultrasonic ultra;


    public void robotInit() {
        myRobot = new RobotDrive(0, 1);
        stick = new Joystick(0);
        ultra = new Ultrasonic(0, 1);
	public void operatorControl() {
        while (isOperatorControl() && isEnabled()) {
            while (stick.getRawButton(4)) {
            	myRobot.tankDrive(1, -1);
            while (stick.getRawButton(5)) {
            	myRobot.tankDrive(-1, 1);
	public void Test() {
		while (isTest() && isEnabled()) {

Here’s your problem:

public void robotInit() {
        ultra.setAutomaticMode(isEnabled()); <---
        ultra.setEnabled(isEnabled()); <---
        ...other stuff...

When robotInit is run, the robot is disabled. That means that isEnabled() will return false, which means your ultrasonic sensor is never turned on, along with automatic mode being set to false. I’d suggest just changing the lines to ultra.setEnabled(true) and ultra.setAutomaticMode(true).