Custom Algorithm Development

This tutorial will guide you how to develop your own algorithm

In the previous tutorial, we can get the RMS or Mean value through the sample code CustomAlgorithm.zip. This tutorial will modify the sample code to get the RMS and Mean value at the same time.

Prerequisites

  • Operating System : Ubuntu 18.04 installed and up-to date

Step 1: Change the output data from 1 to 2.

Find the following code from customAlgo.c.

double* CustomAlgo(uint16_t chIndex, void *rawData, DeviceInfo devInfo, char* customParams, uint32_t* outCount)
{
    uint32_t* raw = (uint32_t*)rawData;
    double inputRange = devInfo.inputRange == B10 ? 10.0 : 1.25;
    double scalingFactor = inputRange/8388607.0*1000.0/devInfo.sensor.sensitivity;//for convert rawData to g
    const uint32_t COUNT = 1;
    double* data = (double*)malloc(sizeof(double) * COUNT);
    double* gArray = (double*)malloc(sizeof(double) * devInfo.dataCount);
    raw = raw + chIndex*devInfo.dataCount;
    *outCount = COUNT;
    
    for(int i = 0; i < devInfo.dataCount; i++)
        gArray[i] = scalingFactor*(((raw[i] & 0x00800000) == 0x00800000) ? (int32_t)(raw[i] | 0xFF000000) : (int32_t)raw[i]);
    
    if(strcmp(customParams, "rms")==0)
        data[0] = GetRms(gArray, devInfo.dataCount);  
    else
        data[0] = GetMean(gArray, devInfo.dataCount); 
     
    free(gArray);
    return data;
} 

Modify COUNT to 2 in line 34.

const uint32_t COUNT = 2;

Step 2: Change the output data.

Modify conditions, then save and deploy the code.

if(strcmp(customParams, "rms")==0)
{
    data[0] = GetRms(gArray, devInfo.dataCount);
}  
else if(strcmp(customParams,"mean")==0)
{
    data[0] = GetMean(gArray, devInfo.dataCount);
}
else if(strcmp(customParams,"both")==0)
{
    data[0]=GetRms(gArray,devInfo.dataCount);
    data[1]=GetMean(gArray,devInfo.dataCount);
} 

Step 3: Apply custom algorithm to device setting.

Please refer to the previous tutorial.

Step 4: Modify Customization Parameter.

Enter both in customization parameter. customization parameter

Step 5: Display custom algorithm result.

Click Data Capture and the data will be displayed at Customization key of returned JSON data.

In this way you can get the RMS and the Mean value at the same time.