MATLAB on GitHub Codespaces: Collaborate on AI Motor Control between MATLAB and Simulink users
I am a MATLAB user. Even though I am familiar with the idea of Control Systems, I do need some help from my colleagues to work on projects that involve embedding AI into hardware. This example from the documentation about Motor Control is a perfect illustration: Field-Oriented Control of PMSM Using Position Estimated by Neural Network.
Another way to frame it is replacing physical sensors to measure the position of the rotor of an electric motor, by a virtual sensor for both cost and practicality reasons.
Here is the workflow we will follow in this post. The blue parts are the ones that my Simulink colleague will take care of. And I will:
In the section choose configuration, there are three options:
You can open the MATLAB development environment in a separate window or tab (or simply use MATLAB in VSCode if you feel comfortable in this development environment).
I ran the live script FOCPMSMUsingPosEstByNeuralNtwExample.m (yes live scripts can now be stored as .m plain text files for versioning). It performs a sweep operation over a large range of speed-torque operating points for the motor, in order to generate the dataset to train the neural network.
The simulations take about 3 hours in total. So you need to update the default idle timeout of your codespace in the settings: https://github.com/settings/codespaces.
The benefit of running the simulation on a Codespace is that I can let it run in the background and continue working on my local machine without heating up and taking all my RAM and CPU.
After scripting the simulation, generating 350MB of data, I’m extracting the right inputs and outputs to train the neural network.
I do not need a GPU for 50 epochs on a fairly shallow neural network, that can capture the non-linearities of the dynamic system. In fact, it only takes 1 min and a half to complete the training.
This workflow is convenient for me because it leverages the project management tools I am familiar with, like GitHub issues. If something is broken, I can simply create an issue and assign it to the person that needs to fix it (either my colleague, or future me, potentially with a little help from my friend Copilot).
And this is the end of the workflow for my part. This showed you how using cloud development environments revolving around your source code repository can facilitate the development process between different experts in a large project, like integrating AI in embedded systems. Don’t hesitate to add a star to my repo if you find this useful.
One option would be that you change the machine type, to rent a bigger one. You might need to have a credit card on file for that, as machines over 4-cores are not part of the free tier of GitHub Codespaces.
You can retrieve the github CLI to manage codespaces programmatically.
Please leave a comment if you find other gotchas. Happy coding.
- Identify the appropriate neural network architecture
- Train the neural network
- (Push back my changes in the repository for my colleague to integrate into the simulation)
Spin up a Codespace with MATLAB
In order to work on this demo with my colleague, I will leverage a feature from GitHub called Codespaces. GitHub Codespaces® are cloud-based development environments hosted by GitHub that allow users to quickly set up and work on code directly from their browsers or through Visual Studio Code. Each codespace is a containerized environment that can be pre-configured with the necessary tools, dependencies, and extensions specified by the repository, making it easier to start coding without worrying about local setup or compatibility issues. This approach streamlines collaboration and enables teams to develop, test, and share code more efficiently, regardless of their local operating system. There are a few options to run MATLAB on a Codespace. Those options are listed in a MathWorks repository as part of the reference architectures for using MATLAB in the Cloud: https://github.com/mathworks-ref-arch/matlab-codespaces. This repository is actually a Public template, so you will see a green button on the top right offering to use it as a template to derive your own repo.
In the section choose configuration, there are three options:
- Use MATLAB only. This isn’t fitting my needs, as this example requires Simulink, Motor Control Blockset & Deep Learning Toolbox.
- Customize MATLAB installation with specific toolboxes or other software, using a Dockerfile. This is the most flexible option, but since I don’t need third party software, I’ll keep this option for some other time. (If you don’t know what Docker is, this isn’t the option you’re searching for.)
- Add MATLAB to a json configuration.
Run MATLAB and Simulink in the Codespace
Once your Codespace is ready, it will prompt you to enter your MATLAB license (via your MathWorks account email + password). Then, you should see something like this when starting your Codespace: (If not look at the gotcha section)
You can open the MATLAB development environment in a separate window or tab (or simply use MATLAB in VSCode if you feel comfortable in this development environment).
I ran the live script FOCPMSMUsingPosEstByNeuralNtwExample.m (yes live scripts can now be stored as .m plain text files for versioning). It performs a sweep operation over a large range of speed-torque operating points for the motor, in order to generate the dataset to train the neural network.
The simulations take about 3 hours in total. So you need to update the default idle timeout of your codespace in the settings: https://github.com/settings/codespaces.
The benefit of running the simulation on a Codespace is that I can let it run in the background and continue working on my local machine without heating up and taking all my RAM and CPU.
After scripting the simulation, generating 350MB of data, I’m extracting the right inputs and outputs to train the neural network.
I do not need a GPU for 50 epochs on a fairly shallow neural network, that can capture the non-linearities of the dynamic system. In fact, it only takes 1 min and a half to complete the training.
Collaborating on Codespaces
Now that I have the AI model trained, I can ask my colleague to join my GitHub repo, and task him to integrate the neural network as a virtual sensor estimating the position of the rotor in real time, and pick up the deployment workflow from there, with rigorous testing.
This workflow is convenient for me because it leverages the project management tools I am familiar with, like GitHub issues. If something is broken, I can simply create an issue and assign it to the person that needs to fix it (either my colleague, or future me, potentially with a little help from my friend Copilot).
And this is the end of the workflow for my part. This showed you how using cloud development environments revolving around your source code repository can facilitate the development process between different experts in a large project, like integrating AI in embedded systems. Don’t hesitate to add a star to my repo if you find this useful.
Gotchas
One thing you will probably want to pay attention to when you add toolboxes to the devcontainer configuration is that you could reach the limit of the disk size of the virtual machine used to install all the products. You will see something like this in the creation.log
One option would be that you change the machine type, to rent a bigger one. You might need to have a credit card on file for that, as machines over 4-cores are not part of the free tier of GitHub Codespaces.
You can retrieve the github CLI to manage codespaces programmatically.
sudo apt update && sudo apt install gh -yFor instance one command enables you to check the timeout:
Please leave a comment if you find other gotchas. Happy coding. - Category:
- Cloud AI



Comments
To leave a comment, please click here to sign in to your MathWorks Account or create a new one.