The vacuum cleaner robot is one of the most useful inventions of the last decade and anyone who says differently means that he does not have one! This fantastic household appliance is a concentration of technology: a complex embedded system composed of some microcontrollers, many sensors, and a lot ofâ¦ software!
But how many times do you get the feeling that your robot is stupid?In particular, in situations when your little helper blocks itself over an obstacle like a home carpet, a drying rack, etc. How to recognize this before itâs too late?
A hack to avoid this annoying situation is to calculate the robotâs path in real-time with respect to the floor and perform decisions according to its current position. For instance, if the slope is over 4Â° degrees, the robot stops itself and goes back.
In this tutorial, I will approach this problem using a data-based technique, Machine Learning, and show how to implement an inclination estimator system based on an accelerometer using an ML model on an Arduino Pro board. To train and deploy this model on a microcontroller, Iâll use Neuton, a TinyML framework that allows to automatically build neural networks without any machine learning experience and embed them into small computing devices.
The microcontroller: Nicla Sense ME
The ML model will be deployed on the Arduino Nicla Sense ME board, a tiny and low-consumption Arduino board with strong computational power. It is based on a 32-bit microcontroller with 4 sensors: motion, magnetometer, pressure, and gas sensor. It is suitable for projects that need to combine sensor measurements and AI tasks on a small device. The perfect match for this experiment!
Nicla is part of the Arduino Pro platform. To get started with Nicla, just use the Arduino IDE and download the âArduino Mbed OS Nicla Boardsâ package from the board manager.
Connect the Arduino board to your computer using a USB cable andâ¦ done! Your board is ready to communicate with the IDE.
Before âgetting your hands dirtyâ with Machine Learning, check if Nicla works correctly: open the âNicla_Blinkâ sketch inside âNicla_Sense_Systemâ examples and upload it. The LED mounted on your Nicla will start to blink green.
Accelerometer measurements will be performed by the Bosch BHI260AP: a 6-axis IMU sensor mounted on Nicla.To verify that all Nicla sensors are working correctly, download âArduino_BHY2â library from the library manager and open the âStandaloneâ example.
Upload this example on the Arduino board and see the results on the Serial plotter. This sketch configures and reads all sensors data (acceleration, temperature, gas, etcâ¦).
Now, the Nicla is really ready!
The system is designed to estimate inclination only along one axis and the slope value is expressed in degrees within the [0Â°; 5Â°] range.The model takes as input a dataset composed of 50 acceleration and 50 gyroscope measures sampled in a 1-second time window (Sampling time: 20msâââ50Hz).
In the Machine Learning context, this task can be approached in two ways:
- - Regression: is the problem to predict a continuous numeric value. The model calculates the inclination as a continuous value between 0Â° and 5Â° (e.g., 2.54Â°).
- Multiclass classification: is the problem of classifying inputs into one of three or more discrete classes.The model identifies 6 classes: 0Â°, 1Â°, 2Â°, 3Â°, 4Â°, and 5Â°.
We will use both approaches and compare them.
The experiment consists of three stages:
- Capture the training dataset
- Train the model using Neuton
- Deploy the model on Nicla
1. Capture the training dataset
The first stage is to create the training dataset that will be used to train the neural network.For each inclination degree, 10 measurements will be captured and stored in a CSV file. Each measurement will be composed of 50 accelerometer and 50 gyroscope readings.
An Arduino sketch is designed to create the dataset according to Neuton requirements. The program will acquire the measurements of each inclination degree and will print the sensor data on the serial port console. Each degree value to be captured will be inserted by the user through the input serial port.
To create an accurate dataset, it will be necessary to perform the measurements by placing the Nicla board above the agent (in this case, the vacuum cleaner robot) and using a precise instrument to measure the real slope, such as, a digital inclinometer. If you donât have it, you can use your smartphone and one of the many inclinometer apps available in the Android and iOS stores. In this project, I use the Measure iPhone app.
Below, the Arduino program:
- Include headers, define project parameters and variables
- Setup serial port, IMU sensor and CSV header
- Wait for user input to perform the measurement
Upload and run the sketch, open the serial monitor and incline your Nicla in the target position (verify it with the inclinometer or with the app). After that, you type the degree value in the input field of the serial interface and press enter: the program will perform the measurementâ¦ Do not move your board during it!Repeat this for each degree to measure. In this experiment, I measure from 0Â° to 5Â° in steps of one degree (0Â°, 1Â°, 2Â°, 3Â°, 4Â°, and 5Â°).
Copy the serial port output in a file naming it âtrainingdata.csvâ.
2. Train the model with Neuton
In this stage, you will train 2 different models using the same dataset: one with the Regression task type and the other with the Multiclass type.
2a. Uploading dataset
- Create two new solutions with the name: âInclination estimator Regâ and âInclination estimator Mulâ.
For each solution:
- Upload the training dataset file and verify if meets the Neuton requirements (a green check will appear next to the file name). Then, click âOkâ.
- In the target variable section, select the column name containing the value of the degrees (e.g., target) and click âNextâ.
2b. Letâs train!
- Once the dataset is successfully validated, Neuton automatically provides the available ML task types. In the first solution select âRegressionâ and in the second one âMulti Classificationâ.
- As the model will be run on a microcontroller, enable the TinyML option in both solutions.
- Turn on the âAdvanced modeâ, go to the âAdvanced settingsâ and select 32 in the Bit depth dropdown menu. By doing so, you will take full advantage of the power of the 32-bit Nicla microcontroller.
- And nowâ¦ press âStart Trainingâ: training procedure will start, and progress will be shown step by step.
- When the âStatusâ changes to âTraining completedâ means that training is over, and the model has reached the best predictive power.
2c. Model is ready
The âPredictionâ tab shows the result of the training phase.
The Regression solution has reached an RMSE of 0.29. RMSE stands for Root Mean Square Error and it is a standard way to measure the error of a model.Low values indicate that the model predicts the data accurately. A good value is between 0.2 and 0.5.
The Multiclass solution has reached an accuracy of 88%. It means that from 100 predicted records, 88had been assigned to the correct class. High values indicate better model fit.
In both solutions, the size of the model for embedding is less than 3KB. It is a very small size compared to the microcontrollerâs (Nordic nRF52832) memory size which is 512KB.
3. Deploy the model on Nicla
To generate the C libraries of the two models, click on the âDownloadâ button of each solution.
The Neuton C-Library consists of:
- /model: the neural network model
- /preprocessing: a set of functions used to perform pre-processing operations: data manipulation, data filtering, etc.
- neuton.câââneuton.h: a set of functions used by the application logic to execute model and read prediction results.
Library integration is simple and consists of 3 steps:
1. Include Neuton library
2. Declare input variable and set input values
3. Run prediction
The main application will be the same for both models, but each solution will include its respective library files. Application is developed to compute the inclination value in degree every 1 second.
Below, the Arduino program of the inclination estimator system.
â¦Itâs time to run the inference on Nicla!Letâs verify and upload the application on the board, incline the system and look at the estimated inclination degree value in the Serial monitor. It will be computed and printed in real-time every 1 second.
For the regression task, the degree value will be outputted as a continuous value in position 0 of the probabilities array.
Below is an example of the regression solution serial output:
For the multiclass task, predictedClass variable will contain the class index of the estimated degree value.The probabilities array will contain the probabilities of the 6 classes. The accuracy of the predicted class will be stored at the position predictedClass of the array.
Below is an example of the multiclass solution serial output:
Letâs get in action!
To show the inclination estimate in action, I put the Nicla powered with a small battery on the vacuum robot.The Nicla indicates slope value with led color:
- Green: if the inclination is less than 4Â°
- Red: if it is over 4Â°