This week’s project is combining my 1D EggBot controller with the Machine Learning textbook that I’m reading this summer. The goal of the controller is to help the EggBot stay upright or at a specified angle. The robot’s accelerometer indicates the current lean angle and the error, or difference between the indicated and the specified lean angles, is fed into a bang-bang controller. The controller then outputs a position for the weight that moves the robot.
For the purpose simplification, I am using a simple bang-bang control, even though such a control might not be super successful in practice. I just wanted something very simple with predictable results. Training the model shouldn’t be difficult. I think that the hard part of this project will be writing the model in C++ for the Arduino controlling the robot.
The Neural Network definition and training takes place using PyTorch running on Google Colab. Here is the Google Colab Notebook approximating a Bang-Bang control law with a Neural network. The details of the model and the control law are shown there.
I am concerned about the number of parameters in the neural network and whether the Arduino will be able keep up with calculating the control output in time. I started with 1024×1024 feature network, dropped it to 8×8, then tried some values in between. The 1024×1024 feature network is probably the only reasonable approximation.
Variation in Accuracy for Various Network Sizes
I trained various networks with changing network sizes: 8×8, 128×128, 256×256, and 1024×1024. Scatter plots of original and predicted for each network are presented below. Increasing the feature count in the model seems to better approximate the original control law. As can be seen below, 8×8 is pretty terrible and would be a useless control law and 1024×1024 approximates the sign function pretty effectively.
Linear Model Inappropriate (?) and Next Steps
The NxN element Linear-Relu Model I used for this experiment is probably inappropriate. Here are some possible next steps.
- Code up the neural network for the Arduino and performance test it
- Try a different neural network topology that fits the problem better
- Move on to training a model directly on a simulation using something like Gymnasium with Mujoco