3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

Seg...stick.

Seg...stick.
Do-it-yourself self-balancing...things...have been around almost as long as commercial self-balancing things. Obviously the homemade versions are not as smooth, reliable, or failsafe as the real thing, but they are still pretty captivating. And they make great mechanical/electrical builds with some interesting control theory mixed in. In the final step, I provide a few references to good DIY self-balancing...whatever...builds.

In 2007, I helped with this other self-balancing scooter build at the MIT Edgerton Center, and since then we've gotten many interesting questions by email about how it works. Baseline self-balancing functionality is actually surprisingly simple, and maybe the purpose of this Instructable is to take this simplicity to the extreme. To that end, I present: Seg...stick.

Segstick is a self-balancing...well, literally some kind of broomstick I found in the MITERS workshop. It is powered directly by two DeWalt cordless drills chucked to two 6" wheels. The controller is an Arduino. Additional supporting devices include an Inertial Measurement Unit (IMU) from Sparkfun and two motor drivers from Pololu.

Is it the best DIY self-balancing vehicle ever? No, not even close. But it only took about two days to build, and it is stripped down to the bare necessities. Thus, I hope to point out the modules and concepts involved in making any self-balancing vehicle rather than the specifics of this one. To start with, some physics...
 
Remove these adsRemove these ads by Signing Up
 

Step 1Physics says it's easier to build a full-size self-balancing thing.

Physics says it\
One question we get a lot is: Can this work on some kind of miniature self-balancing robot? Yes, but, the laws of physics make it harder to control a small inverted pendulum bot than a full-sized rideable self-balancing vehicle. 

For one, the mechanical time constant of a small self-balancing robot is faster. Imagine the difference between trying to balance a broomstick on your finger and trying to balance a pencil on your finger. The controller for a small robot has to be that much faster to keep up with the physical system.

Additionally, a human rider takes some of the burden off the electronic controller, since the human mind is a pretty good controller too. For example, the accelerometers used on self-balancing platforms can't distinguish between standing still and moving at a constant velocity, but a human rider can. The human rider can adjust by leaning forwards or backwards to speed up or slow down.

So, this Instructable focuses on a full-size vehicle, albeit a relatively small one. In the final step, there are some links to balancing robots.
« Previous StepDownload PDFView All StepsNext Step »
118 comments
1-40 of 118next »
Mar 19, 2012. 1:10 PMprabhatnarayan says:
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.
Feb 10, 2012. 12:22 AMludina says:
answer, please! what is the function of steering pot? How it affects on the controlling of ballancing robot?
Dec 29, 2011. 4:26 PMirethedo says:
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
Jan 4, 2012. 5:07 AMirethedo says:
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...
Dec 29, 2011. 10:47 PMirethedo says:
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
Dec 15, 2011. 12:29 AMaEx155 says:
Hey Shane,

I'm working on making a small robot balance, you can see my first attempt in this blog post:
http://makerandomstuff.blogspot.com/2011/12/introduction-fail-way-crappy-balancing.html
I've tweaked the platform a bit to make things neater but I'm still having trouble balancing.

The sensors from the Wii Motion Plus (gyros) and Wii Nunchuck (accelerometers) are outputting data over I2C and I'm filtering it to get g_roll, a_roll, and a filtered angle, so I think that part's good.

When I try to have my robot balance, it either oscillates a lot, or responds too slowly. I think the problem lies in my Kp/Kd values, my motor responsiveness, or my robot's small size (or a combination of 2 or more).

Any suggestions?
Dec 18, 2011. 7:30 PMaEx155 says:
Drifting problems aside, I still haven't been able to get it to balance like some of the other robot segways I've seen. Staying upright for at least 30 seconds is doable, right?

I've gotten my hands on a cheap nunchuck clone so I'll be updating the sensor system soon, and maybe making things a bit more solid.

I also have some motors with quadrature encoders built in (9v LEGO NXT motors) so maybe I'll go somewhere with that. Is there any chance I could take a look at the code you used for your mini-segstick? Maybe I can integrate encoders with that strategy.
Oct 13, 2011. 9:06 PMmarquinho-a says:
Hello Scolton,

