It was a brutal fight but I coerced Sparky under the control of his little Basic Stamp 2 brain. As mentioned previously, Sparky's little DC motor is controlled by an IRF510A MOSFET which in turn is controlled by the BS2 which in turn kept resetting right after powering up the motor. As for the 3rd wheel, the HiTec HS303 I had on hand was broken. Or so I thought. I couldn't get it to center properly with the sample code from the Basic Stamp Manual.
Basic Stamp Servo Control
So that you don't struggle with this yourself, here's how to properly control a servo with a Basic Stamp 2. Timing is everything. Servos require pulses separated by 20ms. The pulse length determines the position of the servo.
A 1.5ms pulse should approximately center the servo. To get the right pulsetrain you have to know a bit more about how your Basic Stamp (or other MCU) works. I just copied source code out of the manual. Big mistake. The manual's source was for a slower Basic Stamp 1 so the timing was off by a factor of 5! You want to use the PULSOUT command but on the BS2, each value lasts 2us instead of 10us on the BS1. So here's the source:
PULSOUT 0, 750 ' The servo signal line is sitting on pin 0
Once I figured this out, the HS303 worked beautifully. So did the brand new servo I had just bought thinking the other was broken. Oh well, I will put the new servo to good use. It's a HiTec HS311 and a cheapie at $11.
Basic Stamp Motor Control
With one problem slain it was onto the motor control issue. DPRG had a thread going about driving inductive loads and MCU resets due to brown out detection kicking in when the motor, relay, or other inductive load was shut off, creating a voltage spike as the magnetic field in the coil collapsed. This voltage spike is seen by the MCU as a raised voltage on the ground line which reduces the effective voltage seen by the brown out circuit on the chip causing the reset.
That wasn't what was happening to me. The MCU was resetting when turning the motor on. I am still trying to figure out what was happening but I did find that putting a 100uF capacitor across the Vdd / Vss (regulated 5V) pins on the BS2 solved the problem (temporarily, I later found out). The capacitor decouples the chip from the batteries so if the ground line rises in voltage the capacitor and chip kind of ride the surge. So I understand. I'll update this if I get more insight.
So, with these two problems solved, Sparky is able to control motors and servo and is very, very close to autonomy. Just a question of tying down batteries and boards and he'll go for his first test run and then I can figure out what powertrain to use for the real firefighter.