Code for Sonar sensor killing all code

Java language. windows 10. Maxbotics 1013-000 sensor. Never used sonar on our bot. Have new software and updated rio. Getting runtime error “Robot arent suposesd to stop but yours did”. Any type of ultrasonic code renders our bot useless. When ultrasonic code is removed the robot drives without trouble. Have moved the code around from autonomous to drive and also drive/automonous. No matter where the code is placed the bot will not work. Any suggestions. code came from WPI Library.

Thanks in advance.

Please post your code and exactly how you wired it on the sonar and roborio.
Did you look at my reply to this posting?
https://www.chiefdelphi.com/forums/showthread.php?t=161744

Gotta see your code to comment properly but it is likely you are spinning in a loop and not replying to the driver station in time (or at all)

Code for our robot:

// RobotBuilder Version: 2.0
//
// This file was generated by RobotBuilder. It contains sections of
// code that are automatically generated and assigned by robotbuilder.
// These sections will be updated in the future when you export to
// Java from RobotBuilder. Do not put any code or make any change in
// the blocks indicating autogenerated code or it will be lost on an
// update. Deleting the comments indicating the section will prevent
// it from being updated in the future.

package org.usfirst.frc5247.robot2018;

import edu.wpi.first.wpilibj.AnalogInput;
//import edu.wpi.first.wpilibj.Talon;
import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj.command.Command;
import edu.wpi.first.wpilibj.command.Scheduler;
//import edu.wpi.first.wpilibj.drive.DifferentialDrive;
import edu.wpi.first.wpilibj.livewindow.LiveWindow;
import edu.wpi.first.wpilibj.smartdashboard.SendableChooser;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import org.usfirst.frc5247.robot2018.commands.;
import org.usfirst.frc5247.robot2018.subsystems.
;
import org.usfirst.frc5247.robot2018.RobotMap;

import edu.wpi.cscore.CvSink;
import edu.wpi.cscore.CvSource;
import edu.wpi.cscore.UsbCamera;
import edu.wpi.first.wpilibj.Ultrasonic;
import edu.wpi.first.wpilibj.CameraServer;
import edu.wpi.first.wpilibj.IterativeRobot;

/**

  • The VM is configured to automatically run this class, and to call the

  • functions corresponding to each mode, as described in the TimedRobot

  • documentation. If you change the name of this class or the package after

  • creating this project, you must also update the build.properties file in

  • the project.
    */
    @SuppressWarnings(“unused”)
    public class Robot extends IterativeRobot {

    Object autonomousCommand;
    SendableChooser<Command> autoChooser = new SendableChooser<>();

    public static OI oi;
    // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=DECLARATIONS
    public static Chassis chassis;

    // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=DECLARATIONS

    /**

    • This function is run when the robot is first started up and should be
    • used for any initialization code.

    */

    Ultrasonic Ultra = new Ultrasonic(1,1); //ports

    @Override
    public void robotInit() {
    RobotMap.init();
    // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTRUCTORS
    chassis = new Chassis();

    // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTRUCTORS
    // OI must be constructed after subsystems. If the OI creates Commands
    //(which it very likely will), subsystems are not guaranteed to be
    // constructed yet. Thus, their requires() statements may grab null
    // pointers. Bad news. Don’t move it.
    oi = new OI();

     // Add commands to Autonomous Sendable Chooser
     // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=AUTONOMOUS
    
     autoChooser.addDefault("Autonomous Command", new Auton());
    

    // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=AUTONOMOUS
    SmartDashboard.putData(“Auto mode”, autoChooser);
    UsbCamera camera = CameraServer.getInstance().startAutomaticCapture();

    Ultra.setAutomaticMode(true);

    }

    //public void ultrasonicStart() {

     double range = Ultra.getRangeInches();
    

// }

/**
 * This function is called when the disabled button is hit.
 * You can use it to reset subsystems before shutting down.
 */
@Override
public void disabledInit(){

}

@Override
public void disabledPeriodic() {
    Scheduler.getInstance().run();
}

@Override
public void autonomousInit() {
    autonomousCommand = autoChooser.getSelected();
    // schedule the autonomous command (example)
}

/**
 * This function is called periodically during autonomous
 */
@Override
public void autonomousPeriodic() {
    Scheduler.getInstance().run();
    Ultra.setAutomaticMode(true);
}

@Override
public void teleopInit() {
    // This makes sure that the autonomous stops running when
    // teleop starts running. If you want the autonomous to
    // continue until interrupted by another command, remove
    // this line or comment it out.
       }

/**
 * This function is called periodically during operator control
 */
@Override
public void teleopPeriodic() {
    Scheduler.getInstance().run();
    /**
	// sensor returns a value from 0-4095 that is scaled to inches
	double currentDistance = ultrasonic.getValue() * kValueToInches;

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

	// drive robot
	RobotDrive.drive(currentSpeed, 0);
	*/
}


//public static void AutonL() {
	//System.out.println("AutonL");
	
	// TODO Auto-generated method stub
	
//}

}

Code for our auton:

// RobotBuilder Version: 2.0
//
// This file was generated by RobotBuilder. It contains sections of
// code that are automatically generated and assigned by robotbuilder.
// These sections will be updated in the future when you export to
// Java from RobotBuilder. Do not put any code or make any change in
// the blocks indicating autogenerated code or it will be lost on an
// update. Deleting the comments indicating the section will prevent
// it from being updated in the future.

package org.usfirst.frc5247.robot2018.commands;
import edu.wpi.first.wpilibj.command.Command;
import org.usfirst.frc5247.robot2018.Robot;
import edu.wpi.first.wpilibj.Ultrasonic;

/**
*
*/
public class Auton extends Command {

// BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=VARIABLE_DECLARATIONS

// END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=VARIABLE_DECLARATIONS

// BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTRUCTOR

Ultrasonic ultraAuton = new Ultrasonic(1, 1);

public Auton() {
	
	requires(Robot.chassis);
	
	ultraAuton.setAutomaticMode(true);

// END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTRUCTOR
    // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=VARIABLE_SETTING

    // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=VARIABLE_SETTING
    // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=REQUIRES

// END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=REQUIRES
}

// Called just before this Command runs the first time
@Override
protected void initialize() {
	

}

// Called repeatedly when this Command is scheduled to run
@Override
protected void execute() {
    double urange = ultraAuton.getRangeInches();	
    
	Robot.chassis.driveforward();
	
	//later
	//while (urange ) {
	
	if (urange &gt; 10) {
		Robot.chassis.driveforward();
	}
	
	else {
		Robot.chassis.driveStop();
		
	}
	
	    // }
	
}

// Make this return true when this Command no longer needs to run execute()
@Override
protected boolean isFinished() {
    return false;
}

// Called once after isFinished returns true
@Override
protected void end() {
	Robot.chassis.driveStop();
}

// Called when another command which requires one or more of the same
// subsystems is scheduled to run
@Override
protected void interrupted() {
}

}

Ignore all our our notes. We haven`t been using any code in them.

Hi,

If you take a look at the javadoc you will see that you are using the Ultrasonic constructor where it asks for the pingChannel & echoChannel.

This would be great if you’re using something like the HC-SR04 (other than the fact that you are giving the same port rather than two different ones, which would cause an error when startCompetition() is called), but as I read in your other post you are connecting the sensor to an analog port.

For this, you want to use AnalogInput to read the voltage value. If you check the datasheet for your sensor, you will find a constant that you can use. In your case, it is roughly 4.88mV per 5mm. So to get the distance in millimeters, divide the voltage reading by 0.00488 and divide by 5.

I implemented the MaxBotix MB1200 earlier this season, so you could use this commit in our repo as a reference.

And as a warning: please keep the minimum distance reading in mind. It is for that reason that we decided not to use the MB1200.

You’ve got a few problems. The reason it’s crashing is that you’re declaring the two ultrasonic sensors, with all four DIOs using DIO1. This is crashing because of duplicate allocation. The error message in riolog or the DS probably said this. It appears that you added the ultrasonic declaration in your code manually, instead of letting RobotBuilder do it. If you used RobotBuilder, it wouldn’t have allowed you do that, all 4 ports would be unique.

Another problem is that you’re not using the appropriate class. Read the screensteps documentation on Ultrasonic sensors.

Hey guys, Im the programmer from 5247. Thanks for the input, its my first year and Im the only programmer on the team. Its our second year using java and our old programmer is no longer here.
So I found some code for an analog sensor and still havent had any luck. Any reason for this? I took all of our previous sensor code out of the program. I looked at the base code provided by one of the repliers, but it didnt work either so I have put it all in as comments.

Here is my sensor code. The v/in may be wrong, not sure if that would do anything.

package org.usfirst.frc5247.robot2018;

import edu.wpi.first.wpilibj.AnalogInput;

public class Sensors {

/**
 * The analog input that the sensor is in.
 */
private AnalogInput sensor;

/**
 * The voltage drop per inch that the target moves
 */
private double voltsPerInch;

/**
 * The voltage at the set zero position
 */
private double zeroPos;

/**
 * Creates a new UltrasonicRangeFinder 
 * @param device - the analog input the range finder is in
 * @param voltsPerInch - the volts the analog reading changes per inch
 * @return 
 */
public void UltrasonicRangeFinder(AnalogInput device, double voltsPerInch){
	device = new AnalogInput(0);
	sensor = device;
	this.voltsPerInch = 5.0 / .512; //.124
	zeroPos = 0;
}

/**
 * Sets the zero point for the sensor at the current distance
 */
public void zero(){
	zeroPos = sensor.getVoltage();
}

/**
 * Resets the zero point to 0
 */
public void resetZero(){
	zeroPos = 0;
}

/**
 * Gets the range in inches to the nearest object
 * @return The range in inches
 */
public double getRange(){
	return (sensor.getVoltage() - zeroPos) / voltsPerInch;
}

}

Sorry about the spam, but I dont know what to do. I had a pro programmer from another team look at the code and he said nothing was wrong, so he doesnt know how to help me. I would highly appreciate it as we are going to need it this year.

Thanks, Lizzie

Hi, we are planning to use 2 or 3 MaxBotix sensors on our robot. I have a few questions:

  1. The data sheet describes different methods of connecting the sensors in a single system. I want to go simple and connect each sensor independently to the roborio. Do you see any problems with that method? We are planning to mount one on the front and one on each side of the robot. It is a KoP robot with 33" x 28" dimensions.

  2. What is the best way to try out analog and pwm connections without actually soldering wires to the sensor? We do not know if this will be useful yet, so we would like to connect the sensor to the roborio and try before soldering.

  3. Is there a publicly available 3D printable case for this sensor?

Thank you!

Here is a minimal Robot class that should publish the distance in mm to NetworkTables. This is assuming you have the sensor connected to Analog 1.

If this works then you can proceed with your abstractions.

Cheers


package org.usfirst.frc5247.robot2018;

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

public class Robot extends IterativeRobot {
    private AnalogInput _sensor;
    private int _SENSOR_PORT = 1;
    private double _mvPerUnit = 4.88;
    private double _mmPerUnit = 5.0;

    @Override
    public void robotInit() {
        _sensor = new AnalogInput(_SENSOR_PORT);
    }

    @Override
    public void robotPeriodic() {
        double mv = (_sensor.getVoltage() * 1000)
        double mm = (mv / _mvPerUnit) * _mmPerUnit;
        SmartDashboard.putNumber("UltraSonic/Distance (mm)", mm);
    }

    @Override
    public void disabledInit() { }

    @Override
    public void autonomousInit() { }

    @Override
    public void teleopInit() { }

    @Override
    public void testInit() { }


    @Override
    public void disabledPeriodic() { }
    
    @Override
    public void autonomousPeriodic() { }

    @Override
    public void teleopPeriodic() { }

    @Override
    public void testPeriodic() { }
}

It works! Thank you!