I still can not stabilize my Segway. Also, I had a problem with my IMU and the gyro stopped working, so I had to use another one. The gyro I had at hand is an Epson XV-3500 (http://www.epson-electronics.de/upload/PresidioIndustries/product/QuartzCrystalDevices/Sensors/GyroSensor/XV-3500CB_710020500_data.pdf) whose sensitivity is 0.67 mV / deg / sec. My accelerometer is the ADXL335. I'm supplying 3.3V.

The calculation bellow is correct?

Accelerometer (ADXL-335):
(57.3 ° / 0.3V) * (5V/1024LSB) = 0.932 ° / LSB

Gyroscope (XV-3500):
[(1 / s) / 0.00067V] * (5V/1024LSB) = 7.287 (° / s) / LSB (?????)

The value for the gyroscope seems very high and the Segway completly unstable.

Kind regards,
Marcos
Oct 18, 2011. 3:02 PMmarquinho-a says:
Scolton, first of all thanks for your paper. I think everyone should read, it's really enlightening.

I think not because this project (http://diysegway.blogspot.com/) uses the same sensor and works very well (really stable). I would like to test the Trevor algoritm. The angle and the angle_rate are the tilt angle of the scooter in radians and its derivative in radians/sec. Is it the same that are in your code (rate and angle)? So, can I use the bellow code together your code? Are in same unit system?

Thanks for your help, I'm learning a lot!

balance_torque = 5.0 * (angle - rest_angle) + 0.4 * angle_rate;

overspeed = max(0, cur_speed - 0.5) ;
if (overspeed > 0) {
overspeed_integral = min(0.4, overspeed_integral + min(0.2, overspeed+0.05) * dt) ;
}
else {
overspeed_integral = max(0, overspeed_integral - 0.04*dt);
}
rest_angle = 0.4*overspeed + 0.7*overspeed_integral;

steer_cmd = 0.07/(0.3+abs(cur_speed)) * steer_knob;
cur_speed += 1.2 * balance_torque * dt;
left_motor_pwm = balance_torque + cur_speed + steer_cmd;
right_motor_pwm = balance_torque + cur_speed - steer_cmd;
Oct 16, 2011. 12:09 PMjapper says:
I have been having problems making mine stable also but just stumbled across something yesterday in another tutorial about building an arduino segway that I plan to investigate.

In that application, they are using a sparkfun razor gyro & accelerometer which is connected to the 3.3v just as it is in Scolton's instructible but what is different is they tie the 3.3v line on the arduino to the Aref pin an then use the following command in the setup section of the code:

analogreference(EXTERNAL);

Here is the explanation:
the normal range for the 10 bit ADC (pins A0-A5) is 0 - 1023, where 0vDC input yields a value of 0 and 5vDC yields a value of 1023. If you plug a 3.3v sensor into an arduino analog input, the maximum value that an arduino can read is approximately 675 or so. (3.3v / 5v *1023), to get full range using a 3.3v device, you must connect the desied reference volage (3,3v) to the analog reference pin (aref) an add a line of code to the end of the setup() function to tell the arduino to use an external analog reference voltage.


I haven't tried it yet but this might smotthen things out as mine has been vey sporatic...

If you get a chance to try this before I do, please report back waht you find.

thanks
Oct 16, 2011. 1:29 PMmarquinho-a says:
What are you talking to me sounds correct, but I think it is not the solution to problems. I think I've seen most of the tutorials found on the internet and I found more stable until today was one of the first to be created: the Trevor's algoritm. He did several tests, both in high and low speed. I'm thinking of rewriting the code from it to a version compatible with the Arduino, but it will not be easy. The code it is large and somewhat complex. Would you be willing to help?

My MSN is marquinho-a [at] hotmail [dot] com, and my Gtalk is marcospassos.com [at] gmail [dot] com. You can add me and talk more to share experiences.

Kind regards,
Marcos
Sep 21, 2011. 5:11 AMmarquinho-a says:
Hello Scolton,

I'm using a Sparkfun 5 DOF, OSMC controller and two 450w 24v motors (MY1018z). Although my Segway can balance itself, it can't balance a person (seems to have no force, slowly). I've noticed my Gyro (IDG-500) has
2 mV/°/s of sensivity, so my LSB is [(1º/s)/0.002V]*(5V/1024LSB) = 2.4140625(º/s)/LSB, right? Anyway, it can't balance as well.

I also have noticed that over time the platform starts to get steep and considers it as level.

Do you have any tip?

Thanks,
Marcos
Sep 25, 2011. 9:19 PMmarquinho-a says:
About the LSB, is correct? Scolton, can you provide or explain how we can move forward and backward programatically? I think that is increase or decrease the zero angle but I don't know how to do it right. Another question is about the best place to put the IMU sensor. Can you tell me it?
Sep 27, 2011. 12:40 PMmarquinho-a says:
Ok about the position of sensor. Yes, with no rider. Do you have any experience about it? I was talking about it with Trevor and he has said it have no secret, it only change the target angle, but in this case the target angle isn't explicit in the algorithm to be changed. I believe that in your algorithm, the target angle is fixed as zero level, and the PID try always keep as 0, right? So, how can it work in this case?
Sep 25, 2011. 10:35 AMjaysettle says:
I have the same setup and have a problem with that too. Increasing KP and KD helped. I have KP at .90 and KD at 1.00! It helps a little but it's still just not fast enough-the vehicle doesn't get under you fast enough.
Sep 25, 2011. 9:16 PMmarquinho-a says:
About the LSB, is correct? Scolton, can you provide or explain how we can move forward and backward programatically? I think that is increase or decrease the zero angle but I don't know how to do it right.
Sep 9, 2011. 1:57 PMjaysettle says:
The ATmega1280 chip has 16-bit resolution on the pins I'm outputting my PWM signals on to the gyro. I noticed you have 10-bit res in your equation:

[(1º/s)/0.00333V]*(5V/1024LSB) = 1.466(º/s)/LSB

I know that my LSB variable would be different but what should it be with 16-bit res and is it even worth it or does it matter?
My DIY segway for some reason runs slower using my ATmega1280(mega) than it did using the ATmega328(Duelmanove)in terms of corrections for it's angle, and corrections for it's angular rate.
I'm adjusting KP and KD in realtime with pots and it seems to help a little but it just isn't like it used to be. I also now have a quick dead spot right when the motors change direction. Not sure if these two symptoms are related but any suggestions would help. Thanks for all your work.
Sep 12, 2011. 9:13 AMjaysettle says:
Ok yea I got it figured out. It's just that segway already is noticeably slower in correcting itself using my ATmega1280(mega) than it was using the ATmega328(Duelmanove). A significantly lower gain on both the gyro and accelerometer would water down the values, slowing down reaction time even more, but hey maybe not.
I figured out the dead spot as I am using OSMC motor controllers which require four inputs and one of mine was off.
Unfortunately I fried the right osmc last night but will try the LSB change when I re-solder some new transistors on the motor controller.

http://web.me.com/jaysettle/Jay/diy_segway.html
Aug 22, 2011. 6:30 PMmarquinho-a says:

Hi Scolton, congratulation for the good job! I'm using a Sparkfun 5DOF IMU (http://www.sparkfun.com/products/9268) and I'm trying get working but the out of output_right or output_left they always are increasing to 255 even when is stabilized horizontally. Do you have any idea what can be?

I have observed the IMU values and this one never stabilizes. The acc_x, for example, is always a random value between 310 and 315. So, even if I have adjusted the a_zero the value of acc_x oscillates.

output_left:
0, -1, -2, -2, -3, -3, -4, -5, -6, -6, -7, -8, -9, -10, -11, -12, -13, -13, -14, -15, -16, -16, -17, -18, -18, -19, -20, -21, -22, -23, -24, -25, -25, -26, -27, -27, -28, -29, -29, -30, -31, -32, -33, -34, -35, -36, -37, -37, -38, -39, -40, -41, -41, -42, -43, -44, -45, -46, -47, -48, -50, -51, -51, -52, -53, -54, -55, -55, -56, -57, -59, -60, -61, -63, -64, -65, -67, -68, -69, -70, -72, -73, -74, -76, -77, -79, -80, -81, -83, -84, -85, -87, -88, -90, -91, -93, -94, -95, -96, -98, -99, -101, -103, -104, -105, -107, -108, -108, -110, -111, -112, -113, -114, -115, -117, -118, -119, -121, -122, -123, -125, -126, -127, -129, -130, -132, -133, -135, -136, -138, -139, -140, -142, -143, -144, -146, -147, -149, -150, -152, -153, -154, -155, -157, -159, -159, -161, -162, -163, -164, -165, -166, -167, -168, -170, -171, -172, -173, -174, -175, -176, -177, -178, -179, -180, -181, -183, -184, -185, -187, -188, -189, -191, -192, -193, -195, -196, -198, -199, -200, -201, -202, -204, -205, -206, -207, -208, -208, -209, -210, -211, -211, -212, -213, -214, -215, -216, -217, -218, -219, -220, -221, -222, -223, -224, -224, -226, -227, -228, -229, -230, -231, -232, -233, -234, -235, -236, -237, -238, -239, -240, -241, -242, -243, -244, -245, -246, -247, -248, -248, -249, -250, -251, -252, -253, -254, -254, -255, -255, -255, -255, -255, -255, -255, -255, -255, -255, -255, -255, -255, -255, -255, -255, -255, -255, -255, -255, -255, -255, -255, -255, -255, -255, -255, -255, -255, -255, -255, -255, -255, -255, -255, -255, -255, -255, -255, -255, -255, -255, -255, -255, -255, -255, -255...
Aug 23, 2011. 6:24 PMmarquinho-a says:
Thanks a lot, Scolton! I'll try it tomorrow and return you if I get working. About the potentiometer, what is the range supported? I have not found in the tutorial.

Regards,
Marcos
Aug 14, 2011. 9:28 AMdreadengineer says:
This is a refreshingly clear explanation of both the angle sensing and the control system. Thanks!
Aug 9, 2011. 7:51 PMteamtestbot says:
Because I want to build your own Segway, I want to do this very project. But I only have a gyroscope, it is sufficient? It is to use any type of motor you have? I do not have a set of wheels without a motor wheelchair. I have found a coconut from fishing vessels that can be adapted in order to understand how to use the Segway. What will i do? Slope of the Segway, or I, I, I do write software or programs? I
Apr 27, 2011. 3:03 AMirethedo says:
I have read somewhere that to minimize gyro drift, that it is best to mount the gyro centered over where the wheel axles are... (the pivot point of the platform) 

But wouldn't mounting the gyro high on the handlebars (even though not centered over the platform's pivot point) make for a more stable balancing platform due to the slight variations in angle at that position?
May 4, 2011. 7:33 PMirethedo says:
I have my prototype together but find it is difficult to get on as it tilts and over reacts...

Should it be able to almost balance itself without the power on?

May 7, 2011. 3:49 AMirethedo says:
I meant with the power off and no one on it, should it be balanced enough to almost balance due to its own weight distribution...

It is a little un-nerving to step onto it when it wants to bolt in one direction or the other...part of this may be due to my wheels are set a bit forward and not centered on the platform. This sets the angle to tilt backwards when someone stands on it and it wants to bolt in reverse like asap...

I did put a "kill switch" button on it that sets the motor controllers to neutral but found that with the Proportion Derivative algorithm that you set up in your code, that even though the motors are held at neutral, the algorithm continues driving the value if the platform is not level.

Needless to say, it leaped like a tiger when I released the kill switch.
To counteract this, I set the angle to zero also so that it doesn't leap when the kill switch is released.

Not sure if there are any other things other than the KP & KD adjustments to smooth this out but I wired in some potentiometers for these values so that I can tweak it without having to keep hooking up the laptop to reprogram...

Once I find these values I will hard code them. What values for KP & KD did you finally end up with on your segstick?
1-40 of 118next »

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
19
Followers
1
Author:scolton