![]() |
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() { |
Re: Autonomous Help
That looks good. Let me know if there is anything else.
|
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Should the encoder be plugged into DIO or analog?
|
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
[quote=notmattlythgoe;1432076]It needs to be plugged into DIO. What kind of encoder are you working with?[/QUOTEh
http://www.andymark.com/product-p/am-0180.htm US Digital Encoder E4P-250-250-N-S-D-D-B |
Re: Autonomous Help
[quote=curtis0gj;1432078]
Quote:
|
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
[quote=notmattlythgoe;1432080]
Quote:
|
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
As you manually rotate the encoder shaft (very very slowly), you should see the voltage going from ~5V to ~0.4V on each channel. |
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Okay we testing out the encoder with our multimeter the volts seem good we got .4 to 5v however I am still not getting a number on my smartdash and I am not stopping. Here's the whole program.
Code:
package org.usfirst.frc.team5033.robot; |
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
start(); but it just gave me errors I guess there's no such thing this year. |
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Code:
double distance2 = encoder.get(); |
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
I know its a basic question, but sometimes they need asked. |
Re: Autonomous Help
Quote:
+5 (orange) to the +5 pin of PWM connector #1 A (blue) to the signal pin on PWM connectore #1 Gnd (brown) to the ground pin of PWM connector #2 B (yellow) to the signal pin on PWM connector #2 Is what we are doing and we have the two PWM's into the DIO ports on the roboRIO. |
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
http://www.chiefdelphi.com/forums/sh...39&postcount=3 |
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
In robotInit(), delete the Encoder part of your encoder instantiaton. It should look like this: Code:
encoder = new Encoder(0, 1, false, EncodingType.k4X); |
Re: Autonomous Help
Quote:
Yeah I think that could do it lol heres what I got. Code:
package org.usfirst.frc.team5033.robot; |
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
e.g. What were you measuring these voltages with? etc |
Re: Autonomous Help
Quote:
So what seems to have been happening in your old code was because you had that Encoder part on the one line you were declaring a new local variable called encoder which was only used locally, and when robotInit ended it was removed from memory. What this means is that the field you called encoder was never given a value. Take a look at the old code, I'm going to bold the local variable and underline the field so you can see what I'm talking about. Code:
package org.usfirst.frc.team5033.robot; |
Re: Autonomous Help
Quote:
Still not getting anything printed to my smartdash board do I need to make a new dashboard or something like that. |
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
I'm surprised you could actually see the hi/lo voltage transitions with just a multimeter if you were measuring while running your autonomous routine. Are you sure that's what was done? I had suggested turning the encoder very slowly by hand. |
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
This is why you should use command based robot, with a turn x degrees command, drive subsyem with your encoders
|
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
Okay, my mentor told me that he was not really seeing a square wave however, he did see a spike in voltage and a drop in voltage. But we will try spinning it slowly by hand, we took the lazy way out last time because we did not really want to take the encoder off. |
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Hey guys I finally got the encoder going, my electrical guy said it was grounding on something. Anyway I am getting a nice reading on smart db however, when I run autonomous mode the robot turns about 180 stops and turns again and this never ends. I think I am stuck in an if loop and it's not entering the straight driving portion of the program...
Code:
public class Robot extends IterativeRobot { |
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
I hope that it is ok to jump in here... It looks like you started off with a Robot that extended SampleRobot and have now migrated to a class that extends IterativeRobot. The way that your autonomous code will be executed is very different in these two cases. SampleRobot When you extend SampleRobot, your autonomous method will get called for you once. You need to write loops in your code that keep running until you have completed your task. This is why you have the while loop: while( isAutonomous() and isEnabled() ) { //stuff } You also put your own calls to Timer.delay() in this type of robot because nothing is pausing your code (nothing is creating a timed structure for you). IterativeRobot When you extend IterativeRobot, your autonomousPeriodic() method will be called every 20 ms. If you put a loop in this method that runs for longer than 20 ms...well, that is bad. You also don't want to put calls to Timer.delay() in here as this will slow down the execution cycle. Since the code in autonomousPeriodic() is being executed every 20 ms, you want to avoid loops. Put the body of your loops in the method; since it is being executed every 20 ms, it is being looped for you. Also, be careful about resetting values in autonomousPeriodic() without checking a condition. Since the code gets executed repeatedly, you will be continuously resetting the value (e.g. the call to reset the gyro at the top of the method is constantly resetting the gyro). Proposed new method: Code:
Quote:
To find the correct value for setDistancePerPulse(), we typically:
Good luck and let me know if this did or didn't work...unfortunately, I didn't test the code :( |
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
If the code above makes sense, though, I would encourage you to explore the IterativeRobot template. The teleop code works in a similar fashion: teleopInit() gets called once when teleop is enabled and teleopPeriodic() gets called every 20 ms for you (so avoid loops that don't exit quickly and do not use Timer.delay() to pause your code). Also, the 20 ms is just a great approximation...the loop timing is reallllly close to every 20 ms (certainly close enough for what we're doing). If you do switch back to SampleRobot, then you will need the loops and the calls to Timer.delay(). Keep plugging away at it and keep asking questions. ChiefDelphi is full of people that want to help :) |
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
Quote:
|
Re: Autonomous Help
In terms of efficiency its negligible. I like it because it breaks the code down into more readable code project. Everything is not crammed into one class! Since Java is an object oriented programming use it for what its made for! Objects!
|
Re: Autonomous Help
Quote:
This is the structure that our team uses and we like it very much. I would be willing to help you if you were interested in moving to a structure like this. As for Iterative vs Sample. I had heard that they were planning on getting rid of the Sample(Simple) robot structure because that name was misleading because they thought teams actually had a harder time with the "Simple" robot structure. There are many resources to help you in any of the structures as most of the people that can help you will know how to help in any of the structures. |
Re: Autonomous Help
Quote:
Switching to command based currently may be an issue because of how much I have already established with the Sample Robot Structure. However, I am interested in switching to command based next season because I am hearing it's far better. Also an update on the gyro/encoder issue, we have our robot turning 180 and driving forward with the encoder and stopping at the proper distance we wanted it just took a bit of math and calibrating but I think we are tuned in and ready for autonomous! :p |
Re: Autonomous Help
Quote:
|
| All times are GMT -5. The time now is 10:30. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi