![]() |
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. |
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. |
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:
...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:
(Protip: if you wrap your code in [code][/code] tags, it will format nicer.) |
Re: Rookie Java FRC programming buttons
Quote:
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) |
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. |
Re: Rookie Java FRC programming buttons
Quote:
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