../../_images/cartesiam_logo.png

Smart current sensor

../../_images/vacuum_preview.jpg ../../_images/banner.png

I. Objectives of the tutorial

This project’s goal is to create a smart clamp meter, that will learn the usual operating state of an electric equipment, and detect potential anomalies in its behavior.

This device will integrate and use Cartesiam’s NanoEdge AI Library, which will be selected via NanoEdge AI Studio.

Project constraints

The device will be able to:

  • register the user’s instructions (learn, detect, reset, etc.)
  • detect current
  • learn current patterns
  • alert the user when an anomaly is detected

II. Requirements

1. Hardware

The required hardware for this project are:


2. Software

The required software for this project are:


III. Making a data logger

This section’s goal is to create a data logger that can acquire electrical current data and log it via serial port (USB).

1. Putting the hardware together

Here we will connect the clamp meter to the NUCLEO-F401RE development board.

The idea is to:

  1. Transform the input current of the vacuum cleaner into another lower current (ratio 1/1000), using our clamp meter,
  2. Transform this current into a voltage, using a burden resistor (150 Ω),
  3. Confine this voltage to a window between 0 V and 3.3 V, and center it around the middle value (1.65 V), using a voltage divider (via two 10 kΩ resistors),
  4. Prevent static DC voltage from interfering, using a 10 μF capacitor between the ground and our voltage divider.

Here is the setup:

../../_images/pince_amp_bb2.png

Follow the instructions below to connect your development board to the clamp meter via the breadboard, and reproduce the setup shown in the schematics above:

  1. Power the breadboard via the red (3V3) and black (ground) wires,
  2. Use the two 10 kΩ resistors to build a voltage divider,
  3. Link the bottom of this voltage divider to the ground, in series with the capacitor,
  4. Plug the burden resistor, in parallel to the clamp meter, in the center of the voltage divider (between the two 10 kΩ resistors),
  5. Link top of the clamp meter / burden resistor to the Analog In (A0) input of your development board (yellow wire).

Warning

The burden resistor value is selected for this setup. The objective of this resistor is to get the voltage value between 0 and 1. If your current is lower, you should try using a higher resistor. For more information about this setup, please visit this page.


2. Data logging

i. Getting started with Mbed CLI

  1. Install mbed CLI.

    Follow the instructions given here.

    Important

    Please make sure that you have correctly installed the GNU Toolchain (“9-2019-q4-major” see above, in required software), and that it is correctly linked to mbed. Here is an example:

    $ mbed config -G GCC_ARM_PATH "path/to/install/directory/GNU Tools Arm Embedded/9 2019-q4-major/bin"
    
  2. Create your main project directory.

    $ mkdir /absolute/path/to/myproject/
    
  3. Clone the neai_current_tutorial repository into this directory.

    $ cd /absolute/path/to/myproject/
    $ git clone https://github.com/cartesiam/neai_current_tutorial.git
    
  4. Import mbed-os and make sure it is set to version 5.9.7. This is the version we will be using in this tutorial.

$ mbed import mbed-os

$ cd mbed-os

$ git checkout mbed-os-5.9.7
HEAD is now at 949cb49ab0 Merge pull request #8035 from ARMmbed/release-candidate
  1. Set the MBED_OS_DIR configuration option as an absolute path to a directory containing an implementation of mbed-os, and create the mbed project.

    $ cd /absolute/path/to/myproject/
    
    $ mbed config -G MBED_OS_DIR /absolute/path/to/myproject/mbed-os
    [mbed] /absolute/path/to/myproject/mbed-os now set as global MBED_OS_DIR
    
    $ mbed new neai_current_tutorial/
    [mbed] Creating new program "neai_current_tutorial" (git)
    

Note

You may encounter issues after installing Python or mbed, where the python or mbed commands do not work because they are not recognized. In that case, you need to add the Python / mbed install directories to your PATH (here are the instructions for Windows 10 and Linux).

Note

You will have 2 sub-directories in your main project directory:

  • mbed-os containing the mbed-os-5.9.7 repository
  • neai_current_tutorial containing this tutorial’s repository

ii. Collecting current data

  1. Connect the NUCLEO-F401RE development board to your PC.

  2. With the vacuum cleaner unplugged, put the clamp meter around your vacuum cleaner’s power cable. It needs to be around either one of the phase cables or around the neutral cable.

    ../../_images/clamp_cut_extension.jpg

    Warning

    Please proceed with caution, at your own risks. Cartesiam can’t be held responsible for electrical shock or any injury. Never cut a live electrical wire under any circumstance.

  3. Compile and flash your code on the MCU.

    $ cd /absolute/path/to/myproject/
    $ mbed compile -t GCC_ARM -m nucleo_f401re --source neai_current_tutorial --source mbed-os --build BUILD/neai_current_tutorial --profile mbed-os/tools/profiles/release.json -f --artifact-name neai_current_tutorial
    
  4. Detect COM port, find port number and use the listed port as serial port.

    If you are on Linux:

    Note

    If you need to open a serial / COM port on Linux, check the FAQ (Section II. 9) for instructions.

    • Open a terminal and use the following command:

      $ ls /dev/ttyACM*
      
    • Copy the device name given by the command above (in our case /dev/ttyACM0) and use it in the following command:

      $ stty -F /dev/ttyACM0 115200 raw -clocal -echo icrnl
      

    If you are on Windows:

    • Open Device Manager > Ports (COM and LPT)
    • Copy the serial port used by your electronic board; here we will use COM7.
  5. Collect regular signals datasets:

    Plug the vacuum cleaner, using an empty bag. Data logging is an infinite loop, stop it when you want. Make sure to have at least 100 lines in your files.

    If you are on Linux:

    • Redirect the serial output to a file:

      $ cat /dev/ttyACM0 > regularLow.csv
      

    If you are on Windows:

    • Download and install PuTTy.
    • Use the serial port that you copied from the previous step (here COM7) and use it in the following command:

      plink -serial COM7 -sercfg 115200 > regular.csv
      
  6. Collect abnormal signals datasets:

    • Replace the clean vacuum cleaner bag by a full / clogged one.
    • Repeat step 5 for abnormal signals collection, to obtain the abnormal_signal.csv file.

Important

Is your setup OK? The burden resistor value depends on your setup.

  • Voltage values should fluctuate between 0 and 1 centered on 0.5 => OK
  • If all the values have variations around 0.5, in other words, your signal has too small amplitude => Burden resistor value is too low.
  • If you have a lot of 0s and 1s in your dataset, the signal saturates => Burden resistor is too high.

IV. Using NanoEdge AI Studio to find the best library

1. Objectives

The goal of this section is to use NanoEdge AI Studio to find the static library that is most relevant to our embedded application.

2. Getting started with NanoEdge AI Studio

  • Click this link and fill the form to receive instructions on how to download NanoEdge AI Studio.
  • After confirming your email address, you will get a license key, that you will need when first launching the software.
  • For more information on how to set up NanoEdge AI Studio, of for offline activation instructions, please check the FAQ and/or the NanoEdge AI Studio docs.

3. NanoEdge AI Studio step-by-step guide

i. Creating a new project

In the main window, create a new project.

../../_images/home_settings_create1.png
  • Select microcontroller type: ARM Cortex M4

  • Choose the maximum amount of RAM, in kB: here 32kB

  • Select the sensor type: current sensor (or generic one-axis sensor)

    ../../_images/create.png

ii. Importing signal files

In these two next steps (step 2 and step 3), you will import your two signal files:

  1. The Regular signals file, corresponding to nominal vacuum cleaner behavior, i.e. data acquired by sensors during normal use, when everything is functioning as expected: regular.csv.
  2. The Abnormal signals file, corresponding to abnormal vacuum cleaner behavior, i.e. data acquired by sensors during a phase of anomaly: abnormal.csv.

For each .csv file, you will consecutively:

  • Import the file.

    ../../_images/22_screen2_top1.png ../../_images/33_screen3_top1.png
  • Select the correct delimiter (space) and file path, and then validate import.

    ../../_images/select_signal_1.png ../../_images/select_signal_2.png
  • Make sure that your file is correctly formatted.

    ../../_images/import_nomi.png ../../_images/import_aty.png

iii. Benchmarking the library

In this 4th step, you will start and monitor the library benchmark. NanoEdge AI Studio will search for the best possible library given the signal files provided in steps 2 and 3 (see previous section).

a. Starting the benchmark:
../../_images/44_screen4_top_notstarted1.png

Click START to open the signal selection window.

../../_images/select_signals.png

Then, select the number of microprocessor cores from your computer that you wish to use (see below). When you are ready to start the benchmark, click Validate.

../../_images/55_cpu_and_validate.png
b. Monitoring the benchmark:

NanoEdge AI Studio uses 3 indicators to translate the performance and relevance of candidate libraries, in the following order of priority:

  • Balanced accuracy
  • Confidence
  • RAM
../../_images/perf_indicators.png

You can monitor in real time the performance of the candidate libraries, and the evolution of the 3 performance indicators.

../../_images/perf_plot.png ../../_images/graph2.png

See Studio docs: graphs and performance indicators for more information.

Note

The benchmark process may take some time.
Please be patient; have a break, grab a drink.
../../_images/time_elapsed.png

Only interrupt the benchmark for testing purposes, and don’t expect good results unless all performance markers are at 90% minimum.

A summary screen will appear as soon as the benchmark is complete. It shows the minimum number of learning iterations needed to obtain optimal performances from the library, when it is embedded in your final hardware application. In this particular example, NanoEdge AI Studio recommended that the learn() should be called 20 times, at the very minimum.

../../_images/plot_iterations.png

Warning

  • Never use fewer iterations than the recommended number, but feel free to use more (e.g. 3 to 10 times more).
  • This iteration number corresponds to the number of lines to use in your input file, as a bare minimum.
  • These iterations must include the whole range of all kinds of nominal behaviors that you want to consider on your machine.

iv. Downloading the NanoEdge AI Emulator

In Step 5: Emulator, you can use the NanoEdge AI Emulator associated to the library that was selected during the benchmark process (Step 4).

../../_images/5_emulator_top1.png ../../_images/download_links.png

NanoEdge AI Emulator is a command-line tool that emulates the behavior of the associated library. It is a clone of the library (same behavior, performances) that can run locally on Windows or Linux, without being embedded in a microcontroller.

You can download the Emulator and use it via the command line, or via the interface provided in the Studio.


v. Compiling and downloading the NanoEdge AI Library

In this last step (Step 6: Deploy), the library will be compiled and downloaded, ready to be used in your embedded application.

../../_images/6_deploy_top1.png

Note

This is only available in the Paid versions of NanoEdge AI Studio, or in the Trial version, but limited to featured boards. Since we are using the NUCLEO-F401RE, which is an authorized board, we will be able to use the Library even with the Trial version of the Studio.

Before compiling the library, several compilation flags are available, both checked by default (in our example and the hardware used int this tutorial, keep default settings).

../../_images/55_screen5_dev_options1.png

If you ran the selection process several times, make sure that the correct benchmark is selected. Then, when you are ready to download the NanoEdge AI Library, click Compile.

../../_images/compile1.png

After carefully reading the license contract terms, click Accept and compile. After a short delay, a .zip file will be downloaded to your computer.

../../_images/zip_file.png

It contains all relevant documentation, the NanoEdge AI Emulator (both Win32 and Unix versions), the NanoEdge AI header file NanoEdgeAI.h, and a .json file containing some library details.

Congratulations; you can now use your NanoEdge AI Library and Emulator!


V. Using NanoEdge AI Emulator to test your library

1. Objectives

With NanoEdge AI Emulator, you can emulate the behavior of your NanoEdge AI Library from the command line, or the interface provided through NanoEdge AI Studio or your development board via serial port and a python script to display the results on the screen.

The Emulator comes with each library created by the Studio (Step 5). It has been compiled using the same algorithm. This means that for one library, there is one emulator. Using this tool, you can learn and detect as you would do in your embedded application.

You may learn using the regular signals used in the Studio, or any other dataset you consider as “nominal”. You may detect using the abnormal signals used in the Studio, or any other dataset you consider as “abnormal”.

In this section we explain how to test the library generated via NanoEdge AI Studio using NanoEdge AI Emulator.

2. Testing the library with NanoEdge AI Emulator

i. Via the interface provided in NanoEdge AI Studio

In the 5th step of the NanoEdge AI Studio step-by-step guide, you can test the library that was selected during the benchmark process (step 4). This step provides an interface to test the main NanoEdge AI Library functions via the NanoEdge AI Emulator.

To emulate the behavior of your NanoEdgeAI Library generated from Studio, using NanoEdge AI Studio interface, please refer the following section: using the Emulator within the Studio.

ii. Via command line

Download the NanoEdge AI Emulator via the links provided on the left side of the screen:

../../_images/download_links.png

To emulate the behavior of your NanoEdgeAI Library generated from the Studio, using command line, please refer to the NanoEdge AI Emulator documentation.


VI. Using your NanoEdge AI Library to build your final product

1. Objectives

In this section we will compile our static library and use its functions to build the desired features on our device.

Note

Take some time to read the code we provide in the file main.cpp, in the scr/ directory.

2. Compiling and linking your NanoEdge AI Library

  1. In the Studio (Step 6), download the NanoEdge AI Library. You will obtain a .zip file.

    ../../_images/zip_file.png
  2. Create a lib/ directory in myproject/neai_current_tutorial/.

    Then, copy the static library libneai.a to this lib/ directory. Finally, copy the NanoEdgeAI.h header file to the inc/ directory.

  3. Connect the NUCLEO-F401RE development board.

  4. On the electric cable, set the clamp to one phase, or to neutral wire.

  5. Compile and flash your code on the MCU using the NEAI_MODE flag as shown below.

    $ cd /absolute/path/to/myproject/
    $ mbed compile -t GCC_ARM -DNEAI_MODE -m nucleo_f401re --source neai_current_tutorial --source mbed-os --build BUILD/neai_current_tutorial --profile mbed-os/tools/profiles/release.json -f --artifact-name neai_current_tutorial
    
  6. Repeat steps 4 & 5 of the data logging section to get data from your serial port.

Now you are ready to play with your new smart sensor!


3. Playing around with your smart sensor

This time, the serial output displays the learning percentage, and once completed, the detection will be called in an infinite loop. From 0 to 100%, the detection function gives you the similarity between the current signal and the signals learned (the established knowledge).

Try to replace the bag with a clogged one, or a new bag, and observe the influence on your anomaly detection.


Resources

Documentation
All NanoEdge AI Studio documentation is available here.
Tutorials
Step-by-step tutorials, to use NanoEdge AI Studio to build a smart device from A to Z:

Useful links: