Tuesday, January 29, 2013

Hacking a Hexbug

I wasn't totally sure of all the things I wanted my toy to do by the time I finished hacking it, but I knew I wanted fine motor control. Unfortunately, this bug mechanically cannot turn left or right since its wheels are all locked together by their axles, and it only has one motor.
In any case, I should be able to reverse the motor electronically. For that, I needed the H-bridge. It's got six ports: four switch inputs, and two outputs for the motor. I've got the setup as follows:
switch1: controller
switch2: controller
switch3: ground
switch4: power*
output1: motorPower
output2: motorGround

The controller needs only to switch output voltage between switch1 and switch2 in order to change motor direction. That makes my controller program pretty simple.
*I am going to route power from the battery through a transistor directly controlled by my controller. This gives the controller control over all the current that gets to the motor.
So I made a quick test program that ran the motor back and forth.

The green board is the H-bridge I made earlier. Motor output looks strong!
The last part of the video is explained below.

Next, I wanted some input. I wanted to use the original touch sensors from the Hexbug, but when I soldered wires to them, they stopped working. It was frustrating, but I decided to scrap them. I only have about to week to finish this project, so I went straight to a sensor I've already gotten working - the LDR. (light-dependent resistor)
Seeing as insects are pretty sensitive to light, I figured light sensors would be a good fit. I planned for it to have one LDR on each side, so it would run away from wherever it detected a change in light.
My program simply sets a variable "lightLevel" for each sensor upon setup, then checks every 1/10 second whether the sensor reads differently up to some threshold I choose.

int lightLevelAhead = analogRead(frontLightSensor);
int lightLevelBehind = analogRead(backLightSensor);


boolean lightAheadChanged(){
  boolean didChange = (abs(analogRead(frontLightSensor) - lightLevelAhead) > 50);
  lightLevelAhead = analogRead(frontLightSensor);  //reset comparison variable
  return didChange;
}
boolean lightBehindChanged(){
  boolean didChange = (abs(analogRead(backLightSensor) - lightLevelBehind) > 50);
  lightLevelBehind = analogRead(backLightSensor); //reset comparison variable
  return didChange;
}

My threshold is 50 in this case. The lower the threshold, the more sensitive it is.

Using this code, I have the bug run its motors for a second when light changes - in either direction, depending on which LDR detected change.
I also decided to add a piezo sounder and LED for some audio and visual feedback.


I use my finger to cast a shadow over the LDRs.
The piezo just plays a couple notes in the direction it's running.

So far so good, but this won't work at all as a toy if it's stuck on USB power. I needed to figure out how to get the controller chip working without that red launchpad board. Thanks to YouTube, I found out that the chip runs almost exactly as you would expect, with battery power and ground on pins 1 and 20, but it also needs a 10K resistor connecting power to the reset pin.
I could mount the chip on that white breadboard, but that's a pretty big chunk of plastic for such a small bot. And with one weekend left to finish, I wanted to finalize my design. 
Fortunately, my brother happened to have a flat breadboard that I can cut to size. This breadboard requires soldering, so I needed an IC socket to mount the chip on so it can be removed again.
One trip to Radioshack later, I got started transferring my design to a more permanent form. 

That's the chip in the middle, sitting in its IC socket.

 Lookin' good!

I planned to have the H-bridge sandwich itself between the board and the bug, so I left room for the H-bridge wires to plug directly into it through six holes in the board. That also gives the H-bridge some rigidity, so I don't have to worry about it falling down. I thought it was clever.

Nightmare-fuel.

I have a friend who's actually educated in music composition, so I had him over to help me program a song I like into the controller. I'll have it play on setup.
I turn it on and...

The song's great, but there's clearly something wrong with the power, since the program is rebooting when it tries to power the motor. The board also seems to be shutting off randomly during testing.
I spent more time than I care to mention trying to make sure the batteries were making good connection with the board. I even found that my power switch was malfunctioning, so I added another and moved the connection to that one. 
I also added more voltage to the power by using a couple half-height button-cell 1.5V batteries in place of a single 1.5V. That gave it three cells, totaling about 4V (with an already used up button cell).
I was warned about overpowering the motor, but I have a transistor in place to prevent that.
Speaking of the transistor, that low-gain transistor I was using may not have been allowing enough current to the motor, so as suggested by my professor, I replaced it with a high-gain darlington transistor.

"Help! Assistance is required! I believe there is something wrong with me!"

Well, it's better than before, and I'm not getting anymore random shut-offs.
The problem, I suspect, is that the batteries have a current output limit that's just too low to power the controller AND the motor, even with 4V. When the batteries try powering the motor against the weight of the bot, it either just struggles, or reboots.
I'm sure my professor can tell me how to fix this, but I'm out of time, and that concludes my efforts to make this a functional toy.
At least it works fine when I hold it off the floor. I've got something to present!

"The fun levels will be at maximum efficiency!"

No comments:

Post a Comment