![]() |
Comming Soon! |
FP-AI-MONITOR1 runs learning and inference sessions in real-time on SensorTile Wireless Industrial Node development kit (STEVAL-STWINKT1B), taking data from onboard sensors as input. FP-AI-MONITOR1 implements a wired interactive CLI to configure the node.
The article discusses the following topics:
- Pre-requisites and setup,
- Presentation of Human Activity Recognition (HAR) application,
- Integrating a new HAR model,
- Check the new HAR model with FP-AI-MONITOR1,
This article explains the very few steps needed to replace the existing HAR AI Model with a completely different one.
For completeness, readers are invited to refer to FP-AI-MONITOR1 User Manual.
1. Prerequisites and setup
1.1. Hardware
To use the FP-AI-MONITOR1 function pack on STEVAL-STWINKT1B, the following hardware items are required:
- STEVAL-STWINKT1B development kit board,
- Windows® powered laptop/PC (Windows® 7, 8, or 10),
- Two Micro-USB cables, one to connect the sensor-board to the PC, and another one for the STLINK-V3MINI, and
- an STLINK-V3MINI.
1.2. Software
1.2.1. FP-AI-MONITOR1
Download the FP-AI-MONITOR1 package from ST website, extract and copy the contents of the .zip file into a folder on your PC. Once the pack is downloaded, unpack/unzip it and copy the content to a folder on the PC.
The steps of the process along with the content of the folder are shown in the following image.
1.2.2. IDE
- Install one of the following IDEs:
- STMicroelectronics STM32CubeIDE version 1.9.0,
- IAR Embedded Workbench for Arm (EWARM) toolchain version 9.20.1 or later, or
- RealView Microcontroller Development Kit (MDK-ARM) toolchain version 5.32.
1.2.3. TeraTerm
- TeraTerm is an open-source and freely available software terminal emulator, which is used to host the CLI of the FP-AI-MONITOR1 through a serial connection.
- Download and install the latest version available from TeraTerm.
2. Human Activity Recognition (HAR) application
The CLI application comes with a prebuilt Human Activity Recognition model.
Running the $ start ai
command starts doing the inference on the accelerometer data and predicts the performed activity along with the confidence. The supported activities are:
- Stationary,
- Walking,
- Jogging, and
- Biking.
Note that the provided HAR model is built with a dataset created using the IHM330DHCX_ACC accelerometer with the following parameters:
- Output Data Rate (ODR) set to 26 Hz ,
- Full Scale (FS) set to 4G.
3. Integrating a new HAR model
FP-AI-MONITOR1 thus provides a pre-integrated HAR model along with its preprocessing chain. This model is exemplary and by definition cannot fit the required performances or use case. It is proposed to show how this model can be easily be replaced by another that could differ in many ways :
- input sensor,
- window size,
- preprocessing,
- feature extraction step,
- data structure,
- number of classes
It is assumed that a new model candidate is available and has been pre-integrated as described in user manual.
The model is embedded in a Digital Processing Unit (DPU), which provides the infrastructure for managing data streams and their processing .
We will guide the reader through all the little modifications required for adapting the code to accommodate the new model in its specificities
3.1. New model differences
The Function Pack HAR prebuilt model is a 4-Classes SVC (Support Vector Classifier). A window of 24 samples of 3D acceleration data sampled at 26 Hz, is input to a preprocessing block. It rotates the 3D acceleration vector so that gravity is on Z axis only. This operation is followed by gravity removal. After this first processing step, the data is injected into the SVC model to classify among one of four activity classes (standing , walking, jogging, biking). The output is one vector (probability distribution) and one scalar (classification index) approximately every one second.
The new model is a Deep Neural Network (DNN), that takes raw acceleration vectors input without any preprocessing. The analysis window is larger (48 x 3D samples sampled at 26 Hz), thus the inference frequency is lower : one in every two seconds. Unlike the prebuilt SVC model this new model has only one output vector (softmax output).
3.2. Code Adaptation
3.2.1. Change Sensor and settings
The default sensor parameter setting occurs during eLooM initialization phase, specifically during the " AppController_vtblOnEnterTaskControlLoop
" call back located in Projects\STM32L4R9ZI-STWIN\Applications\FP-AI-MONITOR1\Src\AppController.c
. The first thing is to find the sensor identifier (id
) you need among those registered to sensor manager with a query. Then, if the sensor is available, its id
will be used to enable it and configure its output data rate and full scale value that are consistent with the data set on which the new model have been trained .
In our case , ODR = 26 Hz
and FS = 4G
.
Finally , the sensor is bounded to the AI task by affecting p_ai_sensor_obs
All the steps are highlighted in yellow in the code snippet below.
/* ... */
sys_error_code_t AppController_vtblOnEnterTaskControlLoop(AManagedTask *_this)
{
/* ... */
/* Enable only the accelerometer of the combo sensor "ism330dhcx". */
/* This is the default configuration for FP-AI-MONITOR1. */
SQuery_t query;
SQInit(&query, SMGetSensorManager());
sensor_id = SQNextByNameAndType(&query, "ism330dhcx", COM_TYPE_ACC);
if (sensor_id != SI_NULL_SENSOR_ID)
{
SMSensorEnable(sensor_id);
SMSensorSetODR(sensor_id, 26);
SMSensorSetFS(sensor_id, 4.0);
p_obj->p_ai_sensor_obs = SMGetSensorObserver(sensor_id);
p_obj->p_neai_sensor_obs = SMGetSensorObserver(sensor_id);
}
else
{
res = SYS_CTRL_WRONG_CONF_ERROR_CODE;
}
/* ... */
3.2.2. Adapt input shape
Align data shape definition in all SW modules, namely in AI task located in Projects\STM32L4R9ZI-STWIN\Applications\FP-AI-MONITOR1\Src\AITask.c
:
/* ... */
#define AI_AXIS_NUMBER 3
#define AI_DATA_INPUT_USER 48
/* ... */
and in the AI DPU (Data Processing Unit), embedded in AI task. The code is located in Projects\STM32L4R9ZI-STWIN\Applications\FP-AI-MONITOR1\DPU\Inc\AiDPU.h
. For our case, the input shapes is 3x48 (instead of 3x24 before)
/* ... */
#define AIDPU_NB_AXIS (3)
#define AIDPU_NB_SAMPLE (48)
#define AIDPU_AI_PROC_IN_SIZE (AI_HAR_NETWORK_IN_1_SIZE)
#define AIDPU_NAME "har_network"
/* ... */
3.2.3. Change data structure
The input is a 2D float Array with acceleration coordinates in space as rows and time index as column:
X0 Y0 Z0 X0 Y0 Z0 X1 Y1 Z1 ... ... ... Xn Yn Zn
The current version of the AI task currently supports two types of output structures:
- a unique 1D float array output
- 1 scalar float and 1D float array output
The function pack needs no adaptation for the developed example
In case of need, the type of data can be simply changed in the DPU specific function, AiDPUSetStreamsParam
, that is called to establish data input and output streams , located in Projects\STM32L4R9ZI-STWIN\Applications\FP-AI-MONITOR1\DPU\Src\AiDPU.c
:
uint16_t AiDPUSetStreamsParam(AiDPU_t *_this, uint16_t signal_size, uint8_t axes, uint8_t cb_items)
{
/* ... */
_this->super.dpuWorkingStream.packet.payload_fmt = AI_SP_FMT_FLOAT32_RESET();
/* the shape is 2D the accelerometer is 3 AXES (X,Y,Z) */
_this->super.dpuWorkingStream.packet.shape.n_shape = 2 ;
_this->super.dpuWorkingStream.packet.shape.shapes[AI_LOGGING_SHAPES_WIDTH] = axes;
_this->super.dpuWorkingStream.packet.shape.shapes[AI_LOGGING_SHAPES_HEIGHT] = signal_size;
/* Initialize the out stream */
_this->super.dpuOutStream.packet.shape.n_shape = 1;
_this->super.dpuOutStream.packet.shape.shapes[AI_LOGGING_SHAPES_WIDTH] = 2;
_this->super.dpuOutStream.packet.payload_type = AI_FMT;
_this->super.dpuOutStream.packet.payload_fmt = AI_SP_FMT_FLOAT32_RESET();
/* ... */
Today DPU's are supporting only float (coded in AI_SP_FMT_FLOAT32_RESET()
macro) and integer (coded as AI_SP_FMT_INT16_RESET()
macro) base types defined in ai_sp_dataformat.h
A more complex topology would need specific developments.
3.2.4. Change preprocessing
The preprocessing is in general specific to the use case under consideration. For instance gravity removal has been inserted in the AI DPU for the SVC model. Since with new CNN model, no pre-processing is required, we propose to bypass it in the DPU located in Projects\STM32L4R9ZI-STWIN\Applications\FP-AI-MONITOR1\DPU\Src\AiDPU.c
in the following fashion :
/* ... */
sys_error_code_t AiDPU_vtblProcess(IDPU *_this)
{
/* ... */
if ((*p_consumer_buff) != NULL)
{
GRAV_input_t gravIn[AIDPU_NB_SAMPLE];
/* GRAV_input_t gravOut[AIDPU_NB_SAMPLE]; */
assert_param(p_obj->scale != 0.0F);
assert_param(AIDPU_AI_PROC_IN_SIZE == AIDPU_NB_SAMPLE*AIDPU_NB_AXIS);
assert_param(AIDPU_NB_AXIS == p_obj->super.dpuWorkingStream.packet.shape.shapes[AI_LOGGING_SHAPES_WIDTH]);
assert_param(AIDPU_NB_SAMPLE == p_obj->super.dpuWorkingStream.packet.shape.shapes[AI_LOGGING_SHAPES_HEIGHT]);
float *p_in = (float *)CB_GetItemData((*p_consumer_buff));
float scale = p_obj->scale;
for (int i=0 ; i < AIDPU_NB_SAMPLE ; i++)
{
gravIn[i].AccX = *p_in++ * scale;
gravIn[i].AccY = *p_in++ * scale;
gravIn[i].AccZ = *p_in++ * scale;
/* gravOut[i] = gravity_suppress_rotate (&gravIn[i]); */
}
/* call Ai library. */
p_obj->ai_processing_f(AIDPU_NAME, (float *)/*gravOut*/ gravIn, p_obj->ai_out);
/* ... */
}
3.2.5. Change the number of classes
The number and labels varies from use case to use case. in Projects\STM32L4R9ZI-STWIN\Applications\FP-AI-MONITOR1\Src\AppController.c
, you will be able to tune the number of classes through #define CTRL_HAR_CLASSES
and their labels through sHarClassLabels
c-structure.
/* ... */
#define CTRL_HAR_CLASSES 6
/* ... */
static const char* sHarClassLabels[] = {
"Downstairs",
"Jogging",
"Sitting",
"Standing",
"Upstairs",
"Walking",
"Unknown"
};
3.3. Build & Test
- Build your project: Project > Build All
- Run > Run As > 1 STM32 Cortex-M C/C++ Application. Then click Run.
- Open a serial monitor application (such as Tera Term, PuTTY or GNU Screen) and connect to the ST-LINK Virtual COM port (COM port number may differ on your machine).
- Configure the communication settings to 115200 baud, 8-bit, no parity.
- Type
start ai
and default settings (as discussed above) will be applied.
- Hit
ESC
key to stop .
You can check that the new model is well integrated by simulating with your STWIN1B in hand, various human activities (that can be more or less cumbersome !)