Quote:
Originally Posted by davidalln
Therefore, this is the perfect year for a sort of modular approach to autonomous: i.e., the ability to code general functions such as "drive to initial ball" or "turn towards target" or "kick ball" and have an external source such as a text file or an array of variables determine which actions get done in what order.
|
Boom! You are thinking like a professional programmer now.
Nobody wants to do more work than they have to. Copy-and-paste sucks and is generally considered bad practice. Also, code like this:
Code:
sc1.set(joy.getAxis(4));
is _useless_ to anyone but the original programmer, and even in the case that you are the only code monkey for your team, it puts a lot of pressure on you to remember all of the fine details (which axis is axis 4, which degree of freedom sc1 is, and so on). That's not something you want to trip you up at competition.
Code:
leftDrive.set(joy1.getAxis(JOY_LEFT_Y));
is significantly more readable, and when you need to change which axis is being used, you change the value of the constant, not in 20 places. Trust me, this saves you so much time and sanity.
This absolutely helps for autonomous, too. Breaking bigger functions down into smaller, commonly-used components makes your code more robust and readable. Any place where you are doing something repeatedly, there is an opportunity to clean up your code.
As far as making your code have several modes, you might look into a sequential thumb switch (something similar to
this, except that one seems a bit pricey). You wire it up to a few digital inputs, and read the inputs from least significant to most significant and convert that number to decimal...then use a case structure to select which mode you'd like. I've gone this route before and it is so nice. If you'd like more info on this, shoot me a PM and I'll help you out.
tl;dr version:
- Pull out anything that is in common--anything you'd be tempted to copy and paste. If you've copied and pasted something, you are doing it wrong.
- Name your variables logically. You shouldn't need to scroll around to figure out which variable you need.
- Create smartly named constants to replace literal numbers. It is easier to read for everyone. Convention suggests you use a prefix for a group of constants, e.g. if they are all joystick axes, make them JOY_Y, JOY_X, JOY_SHOULDER, etc.
- Using a switch on the robot, you can switch programs without needing to re-deploy code every match. It is very nice.
That was a bit more long-winded than I meant it to be...I guess I soapbox too easily
My 2 cents...
Jacob