![]() |
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() { |
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
Code:
public void autonomous() { |
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?
|
Re: Autonomous Help
Quote:
Code:
public void autonomous() { |
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() {[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()) {Code:
while(isAutonomous() && isEnabled()) {[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);[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] 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) {[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. |
Re: Autonomous Help
Quote:
Code:
public void autonomousPeriodic() { |
Re: Autonomous Help
Quote:
Code:
public void autonomousPeriodic() {Do create an encoder you'll do something like this: Code:
Encoder encoder = new Encoder(1, 2, false, EncodingType.k4X);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);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(); |
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
Code:
public class Robot extends IterativeRobot { |
Re: Autonomous Help
Quote:
AS for the distance variable, I would create a variable just like you did with the angle and print it to the SmartDashboard. |
Re: Autonomous Help
This quote thing is driving me nuts, anyway this works right?
Code:
double angle = gyro1.getAngle(); |
Re: Autonomous Help
Quote:
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(); |
Re: Autonomous Help
Quote:
Code:
public void autonomousPeriodic() { |
| All times are GMT -5. The time now is 08:07 AM. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi