Realization of neural network recognition on STM32F4

Source: Internet
Author: User
Overview

Hardware on the use of stm32f4+mpu9150 implementation of the neural network recognition gesture, but not with the IMU geomagnetic data, only with the three-axis accelerometer and three-axis gyroscope data, the board is the main reference to the Italian official Development Board schematic diagram (Life painting the first board has not been wrong ha, Let's have a little bit more fun ... )。 MPU9150 drive is used by the InvenSense to provide EMPL hardware abstraction layer, although this driver configuration Imu built-in DMP more convenient, but feel this bias correction and attitude is not very good, and the source code is not public bad change. But the recognition section uses the gesture data that the raw data does not mix, (gesture is used in another function). Considering that the single-chip computing performance is not high (in fact, the training part of the bad transplant ^_^) so the training part of the network on the Matlab do, and then the training completed the network threshold and weight of the value of the export, put into a single-chip microcomputer. Here relates to the single-chip microcomputer collected data how to issue matlab, fortunately, the high version of the MATLAB hardware support has greatly improved, can be through the serial port to collect data. Network on the single-chip computer recognition process is still very large, the original IMU data (6DOF) after a Butterworth low-pass filter is placed in a similar FIFO data structure, from the FIFO first interval to take the number (interval based on gesture action time calculation), and the number of the removed to the normalization, This data is then passed on to the network for identification. In the process of filtering, normalization and network computing are to carry out a large number of floating-point operations, so the Logsig function from the Taylor expansion into a table, also opened the FPU, with CMSIS-DSP. matlab serial port receive

Here is the script to create the serial port obj

Try
    try
        obj=serial (' COM15 ', ' baudrate ', 115200, ' parity ', ' none ', ' databits ', 8, ' stopbits ', 1);
        flag_fetch=1;
    Catch
        fprintf (' Create Obj Error ');
    End
    obj. Bytesavailablefcnmode = ' Terminator ';
    Obj. Terminator = ' C ';
    Obj. BYTESAVAILABLEFCN = @serial_nn_callback;
    Try
        fopen (obj);
    Catch
        fprintf (' Open error\n ');
        break;
    End

    pause;
    flag_fetch=0;
Catch

    fprintf (' Serial Read error!\n ');
End
fclose (obj);
Delete (obj);
Clear obj;

The next computer will be the IMU data in the form of data frames sent up, data Frame custom protocol, the frame with the character ' C ' end, here the serial port is configured as Terminator mode, set terminator= ' C ', so that the mode receives the character ' C ' call a callback function "Serial_ Nn_callback "

Here is the framework for the callback function:

function [c]= serial_nn_callback (obj, ~)

% var start
...
% var end

try
    n = Get (obj, ' bytesavailable ');
    If n>20&& (flag_fetch==1)
        a = fread (obj, Uchar ');% data frame length
        if a (1) ~= ' a '
            fscanf (obj);
        End
        % Protocol resolution start
        % protocol parsing end
    end
    catch
end

After reading here to first determine whether the data frame header is consistent, if not match immediately call FSCANF (obj) clear the serial port cache, otherwise the received data are strings. This makes it possible to successfully receive the data sent by the next computer. Network Training

This part is relatively simple, with the single S activation function, MSE can be limited to the minimum value, and then try to lower the dual S activation function, MSE can be very small. But the recognition rate of the single S activation function is sufficient for the actual test.

Input=input ';
NET = NEWFF (Minmax (input), [Mid_layer,output_layer], {' Logsig ' logsig '}  , ' Traingdx '); 
Net.trainparam.show =;
Net.trainparam.epochs = $;
Net.trainparam.goal = 0.00001;
NET.TRAINPARAM.LR = 0.001;
Network Export
W_i2l=net. iw{1,1};% the input layer to the middle-tier weight
b_i2l=net.b{1,1};% the input layer to the middle-tier threshold value
w_l2o=net. lw{2,1};% Middle to output layer weight
b_l2o=net.b{2,1};% middle to output layer threshold
% Export function start ...
% Export function End
network calculation function on single chip microcomputer
void Layer_to_layer (
    int lin_num,
    int lout_num,
    float32_t* input,
    float32_t* output,
    float32_t* W,
    float32_t* b 
    )
{
     float32_t *temp;
     memset (output, 0, 
                lout_num * sizeof (float32_t)); 
     temp = (float32_t *) malloc (lin_num * sizeof (float32_t));
     for (int i=0;i<lout_num;i++)
     {
#ifdef    arm_m4        
        arm_mult_f32 (input,w+i*lin_num,temp,lin_num) ;    CMSIS-DSP library vectors multiplied
#else for
                (int i=0;i<lin_num;i++)                      //non-DSP instruction
                {
                  * (temp+i) =* (input) * ( w+i*lin_num)
                }         
#endif for 
        (int j=0;j<lin_num;j++)
        {
            * (output+i) =* (output+i) +* (temp+ j);
        }
        * (output+i) =* (output+i) +* (b+i);
        * (output+i) =logsig_fun (* (output+i));
     }
     Free (temp);
}

The descriptions and examples on the CMSIS-DSP library are very detailed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.