Go to Post We try to limit ourselves only by physics. As the first person on the planet to communicate over a quantum encrypted AOL instant messenger session, I try to ignore those limits as well. - EricVanWyk [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 19-01-2015, 18:12
curtis0gj curtis0gj is offline
Registered User
FRC #5033 (Beavertronics)
Team Role: Programmer
 
Join Date: Jan 2015
Rookie Year: 2015
Location: Canada
Posts: 121
curtis0gj will become famous soon enough
Unhappy Autonomous Help

Hey all, newbie programmer here looking for some help with my autonomous code. At the moment I need to be able to turn 90 on the spot using a gyro stop and drive straight also using a gyro and encoder for distance.

Code:
public void autonomous() {
    	
    	while(isAutonomous() && isEnabled())  {
    		
    		SmartDashboard.putNumber("angle", gyro1.getAngle());
    		
    		double angle = gyro1.getAngle();
    		
    		robot.drive(-0.20, 1);
    		
    		if(angle > 83) {
    			
    			robot.drive(0,0);
    			
    			break;
The issue I have is the robot won't stop turning . I need a basic tutorial on setting up an encoder for distance. Also if anyone could show me how I could start driving straight after this turn, Thanks for the help.
Reply With Quote
  #2   Spotlight this post!  
Unread 19-01-2015, 19:31
curtis0gj curtis0gj is offline
Registered User
FRC #5033 (Beavertronics)
Team Role: Programmer
 
Join Date: Jan 2015
Rookie Year: 2015
Location: Canada
Posts: 121
curtis0gj will become famous soon enough
Re: Autonomous Help

Quote:
Originally Posted by curtis0gj View Post
Hey all, newbie programmer here looking for some help with my autonomous code. At the moment I need to be able to turn 90 on the spot using a gyro stop and drive straight also using a gyro and encoder for distance.

Code:
public void autonomous() {
    	
    	while(isAutonomous() && isEnabled())  {
    		
    		SmartDashboard.putNumber("angle", gyro1.getAngle());
    		
    		double angle = gyro1.getAngle();
    		
    		robot.drive(-0.20, 1);
    		
    		if(angle > 83) {
    			
    			robot.drive(0,0);
    			
    			break;
The issue I have is the robot won't stop turning . I need a basic tutorial on setting up an encoder for distance. Also if anyone could show me how I could start driving straight after this turn, Thanks for the help.
I think I have solved this but I don't have a way of testing it until tomorrow, I think the issue is the double angle = gyro1.getAngle(); is in the while loop so it's just getting the angle over and over... But I still need help with setting up an encoder if anyone could help that would be great!
Reply With Quote
  #3   Spotlight this post!  
Unread 19-01-2015, 19:53
curtis0gj curtis0gj is offline
Registered User
FRC #5033 (Beavertronics)
Team Role: Programmer
 
Join Date: Jan 2015
Rookie Year: 2015
Location: Canada
Posts: 121
curtis0gj will become famous soon enough
Re: Autonomous Help

Quote:
Originally Posted by curtis0gj View Post
I think I have solved this but I don't have a way of testing it until tomorrow, I think the issue is the double angle = gyro1.getAngle(); is in the while loop so it's just getting the angle over and over... But I still need help with setting up an encoder if anyone could help that would be great!
Quoting my self for a third time lol.... I spent a bit more time trying to setup the straight driving and what I did was created a new boolean and set it to true and made an if loop that it will enter if its == to true.
Code:
public void autonomous() {
    	
		double angle = gyro1.getAngle();
		
    	while(isAutonomous() && isEnabled())  {
    		
    		SmartDashboard.putNumber("angle", gyro1.getAngle());
    		
    		robot.drive(-0.20, 1);
    		
    		if(angle > 83) {
    			
    			robot.drive(0,0);
    			
    			break; 
    			
    			
    			}
    		
    		//do i need to reset my gryo? do i need to place my get angle here? 
    		

    			boolean turnCompleted = true;

				if(turnCompleted == true) {
					
					double angle2 = gyro1.getAngle(); // i think for driving i need a constant update of my angle
					//so i will place this inside my if loop and i name it angle2 so it's a different variable.
					robot.drive(-1.0, -angle2*Kp); // driving straight in a line.
					Timer.delay(0.004);
					
			
    			}
				
    		}
    		}
If anyone has any ideas for improvement for this please let me know.
Reply With Quote
  #4   Spotlight this post!  
Unread 20-01-2015, 14:16
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,715
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: Autonomous Help

I think I can give you a hand Curtis. Could you please list step by step what it is you want your autonomous mode to do?
Reply With Quote
  #5   Spotlight this post!  
Unread 20-01-2015, 15:45
curtis0gj curtis0gj is offline
Registered User
FRC #5033 (Beavertronics)
Team Role: Programmer
 
Join Date: Jan 2015
Rookie Year: 2015
Location: Canada
Posts: 121
curtis0gj will become famous soon enough
Re: Autonomous Help

Quote:
Originally Posted by notmattlythgoe View Post
I think I can give you a hand Curtis. Could you please list step by step what it is you want your autonomous mode to do?
Hi, thanks for the reply so basically what I am looking to do is make a 180 degree turn stop and drive straight forward for a set distance via encoder. However our team only has one encoder currently I don't know if this will be a problem. Today I figured out the turn finally and I added in the straight away however I am stuck in a loop I believe here's my current program
Code:
    public void autonomous() {
    	
    	gyro1.reset();
		
    	while(isAutonomous() && isEnabled())  {
    		
    		double angle = gyro1.getAngle();
    		
    		SmartDashboard.putNumber("angle", gyro1.getAngle());
    		
    		robot.drive(-0.15, 1);
    		Timer.delay(0.1);
    		
    		if(angle > 160) {
    			
    			robot.drive(0,0);
    			gyro1.reset();
    			Timer.delay(1.00);
    			
    			turnComplete = false;
    			break; 
    			
    			
    			
    			
    			}
    		}
    		turnComplete = true;
				gyro1.reset();
				while (turnComplete == true) {

						
						double angle2 = gyro1.getAngle(); // i think for driving i need a constant update of my angle
						//so i will place this inside my if loop and i name it angle2 so it's a different variable.
						robot.drive(-0.15, -angle2 * Kp); // driving straight in a line.
						Timer.delay(0.004);
						
						if(isAutonomous() && isDisabled())  {
							robot.drive(0,0);
							turnComplete = false;
							break;
						}
					
				
				}
				
				
			
    			}

Last edited by curtis0gj : 20-01-2015 at 16:01.
Reply With Quote
  #6   Spotlight this post!  
Unread 20-01-2015, 17:01
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,715
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: Autonomous Help

Curtis,

You have indeed made some progress, let's go through your program and talk about what is going on.

First, at the beginning of the autonomous period the autonomous() method is called. Anything in this method will run during the autonomous period.
Code:
public void autonomous() {
...
}
There are 2 routes you can take to program this:

[1] You can create different loops that do different things and as one loop finishes the next loop starts. This is the route that you have taken, so I will help you finish this.

[2] You can create one loop that has different checks in it to determine what to do each time.

What we you will want to do is have a loop that looks like the below for each stage of your autonomous period. If you do not check if autonomous mode is running and that you are enabled then you run the risk of getting stuck in autonomous for the entire match.
Code:
while(isAutonomous() && isEnabled())  {
...
Let's examine your first loop, which you are using to turn 180 degrees:
Code:
while(isAutonomous() && isEnabled())  {
    		
[1]	double angle = gyro1.getAngle();
    		
[2]	SmartDashboard.putNumber("angle", gyro1.getAngle());
    		
[3]	robot.drive(-0.15, 1);
[4]	Timer.delay(0.1);
    		
[5]	if(angle > 160) {
    			
[5a]		robot.drive(0,0);
[5b]		gyro1.reset();
[5c]		Timer.delay(1.00);
    			
[5d]		turnComplete = false;
[5e]		break; 
	}
}
Each time the loop executes it follows the following logic while the robot is in the autonomous period:
[1] Get the current gyro angle
[2] Put the current gyro angle on the SmartDashboard
[3] Drive backward at 15% speed and spin at full speed
[4] Delay the loop code for 0.1 seconds
[5] If the angle is > 160 do the folowing
[5a] Stop the robot
[5b] Reset the gyro
[5c] Delay the loop code for 1 second
[5d] Set turnComplete to false
[5e] Break the current loop and continue on with the code

[1] is done correctly.
[2] is also working, but since you have the angle already, it is better convention to use the variable already holding it. Let's change this line to:
Code:
SmartDashboard.putNumber("angle", angle);
[3] will work, but the number seem odd to me. I'd need more information about your robot setup to direct you to the proper numbers.
[4] works, but let's move it the end of the loop instead. Place it right below the closing } for your if statement.
[5], [5a], and [5b] are perfect
[5c] I'm assuming you are wanting to pause for a second after you finish your turn. If this is the case this is fine where it is.
[5d] I'm going to get you to remove this line, we won't need it later.
[5e] is also perfect

Now, let's look at your next loop:
Code:
[1]	turnComplete = true;
[2]	gyro1.reset();
[3]	while (turnComplete == true) {

[4]		double angle2 = gyro1.getAngle();

[5]		robot.drive(-0.15, -angle2 * Kp); // driving straight in a line.
[6]		Timer.delay(0.004);
							
[7]		if(isAutonomous() && isDisabled())  {
[7a]		robot.drive(0,0);
[7b]		turnComplete = false;
[7c]		break;
		}
	}
[1] We're not going to use this because it is unneeded, so let's delete this line.
[2] You actually already did this in the loop above, so you can either remove this line, or the identical line in the other loop.
[3] Let's make this loop run with the same check as the other loop. The reason why is if the autonomous mode ends and this loop hasn't finished then it will automatically finish.
[4] This line works just fine.
[5] This should work to keep you moving in a straight line, you'll just need to tune the Kp value to get it perfect.
[6] Any reason you are waiting for 0.004 seconds?
[7] We will change this line later to read the encoder distance and determine if we need to stop. For now let's make it:
Code:
if (false) {
[7a] This is good.
[7b] Let's remove this since we aren't using it.
[7c] This is also good

Let me know if you have any questions about what I've said so far.
Reply With Quote
  #7   Spotlight this post!  
Unread 20-01-2015, 18:01
curtis0gj curtis0gj is offline
Registered User
FRC #5033 (Beavertronics)
Team Role: Programmer
 
Join Date: Jan 2015
Rookie Year: 2015
Location: Canada
Posts: 121
curtis0gj will become famous soon enough
Re: Autonomous Help

Quote:
Originally Posted by notmattlythgoe View Post
Curtis,

You have indeed made some progress, let's go through your program and talk about what is going on.

First, at the beginning of the autonomous period the autonomous() method is called. Anything in this method will run during the autonomous period.
Code:
public void autonomous() {
...
}
There are 2 routes you can take to program this:

[1] You can create different loops that do different things and as one loop finishes the next loop starts. This is the route that you have taken, so I will help you finish this.

[2] You can create one loop that has different checks in it to determine what to do each time.

What we you will want to do is have a loop that looks like the below for each stage of your autonomous period. If you do not check if autonomous mode is running and that you are enabled then you run the risk of getting stuck in autonomous for the entire match.
Code:
while(isAutonomous() && isEnabled())  {
...
Let's examine your first loop, which you are using to turn 180 degrees:
Code:
while(isAutonomous() && isEnabled())  {
    		
[1]	double angle = gyro1.getAngle();
    		
[2]	SmartDashboard.putNumber("angle", gyro1.getAngle());
    		
[3]	robot.drive(-0.15, 1);
[4]	Timer.delay(0.1);
    		
[5]	if(angle > 160) {
    			
[5a]		robot.drive(0,0);
[5b]		gyro1.reset();
[5c]		Timer.delay(1.00);
    			
[5d]		turnComplete = false;
[5e]		break; 
	}
}
Each time the loop executes it follows the following logic while the robot is in the autonomous period:
[1] Get the current gyro angle
[2] Put the current gyro angle on the SmartDashboard
[3] Drive backward at 15% speed and spin at full speed
[4] Delay the loop code for 0.1 seconds
[5] If the angle is > 160 do the folowing
[5a] Stop the robot
[5b] Reset the gyro
[5c] Delay the loop code for 1 second
[5d] Set turnComplete to false
[5e] Break the current loop and continue on with the code

[1] is done correctly.
[2] is also working, but since you have the angle already, it is better convention to use the variable already holding it. Let's change this line to:
Code:
SmartDashboard.putNumber("angle", angle);
[3] will work, but the number seem odd to me. I'd need more information about your robot setup to direct you to the proper numbers.
[4] works, but let's move it the end of the loop instead. Place it right below the closing } for your if statement.
[5], [5a], and [5b] are perfect
[5c] I'm assuming you are wanting to pause for a second after you finish your turn. If this is the case this is fine where it is.
[5d] I'm going to get you to remove this line, we won't need it later.
[5e] is also perfect

Now, let's look at your next loop:
Code:
[1]	turnComplete = true;
[2]	gyro1.reset();
[3]	while (turnComplete == true) {

[4]		double angle2 = gyro1.getAngle();

[5]		robot.drive(-0.15, -angle2 * Kp); // driving straight in a line.
[6]		Timer.delay(0.004);
							
[7]		if(isAutonomous() && isDisabled())  {
[7a]		robot.drive(0,0);
[7b]		turnComplete = false;
[7c]		break;
		}
	}
[1] We're not going to use this because it is unneeded, so let's delete this line.
[2] You actually already did this in the loop above, so you can either remove this line, or the identical line in the other loop.
[3] Let's make this loop run with the same check as the other loop. The reason why is if the autonomous mode ends and this loop hasn't finished then it will automatically finish.
[4] This line works just fine.
[5] This should work to keep you moving in a straight line, you'll just need to tune the Kp value to get it perfect.
[6] Any reason you are waiting for 0.004 seconds?
[7] We will change this line later to read the encoder distance and determine if we need to stop. For now let's make it:
Code:
if (false) {
[7a] This is good.
[7b] Let's remove this since we aren't using it.
[7c] This is also good

Let me know if you have any questions about what I've said so far.
So far so good thanks a ton for the help here's what I have right now with these fixes
Code:
public void autonomousPeriodic() {

    	gyro1.reset();
		
    	while(isAutonomous() && isEnabled())  {
    		
    		double angle = gyro1.getAngle();
    		
    		SmartDashboard.putNumber("angle", angle);
    		
    		robot.drive(-0.25, 1);
    		
    		if(angle > 150) {
    			
    			robot.drive(0,0);
    			gyro1.reset();
    			Timer.delay(1.00);

    			Timer.delay(0.1);
    			break; 
    			
    			}
    	}
    		
				
				while (isAutonomous() && isEnabled()) {

						double angle2 = gyro1.getAngle(); 
						
						robot.drive(-0.20, -angle2 * Kp); 
					
						if(false)  {
							robot.drive(0,0); //It's calling all this dead code ):
							
							break;
						}
					
				}
				
				
    			}
Now I just need some help with that encoder and that would be perfect, thanks a ton!
Reply With Quote
  #8   Spotlight this post!  
Unread 21-01-2015, 08:05
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,715
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: Autonomous Help

Quote:
Originally Posted by curtis0gj View Post
So far so good thanks a ton for the help here's what I have right now with these fixes
Code:
public void autonomousPeriodic() {

    	gyro1.reset();
		
    	while(isAutonomous() && isEnabled())  {
    		
    		double angle = gyro1.getAngle();
    		
    		SmartDashboard.putNumber("angle", angle);
    		
    		robot.drive(-0.25, 1);
    		
    		if(angle > 150) {
    			
    			robot.drive(0,0);
    			gyro1.reset();
    			Timer.delay(1.00);

    			Timer.delay(0.1);
    			break; 
    			
    			}
    	}
    		
				
				while (isAutonomous() && isEnabled()) {

						double angle2 = gyro1.getAngle(); 
						
						robot.drive(-0.20, -angle2 * Kp); 
					
						if(false)  {
							robot.drive(0,0); //It's calling all this dead code ):
							
							break;
						}
					
				}
				
				
    			}
Now I just need some help with that encoder and that would be perfect, thanks a ton!
Move Timer.delay(0.1) down below the first if statement. Also add another one below the second if statement. It should look like this:
Code:
public void autonomousPeriodic() {
	gyro1.reset();
	while(isAutonomous() && isEnabled())  {
    	double angle = gyro1.getAngle();
    	SmartDashboard.putNumber("angle", angle);
    	robot.drive(-0.25, 1);
    		
    	if(angle > 150) {
    		robot.drive(0,0);
    		gyro1.reset();
    		Timer.delay(1.00);
    		break; 
    	}
		Timer.delay(0.1);
    }
    				
	while (isAutonomous() && isEnabled()) {
		double angle2 = gyro1.getAngle(); 
		robot.drive(-0.20, -angle2 * Kp); 
					
		if(false)  {
			robot.drive(0,0); //It's calling all this dead code ):
			break;
		}	
		Timer.delay(0.1);
	}				
}
You are correct in your comment, that section of code is unreachable right now. Once we add in an encoder it will get fixed.

Do create an encoder you'll do something like this:
Code:
Encoder encoder = new Encoder(1, 2, false, EncodingType.k4X);
The first 2 parameters are the digital channels the encoder is plugged into. The next parameter will allow you to reverse the direction of the encoder if you decide that you want it to count in the opposite direction. The 4th parameter is the type of encoder you are using. For this case I'm going to assume you are using a quadrature encoder.

Next lets configure the encoder. Configuring an encoder correctly will make your life that much easier when you want to use it.

Code:
encoder.setDistancePerPulse(0.001);
Setting a value in this method will allow you to get readings from the encoder that actually mean something. Figure out how far your robot moves in one rotation of the encoder, then divide that distance by the number of counts your encoder has.

Then, to get the reading all you have to do is call the below method, this method will return how far the encoder has. If you have configured the encoder with the correct distance per pulse then it will return how far your robot has moved.
Code:
encoder.getDistance();
Now that we have an encoder we can plug it into your code. Replace the false with a check to see if you have traveled the distance you want.
Reply With Quote
  #9   Spotlight this post!  
Unread 21-01-2015, 08:12
curtis0gj curtis0gj is offline
Registered User
FRC #5033 (Beavertronics)
Team Role: Programmer
 
Join Date: Jan 2015
Rookie Year: 2015
Location: Canada
Posts: 121
curtis0gj will become famous soon enough
Re: Autonomous Help

Quote:
Originally Posted by notmattlythgoe View Post
Move Timer.delay(0.1) down below the first if statement. Also add another one below the second if statement. It should look like this:
Code:
public void autonomousPeriodic() {
	gyro1.reset();
	while(isAutonomous() && isEnabled())  {
    	double angle = gyro1.getAngle();
    	SmartDashboard.putNumber("angle", angle);
    	robot.drive(-0.25, 1);
    		
    	if(angle > 150) {
    		robot.drive(0,0);
    		gyro1.reset();
    		Timer.delay(1.00);
    		break; 
    	}
		Timer.delay(0.1);
    }
    				
	while (isAutonomous() && isEnabled()) {
		double angle2 = gyro1.getAngle(); 
		robot.drive(-0.20, -angle2 * Kp); 
					
		if(false)  {
			robot.drive(0,0); //It's calling all this dead code ):
			break;
		}	
		Timer.delay(0.1);
	}				
}
You are correct in your comment, that section of code is unreachable right now. Once we add in an encoder it will get fixed.

Do create an encoder you'll do something like this:
Code:
Encoder encoder = new Encoder(1, 2, false, EncodingType.k4X);
The first 2 parameters are the digital channels the encoder is plugged into. The next parameter will allow you to reverse the direction of the encoder if you decide that you want it to count in the opposite direction. The 4th parameter is the type of encoder you are using. For this case I'm going to assume you are using a quadrature encoder.

Next lets configure the encoder. Configuring an encoder correctly will make your life that much easier when you want to use it.

Code:
encoder.setDistancePerPulse(0.001);
Setting a value in this method will allow you to get readings from the encoder that actually mean something. Figure out how far your robot moves in one rotation of the encoder, then divide that distance by the number of counts your encoder has.

Then, to get the reading all you have to do is call the below method, this method will return how far the encoder has. If you have configured the encoder with the correct distance per pulse then it will return how far your robot has moved.
Code:
encoder.getDistance();
Now that we have an encoder we can plug it into your code. Replace the false with a check to see if you have traveled the distance you want.
Would the if statement be if(encoder.getDistance > 50) work at all?
Reply With Quote
  #10   Spotlight this post!  
Unread 21-01-2015, 08:14
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,715
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: Autonomous Help

Quote:
Originally Posted by curtis0gj View Post
Would the if statement be if(encoder.getDistance > 50) work at all?
Add parameters to your method call and it's perfect.
Reply With Quote
  #11   Spotlight this post!  
Unread 21-01-2015, 08:33
curtis0gj curtis0gj is offline
Registered User
FRC #5033 (Beavertronics)
Team Role: Programmer
 
Join Date: Jan 2015
Rookie Year: 2015
Location: Canada
Posts: 121
curtis0gj will become famous soon enough
Re: Autonomous Help

Quote:
Originally Posted by notmattlythgoe View Post
Add parameters to your method call and it's perfect.
I think I have this right but before we wrap this up I just wanted to check a few things. Do I need < , > or ==. Also I would like to print a distance traveled to my smart DS is that just SmartDashboard.putNumber("Distance", encoder.getDistance()); or can I make a double distance = encoder.getDistance(); and just put distance in there?? Here's my code so far.
Code:
public class Robot extends IterativeRobot {
	
	RobotDrive robot;
    Joystick stick;
    Encoder encoder;	
    Gyro gyro1;
    
    static final double Kp = 0.05;
    
    public void robotInit() {
    	
    	 robot = new RobotDrive(0, 1);
         stick = new Joystick(0);
         gyro1 = new Gyro(0);
         gyro1.reset();
         gyro1.initGyro();
         Encoder encoder = new Encoder(1, 2, false, EncodingType.k4X);
         
         encoder.setDistancePerPulse(0.001);
         encoder.getDistance(); // does this belong in the init or my if loop?

    }

   
    public void autonomousPeriodic() {

    	gyro1.reset();
		
    	while(isAutonomous() && isEnabled())  {
    		
    		double angle = gyro1.getAngle();
    		
    		SmartDashboard.putNumber("angle", angle);
    		
    		robot.drive(-0.25, 1);
    		
    		if(angle > 150) {
    			
    			robot.drive(0,0);
    			gyro1.reset();
    			Timer.delay(1.00);

    			break; 
    			
    			}
    		
    			Timer.delay(0.1);
    	}
    	
				while (isAutonomous() && isEnabled()) {

						double angle2 = gyro1.getAngle(); 
						
						robot.drive(-0.20, -angle2 * Kp); 
					// This can't be right lol..... at least its no longer dead code (: also in my if statement do i need < or > or ==?
						if(encoder.getDistance() > 50)  {
							robot.drive(0,0); 
							
							break;
						}
						Timer.delay(0.1);	
				}
				
    			}
Reply With Quote
  #12   Spotlight this post!  
Unread 21-01-2015, 08:43
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,715
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: Autonomous Help

Quote:
Originally Posted by curtis0gj View Post
I think I have this right but before we wrap this up I just wanted to check a few things. Do I need < , > or ==. Also I would like to print a distance traveled to my smart DS is that just SmartDashboard.putNumber("Distance", encoder.getDistance()); or can I make a double distance = encoder.getDistance(); and just put distance in there?? Here's my code so far.
Code:
public class Robot extends IterativeRobot {
	
	RobotDrive robot;
    Joystick stick;
    Encoder encoder;	
    Gyro gyro1;
    
    static final double Kp = 0.05;
    
    public void robotInit() {
    	
    	 robot = new RobotDrive(0, 1);
         stick = new Joystick(0);
         gyro1 = new Gyro(0);
         gyro1.reset();
         gyro1.initGyro();
         Encoder encoder = new Encoder(1, 2, false, EncodingType.k4X);
         
         encoder.setDistancePerPulse(0.001);
         encoder.getDistance(); // does this belong in the init or my if loop?

    }

   
    public void autonomousPeriodic() {

    	gyro1.reset();
		
    	while(isAutonomous() && isEnabled())  {
    		
    		double angle = gyro1.getAngle();
    		
    		SmartDashboard.putNumber("angle", angle);
    		
    		robot.drive(-0.25, 1);
    		
    		if(angle > 150) {
    			
    			robot.drive(0,0);
    			gyro1.reset();
    			Timer.delay(1.00);

    			break; 
    			
    			}
    		
    			Timer.delay(0.1);
    	}
    	
				while (isAutonomous() && isEnabled()) {

						double angle2 = gyro1.getAngle(); 
						
						robot.drive(-0.20, -angle2 * Kp); 
					// This can't be right lol..... at least its no longer dead code (: also in my if statement do i need < or > or ==?
						if(encoder.getDistance() > 50)  {
							robot.drive(0,0); 
							
							break;
						}
						Timer.delay(0.1);	
				}
				
    			}
> will work perfectly fine.

AS for the distance variable, I would create a variable just like you did with the angle and print it to the SmartDashboard.
Reply With Quote
  #13   Spotlight this post!  
Unread 21-01-2015, 08:47
curtis0gj curtis0gj is offline
Registered User
FRC #5033 (Beavertronics)
Team Role: Programmer
 
Join Date: Jan 2015
Rookie Year: 2015
Location: Canada
Posts: 121
curtis0gj will become famous soon enough
Re: Autonomous Help

This quote thing is driving me nuts, anyway this works right?
Code:
double angle = gyro1.getAngle();
    		double distance = encoder.getDistance();
    		
    		SmartDashboard.putNumber("Angle", angle);
    		SmartDashboard.putNumber("Distance", distance);
Also now that I have made my distance variable can I use it in my if statement like so, if(distance > 50) ?
Reply With Quote
  #14   Spotlight this post!  
Unread 21-01-2015, 08:52
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,715
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: Autonomous Help

Quote:
Originally Posted by curtis0gj View Post
This quote thing is driving me nuts, anyway this works right?
Code:
double angle = gyro1.getAngle();
    		double distance = encoder.getDistance();
    		
    		SmartDashboard.putNumber("Angle", angle);
    		SmartDashboard.putNumber("Distance", distance);
Also now that I have made my distance variable can I use it in my if statement like so, if(distance > 50) ?
Yes you can, but you are going to want to make sure that you put the distance variable in the second loop so it gets updated when you are driving.

Another thing I forgot to have you do is reset the encoder prior to the second loop. You can do this in the same place you reset the gyro.
Code:
encoder.reset();
Reply With Quote
  #15   Spotlight this post!  
Unread 21-01-2015, 08:56
curtis0gj curtis0gj is offline
Registered User
FRC #5033 (Beavertronics)
Team Role: Programmer
 
Join Date: Jan 2015
Rookie Year: 2015
Location: Canada
Posts: 121
curtis0gj will become famous soon enough
Re: Autonomous Help

Quote:
Originally Posted by notmattlythgoe View Post
Yes you can, but you are going to want to make sure that you put the distance variable in the second loop so it gets updated when you are driving.

Another thing I forgot to have you do is reset the encoder prior to the second loop. You can do this in the same place you reset the gyro.
Code:
encoder.reset();
I think this is perfect now I am very excited to test this.
Code:
public void autonomousPeriodic() {

    	gyro1.reset();
		
    	while(isAutonomous() && isEnabled())  {
    		
    		double angle = gyro1.getAngle();
    		double distance = encoder.getDistance();
    		
    		SmartDashboard.putNumber("Angle", angle);
    		SmartDashboard.putNumber("Distance", distance);
    		
    		robot.drive(-0.25, 1);
    		
    		if(angle > 150) {
    			
    			robot.drive(0,0);
    			gyro1.reset();
    			encoder.reset();
    			
    			Timer.delay(1.00);

    			break; 
    			
    			}
    		
    			Timer.delay(0.1);
    	}
    	
				while (isAutonomous() && isEnabled()) {
					
					double distance2 = encoder.getDistance();

					double angle2 = gyro1.getAngle(); 
						
					robot.drive(-0.20, -angle2 * Kp); 
					
						if(distance2 > 50)  {
							robot.drive(0,0); 
							
							break;
						}
						Timer.delay(0.1);	
				}
				
    			}
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 10:30.

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