Log in

View Full Version : Beginner FRC Java Programmer - Help


hasin5
10-12-2013, 20:21
I ama beginner programmer in terms of the Robotics API (WpiLibJ) and I've been studying a bit of it and I came up with this piece of code to drive the robot forward and then have the piston retract and extract with 1 second delays as test. Can someone clarify if this works, and if not, please help me figure out why it doesn't work. It'd also be awesome if someone can reference a FRC Java guide that takes me through the basic procedure of getting me started in learning the API.

Code:

# = placeholder for real value

RobotDrive theRobot = new RobotDrive(#);
Timer timer = new Timer();
Relay spike = new Relay(#);
Solenoid piston = new Solenoid(#);
Compressor compressor1 = new Compressor(#);

public void autonomous() { //checks code every 50 seconds

theRobot.setSafetyEnabled(false);
theRobot.drive(-1, 0.0);
timer.delay(2.0);
theRobot.drive(0.0, 0.0);
compressor1.start();
piston.set(true);
for (int x = 0; x < 5; x++) {
spike.set(Relay.Value.kForward);
timer.delay(1.0);
spike.set(Relay.Value.kReverse);
timer.delay(1.0);
}
piston.set(false);


}

Domenic Rodriguez
10-12-2013, 21:47
I'm assuming the code you posted here was copied from inside a class that inherits from the SimpleRobot class? If so, then everything is mostly good. One note is that the Timer.delay() method is static, meaning you don't have to create a Timer object to use the method. In other words, use "Timer.delay(...)" instead of creating a Timer object and using "timer.delay(...)". Also, could you clarify on what the Relay is for? I'm not sure how that is fitting in to the behavior you described.

The control system documentation site has some excellent resources on the WPILibJ API. Of particular relevance is the Java Getting Started Guide (http://wpilib.screenstepslive.com/s/3120/m/7885) and the WPILib Programming Guide (http://wpilib.screenstepslive.com/s/3120/m/7912). The Javadocs for WPILib are also included with your installation. You can find them in the C:\Users\username\sunspotfrcsdk\doc\javadoc folder on your computer (~/sunspotfrcsdk/doc/javadoc on OS X and Linux).

hasin5
11-12-2013, 11:01
Well I thought the Relay class would be the one to actually have the piston shoot forward and backward whereas the Solenoid class simply sets if the piston is set to be run (true/false). Can you elaborate on this? I am just assuming things here.

otherguy
11-12-2013, 13:32
Javadocs for the 2013 season are here: http://team2168.org/javadoc/
This likely will be updated once 2014 code is available.

The Solenoid class(es) and the Relay class can both be used to achieve motion of a pneumatic cylinder. The difference between them is in the type of hardware each one controls.

As you noted, the Relay (http://team2168.org/javadoc/edu/wpi/first/wpilibj/Relay.html) class controls a Spike. The Spike would have it's M+ and M- terminals connected to the red wires on the solenoid (assuming the valve has two solenoids on it). The black wires from the two solenoids would be tied together and brought back to the power distribution board or DSC ground.

The Solenoid (http://team2168.org/javadoc/edu/wpi/first/wpilibj/Solenoid.html) and DoubleSolenoid (http://team2168.org/javadoc/edu/wpi/first/wpilibj/DoubleSolenoid.html) classes take the spike out of the equation. They control the same solenoid valve hardware, but through the NI relay module (9472), which resides in the 3rd slot of the cRIO (http://team358.org/files/programming/ControlSystem2009-/cRIO%20FRC%20II.jpg).

Also, for the compressor... take a look at the javadoc for the Compressor (http://team2168.org/javadoc/edu/wpi/first/wpilibj/Compressor.html) class. The constructor needs two parameters: the location of the pressure switch and the location of the relay turning the compressor on/off. If you've wired things up right, this is a set-it-and-forget-it class. Al you need to do is enable the compressor and it will automatically turn itself on and off. I would suggest placing compressor code in the robotInit() method as follows:

Compressor compressor;
int switchChannel = 1; //wherever these are plugged in on the DSC
int relayChannel = 1;

robotInit() {
compressor = new Compressor(switchChannel, relayChannel);
compressor.start();
}


What you had would work, but moving the compressor code out of the auto method helps to organize the code a bit better IMO.

Joe Ross
11-12-2013, 13:47
I'm assuming the code you posted here was copied from inside a class that inherits from the SimpleRobot class?

This is an important distinction. A description of SimpleRobot and IterativeRobot is available here: http://wpilib.screenstepslive.com/s/3120/m/7912/l/130578-choosing-a-base-class

public void autonomous() { //checks code every 50 seconds

By having an autonomous method, it appears to inherit from SimpleRobot, rather then IterativeRobot. However, the comment appears to be based on IterativeRobot. In SimpleRobot, the autonomous method is called once. In IterativeRobot, the autonomousInit method is called once at the beginning of Autonomous, and the autonomousPeriodic method is called ever 1/50th of a second.

If you use SimpleRobot, you want to make sure your code in the autonomous method takes less then 15 seconds to run (if you want it to work on the real field). Alternately you can check isAutonomous() and isEnabled() methods and return if you are no longer in autonomous or no longer enabled). Your code right now should take less then 15 seconds, but you might run into issues later.

If you use IterativeRobot, you can't use a Timer.delay in the autonomousPeriodic method. It needs to return quickly so that new driver station data is read. Instead, you can check a timestamp and only do something when a certain amount of time has elapsed.

Domenic Rodriguez
11-12-2013, 14:07
...
The Solenoid class(es) and the Relay class can both be used to achieve motion of a pneumatic cylinder. The difference between them is in the type of hardware each one controls.

As you noted, the Relay (http://team2168.org/javadoc/edu/wpi/first/wpilibj/Relay.html) class controls a Spike. The Spike would have it's M+ and M- terminals connected to the red wires on the solenoid (assuming the valve has two solenoids on it). The black wires from the two solenoids would be tied together and brought back to the power distribution board or DSC ground.

The Solenoid (http://team2168.org/javadoc/edu/wpi/first/wpilibj/Solenoid.html) and DoubleSolenoid (http://team2168.org/javadoc/edu/wpi/first/wpilibj/DoubleSolenoid.html) classes take the spike out of the equation. They control the same solenoid valve hardware, but through the NI relay module (9472), which resides in the 3rd slot of the cRIO (http://team358.org/files/programming/ControlSystem2009-/cRIO%20FRC%20II.jpg).
...

From my understanding, you would use either a Relay or the Solenoid class, but not both. Is that correct? Assuming the OP's solenoid valve is wired to a Spike Relay, the code should work but the Solenoid instance "piston" is not serving any purpose and could be removed.

hasin5, could you describe your wiring setup? That way we won't have to continue to make assumptions about it.

otherguy
12-12-2013, 13:04
From my understanding, you would use either a Relay or the Solenoid class, but not both. Is that correct? Assuming the OP's solenoid valve is wired to a Spike Relay, the code should work but the Solenoid instance "piston" is not serving any purpose and could be removed.

Yes, I should have paid more attention to the original code.
You would choose either the Relay or Solenoid class to control the output device. Using both classes simultaneously and wiring both hardware outputs to a solenoid valve would be like wiring two motor controllers to a single motor.




import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.Relay;
import edu.wpi.first.wpilibj.Compressor;
import edu.wpi.first.wpilibj.SpeedController;
import edu.wpi.first.wpilibj.Victor;

public class MyRobot extends SimpleRobot {

RobotDrive theRobot;
SpeedController leftMotor, rightMotor;
Compressor compressor;
Relay piston;


//These need to match your wiring on the DSC
//Digital Inputs/Outputs
int compressorSwitchChan = 1;

//Relay Channels
int compressorRelayChan = 1;
int pistonRelayChan = 2;

//PWM Channels
int leftMotorChan = 1;
int rightMotorChan = 2;

robotInit() {
//These need to match the motor type you are using.
//If you are using Jaguars or Talons, use thier respective class.
leftMotor = new Victor(leftMotorChan);
rightMotor = new Victor(rightMotorChan);
theRobot = new RobotDrive(leftMotor, rightMotor);

piston = new Relay(pistonRelayChan);

compressor = new Compressor(compressorSwitchChan, compressorRelayChan);
compressor.start();
}

public void autonomous() { //checks code every 50 seconds
theRobot.setSafetyEnabled(false);
theRobot.drive(-0.5, 0.0);
Timer.delay(2.0);
theRobot.drive(0.0, 0.0);

for (int x = 0; x < 5; x++) {
piston.set(Relay.Value.kForward);
timer.delay(1.0);
piston.set(Relay.Value.kReverse);
timer.delay(1.0);
}
}

public void operatorControl() {
//your teleoperated code here...
}
}


This isn't tested, but should be pretty close to correct.