sample code to move robot autonomously

I am a neophyte with labview, would anyone be willing to send a small autonomous code to simply move a robot. I have downloaded the default code but all i get when i push the autonomous button on the drivers station is that the watchdog is not fed.
any help is greatly appreciated

thanks john

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”. 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 (28.4 KB)

Autonomous (28.4 KB)

well i am at home right now, but will try in in about 2 hours when i go to school
thanks much and yes i hope it works too,
thanks alot for the instructions

nice post luke thanks!


We have been trying to complete something similar without success. Your simple explanation w/example code is exactly what we are looking for.

Professional Graciousness are its best!

great post Luke.

Luke it worked great thanks a bunch

I was wondering if I want to add a solenoid action at the end of say 2 seconds of the motor motion do i just tag that on with in the case loop?
or would i have to make another cas loop?

thanks again for all your help.

Great, glad it worked!

If you want to add more functionality, you can add more states. Just right click on the enum outside the while loop, select “Edit Items…”, and add an item. For your case, call it “Fire Solenoid” or something more descriptive of what that does.

As soon as you do this, you’ll notice that the enum inside the case structure breaks. This is because the enum changed and the old one is different. It’s not that hard to delete the old enum and copy in a new one (making sure the value is the same as before) for this state machine, but as it gets more complicated, it can be a real pain.

To solve this, we can make our enum a typedef. This means that when we change it, any other instance of it, in any location, gets updated as well. To change our enum into a typedef, first right click it and select “Change to Control”. Go to the front panel and right click the enum and select “Advanced->Customize”. This puts our enum in a new window. You’ll notice at the top a pop down which is set to “Control”. Click this and change it to “Typedef”. Save the typedef, giving it some meaningful name like “State Machine Enum” or something like that. After you close that window, it will ask you if you want to replace the control with the typedef. Hit yes, and change the enum back into a constant by switching to the block diagram and reversing what you did to change it into a control. Now use copies of this enum to switch cases in the state machine. To add more states, right click the constant and select “Open Typedef”. Make your changes, then save. All the other constants with that typedef are updated.

Okay, now that that’s out of the way, we can add a case to our case structure to activate a solenoid. If you want to wait for a period of time after you stop the robot, you can copy the code from the first state and paste it into the “Stop” case. Change the enum that is passed into the shift register to the new state. Or, you can switch to the solenoid case right after the “Stop” case. Just delete the wire and use an enum constant set to the solenoid item.

To add the solenoid state to our case structure, right click it and select “Create Case for Every Value” (I think that’s what it’s called). Go to the new case and paste your solenoid code.

A few notes:

Shift registers need something wired into them always, which is why I wire the value from the “?” box of the case structure into the shift register even though it’s the last case.

Remember, a state machine does something until a condition changes (a digital input changes, an encoder value goes above some number, etc.), then goes to a new state. It doesn’t even have to be the next one, which allows for some pretty cool abilities.

THANK YOU SO MUCH! We are a rookie team and we just need a bunch of help! and once we understand these examples it will help us expain it to other teams in our area! =D

If you have LEGO programmers who worked in Robolab, there is another way they might appreciate.

Create a VI for each main action - Drive X Distance, Turn X degrees, etc. It will be a blocking VI that is called once and expected to perform an action. It contains its own while loop and runs until it has reached its destination.
Then, create a “Flow” control and indicator on each one. Make the control and indicator inputs and outputs by right-clicking on the icon on the front panel, clicking “Show Connector”. Click the position in the grid where you want the input to show up, then click the input. It will now appear the color of the data type. Do the same for the indicator (output). It dosen’t matter what data type it is, I use Double because I like orange, but anything works.
In Autonomous Independent, place the blocks in the order of what you want to do. E.G. Drive_Straight for 18 inches, Turn -90 degrees, Drive_Straight 36 inches, etc.
Wire the Flow from one VI to the next in the order you would like them to execute. Since data must move from one VI to the next (Whatever data is on the Flow wire), LabVIEW will make the VI’s execute sequentially.
Done. You might want to put common actions, such as wait x time or set kicker to kick in VI’s too, just so you have a flow wire to wire between them.

I have a picture of an example. I don’t have icons on my VI’s since the graphics team is still making them, but I did label them in comments. I also put my VI’s in a palatte, but that is more advanced and not really necessary.

I also posted my Drive Straight Vi.

If you prefer the loop-based method shown in previous posts, you can put it in Autonomous Iterative and it will be called at 50hz. You will need to replace the shift register with either a front panel control (local variable) or global variable. (16 KB) (16 KB)

gosh thanks to Luke and parady

these are very very helpful and well explained, i am trying them as soon as i type
thanks again John

luke i think i did what you instructed, it was alittle hard to follow but it
did not work, i am attaching the auto inde code. maybe you could look at it if you get a chance and let me know what to do next.
thanks John

Autonomous (30.7 KB)

Autonomous (30.7 KB)


You are almost there, just some things that were a little off that I didn’t explain. I changed your code so you can see.

First, all the booleans wired into the stop box are all true. This means that as soon as the code runs, it stops the loop. Make all of them false except the one in the last case. This makes sure that we stop the loop only when we are done. In fact, you could just wire a false all the time. Just delete all those booleans and create one false boolean outside the case structure and wire that into the stop box. This will keep the code running until someone stops autonomous mode. This is probably better anyway, since you will probably be doing stuff right until the end

Second, in the “stop” case, you never wire a new state into the shift register, you just pass the old one back through. In order to go to the next state, you have to wire a constant set to the state you want to move to. Every state should have a way to move to another, except possibly the last one, where you don’t want to ever go to another state.

Other than that, you pretty much nailed it. Should work, I had to disconnect the typedef since I didn’t have that file, you can replace the constants with your typedef by dragging the file into the block diagram as many times as you need.

Autonomous (30.9 KB)

Autonomous (30.9 KB)

ok great thanks much and thanks again for taking time.
this neophyte really needs the help
i will try it today
thanks john

HI Luke me again well i think i followed your instructions but no go on the solenoid part, it moves ahead fine, but no solenoid action.
I have the solenoid actuated by button one on one of the joysticks in teleop and it works fine there, but I cannot see where in autonomous where we are actually causing the solenoid to actuate. shouldn’t there be a place to do that? Liek maybe call a button or something?

what i did was create another sample and started from the beginning including the solenoid case. but still no go.

I was wondering if you would be so kind as to maybe walk me through the steps again?
I am not real confident that i followed your last instructions on adding a case real well.
thanks a bunch

In your code, when you pass a Boolean true constant to the solenoid vi, it should actuate. Take a look at what you do in Teleop. You’re probably unbundling the buttons cluster from the joystick and wiring one Boolean from that into the solenoid vi, right? It should work the same in autonomous, only instead of using a button, we use a Boolean constant. They’re both the same type.

If you could, can you take a picture of your Teleop code and how you actuate the solenoid? That will help me see what’s going on.

Hey Luke i found out the problem i used Soleniod to call the solenoid in teleop but had used a small “s” in the autonpmous code …gosh one littel letter makes a huge difference, maybe labview needs to have a watch dog for goofy programmers:rolleyes:
thanks john for now i think i have it,
one more thing i believe should be able to add case structures as i want to add a few more moves in autonomous right???

thanks again John

Yeah, gotta be careful with that reference registry.

Yep, all you need to do is add states (items) to your enum and add cases in the case structure to handle them. Just make sure to add a condition to move to the new state.

Also, check out FRCmastery ( too), they have a state machine video tutorial over there.

HI me again
In a step back we changed the enum into a type def, when that happens if you right click on the enum there is no way that i can see to add more states, an edit itme never shows up, i did go to properties and there an edit item there but it does not allow changes, i can go back and start from the beginning but add a number of states and then go thru and add all the case states, but it seems to me there must be an easier way, also if I do change the enum to a constant if i were to add any states then the pull down menu at the top of the case loop changes and does not reflect what is in the enum??
thanks again for your help, i do think i am getting way closer to what i want to do.
thanks John

If your enum is now a typedef, then when you want to add items, you need to open the typedef and perform the edits there.

Right-click the enum, open typedef. Right-click THAT enum and you’ll be able to modify its properties. When you save/close that typedef, it’ll update every instance for you.