Fan Anomaly Detection with Cartesiam

 Objective

This tutorial will guide you through the process of building a vibrational anomaly detector using NanoEdge AI Studio and deploying it to the SAMD21 development board.

We'll be capturing accelerometer data using the IMU2 Click board and use NanoEdge AI Studio to train a library that can model the nominal behavior of our USB case fan. Then we'll deploy that library to the SAMD21 MCU to detect anomalous events (in this case, fan blockage) in real-time.

Once you've completed this project, you should have the knowledge and starter code to build an anomaly detector that fits your own needs.

 Materials

Hardware Tools

SAMD21_ML_Kit
  • USB powered PC Case Fan
fan11.png
  • Double sided tape or mounting squares (for adhering development board to fan)
adhesive-foam-squares.jpg
  • Post-it (or similar to partially block fan intake)
postit1.jpg

Software Tools

Exercise Files

File Download
Installation
Instructions
Windows Linux Mac OSX
Project and Source Files

 Procedure

Before we get started, you'll need to install and set up the required software as detailed in the steps below.

First, install the MPLAB X IDE and XC32 compiler. These are required to program the SAMD21. You can use the default free license for the XC32 compiler as we won't need any of the pro functionality here.

Secondly, download NanoEdge AI Studio. We'll use this to analyze our sensor data and generate the anomaly detection library. Head over to the NanoEdge AI Studio download page and fill out the form to get the free trial version. Once installed, we strongly recommend you peruse the NanoEdge AI Studio documentation to get familiarized with the development flow.

Finally, download the MPLAB X project which includes all the source code and configuration files required for this tutorial.

Connect the Mikroe IMU2 Click

While the SAMD21 board is powered off, plug the Mikroe IMU2 Click into the MikroBUS socket.

Mount the SAMD21 board to your fan

Before we start collecting data, we’ll need to attach the SAMD21 board to our fan. We recommend using double-sided tape to adhere the SAMD21 board to the fan in order to create a reliable coupling between the fan and sensor.

fan_mounting.jpg
Figure 1

Note that simply placing the board on top of the fan without bonding may give inconsistent results as any minor perturbation could change the positioning of the board thereby changing the vibrational patterns recorded by the IMU.

Program the MCU with Data Logger firmware

Before training our anomaly library, we’ll need to program the SAMD21 with the appropriate firmware to print the IMU sensor data to the USB UART port so that it can be imported into NanoEdge AI Studio. There are actually two avenues for this:

a

Use the ML Plugin within MPLAB Data Visualizer to collect your data and export it to a file readable by NanoEdge AI Studio.

b

Capture data directly from NanoEdge AI Studio.

For the remainder of this guide, we’ll assume you’re using option (b) from above. This is the default configuration of the project so we won’t be making any code changes in this step of the project.

1

If you haven’t already done so, connect the SAMD21 board to your PC via the USB port; take note of the serial COM port corresponding to the device as you’ll need it later.

2

Download the MPLAB X project archive (ZIP file), and extract the archive to some local directory; for the rest of this guide, we’ll refer to this directory as <project_path>.

3

Start MPLAB X.

4

Click File > Open Project… and in the resulting Open Project dialog box find the cartesiam.X project folder located at <project_path>/cartesiam/firmware/cartesiam.X where <project_path> is the path to the extracted archive from Step 2. See Figure 2 for guidance.

open_project.png
Figure 2

5

From the project tree, open the user.h file under Header Files > config > SAMD21_WG_IOT. See Figure 3 for guidance.

mplab_user.h.png
Figure 3

6

(Optional) If you’re using MPLAB X ML Plugin:

  • Find the line in user.h that says #define DATA_VISUALIZER_BUILD false and replace it with #define DATA_VISUALIZER_BUILD true. Also replace the line that says #define DATA_LOGGER_BUILD true with #define DATA_LOGGER_BUILD false.
  • If you’re not using the ML Plugin then the default configuration (DATA_LOGGER_BUILD) will work fine.

7

Click the Make and Program button on the MPLAB toolbar as indicated in Figure 4.

make_and_program.png
Figure 4

Once programming completes, the board will reset and start logging three-axis accelerometer data over the USB UART port.

Generate NanoEdge AI Library

Now that the SAMD21 board is set up to stream accelerometer data, we can use NanoEdge AI Studio directly to capture the data we need to build our anomaly detection library.

The following sections will give a brief run-through of the steps required to generate an NanoEdge AI library. For a more detailed guide, check out Cartesiam’s own video guide, which is very similar to our own example.

Create a new project

1

Start NanoEdge AI Studio.

2

Create a new project with the following parameters:

  • Target: M0+
  • Max RAM: 16 kB
  • Sensor Type: Accelerometer three axes
cartesiam_create_project.png
Figure 5

3

Click CREATE. This will take you to Step 2 of the development pipeline.

Note that it is required to select the three axes accelerometer here, any other configuration would require changes to the firmware provided in this tutorial.

Collect Regular Signal Data

fan_wo_postit.jpg
Figure 6

1

In “Regular signals” step in the NanoEdge AI pipeline, click the Choose Signals button.

2

In the window that opens, click the From serial (USB) tab

3

In the From serial (USB) tab set the following options

  • For Serial Port, select the COM port corresponding to the SAMD21 board
  • For Baud rate, enter 115200
  • For Delimiter, choose Space
  • Mark the checkbox labeled Max Lines and in the adjacent box enter 10 for Number of lines. With the default firmware configuration, this should correspond to ~25 seconds of data.

4

Take a second to check that the SAMD21 board is firmly mounted to the fan, then click the red record icon to start capturing data. Make sure when collecting this data that no spurious vibrations are being introduced by e.g., closing doors, keyboard typing, etc.

5

Once the collection is complete, click the Validate import button

6

NanoEdge Studio AI will now perform several validation checks on your data. The data you collected should pass all these tests.

Collect Abnormal Signal Data

fan_w_postit.jpg
Figure 7

1

Attach the post-it note to the grill of the fan so that it obstructs air intake.

2

Repeat the steps detailed in the previous section to capture and verify the anomaly data.

Generate NanoEdge AI Library

1

Click the “Optimize and Benchmark” step in the NanoEdge AI pipeline.

2

Click Start. NanoEdge AI will now run some optimizations algorithms to generate a library for the target that works well for the data we previously collected.

3

Once optimization is complete, note down the number of iterations needed to give meaningful results. We’ll need to enter this number into the firmware configuration later on.

Compile NanoEdge AI Library

Finally, we can download our generated library. Note that we’re skipping the optional “Emulator” step - we’ll be going straight to hardware.

1

Click the “Deploy” step in the NanoEdge AI pipeline.

2

Click Compile and choose Development version in the resulting window to download your library.

By the way, don’t worry about the “hello world” code that NanoEdge AI generates - our firmware has this covered already.

Program the MCU with NanoEdge AI Firmware

With our library now built, it’s time to program the SAMD21 to utilize our newly generated NanoEdge AI library.

1

Unzip the contents of the NanoEdge AI archive downloaded in the previous section directly into <project_path>/cartesiam/firmware/neai. Note this should replace the existing placeholder files in our project.

2

Open up the cartesiam.X project in MPLAB X.

3

From the project tree, open the user.h file under Header Files > config > SAMD21_WG_IOT.

4

In the user.h file, find the line that says #define DATA_LOGGER_BUILD true and replace it with #define DATA_LOGGER_BUILD false. This will disable the data logging functionality in the firmware and run the NanoEdge AI library instead.

5

Also in user.h, find the line that says #define LEARNING_ITERATIONS and replace the value at the end of the line with the number of iterations recommended by NanoEdge AI studio. In case you forgot, the recommended value is output in the “Optimize and Benchmark” step of the NanoEdge AI pipeline.

6

Click the Make and Program button to load the NanoEdge AI firmware.

Training and Testing the Anomaly Detector

Program Flow

In a typical machine learning application, we would deploy a fixed model to our MCU and be done; however, the library generated by NanoEdge AI only defines the skeleton of a model - the model itself will actually learn on the MCU. That being said, there are three states in the firmware that will be indicated by the green LED on the SAMD21 board:

status_led.jpg
Figure 8

1

initialization - Once the firmware has booted up, it will wait for you to hit SW0 before proceeding to training. During this state, the green LED will be turned off.

2

training - When SW0 has been pressed, the firmware will pause for a few seconds and then start the training process. During this state, the green LED will blink slowly.

3

inference - Once training completes, the firmware will go into the anomaly detection state indefinitely. During this state, the green LED will be solid green during regular operation and will blink quickly if an anomaly is detected.

Running the Firmware

At this point your SAMD21 board should be on and loaded with the NanoEdge AI inferencing firmware.

1

(Optional) Connect to the SAMD21 serial port using a terminal emulator of your choice (PuTTY is great if using Windows) with the following settings:

  • Baud rate 115200
  • Data bits 8
  • Stop bits 1
  • Parity None
  • Flow Control XON/XOFF

This step is optional, but it is useful for getting more detailed information on what’s happening in the firmware.

2

The procedure for the training step that’s done on the firmware is the same as was done in the “Regular signals” acquisition step in NanoEdge AI Studio, so start by checking that your SAMD21 board is properly mounted to your USB fan and that the fan is operating normally.

3

When you’re ready, press SW0 to start the training process. Note the firmware will give you a few seconds after the button press before starting training to avoid capturing any disturbance caused by touching the board. The status LED will start blinking slowly when training starts.

4

When training is complete (solid green LED state), try attaching the post-it to the fan intake again - the green LED should start blinking quickly indicating an anomaly - on removal of the post-it the green LED should stop blinking.

 Conclusions

Note that the included firmware has a fixed buffer length of 256 (this fixes the number of samples that are considered by the NanoEdge AI library every time the detection function is called) and a fixed IMU sampling rate of 100Hz. Changing either of these parameters will require you to go through the process of generating a new NanoEdge AI library, including capturing new data.

That’s it! Now you can start developing your own anomaly detector with NanoEdge AI.

To learn more about the NanoEdge AI library, including how to tune the sensitivity and threshold of the detector, check out the documentation.

To learn more about the MPLAB X ML Plugin check out the documentation.


© 2020 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.