Team 254 2011 FRC Code

Hey all,

I’m Eric Bakan and I’m Team 254’s Head of Programming/Controls aka Chief Executive Brogrammer. With the conclusion of our last offseason event, the MadTown Throwdown, Team 254 is releasing our 2011 FRC codebase.

The code can be viewed and downloaded from our team GitHub at https://github.com/Team254/FRC-2011

We had used SVN for the duration of the season but unfortunately our server died and we lost all our SVN backlogs. Based on our programming team’s previous experience we’ve decided to move to Git for our version control, primarily because it allows us to make offline commits when we have intermittent internet access on our developer laptop. At the moment this is merely the snapshot of our code as of the end of Championships.

We hope that releasing this code will prove useful to other teams. The included README should provide useful details about the architecture of the system and the individual files should have further notes.

Our only request is that you leave a reply if you found any of this code helpful. Everything is released under a 2-Clause BSD License so feel free to use any part of it.

Thanks for posting! The attention to detail is astounding. 4 different sets of PID gains for the elevator, depending on direction and number of stages engaged? :yikes:

EDIT: And that’s before I even saw the state space controller for the drive :slight_smile:

Incredibly exciting to see such sophisticated robot code being open sourced. I would love to see other teams follow suite :slight_smile:

Thanks! We had a lot of fun this year. Did you also notice the threading and how we implemented Auto mode? We use trapezoidal motion profiles for all the drive commands to generate very smooth motions. We also improved the drive code so that it is even smoother and more responsive than the 2010 code. We learned a lot and will be applying what we learned to next year’s code.

Drove the robot forwards for 3 seconds, spun for 3 seconds, modeled the robot from first principles, plugged in the ideal constants, fit the constants to the actual response, designed a controller in Matlab, deployed it to the robot, and it worked first try. Sooo tempting to try it for the other systems next year. I decided to model the drivetrain as MIMO. Previous attempts to model it as SISO were resulting in the bot twitching left and right a bit once it stopped due to the false assumption that accelerating one side did not result in any force on the other side.

If you or anyone else has any questions, we’d be glad to try and answer them.

Thanks for posting the code Austin. I’d never heard of trapezoidal motion profiles, but the first Google link seems to give a basic understanding. I haven’t been able to view the code yet, but have what assumptions about acceleration/deceleration of the manipulator parts (lift & wrist) were you able to make when modeling their movement?

We used a simple PD loop on the wrist and elevator with some gain scheduling, since it worked well enough after hand tuning. The elevator changes mass when it starts to lift the second stage, so there is a second set of constants for that case. For both the arm and elevator, they would undershoot when approaching the goal from below, and overshoot when approaching the goal from above. To solve this, we have 2 sets of constants, one per direction. This solved our overshoot problems. I also avoid integral whenever possible. We just accepted the small steady state error on the elevator and wrist, and moved on, rather than spending the extra time to tune the integral constant across 6 sets of constants. We pass a goal into the two sub systems when we hit the setpoint buttons. The manual joysticks for those two systems also tweak the goals, rather than doing anything with power.

One thing we realized when driving the robot was that it was very useful for the arm to be angled ~50 degrees above horizontal when lining up to place a tube. To facilitate this, whenever a setpoint button is hit, the elevator raises up, and then when it is all the way up, the arm tilts forwards. When the driver hits another button, the robot then goes through an “auto-score” motion which opens the claw, spits out, lowers the elevator, then raises the arm back up, and backs the robot up. This offloads a lot of stuff from the drivers.

Most of the fancy controls is in the drivetrain, which we spent easily over a month working on to tune the robot so that it was accurate and precise enough to score two tubes.

Ah, if only I knew enough C to appreciate it. (LabVIEW team here) I’m sure it would make me wanna tear up a bit…

With that kind of automation, you can make any robot score quickly and effectively. Put it on a 254 robot and you see why they have a Championship banner.

Interresting…

Is there any reason that you didn’t run the elevator and arm as a single integrated system, to simplify things like this? We ran our elevator/arm as a single state machine, and part of the state transition handled actions like this (with the data input being a requested state, and the data output being a pair of setpoints), and fed the positions output into a pair of setpoint controllers and anti-death logic.

The system evolved from the two separate systems with minimal automation to being connected together, and we never stopped to think about re-architecting. We also wanted to be able to move the elevator or wrist at any point in time manually and have it cancel the action.

Looking back, I really like how easy it was to write auto modes with the multi-threading that we did, and would like to make it that easy to write auto-score functions as well.

Yes, multithreading can lead to prettier/less verbose auto modes, in my experience. We have been prototyping a trapezoidal motion profile as well, and I really like how smoothly it achieves distance setpoints with sub-inch accuracy. Are you using a trapezoidal profile for turning in place, as well? (I didn’t notice it, but haven’t gone through line by line - yet)

EDIT: Yes, I see that you did :slight_smile:

I perused the code over lunch and noticed something that confused me. In your ports file you’ve assigned both the “B” port of an encoder and a limit switch to digital I/O 10. In order to facilitate this, was there a specific order the wires had to go into the DIN for I/O 10?

We rely heavily on limit switches to act as redundant safeties during programming as well as sensor failures, so being able to pair limit switches with encoders in this manner could save us from having to make I/O port tradeoffs.

The port definitions for the top and bottom roller encoders are probably obsolete. It doesn’t look like either of those encoders is actually used in the code, so there’s no real conflict.

Correct. There are no encoders on the rollers. That sounds like a very old piece of code that didn’t get removed as the code evolved…

Austin,

Could you or one of your programmers explain the rationale behind the design of your victor_linearize function? You average 5th and 7th order polynomials together, but it isn’t obvious why you do this.

Thanks

This question was brought on by this thread: http://www.chiefdelphi.com/forums/showthread.php?p=1085502#post1085502

Sure. I wrote this function in 2010, so I’m quite qualified to answer any questions.

Here is the data and the three polynomials that are in the function.

I generated the red data points by putting the robot up on blocks and applying PWM to the motors. I then read out the wheel speed at steady state for various PWM values.

From there, I tried to fit the data. I first started with a 5th order odd polynomial (The + and - response should be the same, which means that f(x) = -f(-x)). It is shown in green. That wasn’t a great fit, so I tried a 7th order polynomial, shown in blue. Neither of them were great fits. They are not monatonically increasing functions. When you drive the robot with them, the robot doesn’t feel like the throttle is a consistent function, and it feels weird (it has been a while, and feelings don’t translate to words so well.)

From there, on a whim, I tried averaging the two functions. This actually turned out quite well. But, when I put it on the robot, it felt like the power was reduced too much at low speeds. To try to compensate for this, I added in a bit of y=x to get the equation shown in the legend above for the pink plot and to boost the power applied at low speeds. This is what is in use today in the victor_linearize function.

Is there a reason you choose to use a polynomial function instead of a piecewise linear function?

From your empirical data it looks like no more than 5 linear sections (see red lines in figure below) would be needed to characterize the curve quite well.

This would significantly cut down on the number of computations needed to return a result, and in this case it look like it might even more accurately fit the input data.

http://img221.imageshack.us/img221/9817/58307875yw.jpg

Did you do an analysis to determine whether the piecewise linear code plus the conditional logic it requires executes faster than the single polynomial ?

If speed is what you need, it’s hard to beat a complete lookup table (no interpolation required):

**

It’s easier to generate the polynomial with software (completely avoiding any transcription by hand or hand calcs). This is unless there is a piece-wise linear approximation tool I am unaware of.

Also, for simple math, it’s not really a concern when running on the cRio.

Don’t forget that another option is pre-processing of lookup tables, if the memory will hold it. 256 data points, 1 per PWM step, would be easy to calculate during sensor initialization and also cut down on cycles. Preprocessing it instead of pre-programming it also allows you to make adjustments to parameters before each match (if needed).

I agree that it’s probably not needed for the cRIO, but was very useful for old-school quadrotors on primitive processors. It also helped back in the 90’s with signal processing algorithms that needed to be realtime.

Theoretically one could pre-process all of the possible motor outputs versus inputs such that a lookup was done given sensor/driver input instead of a calculation – but that’s a bit overboard.