INTRODUCTION
Some time ago I started working with Artificial Neural Networks (ANN)...
... and had some success re-creating a robot learning to navigate an environment.
I based the initial work on the same paper @thrandell suggested - Automatic Creation of an Autonomous Agent (ACAA) that performed the study with a physical robot. I felt I didn't have the patience to watch a robot run around in circles for months and thus started out by simulating it in a virtual world. I had every intention of moving it to the real robot and continuing the study. Life kind of got in the way.
Three years later, l find I'm resurrecting my interest in ANN by starting a new journey. My long term goals of a self-balancing robot include adding environment awareness among other "intelligent" features. I acknowledge that artificial intelligence is a figment and that it is merely statistics. But... I wanted to get started by using ANN early in the design. The typical route for self-balancing robots is to use PID to balance and dampen out oscillations. I've already used PID for other projects, so I decided to use ANN as the method for this robot. https://forum.dronebotworkshop.com/user-robot-projects/inqster-the-re-awakening (I am not a masochist. I am not a masochist. I'm...)
At one point I had every intention of offering up the source code in case someone might be interested. Because of the organic feature creep it became very ugly coding and something I wouldn't want to be associated with. This time around, having a little experience under the belt, I have started restructuring and compartmentalizing it into separate, readable and hopefully concise functional classes. I will be publishing the code.
Although I hope this topic will be of interest and help others in their learning process, I also admit that I often write on the forum as a place to keep verbose notes that I can (and will) call on to refresh my memory on how to do something. I also use it as a peer review by others that will avidly catch my mistakes or make suggestions. 😉
Inq
Overview of ANN
The actual Math to solve this is typically taught in First Grade, Primary School.
- The Sigmoid function is the classical (meaning the original) function used. These days, it is not often used because it is very costly (compared to others) for a computer to calculate. No matter what the input value, the output value ranges from 0 to 1.0 and looks like:
- y = 1.0 / (1.0 + Exp(-x))
- The ReLU function is very easy on the computer and has some very significant benefits. It ranges from 0 to ∞ and looks like:
- y = Max(0, x)
- The FastSigmoid function is computationally cheap and doesn't have the one-sided effects of the two above. It ranges from -1.0 to +1.0 and looks like:
- y = x / (1.0 + Abs(x))
My schooling mathematics only covered a bit a algebra and geometry. For some reason I took the Accounting stream, and normal maths was supplanted with Commercial Arithmetic (whatever that was) and Statistics (and Debits and Credits) so these days I find it to too much effort to follow the likes of Signoids as they were not in my noddy algebra classes as far a I remember.
But I note this to see if I feel up to it at a later date. It all looks very well presented. 👍
At this point you've been exposed to everything that is needed to run what is called an Inference. This is the series of steps completed to give an answer. This is what happens when you ask Gemini or ChatGPT a question. It runs an Inference. Actually, it probably runs an Inference for every word in its response. When I first started out, I'd usually get lost trying to interpret the steps of an Inference when they just described the mechanics of the flow. I'll try to clear that up by also including a concrete, yet basic example. You'll notice there is a lot of hand-waving and fuzzy logic that seem to have no bearing on writing a story, or doing Physics. But here we go:
My schooling mathematics only covered a bit a algebra and geometry. For some reason I took the Accounting stream, and normal maths was supplanted with Commercial Arithmetic (whatever that was) and Statistics (and Debits and Credits) so these days I find it to too much effort to follow the likes of Signoids as they were not in my noddy algebra classes as far a I remember.
Actually, this makes you far more qualified than me! The Activation functions are merely ways to massage the data, they adjust it, but in a blind/dumb way. Although they are equations, they are not like an equation in Geometry like...
sin(theta) = opposite / hypotenuse
... that gives a definitive answer, the Activation functions simply nudge data in a organized, but unknowing way if it'll make things better or worse... Just non-linear.
Later on, when we get to seeing some of this in action, I can describe further where one "might" be better than another, but most of the time, its easer to just swap one out and see if things get worse or get better.
But when we get to teaching side of the problem, it is ALL about statistics and I've never had one single course that even used the word. Maybe you'll slide in knowing why something is converging or not.
Inq
But when we get to teaching side of the problem, it is ALL about statistics and I've never had one single course that even used the word. Maybe you'll slide in knowing why something is converging or not.
Ha, when doing statistics back in the days we were allowed to take a slide rule into the exam. I remember scratching some formula's into the rule to cheat in the exams. But cheating did not pay off as now I don't remember anything much about statistics at all, and never had much use for them. Lies, Dam Lies and Statistics and all that. Debits and Credits yes, I became an Accountant, (I know, I know, bloody bean counter, though not for too long as computers crept in and diverted my attention 😎 ) but I've never had need to make any use of statistics in all these years. Should @inq educational enterprises ltd. take off, I'll give you a bargain price to audit your books. 😀
This is off topic so I hope @inq is ok with it but I will comment no more after.
In everyday life most people don't need math beyond arithmetic. In some jobs they might need to learn some math related to their job. But without the language of math the modern world would never have emerged and is essential for those working in science. One of my regrets is I never appreciated the power of math until the third year in secondary school. It does not come easy to me but was essential to have some understanding of how the world works and that includes the amazing advance they have made with neural network architectures that we can now run on super computer systems using specialized chips.
There are libraries that can do the heavy lifting for neural networks in your code and we all know you can train and use a trained neural network without having any idea how it works.
Does the ANN description so far sound too simple and totally removed from the Physics of a balancing robot? No gravity calculations. No mass, inertia calculations. Certainly none of those ugly trigonometry equations. No PID. No Kalman filtering. How is this possible?
I've already run a learning process having 1 Hidden Layer using 8 neurons parameters, so I'll illustrate the rest of this post using my first attempt. I'm not completely satisfied with the results and will likely try all of these suggestions, but will start with this:
Again, the general term for these is layers, but in Mathematics they are one dimensional matrices (vectors) - an ANN problem always has one Input Layer, one or more Hidden Layers and one Output Layer. As was mentioned, a Hidden Layer is simply another vector, but you can't get from a vector of 3 (input) to this hidden layer of 8 directly. Working with ANN, a Weights Matrix is multiplied and a Bias Vector is added between each and every layer. If you recall from the second post of this topic, the size of a matrix used in multiplication will have the same number of columns as the input vector and the same number of rows as the output. So the matrices will look like:
Step 1 Input to Hidden
Step 2 Hidden to Output
This shows all the parameters needed for the entire ANN. Using Matrix notation all this turns into:
W * I + B = H
W' * H + B' = O
If you are using a matrix library this makes your Arduino sketch look trivial. I'll be supplying the library for C# on the computer's virtual robot and C++ on the physical robot just because I said, "...from the ground up." Now if you wanted to use something like Python, it is my understanding (don't know) there is a library called Numpy that I know has all the matrix mathematics and I've also seen it has the functions like Sigmoid, and FastSigmoid. It wouldn't surprise me if it had a full ANN routine. It wouldn't be that difficult.
That's enough for this post.
Inq
I'm following this with interest. It's been many moons (ahem...65 or so years) since I've had any formal training in mathematics (at one time, I even thought about it as my major in college, but alas Criminal Psychology won out and later I was able to finish a MSEE/CS degree).
As you may recall, I'm building a self-balancing robot (it's built), but I'm having some difficulty in getting the robot to reliably stand up and remain standing. Finding the values for Kp, Ki, Kd, seem to be some sort of 'black magic' (read: trial and error) exercise. I guess I'm not very good at picking numbers.
Yes, Python has a NumPy library that does all this math 'stuff'!
Regards,
I'm following this with interest. It's been many moons (ahem...65 or so years) since I've had any formal training in mathematics (at one time, I even thought about it as my major in college, but alas Criminal Psychology won out and later I was able to finish a MSEE/CS degree).
As you may recall, I'm building a self-balancing robot (it's built), but I'm having some difficulty in getting the robot to reliably stand up and remain standing. Finding the values for Kp, Ki, Kd, seem to be some sort of 'black magic' (read: trial and error) exercise. I guess I'm not very good at picking numbers.
Yes, Python has a NumPy library that does all this math 'stuff'!
Regards,
First, it is nice to hear if/when something is appreciated.
Too bad distances prevent, but I'd love to hear some stories from YOUR profession... over a beer or two. 🍻
PID
Manual - Have you followed any strategies so far? If I recall correctly, it's been years (I did it for a COVID aid project) you start out doing it with each parameter, in turn starting with "P". You get it to where it looks like you've bested as much as you can. Then you refine it with "I" getting supposedly better. Then the final touches with "D".
Automatic??? - I don't know anything about this, but I noted some methodology in a link @robotbuilder sent me. I bookmarked it for the day I need PID again. Down toward the bottom, there is a section, Auto Tuning via Twiddle Algorithms. If you try this out, please report back you experience. https://projecthub.arduino.cc/RolfK/two-wheeled-self-balancing-robot-with-stepper-motor-9ecd74
Either way, how are you protecting it from face plants? 😳 I'm behind on my project, but keep thinking how one good drop on mine will break something. I've considered tethering it to the ceiling so it can't go past a certain angle or just putting padding all over it. Neither sounds like a good plan.
Also, I would like to highly encourage you (cattle prod time) to update your robot topic. Your trials are just as important as your successes. I'm sure others have and will have the same types of problems you're running into. And seeing you work through them might give them hope whereas posting only a final success just discourages them. Plus, us engineering types like pictures and videos... being reading and writing impaired. I'm certainly subscribed to your topic!
Good luck
Inq
Either way, how are you protecting it from face plants?
I'm behind on my project, but keep thinking how one good drop on mine will break something. I've considered tethering it to the ceiling so it can't go past a certain angle or just putting padding all over it. Neither sounds like a good plan.
Could you have training guards that prevent the robot falling more than say 20 degrees or at least not actually falling completely over? These guards could provide a learning signal as well.
I seem to remember reading somewhere that a sudden burst of acceleration will throw the base backward into a upright position?
Could you have training guards that prevent the robot falling more than say 20 degrees or at least not actually falling completely over? These guards could provide a learning signal as well.
I seem to remember reading somewhere that a sudden burst of acceleration will throw the base backward into a upright position?
I think I can. They'll be pretty big, and add a lot of inertia, but I think that is the best idea.
Could be built out of light weight foam. It just seems a good idea so you can leave it alone while it uses the guards as feedback to the neural network when it tips too far. Maybe with sonar obstacle avoidance so it learns to balance while changing direction all by itself.
That was a great little demo video of your first robot in the other post.
Can't wait to see your progress on your ANN controlled balancing robot.













