The Forthcoming RageBridge: Stick Calibration

They’re here!

All of the RageBridges! All of them ever!

Or rather, 100 of them, in neatly packaged panels of two. Here’s what one of the manufactured panels looks like.

Pretty cool.

Okay, so these actually got in last week, but I’ve been doing some background and infrastructural work since then. I’m planning on gathering a few friends and flashing, populating headers, and testing all of these this coming weekend. I decided to leave the chips unflashed because I was not yet satisfied with the firmware (and the flashing was a few hundred extra dollars), and the headers are purposefully left unpopulated because many people prefer soldered-on pigtails or their own wiring configuration. It’s definitely a little more legwork on our end, but for such a small run (100, maybe with a few duds) I think it’s still manageable, and will allow the fine settling into best practices that inevitably accompany any parts business. Time to be a little adventurous.

From a firmware perspective, the only thing RageBridge has been missing since the last board revision is a way to calibrate the servo PWM range. This is generally preferred because not all radios are created equally, and some may have more stick travel & adjustable PWM endpoints than others. For instance, my (nice for 2006) Spektrum radio can hit the full 1000-2000uS servo range if I navigate through the menus and tell it to stretch its range a bit. But my cheesy Hobbyking 6 channel radios can’t do the same unless connected to a computer to program them – the stock range is more like 1200 to 1800us. So, any fixed input range will inevitably be a compromise – for some people, the stock settings will be too twitchy (i.e. stick travel range is greater than the board’s accepted input range), or you might not ever hit full throttle (because the radio stick travel doesn’t go far enough to match the controller input range).

Calibration has always been one of those things that I accepted as working, but never thought about implementing. Depending on how it’s done, it can be relatively simple, or a mess of state machine code.

One of the most common modern calibration paradigms is the “stick-high” method, used by most R/C model controllers, whether air, sea, or ground. This involves powering on the controller with a receiver attached (and generally, already powered and outputting valid pulsewidths) and you commanding full positive travel. If the first thing that the ESC sees, or sees within 1-2 seconds, is a full positive signal, then it waits for a few seconds and checks again. If you are still holding full positive, then it beeps or blinks (accepting the full positive position), and then you back off to full negative and hold for a few seconds, then back to neutral. For single channel ESCs without many other features, it’s pretty much the way to go.

I decided to implement stick-high calibration for RB, except modifying it for both channels at once. This entailed adding a timed loop in the middle where normally you would just check for full positive and full negative signals. The idea is that during the timed loop, the user just swirls the sticks around all at once and the controller will capture both ranges simultaneously. At any point, if the signal returns to neutral for more than 2 seconds, the calibration routine exits and writes the results to EEPROM so it is persistent.

Of course, there’s other little touches to make the system more robust like checking for pulse validity (rejecting malformed or misread pulses) and fail-safe performance if the user bails out in the middle. In all, it took me a few hours of planning and a few hours of beasting Arduino code, and I’m quite pleased with the final result.

So, in a completely out of context brain bump, here’s RageBridge’s stick calibration code. When I ship it through e0designs.com, the full source code will be released along with board files, but I’m putting this early because I’m particularly proud of it.

I subscribe to the Expository Essay school of code commenting – I literally write what every line and every procedure does, in detail, and in natural english. To hardcore and experienced coders this might seem like a waste of characters, but I find it particularly easy to read my way back through old code and decipher what the hell I thought I was doing when I wrote it. So there’s actually plenty of paragraphs in the comments.

As a bit of backstory since I didn’t post all the variable definitions, anything with a _1 or _2 is referring to the channel of RB it is relevant to. The code is written with Arduino 1.0.1 using the PinChangeInt and EEPROM libraries (PCI is not used in this section as the interrupt procedures that capture servo pulsewidths are not shown).

  • RCMAX, et. al. are all declared as signed integers at the beginning of the code (not shown).
  • CALIBRATION_CONSTANT is a fraction of the total stick travel, right now 0.66. In other words, anything ABOVE 66% of the current maximum travel is grounds for entering calibration mode. This is one of the tricks I added to prevent calibrating yourself out of your own radio travel bounds. So, basically what it entails is each time you calibrate you could reduce the range by 1/3rd if you had to.
  • neutral_counter and NEUTRAL_CYCLES_MAX refer to how many iterations of the neutral signal checking routine can run before the calibration procedure is declared to be done.
  • NEUTRAL_DELTA is the pulse width deviation around the current neutral point that can be recognized as a new valid neutral point. For instance, the default neutral pulsewidth for servo PWM is 1500us. If NEUTRAL_DELTA is defined as 100, which it is, then anything from 1400 to 1600 may be set as a new neutral point. This is to make sure you cannot drift the center stick zero throttle point too far, even on purpose. You can, of course, do so by running multiple calibration cycles. But why!?
  //Collect the user calibration endpoints from EEPROM. If the EEPROM values return 255
  //then calibration has never occurred, so use defaults.
  RCMAX_1 = (signed int)EEPROM.read(100);
  RCMAX_1 += (signed int)(EEPROM.read(101) << 8);
  RCMIN_1 = (signed int)EEPROM.read(102);
  RCMIN_1 += (signed int)(EEPROM.read(103) << 8);
  RCCENTER_1 = (signed int)EEPROM.read(104);
  RCCENTER_1 += (signed int)(EEPROM.read(105) << 8);
  RCMAX_2 = (signed int)EEPROM.read(106);
  RCMAX_2 += (signed int)(EEPROM.read(107) << 8);
  RCMIN_2 = (signed int)EEPROM.read(108);
  RCMIN_2 += (signed int)(EEPROM.read(109) << 8);
  RCCENTER_2 = (signed int)EEPROM.read(110);
  RCCENTER_2 += (signed int)(EEPROM.read(111) << 8);

  //Bytes will return 255 if they were never read; full 16 bit result is 65535
  if(RCMAX_1 == 0xffff ) {
    RCMAX_1 = RCMAX_DEFAULT;
  }
  if(RCMIN_1 == 0xffff) {
    RCMIN_1 = RCMIN_DEFAULT;
  } 
  if(RCCENTER_1 == 0xffff) {
    RCCENTER_1 = RCCENTER_DEFAULT;
  }
  if(RCMAX_2 == 0xffff) {
    RCMAX_2 = RCMAX_DEFAULT;
  }
  if(RCMIN_2 == 0xffff ) {
    RCMIN_2 = RCMIN_DEFAULT;
  } 
  if(RCCENTER_2 == 0xffff) {
    RCCENTER_2 = RCCENTER_DEFAULT;
  }  

  delay(1000); //Just chill for a bit to make sure radios have bound and begun sending out good signal.

  //Calibration mode procedure.
  //After initialization, check for good signal on channels 1 and 2
  //If the signal is above the calibration threshold on both channels, keep waiting. If not, enter main program loop.
  //If at the end of 5 seconds the stick has been held, enter calibration mode. If it has not, enter main program loop.
  //Even if there is no good signal, proceed to main loop anyway - the main loop begins in failsafe mode. 
  if(is_pulse_good(vol_ch1_pw,RC_ABS_MAX,RC_ABS_MIN) && is_pulse_good(vol_ch2_pw,RC_ABS_MAX,RC_ABS_MIN)) {
    signed int init_calib_threshold_1 = (signed int)(CALIBRATION_CONSTANT*(RCMAX_1-RCCENTER_1))+RCCENTER_1;
    signed int init_calib_threshold_2 = (signed int)(CALIBRATION_CONSTANT*(RCMAX_2-RCCENTER_2))+RCCENTER_2;

    if(vol_ch1_pw > init_calib_threshold_1 && vol_ch2_pw > init_calib_threshold_2) {

      //To check for stick hold, begin the timer and set a flag that will be cleared if the stick is released
      unsigned long calibration_timer = millis();
      boolean entered_calibration = true;

      //Enter 5 second wait loop, constantly checking for stick release. Invalid signal also constitutes "stick release".
      while(millis() - calibration_timer < 5000) {
        if(is_pulse_good(vol_ch1_pw,RC_ABS_MAX,RC_ABS_MIN) && is_pulse_good(vol_ch2_pw,RC_ABS_MAX,RC_ABS_MIN)) {
          if(vol_ch1_pw < init_calib_threshold_1 || vol_ch2_pw < init_calib_threshold_2) {
            entered_calibration = false;
            break;
          } //End stick release check
        } else {
          entered_calibration = false;
        } //End pulse good check

      } //End wait for 5 seconds to check for stick hold

      //If the flag is still true, then the user has held the stick for > 5 seconds above the threshold
      //Calibration mode description:
      //Resetting the old max and min bounds to a lower threshold (CALIBRATION_CONSTANT * RCMAX_n)
      //If a newly captured value exceeds the thresholds, save it. 
      //If a newly captured value is within a certain % of neutral (NEUTRAL_CONSTANT * RCCENTER_n above or below RCCENTER), start a timer
      //After 3 seconds of this, assume the user has returned stick to desired neutral and take several samples for neutral position
      //If any pulse is invalid, bail out of calibration and retain old values.
      if(entered_calibration) { 

        digitalWrite(BLINK_FURIOUSLY,HIGH); //Set the LED on solid to let the user know that calibration has begun

        //Set initial thresholds to exceed. The thresholds are a certain % of the current set of maxes and mins
        //such that the controller can never be "calibrated out". By only exceeding the threshold very slightly
        //subsequent iterations of calibration can narrow the range of signals too, if it is necessary.
        signed int rcmax_1_temp = (signed int)(CALIBRATION_CONSTANT*(RCMAX_1-RCCENTER_1)) + RCCENTER_1;
        signed int rcmin_1_temp = RCCENTER_1 - (signed int)(CALIBRATION_CONSTANT*(RCCENTER_1-RCMIN_1));
        signed int rcmax_2_temp = (signed int)(CALIBRATION_CONSTANT*(RCMAX_2-RCCENTER_2)) + RCCENTER_2;
        signed int rcmin_2_temp = RCCENTER_2 - (signed int)(CALIBRATION_CONSTANT*(RCCENTER_2-RCMIN_2));
        signed int rccenter_1_temp = RCCENTER_1;
        signed int rccenter_2_temp = RCCENTER_2;

        boolean am_calibrating = true;
        boolean commit_calibration = false;

        byte neutral_counter = 0;

        //Set the main calibration loop going. 
        //First, check for good pulses. If any are bad, the routine is immediately ended and changes are NOT saved.
        //Next, check for pulses near neutral. If any are, increment a counter. If the counter is over a threshold,
        //then the user has returned the stick to netural and calibratoin can exit/changes be saved.
        //If any subsequent pulses are outside of the neutral bound, zero the counter. 
        //Next, expand the pulse range if the new set of pulsewidths are greater in magnitude deviation
        while(am_calibrating) {

          //Sanity check - if any pulses are out of range or invalid, exit.
          if(is_pulse_good(vol_ch1_pw,RC_ABS_MAX,RC_ABS_MIN) && is_pulse_good(vol_ch2_pw,RC_ABS_MAX,RC_ABS_MIN)) {

            //Save a copy of the current "volatile" pulsewidths (which can change randomly due to the interrupt)
            ch1_pw = vol_ch1_pw;
            ch2_pw = vol_ch2_pw;

            //Neutral check. If the signal is within the box of valid neutrals for enough cycles, exit and save changes.
            if(ch1_pw < (RCCENTER_1 + NEUTRAL_DELTA) && ch2_pw < (RCCENTER_2 + NEUTRAL_DELTA)) {
              if(ch1_pw > (RCCENTER_1 - NEUTRAL_DELTA) && ch2_pw > (RCCENTER_2 - NEUTRAL_DELTA)) {
                if(neutral_counter > NEUTRAL_CYCLES_MAX) {
                  am_calibrating = false;
                  commit_calibration = true;

                } else {
                  neutral_counter++; //Within bounds, but not exceeding number of allowed cycles, so increment and move on.
                  rccenter_1_temp = ch1_pw;
                  rccenter_2_temp = ch2_pw;
                }
              } else {
                neutral_counter = 0; //If any of the within-bounds checks fails, reset the counter since the stick has moved.
              }
            } //End neutral check

            //Bounds check; If the new pulses exceeds the current set of temporary calibration bounds in either direction
            //set them as the new bounds.
            if(ch1_pw > rcmax_1_temp) rcmax_1_temp = ch1_pw; else if(ch1_pw < rcmin_1_temp) rcmin_1_temp = ch1_pw;
            if(ch2_pw > rcmax_2_temp) rcmax_2_temp = ch2_pw; else if(ch2_pw < rcmin_2_temp) rcmin_2_temp = ch2_pw;
          } else {
            am_calibrating = false;
            commit_calibration = false; //Something has gone wrong and caused the calibration loop to terminate early...
          } //End sanity check
          delay(20);
        } //End main calibration loop

        //Save changes?
        if(commit_calibration) {

          //First, reassign the constants for this power cycle...
          RCMAX_1 = rcmax_1_temp;
          RCMIN_1 = rcmin_1_temp;
          RCCENTER_1 = rccenter_1_temp;
          RCMAX_2 = rcmax_2_temp;
          RCMIN_2 = rcmin_2_temp;
          RCCENTER_2 = rccenter_2_temp;

          //Next, write these new values to EEPROM!
          byte rcmax_1_lowbyte = (RCMAX_1 & 0x00ff);
          byte rcmax_1_highbyte = ((RCMAX_1 >> 8) & 0x00ff);
          EEPROM.write(100,rcmax_1_lowbyte);
          EEPROM.write(101,rcmax_1_highbyte);

          byte rcmin_1_lowbyte = (RCMIN_1 & 0x00ff);
          byte rcmin_1_highbyte = ((RCMIN_1 >> 8) & 0x00ff);
          EEPROM.write(102,rcmin_1_lowbyte);
          EEPROM.write(103,rcmin_1_highbyte);

          byte rccenter_1_lowbyte = (RCCENTER_1 & 0x00ff);
          byte rccenter_1_highbyte = ((RCCENTER_1 >> 8) & 0x00ff);
          EEPROM.write(104,rccenter_1_lowbyte);
          EEPROM.write(105,rccenter_1_highbyte);

          byte rcmax_2_lowbyte = (RCMAX_2 & 0x00ff);
          byte rcmax_2_highbyte = ((RCMAX_2 >> 8) & 0x00ff);
          EEPROM.write(106,rcmax_2_lowbyte);
          EEPROM.write(107,rcmax_2_highbyte);

          byte rcmin_2_lowbyte = (RCMIN_2 & 0x00ff);
          byte rcmin_2_highbyte = ((RCMIN_2 >> 8) & 0x00ff);
          EEPROM.write(108,rcmin_2_lowbyte);
          EEPROM.write(109,rcmin_2_highbyte);

          byte rccenter_2_lowbyte = (RCCENTER_2 & 0x00ff);
          byte rccenter_2_highbyte = ((RCCENTER_2 >> 8) & 0x00ff);
          EEPROM.write(110,rccenter_2_lowbyte);
          EEPROM.write(111,rccenter_2_highbyte);
        }

        //We're done with calibration! Flash the LED a few times to indicate success.
        for(byte caliblink = 0; caliblink < 10; caliblink++) {
          PORTB |= (1 << 5);
          delay(BLINK_DURATION);
          PORTB &= ~(1 << 5);
          delay(BLINK_DURATION);
        } 
      } //End of master calibration routine

    } //End of check signal over calibration threshold

  } //End of check signal good

RageBridge Revision 3: Boost Converters and the Market Survey

Continuing the story of Ragebridge’s quest to find a stable power converter design, I received the updated version 3 boards earlier last week, and got around to assembling and testing them over the weekend. I think RB is ready, at this point, to enter the release candidate & fine tuning stage of the development process. But first, I had an adventurous time remembering what boost converters did.

First, if you want to skip straight to the RageBridge potential customer & user survey, feel free to just click through.

To summarize, the RB project has seen me move away from a known stable (but not that vintage) power supply design to exploring other ways of generating logic voltage and gate drive voltage in order to push the lowest operating voltage down below about 18 volts. My general setup has been dropping a switching regulator on the battery input and buck converting to 15 volts (gate drive voltage) and then a linear regulator to 5v (logic voltage).

This arrangement has been reliable, but it pretty much limits the working voltage of the controller to greater than 13 volts or so. The switching regulator, a LM2594, can turn entirely on and pass the input voltage, but it has a 1.3 volt saturation voltage penalty. Add to that the other 1 volt or so of diodes (one before and one after), and the gate drive ICs turning off at 10 volts, means that operation down to that level or below is undefined.

My intention is to make this board also useful for 12v lead-acid and 3S lithium systems, of which there are still plenty, and hence I needed a way to make sure the board is logically determinate down to maybe 8 volts. The previous board version, as detailed in its post, explored an integrated buck-boost converter, the LT3433.

Unfortunately, while it could have worked in principle, it turned out the chip couldn’t supply enough current to run everything on the board. At around 20 volts, the output began sagging out of spec.

Turning my attention to a more serial arrangement of components, I decided to take the slightly less efficient (in principle) method of chaining a boost converter after a buck converter. In this case, I was going to convert battery voltage directly to 5v logic, and then from there, boost to 15v gate drive. This keeps the regulators (nominally) separate and out of eachothers’ affairs. The chip used was the LT3460, a small dedicated boost converter controller. There would be no more linear regulator, so I had to pile on the capacitors to keep ripple voltage lower and also added a transient voltage suppressor diode to the 5v bus.

That’s where the design stood as of 3 weeks ago, and the corresponding board layout is:

I pretty much followed the recommended layout of the 3460 exactly. This had better work. I sent out this design to MyroPCB, once again, but in 1oz only (since I was just out to test this converter, more or less) and…

in black. I’ve always wanted black boards – they just look so much more badass. As usual, I placed my Digikey order when Myro notified me of the boards shipping, but that seems to have gotten delayed, so my Digikey order appeared a few days before the boards.

The first thing I wanted to test was the power converter, so it was assembled first thing. It’s important to note here that I also bought a same-footprint-and-pinout equivalent of the LT3460, the MIC2288. The alternate chip has a much higher (1.3A) switch current limit than the 3791 at 300mA. This will become important later.

With the power converter testing out good, I finished one board fully and did a load test with a small 5v fan (0.15A) and a spektrum receiver (0.09 to 0.12A, because what?). This was just to make sure the thing could hold up under load. The gate drive, at this point, was not yet enabled (the receiver was in failsafe mode).

After that was done, it was time to drive motors. I remembered that I had Segfault’s old drive motors, which are CIM motors stirring a hydraulic brake a 27:1 Banebots P80 gearbox filled to the brim with thick grease. This thing draw like 15 amps just running freely, and an overvolted CIM can certainly push enough current  to scare most controllers. One of Überclocker’s batteries provided the bus voltage. Even though testing unknown controllers on a battery (a source of potentially infinite amps) is a bad idea, I did it because..

ouch. The sketchy switching lab supply has that as its output voltage waveform. I thought I was chasing down a noise issue on the board when I realized anything the scope probe touched was displaying this waveform or some linearly transformed version of it! Oops. I had forgotten that switching power supplies are usually pretty bad in terms of voltage ripple and noise, cheap ones especially.

It might be worth iterating to other aspiring secret board ninjas that you DON’T EVER USE A SWITCHING POWER SUPPLY TO TEST SENSITIVE LOGIC CIRCUITS. Linear power supplies, though lower in amperage, supply a much cleaner power rail that can be essential for correct operation of circuits, especially analog ones.

Anyways, after switching to the battery, I realized the noise issue was probably something else. The symptom was, like just about every disease that afflicts motor controllers, frequent and unexplained resetting. Maybe it was some weird positive feedback effect between 2 switching regulators? I tried scoping the 5v and 15v supplies to check for their stability, and it seemed okay. What was weirder was that the behavior was only present if the radio was on and the signal was valid. The board wouldn’t exit failsafe mode.

Leading me closer to the culprit was the fact that the behavior was basically the same as hitting the reset button. It took a little while of sitting and watching the 5V rail, but finally I saw a strange transient:

Wow. That’s my 5 volt bus dipping to about 2.8 volts, then flying back up to 6.5 volts. Something weird was happening.

The critical clue was seeing this transient occur whenever the board entered running mode (i.e. left failsafe mode). It also happened immediately after reset, before the program has a chance to load. The transition from running to failsafe mode was smooth, and by pure accident, I left the USB cable plugged in (while trying to mess with the code to see if I could get the reset to occur elsewhere) and noticed this transient did not occur. I could, in fact, “bootstrap” the board with USB power, then after it enters running mode, unplug the USB. And it will be fine.

It was clear to me now that it was caused by something suddenly wanting a ton of power from 5 volts. But what? The microcontroller doesn’t need that much of a load at the start.

