|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||
|
|||
|
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 |
|
#2
|
||||
|
||||
|
Re: sample code to move robot autonomously
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. |
|
#3
|
|||
|
|||
|
Re: sample code to move robot autonomously
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 john |
|
#4
|
||||
|
||||
|
Re: sample code to move robot autonomously
nice post luke thanks!
|
|
#5
|
|||
|
|||
|
Re: sample code to move robot autonomously
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! |
|
#6
|
||||
|
||||
|
Re: sample code to move robot autonomously
great post Luke.
|
|
#7
|
|||
|
|||
|
Re: sample code to move robot autonomously
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. john |
|
#8
|
||||
|
||||
|
Re: sample code to move robot autonomously
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. |
|
#9
|
|||
|
|||
|
Re: sample code to move robot autonomously
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
|
|
#10
|
|||||
|
|||||
|
Re: sample code to move robot autonomously
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. Last edited by apalrd : 15-02-2010 at 15:56. |
|
#11
|
|||
|
|||
|
Re: sample code to move robot autonomously
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 |
|
#12
|
|||
|
|||
|
Re: sample code to move robot autonomously
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
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 |
|
#13
|
||||
|
||||
|
Re: sample code to move robot autonomously
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 (lvmastery.com too), they have a state machine video tutorial over there. Last edited by Luke Pike : 17-02-2010 at 15:54. Reason: Added links |
|
#14
|
|||
|
|||
|
Re: sample code to move robot autonomously
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 |
|
#15
|
||||
|
||||
|
Re: sample code to move robot autonomously
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. |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| sample code | johncap100 | NI LabVIEW | 2 | 01-02-2010 08:18 |
| Missing sample code | ajdecker1022 | Java | 3 | 28-01-2010 19:39 |
| Sample Code | Belly | Programming | 1 | 21-01-2010 14:34 |
| Sample Code | Japper | Programming | 2 | 01-12-2008 22:36 |
| Sample PID code??? | EHaskins | Programming | 14 | 08-08-2006 14:18 |