Programming a Simple Limit Switch

So we are trying to learn how to use a limit switch… we are programming in java command based… This switch is wired to the DIO port 0

we successfully used a few on a motor controller…Talon SRX as reverse and forward limit… This one is a standalone , basically tell me if a gear is in place or not…

in my RobotMap i have the following:

public class RobotMap {

public DigitalInput gearSwitch;

in my Robot.java i have the following:

public class Robot extends IterativeRobot {

DigitalInput gearSwitch;

and

public void robotInit() {
  gearSwitch = new DigitalInput(0);

If i want to display the switch value on the smartdashboard, what would be the appropriate code and most importantly where should i add to my code…

Any example would be appreciate. I research quite a bit and see a lot of great article, but they are not very specific for newbie like us…

You would use SmartDashboard.putBoolean somewhere that will be repeatedly called. This could be in a command or part of Robot.

In extension, use the RobotMap as a storage class for port numbers. For example, instead of

public DigitalInput gearSwitch;

in the RobotMap, use

public static final int GEAR_SWITCH_PORT = 0

Then when initializing your gearSwitch instance, call

  gearSwitch = new DigitalInput(RobotMap.GEAR_SWITCH_PORT); 

and change that variable in the RobotMap if you have to change a port number.

You are very close, just need to call the get() method on gearSensor object. Also, remove the gearSensor declaration in RobotMap.java because you already have one in Robot.java. Here is what we are using:

RobotMap.java


	// DIO
	public static final int GEAR_SENSOR_DIO_ID = 0;

GearIntake.java (subsystem, but you could also put this in Robot.java)


	private DigitalInput gearSensor  = new DigitalInput(RobotMap.GEAR_SENSOR_DIO_ID);


	public boolean isGearPresent() {
		return !gearSensor.get();
	}
	
	public void updateStatus(Robot.OperationMode operationMode) {
		SmartDashboard.putBoolean("Gear Sensor", isGearPresent());
		if (isGearPresent()) {
			Robot.ledLights.setGearLoaded(true);
		}
		else {
			Robot.ledLights.setGearLoaded(false);
		}
	}


If you want to use the limit switch to trigger a command like pressing a button you can create a DigitalIOSwitch class as shown below. You can then set up an event in OI.java just like you would for a controller button.


public class DigitalIOSwitch extends Button {
	DigitalInput digitalInput;

	public DigitalIOSwitch(int channel){
		digitalInput = new DigitalInput(channel);
	}

	public boolean get() {
		return digitalInput.get();
	}
}

Thank you all for your help!!!

quick question, where will i put the DigitalIOSwitch ?

Anywhere you like. You could create a new package (directory) to organize your project (ours is called ‘buttons’, placed below the main src directory) or you could just save it in an existing directory like ‘commands’. Below is our full code.


package org.usfirst.frc.team3310.buttons;

import edu.wpi.first.wpilibj.DigitalInput;
import edu.wpi.first.wpilibj.buttons.Button;

/**
 * @author rhhs
 */
public class DigitalIOSwitch extends Button {
	DigitalInput digitalInput;

	public DigitalIOSwitch(int channel){
		digitalInput = new DigitalInput(channel);
	}

	public boolean get() {
		//System.out.println("Switch = " + digitalInput.get());           
		return digitalInput.get();
	}
}

Thank you everyone!!!

Does anyone has a github reference they used as an example to refer to ?

We are getting better at this but the main concern is not knowing where to put a new line of code or not understanding all the relationship between class and where to put stuff in…

One day…hopefully i can go back to project management and let them all wit this java stuff!!! lol
:ahh: :ahh: :ahh: