Integrate HMI with an Internal Measurement Unit Sensor using SCADE — Lesson 4

Aim: Update a six-axis IMU (MPU650 in our case) and update the display on the screen depending on the IMU movement.

 


4. A. On the Raspberry Pi

  1. Connect the IMU hardware to the respective pins as illustrated in the connection diagram.
  2. Install the WiringPi library using the following command on the Raspberry Pi command Window:
    • sudo apt-get install wiringpi
  1. Run the following command in the Raspberry Pi command Window to ensure that the I2C IMU device has been detected:
    • i2cdetect -y 1

You should see an output like the following:

Figure 4a: Output to detect i2c connection with IMU

This implies that the device address is 0x68.

 


4. B. On the Host


4. B. I. Create the Folder Structure

We would be creating a SCADE Suite program to create a logic for IMU data processing to move the PFD screen as the IMU moves.

  1. Create three folders: one for the SCADE Suite project, one for the SCADE Display project, and one for generated code.

Figure 4b: Recommended folder structure for the working project

  1. Copy the SDY project (FlightControlDisplay folder) to the folder location.
  2. Create a new SCADE Suite project to create a counter to increment roll and pitch value as in the next step.

Note: Skip steps 1-3 if you use SCADE Suite and Display projects from SS and SDY folders respectively from this link.

 


4.B.II. Create the Logic using SCADE Suite

Note: Skip steps 1-10 if you use the model from the SS and SDY folder under Lesson 4. Note that the calibration values could change based on the sensor used.

  1. Create a SCADE Suite model (e.g., PiIMUInterface) with three inputs (Acc_x, Acc_y, and Acc_z) and two outputs (pitch and roll).
  2. Create the logic to preprocess the values of Acc_x, Acc_y, and Acc_z to output the values of pitch and roll.

Figure 4c: Model to receive the accelerometer data and convert it to pitch and roll

  1. Drag and drop the SCADE Display ETP file (e.g., DemoPFD.etp) in the File View of the SCADE Suite project:

Figure 4d: Adding the SCADE Display file to the SCADE Suite model

  1. Select "KCG" under settings and click on the Settings button.
  2. Set Root Operator (for example, IMUCalc).

Figure 4e: General configuration settings for IMUCalc operator

  1. Under Code Integration -> Adaptor, select SCADE Display Co-generation.

Figure 4f: Code integration configuration settings for IMUCalc operator

  1. Under Graphics panels, select PFD2 configuration as Code Generation (press F2 to enable the change).

Figure 4g: Graphical Panels configuration settings for IMUCalc operator

  1. Under SCADE View, right-click on the IMUCalc operator, and select "Connect to Graphical Panels."

Figure 4h: Snapshot to select the option to Connect to Graphical Panels for IMUCalc operator

  1. Connect pitch and roll­ to the corresponding elements in the Graphical Panels.

Figure 4i: Connecting SCADE Suite components to SCADE Display components

  1. Connections will look like the following:

Figure 4j: SCADE Suite Components connected to SCADE Display Components

  1. Save the model and generate the code.

 


4. B. III. Prepare Files to Copy

Note: You can skip steps (1) to (5) if you use files located in folder CodeForPi under Lesson 4.

  1. Copy all the .c and .h files from the generated SCADE Display and SCADE Suite projects into the code folder.
  2. Copy the kcg_assign.h file from the install folder location into the code folder.

Figure 4k: Folder structure of the .h and .c files copied into the code folder

  1. Create a folder structure called libraries in the code folder. You should also create a subfolder SC65/libmathext inside the libraries folder.

Figure 4l: Folder structure with the libraries folder copied into the code folder

  1. Copy macro_libmathext32.h and macro_libmathext64.h files from C:\Program Files\ANSYS Inc\v231\SCADE\SCADE\libraries\SC65\libmathext to code\libraries\SC65\libmathext.
  2. Open the IMUCalc_display_adaptor.c file and make the following changes.
    • Add the following lines after #include "IMUCalc_display_adaptor.h".
      • #include "Square_mathext_float32.h"
      • #include <wiringPiI2C.h>
      • #include <stdlib.h>
      • #include <stdio.h>
      • #include <math.h>
    • Add the following lines after #undef DISPLAY_ADAPTOR_ASSERT.

      • #define Device_Address    0x68
      • #define PWR_MGMT_1        0x6B
      • #define SMPLRT_DIV        0x19
      • #define CONFIG            0x1A
      • #define INT_ENABLE        0x38
      • #define ACCEL_XOUT_H      0x3B
      • #define ACCEL_YOUT_H      0x3D
      • #define ACCEL_ZOUT_H      0x3F

Note: The values are defined considering that the device address detected in step 4 is 0x68. Please make changes to the values depending on the device address.

    • Add the following line after #include "IMUCalc.h."
      • int fd;
    • Add the following line after aol_typ_symbology_layer* p_symbology_layer_ctx = &symbology_layer_ctx;
      • void MPU6050_Init()
        {
        wiringPiI2CWriteReg8(fd, SMPLRT_DIV, 0x07);
        wiringPiI2CWriteReg8(fd, PWR_MGMT_1, 0x01);
        wiringPiI2CWriteReg8(fd, CONFIG, 0);
        wiringPiI2CWriteReg8(fd, INT_ENABLE, 0x01);
        }
      • short read_raw_data(int addr)
        {
        short high_byte, low_byte, value;
        high_byte = wiringPiI2CReadReg8(fd, addr);
        low_byte = wiringPiI2CReadReg8(fd, addr + 1);
        value = (high_byte << 8) | low_byte;
        return value;
        }
    • Add the following lines in the void Step(void) function before the ConnectSuiteToDisplay(); function call:
      • float Sensor_Acc_x, Sensor_Acc_y, Sensor_Acc_z;
        fd = wiringPiI2CSetup(Device_Address);
        MPU6050_Init();
        
        /* Connect SCADE Display => SCADE Suite */
        ConnectDisplayToSuite();
        /* Reset SCADE Display KCG outputs */
        aol_symbology_layer_predraw(p_symbology_layer_ctx);
        
        Sensor_Acc_x = read_raw_data(ACCEL_XOUT_H);
        Sensor_Acc_y = read_raw_data(ACCEL_YOUT_H);
        Sensor_Acc_z = read_raw_data(ACCEL_ZOUT_H);
        
        CTX_IMUCalc.inputs_ctx.Acc_x = Sensor_Acc_x;
        CTX_IMUCalc.inputs_ctx.Acc_y = Sensor_Acc_y;
        CTX_IMUCalc.inputs_ctx.Acc_z = Sensor_Acc_z;
        
        /* Perform SCADE Suite cycle computation */
        IMUCalc(
        &CTX_IMUCalc.inputs_ctx /* input context */,
        &CTX_IMUCalc.outputs_ctx /* output/memories context */

 


4.C. Configure the Files on the Raspberry Pi

  1. Create a copy of the PiPFD_Counter folder on Raspberry Pi and call it PiPFDIMU.
  2. Copy the code folder on the Raspberry Pi in the PiPFDIMU folder. 

Figure 4m: Folder Structure of the code folder on Raspberry Pi

  1. Execute the following command in the Raspberry Pi command window:
    • gcc -o out main.c specific.c ../code/*.c ../../../extras/utils/src/*.c 
      ../../../lib/libOGLX.a -I../code/ -I../src/ -I../../../include/ 
      -I../../../extras/opengl/ -I../../../extras/utils/include 
      -DCOEXECUTION -L/usr/lib -lX11 -lGL -lglut -lm -lwiringPi
  1. Run the out file using the command ./out. You will observe the executable run in co-execution mode.

Figure 4n: Video of the PFD screen changing its orientation along with the change in IMU orientation