Step 41: Code for use with new 6dof DIGITAL IMU from Sparkfun

Step 42: Arduino Shield idea for a self-balancer

Just an idea for an arduino shield. I have just made this for someone. The whole control system assembled in one single lump with IMU and hand-control...

What is it? Twin wheeled skateboard that works like a Segway. Electric skateboards exist already with powered rear wheels. Plan here was to build something like a Segway but in the form of a skateboard. It knows which way is "up" via a combination of gyroscope and and accelerometer sensors, using a complementary (not complimentary) filter which reads and combines data from both 100 times per second. Steering by a simple rocker switch in hand controller (or a rewired Wii-Nunchuck as in photos if you are more ambitious). Upper photo is original budget version with two lead-acid batteries and solid wheels. Lower photo is 2013 not-so-easy-build version, with pneumatic tyres and Headway LiFePO4 batteries just to see how far I could push this overall concept. More on the new one is here: https://sites.google.com/site/onewheeledselfbalancing/Home/18-2013superskate

UPDATE regarding IMU's and CODE (December 2014): This Instructable is a little old now and the IMU used is no longer available. Therefore I have removed the pages that describe the IMU wiring and the code as it is confusing people.

How does it stay level? It controls the wheel motors so the wheels always stay under your centre of balance, like balancing a broomstick on your fingertip. This is called a "PID" control system and is used for all sorts of control situations. Think of the 363 feet high Apollo rockets used in the moon landings.......... Q: How come they didn't just fall over when they took off? They took off incredibly slowly for the first few seconds, tailfins would have no effect, far slower than Shuttle launches. Watch this video - it takes a full 10 seconds just to get to 100m and clear the launch tower:http://www.youtube.com/watch?v=_PEGi3k6yNQ A: They had engines mounted on swivelling mounts hydraulically controlled by a PID control system (lots of analog electronics I think involved too). First stage projected guidance system failure rate was approx. 1 in 256 which was considered an acceptable risk (!)

Background: In 2008 I saw a YouTube video by Ben Smithers of his one-wheeled self balancing skateboard whizzing around a car park in Norwich UK. http://www.robosys.co.uk/ Video: http://www.youtube.com/watch?v=HGbbag9dklU It turns out he was a Lotus cars controls system engineer - which makes sense. Also see Trevor Blackwell's site: http://www.tlb.org/eunicycle.html Meanwhile I wanted to teach myself microcontroller programming and, totally underestimating the task, thought this would be a great fun way to do this. Advantage of two wheelers is that they turn more easily and can turn and balance even when stationary - which is fun. I (used to) prefer skateboards to segway clones as you just jump off if it goes wrong without tangling in the handlebars.

Why an Instructable? Having learned lessons the hard way I thought it would be worth redesigning the project around an Arduino microcontroller, then seeing how low-cost and easy-build I could possibly make it. Something like this is not for the complete Arduino beginner, nor is it that "easy" however this is about as easy as a self-balancing machine is realistically ever going to get.

Skills: Projects like this lend themselves to being built as a team. Some examples below were built as college projects. There are i) some electronics (not making circuit boards, just wiring and soldering) to master, ii) some mechanical fabrication; this version is designed to require no welding, just nuts bolts and some woodwork. Wheel/sprockets/axles/bearings come as a unit (electric scooter rear wheel assemblies). iii) some programming; the programs (Arduino sketches) you need, including those to help debugging, are attached (P43 - 47). There are; IMU tester, motor tester, balances-nothing-else, rocker switch steered and potentiometer steered code examples.

How much does it cost? I realised when costing them up that the cost of a self balancing robot would only be a little lower than that of a ride on machine, therefore I went for a ride-on machine! For me the cost was about $300 equivalent PLUS whatever batteries you choose to use. I recommend starting with lead-acid batteries then make improvements later once you have a working machine.

Why do it? i) For the challenge of doing something original. Segway skateboards have been invented in principle BUT there is huge room for improvement. ii) Making something that is practical and intuitive to ride is quite a challenge in terms of both electronics/software and fabrication/packaging. Cannot all be done on a computer. Eventually you have to actually build something then incrementally improve it. Despite the myth of "Eureka" moments, the truth is that this is how most innovations come about, by slow incremental development and hard work. Edison did not invent the lightbulb. He developed the first practical lightbulb. iii) To educate yourself, you might become a world expert, there are no textbooks so you are genuinely pushing into the unknown with each machine you make. Few things as an individual hobbyist allow you to truly do this. iv) These machines are really good fun to ride!

This instructable: There are a large number of pages in this. This is deliberate, if you are serious about building one then you need every single step documented.

Can I do this as a beginnner? The fabrication has been deliberately kept really simple. The soldering between sensors and arduino board needs to be good quality! If you are new to Arduino I would recommend buying an Arduino starter kit. These come with some ancillary sensors etc. and a set of about 12 tutorials. Work through them all (about 2 days work) and read a beginners book to Arduino. You will then be ready.

Potential areas of improvement It would be cool if people took this design and improved upon it. The only way I can envisage further improvements in terms of weight reduction and compactness on my 2013 design with Headway cells and pneumatic tyres would be as follows: a) Make frame from welded alloy. b) The cheap scooter motors are quite heavy so use equally powerful but lighter, smaller combat robot motors. c) To reduce length and make even more compact, someone, say a mech eng student, could design a neat hub motor for each wheel using epicyclic reduction gears.

NOTE: You build these at your own risk. If tilted they WILL accelerate to correct the tilt. If you are not on the board, this means it can fly across a room or into your head. This is why you have to have an emergency hand switch that cuts the power if you let go of it. If it develops a fault it does not have multiple redundant systems like a real segway, most likely you will fall off! The code is not guaranteed against any bugs. If you don't believe me here is a video of Clint Rutkas developing a similar machine, also featuring some holes it punched in the walls of his apartment! http://vimeo.com/2013773

Have fun. Treat it as an adventure. Once you get it to balance there are many ways to improve it.

Just pointing out that the IMU above is a digital output one that does not work with the code in this instructable that was written to work with analog (variable voltage) output IMU's. These are becoming quite hard to find however.

This is why I have had to recently rewrite the code and add a new related instructable that shows you how to make a balance control system using a digital IMU.

Oh, i already ordered this IMU, i even started the project. I am making more like a onewheel-frame. i got all the aluminum parts and cut them up to shape, i picked the aluminum frame up from the welder today, i am using a small 24 volt DC motor. i got 4 small 7 ah 6 volt batteries and a velleman P8004 motor controller. I dont know if it works with my arduino UNO yet but its what i have (i am using a cigarette lighter 24-5 volt adapter as a power supply for my arduino, they're really cheap and function well) , otherwise i will buy an arduino motor controller like the sabertooth. Also, i am planning to add a light sensor as a dead-man switch, that way when somebody steps off the board it stops, i dont need a steering switch etc so i think i am just gonna remove those lines of code. i added pictures (the pics of the frame have low quality because my camera sucks at low-lighting). maybe i am going to upload videos someday. Thanks for the new code, helps a lot!

Sorry, i dont know why but my English is sort of broken today, usually it's better. :/ (i am dutch).

Looks good. I have no idea if the digital IMU you have bought will work with my new digital IMU code as I have only tested it with the 6dof digital IMU from Sparkfun. I am sure you will find out soon enough!

I have not done an update in a long time (i think about 2 months). I have been working on it but i ran into a lot of problems and i didn't quite get the time to fix them because school is busy lately. No time for excuses tough. I am gonna do an update once it is finished, nothing in between. I will make a video of me riding it and showing the inside lay out etc. I will post it right here. See you then!

Without your advice I am thinking it is like a solar cell and if you block it with your foot and so your logic is drive the motor when the sensor is not generating or sending any voltage (if solar cell). But problem in my mind is if you put some dirt from your shoe on the sensor then it stay in drive mode when you are off it?

Such that I am interested to learn what you think about the logic for this process. thanku

Another option is the Sharp infra red rangefinder available from robot sites. It fires an infrared beam at an angle and looks at the reflected light. If you have it facing upwards and you are on the board, it will see light reflected from your foot. If not on the board it will not. You would have to recess it about 3cm into the board (for the shortest range one they make).

Another option is a cheap pressure sensitive resistor mounted under a rubber disc. Look how its resistance changes, set up a voltage divider with another resistor, feed the changing voltage into an analog input and with some adjustment of the limits in the code it will work.

You can also use a rugged metal push to make foot switch that you stand on, - recess it into the deck. These exist as foot switches to trigger alarms by shop cashiers for example.

You have a very good point, but i think that my shoes don't get dirty enough to completely block the light sensor, i live in a city with mainly pavements, i dont think dirt will be a problem for this idea. (i included a pic of the light sensor)

(general update)

I also just got the IMU in the mail! It has the right voltage and the pins have the same names (i included a pic of the soldering so far), i hope it works, if it doesn't it will be hard to determine what doesnt work, its either the IMU not working with the code or it's the motor controller not working with arduino. i still need to find the 2 100 Ohm resistors. I really hope it just works, that way it stays simple.

My current thought is a two wheel unit with a pressure sensitive system. No pressure applied is signal to cut out the motor, then I'd like to be able to bias left and right side pressure to bias drive to one motor in order to steer. Probably there are some floors with this concept.

further, I am considering using the hub motors (8 or 9 inch), in order to reduce mass. Do you know if sabertooth motor driver will provide the power and control through to these motors....? I will chase up some specs if necessary.

The Sabertooth only works with brushed motors. There are some small brushed hub motors out there, but in general, hub motors tend to be brushless nowadays. I have built a machine like a segway with brushless hub motors and a very expensive Roboteq Brushless motor controller but that is another story. It can be done but it was expensive and it would not go up any kind of slope (lack of torque).

Also, hub motors do not generate as much torque (twisting force) as a regular wheel with a sprocket chain or toothed belt drive from a motor, i.e. small sprocket on motor and big one on wheel). With self-balancers a generous amount of torque is very useful to have, rather than a high top speed.

so what I can determine is that I can get a hub motor (9inch) to give 12N.m of Torque which would allow drive up a slope incline of 0.054 (on a 0 to 1 scale), I guess if I had two of these it would double the Torque capacity and allow for slope incline of 0.108 (all based on a 100kg loading). This suggests capability to climb a 9 degree slope best case.

So now I am interested to find what your design rates in terms of Torque generated through the motor and sprockets. Maybe your design can carry a 100kg load on a 20 degree slope!?

Seems that the 6 inch motors are reversible, there is a 9 inch version that has variable speed options, however i have a feeling that a control circuit should be able to provide such convenience with any of these hub motor selections (??)

thanku for the feedback on the hub motors. It is unfortunate that there is a problem with the torque. I assumed that because they are advertised for scooters and wheelchairs that torque would not be lacking.

Suppose I wanted to build one of these... but I wanted to add a third caster wheel and not worry about the whole self-balancing thing. I just want something I can stand on, be self-propelled, and be able to turn it by some really simple means.. What all electronics could I eliminate? Just the IMU's? Can anybody point me to maybe a SIMPLER Instructable that might show me such a thing?

I am building a regular electric skateboard with four wheels that tilts forward for acceleration. I am using a 2G accelerometer to measure the forward tilt but it does not go full power unless I tilt the board to almost a 90 degree angle. Should I use a higher G accelerometer like the ones capable of measuring up to 250G so that it can go full power at less than a 45 degree angle? Or should I use a gyroscope sensor instead? I am just using analog accelerometer that outputs 0-5 volts to the generic speed controller as I do not know how to program. Any help will be much appreciated. Thanks!

Thanks John. The lowest and readily-available I could find is 1.5G. I am also using Dimension Engineering controllers and they told me that I can use their DEScribe software to modify its voltage range. I am checking on this right now. Great instructable BTW.

I would use a microcontroller to look at voltage from accelerometer and send correct voltage to the motor controller, altering the performance by tweaking the code.

You could alternatively have an electronic circuit design that outputs double the voltage coming in from the accelerometer. Accelerometers are also very sensitive to vibration so the signal to your motor power controller when at less than full speed will have higher voltage spikes in it especially on rough ground that may make it behave oddly. You could also use a capacitor to smooth out these spikes.