Instead of just giving you code, I’m gonna tell you how to do it too.
If you didn’t change anything in the default code, the autonomous code will be located in the “Autonomous Independent.vi”. This VI gets run once when autonomous is turned on using the driver station.
So, you should have a while loop (a grey-bordered box) and some other junk in there. Delete all the stuff inside and outside the while loop EXCEPT the watchdog code. We’re gonna make a state machine which moves the robot for a time period and then stops. Now, you might ask if a state machine’s a little overkill for just moving a robot. It might, but state machines can be easily extended to do more stuff, which I assume you’re going to do.
So, right-click the area inside the while loop and select structures->case structure. Make this pretty big inside the loop.
Now, right-click to the left of the while loop and select numeric->enum. This will hold all the states our machine will handle. When you drop this on the block diagram, right-click it and select “Edit Items…”. In the window that pops up, enter two items, “Move” and “Stop”. That’s pretty basic, but we’ll go with that for now.
Once that’s done, make sure its outside the while loop and to the left. Click the right-most side of the enum and wire it into the “?” box of the case structure we created earlier. This will create a tunnel through the while loop. Right click the tunnel and select “Replace with Shift Register”. This changes the tunnel into a bigger box with another one just like it on the opposite side of the while loop. Whatever goes into the right side of the shift register comes out the left side on the next iteration. This allows us to save our state between while loop runs.
Now for the fun bit. Go to the “Move” case in the case structure. I’m not sure how you have your drive set up, but if the default code works, just copy the drive code from the “Teleop” VI (including the get from registry) and paste it inside the “Move” case of the case structure. Right click the “Y axis value” input and select create constant. Type some value in here, like 0.5. Do the same for the “X axis value” input, but put 0.
Since we want to move the robot for a time period then stop, we’ll need a timer. Luckily for us, there’s one for us to use. Right click inside the case structure and select timing->elapsed time. Place it inside the case structure and a dialog will pop up. Enter a time or leave it at the default 1 second. You’ll notice the blue box has an boolean output labeled “Time has Elapsed”. When this is true, we need to move to the “Stop” case of our state machine.
Okay, some stuff we need to do that might not make sense now, but needs to be done. Right click and select comparison->select and place it inside the case structure. Wire the “Time has Elapsed” output of the elapsed time into the middle input of the select function.
Copy the enum from outside the while loop and paste it into the case structure. Click on it and select “Stop”. Wire that into the top input of the select function (the true input). Next, find where the little blue “?” box is on the case structure. You’ll notice you can drag a wire off it. Wire this into the lower input of the select function (the false input).
We’re almost done, wire the one output of the select function into the right side of the shift register. Copy the drive code and paste it into the “Stop” case. Change all the inputs to 0. Wire the “?” of the case structure into the right side of the shift register. Finally, right click and select boolean->true constant and drop it inside the “Stop” case. Wire this into the red stop box of the while loop. Right click the tunnel it creates in the case structure and select “Use Default If Unwired”.
What have we done? Well, we stay in the “Move” state until the timer indicates 1 second has passed. Then, we move to the “Stop” case which stops the robot and ends the loop. You can change and add more states to do more complicated stuff if you want.
I’ve attached the code so you can look at it and get a feel for how it works. Hope this helps.
Autonomous Independent.vi (28.4 KB)
Autonomous Independent.vi (28.4 KB)