Log in

View Full Version : Programming Ring Light [Java]


TheoBlacksmith
13-02-2014, 15:12
Hi, Even though we are not a rookie team, this is the first year we have used a LED ring light. I have a few years experience with Java and though i have put several days into trying to program this light in java, I cannot figure it out. Right now we have 1 LED ring light hooked up to a Spike that is in turn connected to Digital I/O port 2 on the Digital Sidecar. Right now I just want to toggle the light with a trigger on the joystick. This is not the final thing I want though, it's just for testing. Any help will be appreciated.

Domenic Rodriguez
13-02-2014, 15:30
If you're using a Spike Relay, then the signal cable should be connected to a Relay port on the sidecar, not a DIO port.

In your code, you'll want to use the Relay class. You use the Relay#set() method to control the output of the relay:

// Create the relay
Relay ledRelay = new Relay(RELAY_PORT);

// Control the relay
ledRelay.set(Relay.Value.kOn);
ledRelay.set(Relay.Value.kOff);

TheoBlacksmith
13-02-2014, 15:37
Thanks for that, but that is the first thing that i tried, I moved over to the I/O ports because others seemed to be doing that. Ive been all over the Digital Sidecar.

Domenic Rodriguez
13-02-2014, 15:54
When you call the Relay#set() method, the LEDs next to the relay ports on the sidecar change to indicate the state of each relay (red for reverse, green for forwards/on). Did you see these lights change while testing?

Could you post the relevant portions of your code? That will help to track down the problem.

Another option you might have would be to wire your LED light to the solenoid module and use the Solenoid class to control it.

TheoBlacksmith
13-02-2014, 16:06
While testing, I see the lights light up on the right relay channels. we also know that the spike and LED are wired up correctly because we placed the PWM for the compressor spike in the LED spike and the LED lit up. The only issue we could be having is a faulty PWM, but we switched it out twice.

MagiChau
13-02-2014, 16:08
Perhaps you wired the ring light incorrectly. Try reversing the wires.

TheoBlacksmith
13-02-2014, 17:33
Okay, it seems that the PWMs weren't in place properly. The light is toggled now, but i have another issue. when the button is pressed, the light simply flashed and the light on the spike shuts off. This can be repeated by pressing the button again and again, but the light never stays on.

nyaculak
13-02-2014, 17:46
We will be able to better help you debug your code if you post it here.

On another note...Do you really need to be able to turn the led ring on and off? It would be a lot easier to just connect it to the PDB.

TheoBlacksmith
13-02-2014, 17:52
Here is the Code:

import edu.wpi.first.wpilibj.Talon;
import edu.wpi.first.wpilibj.Relay;
import edu.wpi.first.wpilibj.Victor;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.Solenoid;
import edu.wpi.first.wpilibj.Compressor;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.SimpleRobot;
import edu.wpi.first.wpilibj.SpeedController;
import edu.wpi.first.wpilibj.GenericHID.Hand;
import edu.wpi.first.wpilibj.AnalogPotentiometer;

public class ASimpleJavaBot extends SimpleRobot {
// Constants
private static final Hand LEFTHAND = Hand.kLeft;
private static final Hand RIGHTHAND = Hand.kRight;

Solenoid launcherClosed = new Solenoid(1);
Solenoid launcherOpen = new Solenoid(2);
Solenoid armsClosed = new Solenoid(3);
Solenoid armsOpen = new Solenoid(4);
SpeedController frontRight = new Victor(3);
SpeedController frontLeft = new Victor(2);
SpeedController rearRight = new Victor(1);
SpeedController rearLeft = new Victor(4);
SpeedController rightArmMotor = new Talon(6);
SpeedController leftArmMotor = new Talon(7);
Compressor compressor = new Compressor(1, 1);
Relay Light = new Relay(2);
Joystick joy1 = new Joystick(1);

XboxController cont1 = new XboxController(1);
XboxController cont2 = new XboxController(2);

RobotDrive drive = new RobotDrive(frontLeft, rearLeft, frontRight,
rearRight);

/**
* This function is called once each time the robot enters autonomous mode.
*/

public void autonomous() {
}

/**
* This function is called once each time the robot enters operator control.
*/
public void operatorControl() {
int count = 0;
compressor.start();
boolean armsDown = false;
boolean ltPressed = false;
drive.setInvertedMotor(RobotDrive.MotorType.kFront Left, false);
drive.setInvertedMotor(RobotDrive.MotorType.kRearL eft, false);
drive.setInvertedMotor(RobotDrive.MotorType.kFront Right, true);
drive.setInvertedMotor(RobotDrive.MotorType.kRearR ight, true);

if (ltPressed == false) {

if (cont2.getTrigger(LEFTHAND) == true) {

if (armsDown == true) {

armsOpen.set(false);
armsClosed.set(true);

armsDown = false;
ltPressed = true;

}

else {
armsOpen.set(true);
armsClosed.set(false);
ltPressed = true;
armsDown = true;

}

}
} else {

if (cont2.getTrigger(LEFTHAND) == false) {
ltPressed = false;
}
}

if (cont2.getBumper(LEFTHAND) == true) {
// up
rightArmMotor.set(1);
leftArmMotor.set(-1);
}

if (cont2.getBumper(RIGHTHAND) == true) {
// down
rightArmMotor.set(-.7);
leftArmMotor.set(.7);
}
if ((cont2.getBumper(RIGHTHAND) == false)
&& (cont2.getBumper(LEFTHAND) == false)) {

rightArmMotor.set(0);
leftArmMotor.set(0);
}

if (cont1.getBumper(RIGHTHAND) == true) {
Light.set(Relay.Value.kOn);

}
if (cont1.getBumper(RIGHTHAND) == false) {
Light.set(Relay.Value.kOff);

}

}

}

private static double RoundToDecimalPlace(double value, int decimalPlaces) {
double exponent = 10 ^ decimalPlaces;
int rounded = (int) (value * exponent);
return (double) rounded / exponent;
}
}

Joe Ross
13-02-2014, 17:57
In your code, you'll want to use the Relay class. You use the Relay#set() method to control the output of the relay:

// Create the relay
Relay ledRelay = new Relay(RELAY_PORT);

// Control the relay
ledRelay.set(Relay.Value.kOn);
ledRelay.set(Relay.Value.kOff);


This will not do what you expect. kOn sets both outputs of the spike to 12v, which will cause a motor or an LED to be off. You should use kForward.

You can use kOn if you set the direction of the relay to Forward only.

TheoBlacksmith
13-02-2014, 18:09
Thank you,
The kForward worked for me.

Domenic Rodriguez
13-02-2014, 18:54
This will not do what you expect. kOn sets both outputs of the spike to 12v, which will cause a motor or an LED to be off. You should use kForward.

You can use kOn if you set the direction of the relay to Forward only.
I guess I was assuming that kOn would be equivalent to kForward by default; thanks for the heads up. So adding a call to Relay#setDirection() would give the desired behavior, right?

// Create the relay
Relay ledRelay = new Relay(RELAY_PORT);

// Specify the direction for the relay
ledRelay.setDirection(Relay.Direction.kForward);

// Control the relay
ledRelay.set(Relay.Value.kOn);
ledRelay.set(Relay.Value.kOff);

Normally I would just use kForward. The reasoning here was to avoid accidentally setting the relay to kReverse and potentially damaging the LEDs. Granted, it's easy enough to just remember not to use kReverse...

Joe Ross
13-02-2014, 19:02
I guess I was assuming that kOn would be equivalent to kForward by default; thanks for the heads up. So adding a call to Relay#setDirection() would give the desired behavior, right?

Yes.
Normally I would just use kForward. The reasoning here was to avoid accidentally setting the relay to kReverse and potentially damaging the LEDs. Granted, it's easy enough to just remember not to use kReverse...

The nice thing about LEDs is they can't be damaged by reverse polarity (unless the thing you're driving has integrated control electronics also, but then you probably wouldn't be driving it from a spike).

nyaculak
13-02-2014, 21:11
The code supplied is not going to work at all. The operatorControl() function is only called once, so it's necessary to surround all of the control logic in a while loop

while(isEnabled() && isOperatorControl()) {
// code goes here
}

TheoBlacksmith
14-02-2014, 12:22
I'm not sure what you are talking about Nyaculak. The code here works just fine for me.

nyaculak
14-02-2014, 20:02
I'm not sure what you are talking about Nyaculak. The code here works just fine for me.

It's my understanding that the functions of the SimpleRobot template have to be encompassed in the while loop to be called repeatedly. Can anybody shed some light on why this seems to not be the case?