|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||
|
|||
|
Help programming joystick buttons
Hello we are team number 4591 we are a rookie team. Our programming team consists of 2 people and we have just started programming a few months ago.
We have sucessfully made our robot move forward and backwards using two joysticks. We need to move two motors at the same time while holding one button to go clockwise and holding another button to go counter clockwise. We have tried everything we know, but nothing seemed to work. It would be very helpful if someone could give us an example of a code we can use to run the motors. Here's what we have so far: Code:
package edu.wpi.first.wpilibj.templates;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.SimpleRobot;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.buttons.Button;
public class RobotTemplate extends SimpleRobot{
RobotDrive chassis= new RobotDrive(1, 2);
Joystick leftStick= new Joystick(1);
Joystick rightStick= new Joystick(2);
public void autonomus() {
chassis.setSafetyEnabled(false);
chassis.drive(-0.05, 0.0);
Timer.delay(30.0);
chassis.drive(0.0, 0.0);
}
public void operatorControl() {
chassis.setSafetyEnabled(true); {
chassis.tankDrive(leftStick, rightStick);
Timer.delay(0.01);
}
}
}
|
|
#2
|
|||
|
|||
|
Re: Help programming joystick buttons
There is a function called getButton() that allows you to use the trigger and top button, and one called getRawButton() that works for the rest of the buttons by number. This page should help you get a complete understanding of the joystick, the buttons, and the rest of the code for the robot.
http://www.wbrobotics.com/javadoc/ed...ge_description |
|
#3
|
||||||
|
||||||
|
Re: Help programming joystick buttons
You can use the getRawButton method of the joystick class to get whether a button is being pressed or not.
|
|
#4
|
|||
|
|||
|
Re: Help programming joystick buttons
Quote:
if you could show us an example of a code using getRawButton that would be very helpful. |
|
#5
|
||||
|
||||
|
Re: Help programming joystick buttons
Quote:
Joystick j = new Joystick(1); JoystickButton a = new JoystickButton(j,1); a.whileHeld(new YourCommand()); in command based or Joystick j = new Joystick(1) if(j.getRawButton(1)){ do stuff } in iterative. both will run when A is pressed |
|
#6
|
||||||
|
||||||
|
Re: Help programming joystick buttons
Quote:
Here is an example of using getRawButton to switch between tank drive and arcade drive when the trigger (button one) on the left joystick is pressed. Code:
public void operatorControl() {
chassis.setSafetyEnabled(true);
if (leftStick.getRawButton(1))
{
chassis.tankDrive(leftStick, rightStick);
Timer.delay(0.01);
}
else
{
chassis.arcadeDrive(leftStick);
}
}
|
|
#7
|
|||
|
|||
|
Re: Help programming joystick buttons
Have you heard about command based programming? It is a lot cleaner and will help you avoid creating hundreds (okay, not hundreds) of "else if" blocks and actually set up a polymorphic structure and abstracting your code. It's a specifically tailored design pattern. It's probably too late now, but it's something you should consider next year: http://firstforge.wpi.edu/sf/docman/...tation/doc1297
Last edited by arithehun : 11-02-2013 at 02:27. |
|
#8
|
|||
|
|||
|
Re: Help programming joystick buttons
Code:
public void operatorControl() {
chassis.setSafetyEnabled(true); {
chassis.tankDrive(leftStick, rightStick);
Timer.delay(0.01);
}
}
Because you are using SimpleRobot you need to put the teleop code in a loop, e.g.: Code:
public void operatorControl() {
chassis.setSafetyEnabled(true);
while (isOperatorControl && isEnabled() ){
chassis.tankDrive(leftStick, rightStick);
Timer.delay(0.01);
}
}
Just noticed you have a 30s delay in autonomous. Autonomous mode is only 15s. Unless something changed this year operatorControl() won't be called until autonomous() exits. So you'll be waiting 15s after teleop starts to get control of the 'bot. Last edited by omalleyj : 12-02-2013 at 13:07. Reason: noticed autonomous delay |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|