Go to Post Remember, FIRST is a great opportunity that will get you ahead in the long run but don't forget about the short run either. - ChrisMcK2186 [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 10-16-2015, 09:06 AM
VrewDaive VrewDaive is offline
Registered User
FRC #4284
 
Join Date: Sep 2015
Location: Cincinnati
Posts: 21
VrewDaive is an unknown quantity at this point
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!
Reply With Quote
  #2   Spotlight this post!  
Unread 10-16-2015, 09:20 AM
notmattlythgoe's Avatar
notmattlythgoe notmattlythgoe is offline
Flywheel Police
AKA: Matthew Lythgoe
FRC #2363 (Triple Helix)
Team Role: Mentor
 
Join Date: Feb 2010
Rookie Year: 2009
Location: Newport News, VA
Posts: 1,712
notmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond repute
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;
			}
		}
	}
}
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:
Code:
boolean buttonPressed = stick.getRawButton(3);
Next you can use that to determine which drive to use:
Code:
if (buttonPressed) {
	drive1.arcadeDrive(stick);
} else {
	drive.arcadeDrive(stick);
}
So your teleopPeriodic method should look like this:
Code:
public void teleopPeriodic() {
	boolean buttonPressed = stick.getRawButton(3);

	if (buttonPressed) {
		drive1.arcadeDrive(stick);
	} else {
		drive.arcadeDrive(stick);
	}
}
Reply With Quote
  #3   Spotlight this post!  
Unread 10-16-2015, 12:47 PM
VrewDaive VrewDaive is offline
Registered User
FRC #4284
 
Join Date: Sep 2015
Location: Cincinnati
Posts: 21
VrewDaive is an unknown quantity at this point
Re: Help first time programmer

Thanks a ton, really appreciate the quick reply!
Reply With Quote
  #4   Spotlight this post!  
Unread 10-16-2015, 03:28 PM
dwhite's Avatar
dwhite dwhite is offline
I can SERTainly make puns.
AKA: Dani White
FRC #2521 (SERT)
Team Role: Programmer
 
Join Date: Jul 2015
Rookie Year: 2015
Location: Eugene, OR
Posts: 3
dwhite is an unknown quantity at this point
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);
	}
}
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:
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;
}
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!
Reply With Quote
  #5   Spotlight this post!  
Unread 10-16-2015, 03:31 PM
notmattlythgoe's Avatar
notmattlythgoe notmattlythgoe is offline
Flywheel Police
AKA: Matthew Lythgoe
FRC #2363 (Triple Helix)
Team Role: Mentor
 
Join Date: Feb 2010
Rookie Year: 2009
Location: Newport News, VA
Posts: 1,712
notmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond repute
Re: Help first time programmer

Quote:
Originally Posted by dwhite View Post
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);
	}
}
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:
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;
}
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.
Reply With Quote
  #6   Spotlight this post!  
Unread 10-16-2015, 04:00 PM
GeeTwo's Avatar
GeeTwo GeeTwo is online now
Technical Director
AKA: Gus Michel II
FRC #3946 (Tiger Robotics)
Team Role: Mentor
 
Join Date: Jan 2014
Rookie Year: 2013
Location: Slidell, LA
Posts: 3,536
GeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond repute
Re: Help first time programmer

Quote:
Originally Posted by dwhite View Post
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;
}
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:

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);
        }
}
an enum would be even neater...
__________________

If you can't find time to do it right, how are you going to find time to do it over?
If you don't pass it on, it never happened.
Robots are great, but inspiration is the reason we're here.
Friends don't let friends use master links.

Last edited by GeeTwo : 10-16-2015 at 04:05 PM.
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 07:59 AM.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi