|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools |
Rating:
|
Display Modes |
|
|
|
#1
|
|||
|
|||
|
Beginner FRC Java Programmer - Help
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 Code:
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);
}
Last edited by hasin5 : 10-12-2013 at 20:27. |
|
#2
|
||||
|
||||
|
Re: Beginner FRC Java Programmer - Help
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 and the WPILib Programming Guide. 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). Last edited by Domenic Rodriguez : 10-12-2013 at 22:08. |
|
#3
|
|||
|
|||
|
Re: Beginner FRC Java Programmer - Help
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.
|
|
#4
|
||||
|
||||
|
Re: Beginner FRC Java Programmer - Help
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 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 and DoubleSolenoid 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. Also, for the compressor... take a look at the javadoc for the Compressor 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: Code:
Compressor compressor;
int switchChannel = 1; //wherever these are plugged in on the DSC
int relayChannel = 1;
robotInit() {
compressor = new Compressor(switchChannel, relayChannel);
compressor.start();
}
Last edited by otherguy : 11-12-2013 at 13:42. Reason: compressor details |
|
#5
|
||||
|
||||
|
Re: Beginner FRC Java Programmer - Help
Quote:
hasin5, could you describe your wiring setup? That way we won't have to continue to make assumptions about it. |
|
#6
|
||||
|
||||
|
Re: Beginner FRC Java Programmer - Help
Quote:
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. Code:
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...
}
}
|
|
#7
|
||||||
|
||||||
|
Re: Beginner FRC Java Programmer - Help
Quote:
Code:
public void autonomous() { //checks code every 50 seconds
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. |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|