Spike relay help

Here is our program part for the spike but it doesn’t work. Why?
It says there are no errors.

package edu.wpi.first.wpilibj.templates;
import edu.wpi.first.wpilibj.Jaguar;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.Victor;
import edu.wpi.first.wpilibj.Relay;

import edu.wpi.first.wpilibj.SimpleRobot;

public class RobotTemplate extends SimpleRobot {
public Joystick leftStick = new Joystick(1);
public Victor leftDrive = new Victor(1);
public Victor rightDrive = new Victor(2);
public Jaguar middleDrive = new Jaguar(3);
public Relay spike = new Relay(1);

public void autonomous() {     
}

public void operatorControl() {
    while (isOperatorControl() && isEnabled()) {

            {if (leftStick.getRawButton(1)== true) {
    spike.set(Relay.Value.kOn);
} 
else {
spike.set(Relay.Value.kOff);}}

{if (leftStick.getRawButton(2)== true) {
       leftDrive.set((leftStick.getX()+ leftStick.getY()));
       rightDrive.set((leftStick.getX()- leftStick.getY()));

}else{
               leftDrive.set(0);
               rightDrive.set(0);
               }}

    }
}    public void test() {

}

}

“spike.set(Relay.Value.kOn;” should be “spike.set(Relay.Value.kOn**)**;”

Try putting System.out.println statements inside the for statements to see if it’s getting to the set commands.

I can’t see all of your code, but you probably shouldn’t use delay().

Next time please give more details, if adding the parenthesis doesn’t help and you can’t figure it out with the println statements, give some more details and I’ll try to help more.

Is kOn what you really want? Read the description here: http://wpilib.screenstepslive.com/s/3120/m/7912/l/132406-on-off-control-of-motors-and-other-mechanisms-relays

Why do you think it doesn’t work? Does the Spike LED change between yellow/orange and off? Do the red and green relay status LEDs on the Digital Sidecar both go on, then both go off?

The kOn constant sets both M- and M+ outputs of the Spike to battery voltage. Depending on what you have connected to it, that probably won’t do what you want. Instead, use kForward. That will set M+ to battery and M- to “ground”, powering whatever is connected between them.

Here are some simple but important troubleshooting steps in addition to the great responses so far. Do make sure you have communication to the Relay, and proper power wired to it and that you are using the proper relay channel to control your spike. When the robot turns on, the LED on the spike should be orange, signaling that it has power.

Can you paste the code of your call to the constructor as well?

Again, you are only trying to set the value of the relay in either Teleop, Auto, or Test modes correct, while the robot is enabled?

Relay outputs are non-active while the robot is disabled. Are you also sure that you are hitting the proper button on the left stick. I am not sure what controller you used, but make sure you are pressing the button defined as “1”.

Your code is fine and should command the relay (assuming you want kOn instead of kForward), if everything is wired up correctly and your code is placed in the proper location. So start with checking those first.

Also what kind of Robot Framework are you using, Simple, Iterative, or CommandBase? If the code running is not in a loop which is the default for SimpleRobot, then the code will run once and end, never giving you the option to hit the button. You would need to place it in a loop. If you are using Iterative, or Command Base, then the code should be in one of the Autonomous, Teleop, or Test - Periodic methods.

Regards,
Kevin

Also,

what is the purpose for Timer.delay?

Unless you are running a multi-threaded robot, you should remove this line of code. This code halts your entire robot for that amount of time.

Generally speaking, you should never use a delay function on a robot, unless you are doing it purposefully, understand exactly what is being delayed, and are running a multi-threaded robot.

This line, Timer.delay(0.1) will halt your entire robot (unless it is in a separate thread from Robot Main) for 1/10th of a second. The control loop of the robot, expects to run once every 50ms, this line of code is preventing your robot from doing so, and it can/will lead to watchdog errors or communication problems, especially if you use larger delays else where in code.

Regards,
Kevin

Thanks for posting more of your code.

I do not see any obvious errors with your code. But I would do the following in your code:

  1. Change kOn to kForward
  2. Post what joystick model you are using, and ensure you are pressing the correct button.
  3. Add print statements to your code so you can see if you are pressing the proper button

for example:


if (leftStick.getRawButton(1) == true) 
{
	spike.set(Relay.Value.kOn);
	System.out.println("Button 1 Pressed, Relay LED should be Green");
} 
else 
{
	spike.set(Relay.Value.kOff);
	System.out.println("Button 1 Not Pressed, Relay LED should be Orange");
}


Then check the wiring and answer the following questions here so we can help you further:

  1. Make sure you plugged your spike in to relay channel 1 of the DSC. Are you using a 4 slot cRIO or an 8 slot? \
  2. Does the DSC have its 12V and 5V power leds lit up?
  3. Does the joystick drive commands work when button 2 is pressed?
  4. Ensure that you are enabling the robot in Teleop mode from the driver station.
  5. What is the color of the Relay LED when you enable the robot?
  6. Post the print output of the code from above