Log in

View Full Version : Help first time programmer


VrewDaive
16-10-2015, 09:06
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!

notmattlythgoe
16-10-2015, 09:20
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 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;
}
}
}
}

One issue is that you are trying to use a while loop when you really want to be using an if statement. The teleopPeriodic() method is already repeatedly called aprox every 20ms, which actually creates a kind of loop for you. What you put in the method body should be similar to a loop body.

First, you need to determine if the button is pressed:
boolean buttonPressed = stick.getRawButton(3);

Next you can use that to determine which drive to use:
if (buttonPressed) {
drive1.arcadeDrive(stick);
} else {
drive.arcadeDrive(stick);
}

So your teleopPeriodic method should look like this:
public void teleopPeriodic() {
boolean buttonPressed = stick.getRawButton(3);

if (buttonPressed) {
drive1.arcadeDrive(stick);
} else {
drive.arcadeDrive(stick);
}
}

VrewDaive
16-10-2015, 12:47
Thanks a ton, really appreciate the quick reply!

dwhite
16-10-2015, 15:28
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:

public void teleopPeriodic() {
boolean buttonPressed = false;
if (stick.getRawButton(3)) {
buttonPressed = !buttonPressed;
}

if (buttonPressed) {
drive1.arcadeDrive(stick);
} else {
drive.arcadeDrive(stick);
}
}

However, then the modes would keep switching as long is the button is held. Really what you care about is when the button state changes from false to true. You can keep track of this with two booleans, one for the old state and one for the new.
So what you actually want is this:

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;
}

Also add declarations for oldButtonState and newButtonState as booleans with the rest of your declarations.
There might be a more elegant solution, but hopefully that works.
Hope that helps! :)

notmattlythgoe
16-10-2015, 15:31
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:

public void teleopPeriodic() {
boolean buttonPressed = false;
if (stick.getRawButton(3)) {
buttonPressed = !buttonPressed;
}

if (buttonPressed) {
drive1.arcadeDrive(stick);
} else {
drive.arcadeDrive(stick);
}
}

However, then the modes would keep switching as long is the button is held. Really what you care about is when the button state changes from false to true. You can keep track of this with two booleans, one for the old state and one for the new.
So what you actually want is this:

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;
}

Also add declarations for oldButtonState and newButtonState as booleans with the rest of your declarations.
There might be a more elegant solution, but hopefully that works.
Hope that helps! :)

Good catch.

GeeTwo
16-10-2015, 16:00
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;
}



This will drive in mode "drive1" for one cycle each time the button is pressed. You also need another (Boolean) variable, perhaps driveMode. Inside the if above, toggle the driveMode variable. Then, outside the if, base drive mode on the variable:


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);
}
}

an enum would be even neater...