|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||
|
|||
|
Help first time programmer
Hello all, This is my first year working on the code for our drive code. Our team right now has a pretty basic drivetrain. motors on left and right with basic driving forward backwards and turning to go different directions. This is the code right now.
public class Robot extends IterativeRobot { Jaguar jagLF = new Jaguar(0);// default turn Jaguar jagLR = new Jaguar(1); //default turn Jaguar jagRF = new Jaguar(2);//default turn Jaguar jagRR = new Jaguar(3); // default turn Jaguar jagOmF = new Jaguar(6); //omniwheel turn Jaguar jagOmB = new Jaguar(7); // omniwheel turn RobotDrive drive = new RobotDrive(jagLF, jagLR, jagRF, jagRR); RobotDrive drive1 = new RobotDrive(jagOmF, jagOmB); Joystick stick; int autoLoopCounter; Relay Relay1 = new Relay(0); Relay Relay2 = new Relay(1); /** * This function is run when the robot is first started up and should be * used for any initialization code. */ public void robotInit() { stick = new Joystick(0); } /** * 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(stick); //drive1.arcadeDrive(stick); boolean omni = false; boolean main = true; while(main == true) { drive.arcadeDrive(stick); if(stick.getRawButton(3)) { main = false; omni = true; } } while(omni == true) { drive1.arcadeDrive(stick); if(stick.getRawButton(3)) { omni = false; main = true; } } the goal is to get the button on the stick to swap between the two drives. Main that is the forwards and backwards and omni to go side to side. http://imgur.com/XpNZ8Ke heres a picture of the design. The two vertical wheels are the ones were trying to add. The problem im hitting is that these while loops don't work. they dont switch between the drives when i hit the button. Any help on getting the code to switch between the two would be awesome. Im willing to answer any questons because im sure i wasn't very clear. Thanks! |
|
#2
|
|||||
|
|||||
|
Re: Help first time programmer
It is preferable to use code tags when posting code in a forum. To use a code tag put [/code] at the end of the code and [code] at the beginning.
Code:
public class Robot extends IterativeRobot {
Jaguar jagLF = new Jaguar(0);// default turn
Jaguar jagLR = new Jaguar(1); //default turn
Jaguar jagRF = new Jaguar(2);//default turn
Jaguar jagRR = new Jaguar(3); // default turn
Jaguar jagOmF = new Jaguar(6); //omniwheel turn
Jaguar jagOmB = new Jaguar(7); // omniwheel turn
RobotDrive drive = new RobotDrive(jagLF, jagLR, jagRF, jagRR);
RobotDrive drive1 = new RobotDrive(jagOmF, jagOmB);
Joystick stick;
int autoLoopCounter;
Relay Relay1 = new Relay(0);
Relay Relay2 = new Relay(1);
/**
* This function is run when the robot is first started up and should be
* used for any initialization code.
*/
public void robotInit() {
stick = new Joystick(0);
}
/**
* 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(stick);
//drive1.arcadeDrive(stick);
boolean omni = false;
boolean main = true;
while(main == true)
{
drive.arcadeDrive(stick);
if(stick.getRawButton(3))
{
main = false;
omni = true;
}
}
while(omni == true)
{
drive1.arcadeDrive(stick);
if(stick.getRawButton(3))
{
omni = false;
main = true;
}
}
}
}
First, you need to determine if the button is pressed: Code:
boolean buttonPressed = stick.getRawButton(3); Code:
if (buttonPressed) {
drive1.arcadeDrive(stick);
} else {
drive.arcadeDrive(stick);
}
Code:
public void teleopPeriodic() {
boolean buttonPressed = stick.getRawButton(3);
if (buttonPressed) {
drive1.arcadeDrive(stick);
} else {
drive.arcadeDrive(stick);
}
}
|
|
#3
|
|||
|
|||
|
Re: Help first time programmer
Thanks a ton, really appreciate the quick reply!
|
|
#4
|
||||
|
||||
|
Re: Help first time programmer
I still see one issue with notmattlythgoe's solution: since the buttonPressed is always equal to the state of the button, the modes will only be switched while the button is being held down and return to the default mode when the button is released.
You could then do something like this: Code:
public void teleopPeriodic() {
boolean buttonPressed = false;
if (stick.getRawButton(3)) {
buttonPressed = !buttonPressed;
}
if (buttonPressed) {
drive1.arcadeDrive(stick);
} else {
drive.arcadeDrive(stick);
}
}
So what you actually want is this: Code:
public void teleopInit(){
oldButtonState = false;
newButtonState = false;
}
public void teleopPeriodic() {
newButtonState = stick.getRawButton(3);
if (newButtonState && !oldButtonState) {
drive1.arcadeDrive(stick);
} else {
drive.arcadeDrive(stick);
}
oldButtonState = newButtonState;
}
There might be a more elegant solution, but hopefully that works. Hope that helps! ![]() |
|
#5
|
|||||
|
|||||
|
Re: Help first time programmer
Quote:
|
|
#6
|
|||||
|
|||||
|
Re: Help first time programmer
Quote:
Code:
public void teleopInit(){
oldButtonState = false;
newButtonState = false;
driveMode = false;
}
public void teleopPeriodic() {
newButtonState = stick.getRawButton(3);
if (newButtonState && !oldButtonState) {
driveMode = !driveMode;
}
oldButtonState = newButtonState;
if (driveMode) {
drive1.arcadeDrive(stick);
} else {
drive.arcadeDrive(stick);
}
}
Last edited by GeeTwo : 16-10-2015 at 16:05. |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|