1.  Arduino:  I used an Uno
2.  7.2v battery
3.  Set of motors:  Parralax's 7.2v motor set  http://www.robotshop.com/parallax-7-2v-motor-bracket-wheel-kit-pair.html
4.  Analog accelerometer http://www.robotshop.com/sfe-3g-tripleaxis-accelerometer-breakout-board-adxl335-2.html
5.  Sabertooth 5X2 motor controller   http://www.robotshop.com/productinfo.aspx?pc=RB-Dim-19&lang=en-US
6.  base board
7.  3/4" threaded rod and nuts for balance
8.  Zip ties
9.  Screws


1.  Drill
2.  3/4" and 1/4" drill bits

Step 1: Instructabot Platform

I used a piece of wood 6" x 12" x 0.5"
<p>it was awesome but the arduino uno is showing errorketch_may27a:21: error: expected unqualified-id before '\x616c'</p><p>sketch_may27a.ino: In function 'void initSabertooth()':</p><p>sketch_may27a:26: error: 'SaberSerial' was not declared in this scope</p><p>sketch_may27a.ino: In function 'void setup()':</p><p>sketch_may27a:29: error: redefinition of 'void setup()'</p><p>sketch_may27a:1: error: 'void setup()' previously defined here</p><p>sketch_may27a.ino: In function 'void set_motor()':</p><p>sketch_may27a:102: error: 'SaberSerial' was not declared in this scope</p><p>sketch_may27a.ino: In function 'void loop()':</p><p>sketch_may27a:107: error: redefinition of 'void loop()'</p><p>sketch_may27a:6: error: 'void loop()' previously defined here</p><p>expected unqualified-id before '\x616c'</p><p>so can you help me for solving it</p>
<p>aesome . but i have got a problem that a have not got the sabertooth motor controller anywhere so can i use l298 p</p>
<p>Awesome. I'm doing segway and trying optimal trajectory problem. I was wondering for your RF control what kind of receiver you used. Or anyone use a specific type of receiver?</p>
<p>is possible with mma7361 or gy-521 acclerometer ?</p>
<p>HELLO i have one question i tried to use your program and i couldn&acute;t do it sorry i&acute;m new in this topic :'( but i would like to do that amaizing robot =D can you help me :( please I&acute;ll be very glad with you for the rest of my life =D</p>
<p>Where is schema of the accellerometer and arduino??</p>
Hi again, Can you tell me the max + and - angle from the vertical you based your code on? I'm still having difficulty in smoothing out the motion. Thanks
<p>Wow....that's awsome segway!</p>
<p>Nice job on this. Which wheels sets are you using?</p>
Your bot looks great. The max my accelerometer measured while on the segway was 400 and the minimum was 270 with 330 about flat
<p>Nice instructable you got there. </p><p>But could you please provide me with little more details about the type of motors you used since the link you provided is no longer valid.</p><p>Thank you.</p>
You could use any brushed small motor and you would just have to adjust the program a little. Hope this helps. <br>
<p>These would probably do the job.</p><p>http://www.parallax.com/product/570-00080</p>
<p>Hi, the project is great but I have a one question.Can I use Accelerometer Gyro module for Arduino (something like 6DOF or MUP-6050) and did you use Serial Controlled Motor Driver Arduino (ROB-09571) or other?</p>
<p>Hello,</p><p> I'm sure your acelerometer/gyro assembly would work fine for this but the program would have to be changed most likely. As for the motor controller I used the Sabertooth 5X2 motor controller, a little pricey but does its job well.</p>
<p>Is there sometype of include file that I need for the saber board?</p><p>I get an error if I just try to upload the sketch?</p>
<p>Update: I got mine working. Turned out that I used a 2x12RC board. The &quot;RC&quot; does not allow serial comm. The nice folks at dimension swapped it out for me &amp; with my minor mod to the code I show below, I have it working. </p>
<p>I should post the error. Also, I just realized that I bought a 2x12RC board. Would that make a difference?</p><p> The following line is highlighted:</p><p>SoftwareSerial SaberSerial = SoftwareSerial (SABER_RX_PIN, SABER_TX_PIN );</p><p>ERROR:</p><p>BalanceBot.ino:4:9: error: #include expects &quot;FILENAME&quot; or &lt;FILENAME&gt;</p><p>BalanceBot:13: error: 'SoftwareSerial' does not name a type</p><p>BalanceBot.ino: In function 'void initSabertooth()':</p><p>BalanceBot:18: error: 'SaberSerial' was not declared in this scope</p><p>BalanceBot.ino: In function 'void set_motor()':</p><p>BalanceBot:94: error: 'SaberSerial' was not declared in this scope</p>
<p>I believe I have figured out the error.</p><p>I changed the the first couple of lines to this: <br>#include &lt;SoftwareSerial.h&gt;<br>int x, r, l, s, f, xa, xb, xc;</p><p>And the errors were gone &amp; code uploaded.</p><p>However, the motors dont seem to be doing anything when I angle the bot foward &amp; backward. Not sure if the changes I made are correct.</p>
<p>I am absolutely new to this. I need the code for the L239D with a Groove 3-axis accelerometer. Could you please help me out asap? </p>
<p>I wonder f,s ? caculate s,f ? thanks you</p>
<p>why i try your code, it happen error #include&lt;filename&gt; , you can help me?</p>
<p>Hi,bro ,i want to know how to change the direction, does it have any switch ?</p>
<p>please expand on ur question. The robot goes both directions to balance and reversing it would make it fall</p>
<p>WOW..sorry ...i got it thanks very much very nich work,,</p>
<p>Hey , Is your machine also move forward and backward in balanced condition, like segway moves ??</p>
Very nice. What you need for stability is a PID control algorithm. You already have the basic idea in your code, but the general solution has been studied for many years. <br> <br>The PID is an acronym for 'Proportional, integral, differential' controller. It goes kind of like this: <br> <br>You measure your angle. Your target angle is '0' degrees, right? So, you take your measured angle and subtract your target angle, this gives you an angular error: <br> <br> e = (a - a0) // a is measured angle, a0 is target angle, e is error <br> <br>So, now you know how far off you are, you need to decide how much energy to put into the wheels. We'll start simple with just the 'P' or 'proportional part: <br> <br>f = k * e; // f is the force to apply to the wheels, e is the error, and k is some constant you make up. <br> <br>If k is too big, it goes unstable. if too small, your control algorithm can't keep up and balance. You determine k empirically by trying different numbers until you get it about right. <br> <br>So... in short you 'P' or proportial control algorithm will look like this: <br> <br>float get_motor_power(float angle, float target_angle, float k) { <br> float error = angle - target_angle; <br> float result = k * error; <br> return result; <br>} <br> <br>That's it! I did it all in float, but you may be able to change to ints for the parameters,but float computations makes things much easier. <br> <br>Once you get your proportional algorithm going, you can easily add the integral and differential parts (slightly more complex) to further improve stability and accuracy. <br> <br>-Caleb <br>
<p>Thanks Caleb, that was very useful piece of information for me. I have few more questions, could help me out, please?-</p><ol><li> Can I calculate constant K with some calculations or the trial and error method is only way?<li>Where does Kalman filter comes into picture, do I also need to write code for Kalman filter considering this application?</ol>
Yes you can calculate k in principle, but you need to know all the equations regarding your system. Easiest to simply go through trial and error.<br><br>Unfortunately, Kalman filtering is beyond my knowledge. It's a filter that's able to work out the equations, even when conditions change. It's an adaptive filter of sorts. More than that though, I can't really help.<br><br>PID is pretty much the most basic kind of filter, but it's all I really understand. The more complicated stuff requires math that's really at the limit (and perhaps beyond the limit) of my math skills. (BTW, I took engineering math for 4 years at college :-/ ) <br><br>Sorry I can't be of more help,<br> -Caleb<br><br>
<p>:-).. no worries Caleb, thanks for taking time out to answer.</p><p>I won't need a Kalman filter; for now I am using Savitzky-Galoy filter. I hope it should do the trick with smoothing Accelerometer values.</p><p>I also found nice theory on PID in Bela G Liptak. </p><p>Cheers! Shreyas</p>
Caleb raises a good point, there is a PID library included with the Ardunio IDE <br>(http://playground.arduino.cc/Code/PIDLibrary), might be worth playing about. <br> <br>For tidier code you might want to refactor (generalise) some of your repetitious code. For example: <br>if ((x &gt; 250) &amp;&amp; (x &lt; 280)) { <br>r = 6; <br>l = 133; <br>} <br> <br>if ((x &gt; 390) &amp;&amp; (x &lt; 410)) { <br>r = 122; <br>l = 250; <br>} <br> <br>these two pieces of code are very similar, you could make a function that performs this conditional statement: <br>void setMotorSpeedBasedOnAngle(int minAngle, int maxAngle, int rightMotorSpeed, int leftMotorSpeed) { <br> if (x &gt; minAngle &amp;&amp; x &lt; maxAngle) <br> r = rightMotorSpeed; <br> l = leftMotorSpeed; <br>} <br> <br>that way you can replace the above two statements respectively as: <br>setMotorSpeedBasedOnAngle(250, 280, 6, 133); <br>setMotorSpeedBasedOnAngle(390, 410, 122, 250); <br> <br>This means should you want to change your conditional code, you only have to change it one place, which is benefit enough to use this style of programming. <br> <br>Cool project, I enjoyed reading about it.
I like how you incorperated the instructables robot
<p>you're welcome! Why did it take you so long to reply?</p>
Can I use L239D motor controller instead of sabertooth ? Will the programming change?
<p>You would have to completely rewrite the code to use those speed controllers.</p>
<p>can i use L293D motor driver circuit.</p>
<p>No sorry only Sabertooth speed controllers will work with the code.</p>
Hi can i do this but using a 2 motor with 1hp each one, and with bigger wheels?? like a real segway??
You could but I'm not sure it would be stable enough to stand on. To do it you would just have to get a bigger speed controller, I believe Sabertooth makes a 25 watt model that would be compatible with my code. <br> <br>Hope this helps
thanks for this instructable
nice project! but what are the other alternative cheap components can i used to this project?
You can use any motors, but you would have to change some settings in the program. As for the speed controller any one would work but the program is designed for that speed controller so you would have to right an entire new program. Any other arduino would work for the project.<br><br>
Nice! Would be better with encoders on your motors. You need speed and position for stability.
can we use any arduino???sorry i am new to this <br>
as far as I know any arduino should work. <br> <br>Hope this helps
Very cool! When I was trying to compile the code I noticed that the #include statement was missing the file name, could you show me where to find the file. thanks
I built a version of your Bot. Electronics function correctly but I can't get it to calm down and behave. Reminds me of a pet I once had. It's motion is very erratic. The motors seem to overcompensate. Is there a sequence I should be following in tuning the code for my particular robot configuration? <br>Thanks <br>Sorry, not having any luck including pics
I'm sorry to say there's no tuning sequence. but here's some tips for getting it to work. <br> <br>1. Make sure that the bot is not heavier on one side than another another. <br>2. In the code I included some notes about what each factor does try playing with them. <br>3. Also if make sure the accelerometer is well secured to your platform. <br> <br>Hope this helps (:
Ready to try your circuit, but noticed that the 0v and 5v from the Sabertooth go to 5v and GND on the Arduino. I think they are shown reversed. Is that correct? <br>Also, what are the Sabertooth dip switch positions? <br>Thanks

About This Instructable


314 favorites


More by florman: Ultimate Sensing Texting Unit How to Wire a Trailer Self Balancing Segway Instructabot
Add instructable to: