![]() |
CodeProblem
I run the code and the Driver station accepts it and allows it to run but when I press the buttons nothing happens? Pleas help ASAP
package org.usfirst.frc.team4284.robot; import edu.wpi.first.wpilibj.IterativeRobot; import edu.wpi.first.wpilibj.Jaguar; import edu.wpi.first.wpilibj.buttons.JoystickButton; import edu.wpi.first.wpilibj.buttons.Button; import edu.wpi.first.wpilibj.Joystick; import edu.wpi.first.wpilibj.RobotDrive; import edu.wpi.first.wpilibj.livewindow.LiveWindow; /** * The VM is configured to automatically run this class, and to call the * functions corresponding to each mode, as described in the IterativeRobot * documentation. If you change the name of this class or the package after * creating this project, you must also update the manifest file in the resource * directory. */ public class Robot extends IterativeRobot { RobotDrive Drive; Joystick drivestick; Joystick joystick; int autoLoopCounter; /** * This function is run when the robot is first started up and should be * used for any initialization code. */ public void robotInit() { Jaguar FL = new Jaguar (5); Jaguar BL = new Jaguar (2); Jaguar FR = new Jaguar (3); Jaguar BR = new Jaguar (4); Drive = new RobotDrive(FL,BL,FR,BR); drivestick = new Joystick(0); joystick = new Joystick(1); } /** * This function is run once each time the robot enters autonomous mode */ public void autonomousInit() { autoLoopCounter = 0; } /** * This function is called periodically during autonomous */ public void autonomousPeriodic() { /*if(autoLoopCounter < 100) //Check if we've completed 100 loops (approximately 2 seconds) { Drive.drive(-0.5, 0.0); // drive forwards half speed autoLoopCounter++; } else { Drive.drive(0.0, 0.0); // stop robot } */ } /** * This function is called once each time the robot enters tele-operated mode */ public void teleopInit(){ } /** * This function is called periodically during operator control */ public void teleopPeriodic() { Drive.arcadeDrive(drivestick); Button button1 = new JoystickButton(joystick, 1), button2 = new JoystickButton(joystick, 2), button3 = new JoystickButton(joystick, 3), button4 = new JoystickButton(joystick, 4), button5 = new JoystickButton(joystick, 5), button6 = new JoystickButton(joystick, 6), button7 = new JoystickButton(joystick, 7), button8 = new JoystickButton(joystick, 8), button9 = new JoystickButton(joystick, 9), button10 = new JoystickButton(joystick, 10), button11 = new JoystickButton(joystick , 11); button1.whenPressed(new ElevatorOff()); button2.whenPressed(new ElevatorOn()); button3.whenPressed(new ElevatorReverse()); button4.whenPressed(new ArmsOut()); button5.whenPressed(new ArmsIn()); button6.whenPressed(new ArmsVerticalIn()); button7.whenPressed(new ArmsVarticalOut()); button8.whenPressed(new BlueLightShow()); button9.whenPressed(new LigthShowOn()); button10.whenPressed(new RedLightShow()); button11.whenPressed(new LightShowEnd()); } /** * This function is called periodically during test mode */ public void testPeriodic() { LiveWindow.run(); } } package org.usfirst.frc.team4284.robot; import edu.wpi.first.wpilibj.Talon; import edu.wpi.first.wpilibj.command.Command; public class ArmsIn extends Command { @Override protected void initialize() { // TODO Auto-generated method stub } @Override protected void execute() { Talon LeftArm = new Talon(9); Talon RightArm = new Talon(8); LeftArm.set(1); RightArm.set(-1); try { Thread.sleep(500); //stop for 1/2 second } catch(InterruptedException ex) { // allows for interruption of the sleep period Thread.currentThread().interrupt(); } LeftArm.set(0); RightArm.set(0); } @Override protected boolean isFinished() { // TODO Auto-generated method stub return false; } @Override protected void end() { // TODO Auto-generated method stub } @Override protected void interrupted() { // TODO Auto-generated method stub } } package org.usfirst.frc.team4284.robot; import edu.wpi.first.wpilibj.Talon; import edu.wpi.first.wpilibj.command.Command; public class ArmsOut extends Command { @Override protected void initialize() { // TODO Auto-generated method stub } @Override protected void execute() { Talon RightArm = new Talon(9); Talon LeftArm = new Talon(8); LeftArm.set(-1); RightArm.set(1); try { Thread.sleep(500); //stop for 1/2 second } catch(InterruptedException ex) { // allows for interruption of the sleep period Thread.currentThread().interrupt(); } LeftArm.set(0); RightArm.set(0); // TODO Auto-generated method stub } @Override protected boolean isFinished() { // TODO Auto-generated method stub return false; } @Override protected void end() { // TODO Auto-generated method stub } @Override protected void interrupted() { // TODO Auto-generated method stub } } package org.usfirst.frc.team4284.robot; import edu.wpi.first.wpilibj.Talon; import edu.wpi.first.wpilibj.command.Command; public class ArmsVarticalOut extends Command { @Override protected void initialize() { // TODO Auto-generated method stub } @Override protected void execute() { Talon In_Out = new Talon(7); Talon In__Out = new Talon(0); In_Out.set(-1); In__Out.set(-1); try { Thread.sleep(500); //stop for 1/2 second } catch(InterruptedException ex) { // allows for interruption of the sleep period Thread.currentThread().interrupt(); } In_Out.set(0); In__Out.set(0); // TODO Auto-generated method stub } @Override protected boolean isFinished() { // TODO Auto-generated method stub return false; } @Override protected void end() { // TODO Auto-generated method stub } @Override protected void interrupted() { // TODO Auto-generated method stub } } package org.usfirst.frc.team4284.robot; import edu.wpi.first.wpilibj.Talon; import edu.wpi.first.wpilibj.command.Command; public class ArmsVerticalIn extends Command { @Override protected void initialize() { // TODO Auto-generated method stub } @Override protected void execute() { Talon In_Out = new Talon (7); Talon In__Out = new Talon(0); In_Out.set(1); In__Out.set(1); try { Thread.sleep(500); //stop for 1/2 second } catch(InterruptedException ex) { // allows for interruption of the sleep period Thread.currentThread().interrupt(); } In_Out.set(0); In__Out.set(0); } @Override protected boolean isFinished() { // TODO Auto-generated method stub return false; } @Override protected void end() { // TODO Auto-generated method stub } @Override protected void interrupted() { // TODO Auto-generated method stub } } package org.usfirst.frc.team4284.robot; import edu.wpi.first.wpilibj.Relay; import edu.wpi.first.wpilibj.command.Command; public class BlueLightShow extends Command { @Override protected void initialize() { // TODO Auto-generated method stub } @Override protected void execute() { // TODO Auto-generated method stub Relay relay1 = new Relay(0); Relay relay2 = new Relay(1); relay1.set(Relay.Value.kOn); relay2.set(Relay.Value.kOff); } @Override protected boolean isFinished() { // TODO Auto-generated method stub return false; } @Override protected void end() { // TODO Auto-generated method stub } @Override protected void interrupted() { // TODO Auto-generated method stub } } package org.usfirst.frc.team4284.robot; import edu.wpi.first.wpilibj.Jaguar; import edu.wpi.first.wpilibj.command.Command; public class ElevatorOff extends Command { @Override protected void initialize() { // TODO Auto-generated method stub } @Override protected void execute() { // TODO Auto-generated method stub Jaguar Elevator1 = new Jaguar(5); Jaguar Elevator2 = new Jaguar(0); Elevator1.set(0); Elevator2.set(0); } @Override protected boolean isFinished() { // TODO Auto-generated method stub return false; } @Override protected void end() { // TODO Auto-generated method stub } @Override protected void interrupted() { // TODO Auto-generated method stub } } package org.usfirst.frc.team4284.robot; import edu.wpi.first.wpilibj.Jaguar; import edu.wpi.first.wpilibj.command.Command; public class ElevatorOn extends Command { @Override protected void initialize() { // TODO Auto-generated method stub } @Override protected void execute() { // TODO Auto-generated method stub Jaguar Elevator1 = new Jaguar(5); Jaguar Elevator2 = new Jaguar(0); Elevator1.set(.25); Elevator2.set(.25); } @Override protected boolean isFinished() { return false; } @Override protected void end() { } @Override protected void interrupted() { } } package org.usfirst.frc.team4284.robot; import edu.wpi.first.wpilibj.Jaguar; import edu.wpi.first.wpilibj.command.Command; public class ElevatorReverse extends Command { @Override protected void initialize() { // TODO Auto-generated method stub } @Override protected void execute() { // TODO Auto-generated method stub Jaguar Elevator1 = new Jaguar(5); Jaguar Elevator2 = new Jaguar(0); Elevator1.set(-.25); Elevator2.set(-.25); } @Override protected boolean isFinished() { // TODO Auto-generated method stub return false; } @Override protected void end() { // TODO Auto-generated method stub } @Override protected void interrupted() { // TODO Auto-generated method stub } } package org.usfirst.frc.team4284.robot; import edu.wpi.first.wpilibj.Relay; import edu.wpi.first.wpilibj.command.Command; public class LightShowEnd extends Command { @Override protected void initialize() { // TODO Auto-generated method stub } @Override protected void execute() { // TODO Auto-generated method stub Relay relay1 = new Relay(0); Relay relay2 = new Relay(1); relay1.set(Relay.Value.kOff); relay2.set(Relay.Value.kOff); } @Override protected boolean isFinished() { // TODO Auto-generated method stub return false; } @Override protected void end() { // TODO Auto-generated method stub } @Override protected void interrupted() { // TODO Auto-generated method stub } } package org.usfirst.frc.team4284.robot; import edu.wpi.first.wpilibj.Relay; import edu.wpi.first.wpilibj.command.Command; public class LigthShowOn extends Command { @Override protected void initialize() { // TODO Auto-generated method stub } @Override protected void execute() { // TODO Auto-generated method stub Relay relay1 = new Relay(0); Relay relay2 = new Relay(1); relay1.set(Relay.Value.kReverse); relay2.set(Relay.Value.kOn); try { Thread.sleep(5000); //stop for 1/2 second } catch(InterruptedException ex) { // allows for interruption of the sleep period Thread.currentThread().interrupt(); } //Turn on lights Blue and Green) relay1.set(Relay.Value.kOn); relay2.set(Relay.Value.kReverse); try { Thread.sleep(5000); //stop for 1/2 second } catch(InterruptedException ex) { // allows for interruption of the sleep period Thread.currentThread().interrupt(); } // Turns on blue and red lights relay1.set(Relay.Value.kOn); relay2.set(Relay.Value.kForward); try { Thread.sleep(5000); //stop for 1/2 second } catch(InterruptedException ex) { // allows for interruption of the sleep period Thread.currentThread().interrupt(); } relay1.set(Relay.Value.kReverse); relay2.set(Relay.Value.kOn); } @Override protected boolean isFinished() { // TODO Auto-generated method stub return false; } @Override protected void end() { // TODO Auto-generated method stub } @Override protected void interrupted() { // TODO Auto-generated method stub } } package org.usfirst.frc.team4284.robot; import edu.wpi.first.wpilibj.Relay; import edu.wpi.first.wpilibj.command.Command; public class RedLightShow extends Command { @Override protected void initialize() { // TODO Auto-generated method stub } @Override protected void execute() { // TODO Auto-generated method stub Relay relay1 = new Relay(0); Relay relay2 = new Relay(1); relay2.set(Relay.Value.kForward); relay1.set(Relay.Value.kReverse); } @Override protected boolean isFinished() { // TODO Auto-generated method stub return false; } @Override protected void end() { // TODO Auto-generated method stub } @Override protected void interrupted() { // TODO Auto-generated method stub } } Please and thank you |
Re: CodeProblem
Quote:
|
Re: CodeProblem
Quote:
As for AnnaliseDonavan, could you be more specific in your question? Also, which IDE do you use? Your code is relatively messy, so if you could show me one instance where your code does not work, you could troubleshoot through that pathway and follow example with the rest of the buttons. |
Re: CodeProblem
Quote:
|
Re: CodeProblem
As for the needing of semi colons for this set up when I place semi colons it produces an error and when I looked up the correct way to do the button control this is what I found.
And I apologist I am new to coding but no one on my team knows how to code so we are lost. For the one instance currently all I have in the implementing stage is the buttons so that is all the is and it dose not work. |
Re: CodeProblem
Try moving this code:
Code:
Button button1 = new JoystickButton(joystick, 1), |
Re: CodeProblem
I don't see any Scheduler.run() call in your teleop periodic code. Without that, there is nothing to call the commands that you have assigned to buttons.
|
Re: CodeProblem
This may not solve much, but I suggest moving your button code into teleopInit(), otherwise you are creating buttons in a loop during teleop.
Also, have you tested your commands without the buttons? The issue may be that your commands are not working, not the buttons. To do this in Java, you can add Code:
SmartDashboard.putData(new Command()); |
Re: CodeProblem
Quote:
Code:
Button button1 = new JoystickButton(joystick, 1); |
Re: CodeProblem
Quote:
In the teleopPeriodic() method, you are creating Button objects then calling the whenPressed() methods. Next, the teleopPeriodic() ends and those objects go away (Garbage Collected). While this might work (I have not tested) if you add the Scheduler.run() call, it is a waste of time and effort for the roboRio. Instead, I would recommend making the Button objects as member fields (just move the Button button1 ... ; statement outside of all methods) and moving the whenPressed() method calls to teleopInit(). Don't forget to add back the call to Scheduler.run()! |
Re: CodeProblem
Okay so I tried moving the
Scheduler.run() Button button1 = new JoystickButton(joystick, 1), button2 = new JoystickButton(joystick, 2), button3 = new JoystickButton(joystick, 3), button4 = new JoystickButton(joystick, 4), button5 = new JoystickButton(joystick, 5), button6 = new JoystickButton(joystick, 6), button7 = new JoystickButton(joystick, 7), button8 = new JoystickButton(joystick, 8), button9 = new JoystickButton(joystick, 9), button10 = new JoystickButton(joystick, 10), button11 = new JoystickButton(joystick , 11); code to both outside of the whole code and the robot int and the program fails to recognize that button1.whenPressed(new ElevatorOff()); button2.whenPressed(new ElevatorOn()); button3.whenPressed(new ElevatorReverse()); button4.whenPressed(new ArmsOut()); button5.whenPressed(new ArmsIn()); button6.whenPressed(new ArmsVerticalIn()); button7.whenPressed(new ArmsVarticalOut()); button8.whenPressed(new BlueLightShow()); button9.whenPressed(new LigthShowOn()); button10.whenPressed(new RedLightShow()); button11.whenPressed(new LightShowEnd()); is connected it comes up with a syntax error that the button1 - button11 is not defined. |
Re: CodeProblem
If any of your commands require parameters then that could be why nothing is happening. As for declaring the buttons, that should be in the constructor of your OI file.
|
Re: CodeProblem
The Template I am using dose not have a OI section is there another way around it?
|
Re: CodeProblem
I don't know if it's possible to create one. You could try creating a new robot template to see if it has an OI and try to make one based off of that....
|
Re: CodeProblem
Quote:
|
| All times are GMT -5. The time now is 01:47. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi