Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   General Forum (http://www.chiefdelphi.com/forums/forumdisplay.php?f=16)
-   -   Rookie Java FRC programming buttons (http://www.chiefdelphi.com/forums/showthread.php?t=126280)

aweso_meme 11-02-2014 21:45

Rookie Java FRC programming buttons
 
Hello all,

I have run into some trouble while attempting to hook up pneumatics to the robot. This is my first time programming for FRC, and the first time my team is developing code. I will paste my code below, but first a description of the problem. The robot does drive. All wires are hooked up to where they should be. However, whenever I pull the trigger on the joystick (Logitech Attack 3), the code does not perform. A solenoid is supposed to trigger rapidly on and off, but the solenoid does not trigger on and off in practice. I am using the basic layout for my code.
Without further ado, here is my code (I do my brackets differently than most):

package edu.wpi.first.wpilibj.templates;
//import statements

public class RobotCode extends simpleRobot {

private RobotDrive drivetrain;
private Joystick driveStick;
Solenoid solenoid;
Trigger jButton;
int j = 0; // standard int which will be used in a for loop.

public void initMethod()
{
// Initialization code goes here
drivetrain = new RobotDrive(1,2,3,4);
driveStick = new Joystick(1);
solenoid = new Solenoid(1);
}
public void autonomous() { }


public void operatorControl()
{
while (true && isOperatorControl() && isEnabled())
{
drivetrain.arcadeDrive(driveStick);
Timer.delay(0.001);
if (driveStick.getTrigger() == true)
{
for (j=0 ; j<3 ; j++)
{
solenoid.set(true);
Timer.delay(0.1);
solenoid.set(false);
Timer.delay(0.1);
} //End For Loop
} // End if Statement
} // End While Statement
} // End Method


public void test() { }
}//END CODE

Thank you for your help. For your reference, I have coded in Java before and am currently in AP Computer Science.

tStano 11-02-2014 22:53

Re: Rookie Java FRC programming buttons
 
It appears that you have not initialized your button. You initialized the joystick but you need to initialize the button as well. Thats my best guess.

Also, If this is a pneumatic solenoid and you're expecting it to go in and out; it wont; you'll need a second solenoid object for the retract. If its an electrical or single action solenoid, ignore this bit, I have no experience in those areas.

Domenic Rodriguez 12-02-2014 00:39

Re: Rookie Java FRC programming buttons
 
When using joysticks, I generally use the Joystick#getRawButton() method as opposed to the specialized methods. You might want to try using:
Code:

...
if (driveStick.getRawButton(1))  // '== true' is not necessary in an if statement for a boolean
{
...

You can use System.out.println() statements inside the if statement to help you track down the issue. That will show you if the issue is the Joystick button or the solenoid code.

You may have already done this, but you should check the lights on top of the solenoid module while the code runs to ensure that you indeed have a code issue.

Quote:

Originally Posted by tStano
It appears that you have not initialized your button. You initialized the joystick but you need to initialize the button as well. Thats my best guess.

Correct me if I'm wrong, but the Trigger and Button classes are for use with the Command Based robot patern. OP is using SimpleRobot as the base class, and thus shouldn't be using Buttons or Triggers.

(Protip: if you wrap your code in [code][/code] tags, it will format nicer.)

seg9585 12-02-2014 11:12

Re: Rookie Java FRC programming buttons
 
Quote:

Originally Posted by DomenicR (Post 1341640)
When using joysticks, I generally use the Joystick#getRawButton() method as opposed to the specialized methods.

According to the code above they are using the Joystick class, not Trigger (there is a "jButton" initialized but its not used). They are properly using the Joystick class. I will note that I have had trouble with the trigger in the past as well, and resort to just using buttons.

As a form of troubleshooting, why don't you use a Button instead to test and see if the Trigger is causing the issue?

In other news, I don't think the code you have there will do exactly what you want. When the trigger is pushed you will enter that for{} loop, but every 0.2001 seconds you will be re-entering that loop until the finger is taken off the trigger.
I assume you want to just toggle the valves 3 times and that's it when the button is pushed? If so, you'll need to add a little logic that checks whether the button has just been pressed. For example:

boolean triggered=false;

if(driveStick.getTrigger() && !triggered){
triggered=true;
for{} loop;}

else if(!driveStick.getTrigger()){
triggered=false;}

In other news, avoid using local for and while loops in SimpleRobot. While that loop is running, you will not get any Drive updates. For just 0.2 seconds maybe you can get away with it, but not good practice in general. Instead, you can either create a separate thread (See Thread class), or what I like to do is create a function that will track its own timing when called repeatedly and return a true or false boolean to indicate when it's done (use the Timer class)

aweso_meme 12-02-2014 17:19

Re: Rookie Java FRC programming buttons
 
So what would be the code for creating a Thread class?
Do I need to create a new class inside my current package, and then in the main class call Loop xyz = new Loop();, or do i need a certain constructor? Also, with xyz.start(); I read on the internet that these threads have precedence. Does this mean I can have the main class running at the same time or no?

EDIT: Also, If I do need to make a new class, what methods do I need to define and how do I call them in the tele-op method?

Thanks again.

seg9585 13-02-2014 01:10

Re: Rookie Java FRC programming buttons
 
Quote:

Originally Posted by aweso_meme (Post 1341999)
So what would be the code for creating a Thread class?
Do I need to create a new class inside my current package, and then in the main class call Loop xyz = new Loop();, or do i need a certain constructor? Also, with xyz.start(); I read on the internet that these threads have precedence. Does this mean I can have the main class running at the same time or no?

EDIT: Also, If I do need to make a new class, what methods do I need to define and how do I call them in the tele-op method?

Thanks again.

You need to create a new Java file, with the name the same as your new class (this is the case for any class -- which you can call by adding to your project and creating an object of your new Class name.

For a "Thread" example:

class YourNewClass extends Thread {

YourNewClass(int x) {
//This is the constructor
}

public void run() {
//Put a while() loop in here
. . .
}
}

from your teleop class:

YourNewClass triggeraction = new YourNewClass();
triggeraction.setPriority(1); //The lower the priority, the thread will be executed last in the computing cycles. teleOp is top priority
triggeraction.run();

while(isEnabled()){....


All times are GMT -5. The time now is 05:40.

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