# Self Balancing Robot Using Neural Net

This is a very simple application of a neural net solution for controlling a two wheeled self balancing robot. The robot does not do anything more than self balance; no control for directional control. But the project was simply to see if I could figure out how to use a neural net rather than a PID controller. Currently, I have a very primitive understanding of neural nets and I understand the basic concepts but I believe a lot more sophistication could be utilized in applying an ANN (Artificial Neural Network) to a self balancing robot. I hope there will be some comments by persons in the know about NNs with some ideas that could be applied here. I adapted Sean Hodgins neural net code and you can find more specifics on how it works on his instructable here: https://www.instructables.com/id/Arduino-Neural-Net... I adapted midhun_s excellent code for balancing my robot: https://www.instructables.com/id/Arduino-Self-Bala... His code is some of the simplest and most easily implemented code that I have found for the Arduino for self balancing. However, I modified his code to get rid of the interrupts and instead just use a millis() timing routine so my code is even simpler. I combined both of these excellent routines into a single program. It uses a neural net with a single input node, two hidden nodes and a single output node. The program first calls a training routine which takes only a few seconds so you can run it every time you start up the robot. Then the neural net routine runs and uses the standard sigmoid activation function which contains the node values to within 0 to 1. You simply need to map your input sensor values (which is just the tilt angle of the robot) to between 0 and 1, input the values into the neural net function which then returns an output in the value between 0 and 1. Then map that output value to a useful value for your motors, between -255 and 255 in this case. A single input neural net is kind of overkill for this robot but as I explain later, more inputs could be used. This will give you a starting point for using neural nets on the Arduino. More details here: https://www.instructables.com/id/Arduino-Neural-Network-Self-Balancing-Two-Wheel-Ro/

This is a companion discussion topic for the original entry at https://community.robotshop.com/robots/show/self-balancing-robot-using-neural-net

Brains and things

Check out the work of James Albus https://en.wikipedia.org/wiki/James_S._Albus

especially his CMAC (Cerebellar Model for Articulation Control.)  He wrote a book about it that I think was extended from a series of articles in a computer magazine in the 70s.  He goes into great detail, but in a fairly simple way, of how to do exactly the kinds of stuff you are doing.

Also, look into Google’s Tensorflow.  It is rather large and complex (certainly not for arduinos) but they have some VERY good educational material on the Tensorflow site.

This is a neat project.  Congratulations.

Inverted pendulum

Interesting approach with the battery at the top. I’ll guess the jitter is less from the code than from a bit of play in the gears, and issues with operating reliably at low speeds. You’re taking an excellent approach to learning about NN - not simple at all - well done. Next step is to add gyro values?

Yes, I’m not sure how to

Yes, I’m not sure how to increase the number of inputs - if I make the accelerometer values one input and the gyros two other inputs, do I still need to use the complementary filter? Not sure how that will all work together. Frankly I have no idea what I am doing but perhaps by playing around with it I will develop a better understanding.

Yes, the jitter is partly the gear slop but it works surprisingly tighter than the PID. I also discovered I had a low voltage issue affecting the MPU6050 so it should do a little better now.

Thanks for the input, I will

Thanks for the input, I will check out Albus stuff. I did try to get Tensorflow working on my pc but had some problems and gave it a pass. Will revisit that and their educational materials.

Neural Set vs PID

Typical excellent presentation. I am curious if the “better” results are the result of implementation or better algorithm.

Thank you for doing this.

For me personally, this is one of the most interesting projects I have seen here in a long time.

The balancing part is cool, but using an ANN to do it is what is really impressive.  I have long wondered about practical applications of ANNs in hobby bots, even blogged about it here years ago.  You have now illuminated a path by doing something very practical in a real robot.

I’d like to drop everything else I’m doing to dissect this and learn everything I can.  Thank you very much for doing this work.

Regards,

Martin

Algorithms

With a neural network there isn’t really an algorithm as we normally think of it.  It’s closer to a hardwired feedback controller where all the components are adjustable/programmable.

If you look at the writings/works of Albus that I linked below they explain it quite well.

So happy you are interested

So happy you are interested in practical apps of NN. Hope you figure some stuff out then you can explain it to me. I will post another simple application of NN to motion detecting project that I did a while back. Good luck.

Yeah, “better” is subjective

Yeah, “better” is subjective isn’t it.  It will take someone with better skills to determine what is going on there. I have only seen one application of NN to self-balancing but it was a university project and I would have to buy their research which just seems criminal to me.

This book helped me

This book helped me understand how a NN works - the best explaination I have ever seen and easy to understand.

https://www.amazon.com/Make-Your-Own-Neural-Network-ebook/dp/B01EER4Z4G     by Tariq Rashid on Amazon - Kindle version is only 3.99. Well worth the money.

re: demej00

Thanks for the info.  I look forward to any other posts you make on this.

I am working on a new and larger bot (about 30in tall).  It has tracks.   It balances side-to-side and has adjustable ride height with a linear actuator (4 inches of travel) on each side.  This is slow though.  It can pivot its entire body using rotating hips (with servo gearboxes), to balance front-to-back while driving over obstacles…otherwise it might fall.  This part is fast.  I also need to be careful about starting or stopping too fast…I intend to use the hips to pivot to keep from tipping here as well.  There are 12 sonars, a lidar-like rotating sharp sensor, and an onboard PC.

The point is, there would seem to be a lot of potential applications of NNs.  I look forward to applying insights from here to my bot.  I’d prefer the adaptability of this to custom built algos.

Regards,

Martin