The only thing that happens when the board leaves failsafe mode is that the gate drivers are enabled. Could that be the problem? I began to suspect that the sudden current demand of all the gate drivers enabling at once caused the boost converter chip to go crazy. To test this hypothesis, I ‘staggered’ the re-enabling of the gate drivers into 2 groups, separated by 5 milliseconds.

It was definitely the boost converter beating my 5V buck converter into submission now. The fact that there is a transient whenever the gate drivers turned on was the decisive piece of evidence here. What’s funny is, the board worked fine in this state. It seems the sudden pull from the 5V rail is no longer enough (since only half the gate drivers are demanding current) to reset the chip.

The explanation as to why the gate drives suddenly want a burst of current is that the first thing they do when enabled is turn on the low side FETs on all the H-bridges (i.e. feeding a pretty big capacitor in the form of the gates). The bootstrap capacitor for the high side also charges at the same time. A small dip in voltage occurs on the 15 volt bus as a result.

Because the boost converter is much faster than the buck – 1mHz vs. 150kHz, it responds by pushing more current through the inductor (briefly 1.3 amps worth at least, according to the MIC2288’s rating) and it does so quicker than the buck converter can respond. As a result, by the time the buck converter duty cycle catches up, the transient power demand on the 15v bus has already been fulfilled, so the boost converter backs down output current. Again, because the buck converter is slower to respond, it overshoots and then slowly adjusts itself again. That’s how I think it goes anyway.

So basically, I need a precharge circuit or something for my 15v bus. Well, one way to combat capacitor dumping is with more capacitors:

First, I replaced the small 10uF 16v ceramic output capacitor of the boost with a giant 47uF one. On this board, its brethren are buffering the 5V line.

This had very little effect. It seems that the problem is still the boost slurping from the 5V line when it has a sudden demand on the output (necessitating charging up the inductor with a burst of input current). So I moved it to the other side.

This made the transient less in magnitude, but unfortunately it was still causing the ATMega to brown out.

Facing another potential redesign of the board to add even more capacitors or something, I thought about other bad hacks like isolating the 5V power converter from the rest of the logic with a low-ohm resistor, such that even if the converter itself goes under a little, the local capacitors at the ATMega and current sensors, etc. will keep them afloat briefly.

Then I remembered the LT3460 has a 300mA internal switch current limit. The MIC2288 has a 1.3A limit. I began thinking that maybe putting a converter that can briefly eat 1.3A after one which can only source 0.5A (the LM2594) was a bad idea.

The LT3460 has the exact same footprint, exact same feedback resistor and compensator capacitor needs, and exact same output cap and inductor specs. Alright, why not… I’ll swap it in.

That fixed the problem completely. Even with the “staggered start” removed and the extra 47uF of capacitor gone, there were no startup issues. The transient on the 5v rail had declined to the point where trying to detect it was difficult with the scope’s noise floor.

Above is a picture of the 15v rail under switching conditions, when the gate drive is running. As can be seen, all of the FETs being switched on at once causes a brief dip in the power supply (on the order of 0.2 to 0.3 volts). Meh.

I zoomed way in on the 5V supply and AC-coupled the scope to catch the power converter’s actual output waveform. The 1mhz switching freq. of the LT3460 can be seen over the 150khz switching frequency of the LM2594. The 5V ringing is a bit concerning, but it doesn’t seem to be affecting the controller function.

In the interest of reducing this ripple, I’ve spec’d out a 100uF 6.3v ceramic cap  just for the 5V rail. It has the same package as the 47uF on there now, so it will simply drop into place. I’m also going to add more bypass capacitance at the ATMega and current sensors, upping it from 0.1uF to 1uF.

I waterjet-cut the updated heat sink plate design for RB and laser cut my remaining silpad sheet to make the insulator. This is basically what a stock RB will look like, without an additional fan mount or something. I’ve accepted a quote for punching out the heat sink design from 1/8″ 3003 aluminum, with a silpad die-cut piece bonded to it. The future is exciting.

Here’s a picture of all generations of RB so far!

I’m now going to throw this board into something like Null Hypothesis and ride it around drive it to obtain a reasonable robot drivetrain test for temperature and current draw.

In the mean time, I’d like to rally some help from everyone.

Read more “RageBridge Revision 3: Boost Converters and the Market Survey”