Step 7: And now, the hard part.

Anyone can strap wheels to a stick, right? The magic behind a self-balancing thing is in the electronics. The controller must read in accelerometer and gyro data, calculate the angle of the stick, and command the motors to take corrective action, keeping the rider upright. This process of sensing a physical variable (angle), making a decision, and executing a corrective action that will affect that variable is called feedback control

Usually, the controller is some kind of microprocessor. In some rare cases, one might be compelled for unknown reasons to do it entirely with analog circuitry. Segstick instead uses a more conventional digital controller implemented on everybody's favorite: the Arduino. Specifically, it uses the Arduino Nano 3.0 from Gravitech. But any Arduino will do.

For sensing, Segstick uses the Sparkfun Razor 6DOF IMU. Really, you only need one accelerometer axis and one gyro axis, as will be discussed in the next step, but this package is so convenient and allows you to mount the board any way you want. Since it uses all 3.3V sensors, it will need to be powered from the Arduino's 3.3V output. Each sensor outputs an analog voltage which is read in by the Arduino's analog to digital converter (ADC). More on that in the next step.

For controlling the motors, Segstick uses two Polulu High-Power 18v25 motor drivers. These are tiny but powerful reversible motor controllers capable of providing up to 25A each.  I decided to add a heat sink to them, but they seem to run cool enough without it. They do require large-gauge wiring to the battery and to the drill motor leads.

The circuit schematic is shown below. How you implement it (breadboard, protoboard, printed circuit board) is up to you. I did it on a vector board that I later cut to size. Soldered connections will generally be more reliable than breadboard-type connections on a moving vehicle, so I'd recommend using sockets for the components but directly soldering wired connections.



 
Remove these adsRemove these ads by Signing Up
prabhatnarayan says: Mar 19, 2012. 1:10 PM
hi scolton.
here i got 2 18x25 pololu motor driver but the pins are
TIXN
ERR
RST
TX
RX
VIN
GND

instead of
V+
5V
FF2
FF1
RESET
PWM
DIR
as shown in your 18x25 motor driver

so plz tell me what r the PWM & DIR pin in my motor driver.
scolton (author) in reply to prabhatnarayanMay 29, 2012. 10:08 AM
Seems like you got the 18v25 Motor Controller instead of the 18v25 Motor Driver. Pololu separates "drivers" which have no on-board logic from "controllers" which have an on-board microcontroller. Drivers take a PWM and direction input and directly send it to the motor. Controllers need a different kind of interface.

On the controller, you should be able to use the RC interface (pins labeled RC1 and RC2) and the Arduino servo library to command the motors.
irethedo says: Dec 29, 2011. 4:26 PM
Where did you get the heat sinks for the Polulu High-Power 18v25 motor drivers and how did you attach them?

A picture would be nice...

thanks
scolton (author) in reply to irethedoJan 1, 2012. 10:56 PM
They came from a scrap bin, but any old heat sink should work. A processor heat sink might be the right size (but overkill).

To attach, I used screws from the board into the heat sink. I put a couple of layers of Kapton tape between the board and the sink to insulate it.
irethedo in reply to scoltonJan 4, 2012. 5:07 AM
Thanks Shane-

after a little bit of thought, I am thinking of taking the fan off an old PC CPU heat-sink and just suspending it with 4 standoffs above both Pololu controllers but without a heat sink or mechanical contact point. This should be better than nothing I hope...
irethedo says: Dec 29, 2011. 10:47 PM
Another question for you:

Your schematic shows A0 - A5 of the arduino connected to the Razor 6D0F but your code only uses A0 for the accelerometer input and A4 for the gyro input.

Is it necessary to connect the other Razor 6D0F outputs (A1 - A3 & A5) to the arduino and if so why?

(I might want to use those analog inputs for other things...)

thanks
scolton (author) in reply to irethedoJan 1, 2012. 10:58 PM
No, it's not necessary to connect the other four IMU outputs to the Arduino. I connected them since they were convenient and because I didn't know which way I was going to mount the board. But you only need one accelerometer axis and one rate axis.
theugleymonkey says: Jan 1, 2011. 10:15 AM
Do you think implementing the ArduIMU +v2 would work? How much of the software and configurations do you think would need to be changed?
http://www.sparkfun.com/products/9956
scolton (author) in reply to theugleymonkeyJan 1, 2011. 10:56 AM
Oh, also, MOSI and MISO correspond to D11 and D12, the DIR outputs.
scolton (author) in reply to theugleymonkeyJan 1, 2011. 10:50 AM
Definitely. In fact, if I were starting from scratch that's probably how I would do it. (Less wiring.) The changes would be minimal:

You have access to the two PWM driving pins, labeled PWM0 and PWM1 on the ArduIMU. These are the same as D9 and D10 on the regular Arduino. So no changes there.

The analog pin definitions change a little, but nothing you can't figure out from the schematic. You'd have to use the SDA or SCL pin as the steering pot analog input. (You'll see why on the schematic.)
theugleymonkey in reply to scoltonJan 5, 2011. 8:32 PM
So you say for the steering pot to use SDA or SCL. Why is this? Is this because I can use it as ADC 4 or 5?
scolton (author) in reply to theugleymonkeyJan 5, 2011. 10:06 PM
Yep.
theugleymonkey in reply to scoltonApr 17, 2011. 7:55 AM
So I finally obtained my ArduIMU and I am testing it out, I basically hooked it all up to one motor controller and a digital multimeter for them motor output. My question is, should I be seeing a visible change in voltage when I tilt my device? I have only hooked up a 9 volt battery for testing. Is there a minimum voltage I should be using to see results?
scolton (author) in reply to theugleymonkeyApr 17, 2011. 4:39 PM
You should be able to see the motor output voltage (measured from OUTA to OUTB) ramp from -9V to 9V as you tilt forward and backward. The Pololu drivers can work down to 5.5.
theugleymonkey in reply to scoltonApr 17, 2011. 5:00 PM
Strange, it is staying at a constant voltage. Could it be that the program needs to be modified for my board?
scolton (author) in reply to theugleymonkeyApr 17, 2011. 5:16 PM
What voltage? Is it stuck at 9V?
theugleymonkey in reply to scoltonApr 17, 2011. 5:22 PM
Yup it stays at a constant 9V tilted or not.
scolton (author) in reply to theugleymonkeyApr 17, 2011. 7:13 PM
Okay so I'd say take a step back and use the Serial.println() to check that the PWM value being sent to the motor controller is varying.

Serial.println(output_right);
or
Serial.println(output_left);

depending on which motor controller you are debugging. If that is showing a value that varies as you tilt the platform, then maybe it's a wiring problem between the Arduino and the motor controller. If not, it might be a software problem.
theugleymonkey in reply to scoltonApr 17, 2011. 7:33 PM
I debugged left and right and on the right it varies a little but then jumps back to -133 no matter what angle and on left I am getting the value -255 no matter what angle
scolton (author) in reply to theugleymonkeyApr 17, 2011. 9:25 PM
Not sure then..the only thing I can think of is to go back up a few lines at a time and debug some of the intermediate values to see if they are working properly. Once you figure out where the weirdness starts it'll be easier to pick apart the code and find the problem.
theugleymonkey in reply to scoltonApr 18, 2011. 5:30 AM
Okay well I debugged most the code and it seems right when I debug
//Add/subtract steering and integerize. as well as // Clip as integer. The weirdness starts it gives me similar results as above but for both left and right they varied for half a sec and then go back to their original starting value.
scolton (author) in reply to theugleymonkeyApr 18, 2011. 11:11 AM
So, if you check

Serial.println(output_left);
or
Serial.println(output_right);

right after the "//Clip as integer block" of code, you get values that vary as you tilt it, and continue to vary.

But if you debug those values right before they are sent to the motors you get values that vary for only half a second?
theugleymonkey in reply to scoltonApr 18, 2011. 11:27 AM
If I debug with Serial.println(output_left); or Serial.println(output_right);
// Add/subtract steering and integerize. as well as // Clip as integer. both give me same results the values varies at different angles but shoot right back to its original value no matter what angle. If I debug the raw data like Serial.println(gyro_raw); Then the values vary at different angles and stay that way depending on the tilt.
scolton (author) in reply to theugleymonkeyApr 18, 2011. 12:10 PM
How about the filtered angle and rate variables? Do these look to be correct?

What if you try tiling to extreme angle? Maybe the zeros are not calibrated and it thinks horizontal is at like 45 degrees or something.
theugleymonkey in reply to scoltonApr 18, 2011. 12:31 PM
Okay well for angle it shows about 27 when flat sitting on the table but if I flip it vertical in one direction it shows 0 and in the opposite vertical direction shows about 45. for rate sitting flat it is around 360 when I tilt it depending on its angle goes from 100 to 300
scolton (author) in reply to theugleymonkeyApr 18, 2011. 1:29 PM
Ok so it sounds like the sensor values are the proble, not the motor outputs. I'd first make sure the wiring's all good, then play around with the zeros, especially on the rate sensor, to get it to read zero when stationary and horizontal.
theugleymonkey in reply to scoltonApr 21, 2011. 3:46 PM
Okay I think I got everything working now thanks for your help!!!
scolton (author) in reply to theugleymonkeyApr 21, 2011. 7:44 PM
Sweet! Lemme know if it balances!
Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!