|
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.
|