Go to Post Quality has nothing to do with desire – you can’t wish quality. Quality has nothing to do with time, or money, or the other guy’s attitude. Quality has everything to do with craftsmanship. - Jack Jones [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rating: Thread Rating: 3 votes, 4.33 average. Display Modes
  #1   Spotlight this post!  
Unread 10-12-2013, 20:21
hasin5 hasin5 is offline
Registered User
None #4777
 
Join Date: Jan 2013
Location: Canada
Posts: 20
hasin5 is an unknown quantity at this point
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.
Reply With Quote
  #2   Spotlight this post!  
Unread 10-12-2013, 21:47
Domenic Rodriguez's Avatar
Domenic Rodriguez Domenic Rodriguez is offline
Registered User
FRC #0316 (LuNaTeCs)
Team Role: College Student
 
Join Date: Sep 2010
Rookie Year: 2011
Location: Grove City, PA
Posts: 213
Domenic Rodriguez has a spectacular aura aboutDomenic Rodriguez has a spectacular aura aboutDomenic Rodriguez has a spectacular aura about
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).
__________________

LuNaTeCs - Learning Under Nurturing Adults Teaching Engineering Concepts and Skills - Small and Mighty!

FRC 316 LuNaTeCs - Student (2011-2014), Lead Programmer (2011-2014), Team Captain (2013-2014), Operator (2013), Drive Coach (2014), Mentor (2015-????)
'11 Philly Regional Finalists, '13 Chestnut Hill Finalists, '13 Lenape Champions, '13 Archimedes Division, '14 Chestnut Hill Champions, '14 Lenape Champions
FTC 7071 EngiNerds - Founding Advisor (2013-2014) | FRC 5420 Velocity - Founding Advisor (2015)
Grove City College Class of '18, Electrical/Computer Engineering (B.S.E.E)


Last edited by Domenic Rodriguez : 10-12-2013 at 22:08.
Reply With Quote
  #3   Spotlight this post!  
Unread 11-12-2013, 11:01
hasin5 hasin5 is offline
Registered User
None #4777
 
Join Date: Jan 2013
Location: Canada
Posts: 20
hasin5 is an unknown quantity at this point
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.
Reply With Quote
  #4   Spotlight this post!  
Unread 11-12-2013, 13:32
otherguy's Avatar
otherguy otherguy is offline
sparkE
AKA: James
FRC #2168 (The Aluminum Falcons)
Team Role: Mentor
 
Join Date: Feb 2010
Rookie Year: 2009
Location: CT
Posts: 429
otherguy is a splendid one to beholdotherguy is a splendid one to beholdotherguy is a splendid one to beholdotherguy is a splendid one to beholdotherguy is a splendid one to beholdotherguy is a splendid one to beholdotherguy is a splendid one to behold
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();
}
What you had would work, but moving the compressor code out of the auto method helps to organize the code a bit better IMO.
__________________
http://team2168.org

Last edited by otherguy : 11-12-2013 at 13:42. Reason: compressor details
Reply With Quote
  #5   Spotlight this post!  
Unread 11-12-2013, 13:47
Joe Ross's Avatar Unsung FIRST Hero
Joe Ross Joe Ross is offline
Registered User
FRC #0330 (Beachbots)
Team Role: Engineer
 
Join Date: Jun 2001
Rookie Year: 1997
Location: Los Angeles, CA
Posts: 8,561
Joe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond repute
Re: Beginner FRC Java Programmer - Help

Quote:
Originally Posted by DomenicR View Post
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/...g-a-base-class

Code:
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.
Reply With Quote
  #6   Spotlight this post!  
Unread 11-12-2013, 14:07
Domenic Rodriguez's Avatar
Domenic Rodriguez Domenic Rodriguez is offline
Registered User
FRC #0316 (LuNaTeCs)
Team Role: College Student
 
Join Date: Sep 2010
Rookie Year: 2011
Location: Grove City, PA
Posts: 213
Domenic Rodriguez has a spectacular aura aboutDomenic Rodriguez has a spectacular aura aboutDomenic Rodriguez has a spectacular aura about
Re: Beginner FRC Java Programmer - Help

Quote:
Originally Posted by otherguy View Post
...
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.
...
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.
__________________

LuNaTeCs - Learning Under Nurturing Adults Teaching Engineering Concepts and Skills - Small and Mighty!

FRC 316 LuNaTeCs - Student (2011-2014), Lead Programmer (2011-2014), Team Captain (2013-2014), Operator (2013), Drive Coach (2014), Mentor (2015-????)
'11 Philly Regional Finalists, '13 Chestnut Hill Finalists, '13 Lenape Champions, '13 Archimedes Division, '14 Chestnut Hill Champions, '14 Lenape Champions
FTC 7071 EngiNerds - Founding Advisor (2013-2014) | FRC 5420 Velocity - Founding Advisor (2015)
Grove City College Class of '18, Electrical/Computer Engineering (B.S.E.E)

Reply With Quote
  #7   Spotlight this post!  
Unread 12-12-2013, 13:04
otherguy's Avatar
otherguy otherguy is offline
sparkE
AKA: James
FRC #2168 (The Aluminum Falcons)
Team Role: Mentor
 
Join Date: Feb 2010
Rookie Year: 2009
Location: CT
Posts: 429
otherguy is a splendid one to beholdotherguy is a splendid one to beholdotherguy is a splendid one to beholdotherguy is a splendid one to beholdotherguy is a splendid one to beholdotherguy is a splendid one to beholdotherguy is a splendid one to behold
Re: Beginner FRC Java Programmer - Help

Quote:
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.


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...
  }
}
This isn't tested, but should be pretty close to correct.
__________________
http://team2168.org
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 10:30.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi