So I had been playing around with Claude and AI things lately like many people have been, making silly static websites and doing a few interesting things as I have been getting more access to these tools at work. I am an electrical/mechatronic engineer by trade, so some of these tools have been quite slow to be adopted as they have been quite lackluster in utility for the analog and power folks.
However, I had recently got a Pro level account for Claude and noticed that I had some access to the all-powerful Fable for a few more days. I struggled with what would be a good task to throw at this and eventually decided I wanted to see how it would do at making an FOC motor controller, from scratch, with Rust. So that’s where this all comes from!
Started simple enough and told Fable the following:
We are going to make a Modular Motor Controller This should be the best, most versatile motor control firmware ever! (this is how we make AI work better, right?)
- It should run on a PC against a virtual motor with an abstracted hardware layer (I want to test the algo off hardware, verify controls before flashing)
- It should compile efficiently (hoping for the best!)
- It should be able to run on an STM32G474 to start (a very versatile and commonly supported MCU)
- It should be super effcient and easy to tune (again hope for the best)
- It should have an interface that allows for high bandwidth monitoring for evaluation of the system (if we can read the data, we can let the AI cook)
- It should be able to profile a motor automatically, this should be done by streaming data to the host to offload processing (this can be quite cumbersome with existing tools)
- It should be able to do torque, speed, and position control (traditional controls requirements)
- Decide on the best language for the job (I had opinions here C or Rust, but would see what proposes)
- It needs to be scalable for low resource systems like an M0 ARM or a fast GPU (down the road this could be powerful, but keeping in mind upfront may help…)
So this was all done with a rather common STM32 Nucleo board and Shield with a small, cheap drone motor you can get off Amazon for $15. I supplied it with the schematics and datasheets and it went ahead and got to work. Fable created all the milestones, read the schematics and generated the code from scratch. It did ask if we should do this in Rust or C, and it preferred Rust. I actually only have minimal work experience in Rust, but I can make it do things and was curious how well this path would work. So Rust it is!!
Fable went ahead and built a virtual motor and connected a first pass at an FOC controller to it. This core could also be built on the PC, which made it quite easy to get going and validate the core functionality without having to worry about the hardware HAL. That was especially nice since there were plenty of issues in the controller that it was able to trace down and fix along the way. We even had in-depth test results over various scenarios in a nice looking dashboard! We were seeing the SVPWM signals passing by in no time!
This basically covered Milestone 1 and I was already impressed and had higher hopes than I expected going into this experiment.
I will write some more as we have time and as my limit for Fable refreshes! But end of the day, this project has been able to spin a motor on the target hardware with sensorless FOC and we will get into how all that went/goes soon! Code coming soon, but controls are still lacking some work (and tokens!).