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
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.
Figure 4b: Recommended folder structure for the working project
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.
Figure 4c: Model to receive the accelerometer data and convert it to pitch and roll
Figure 4d: Adding the SCADE Display file to the SCADE Suite model
Figure 4e: General configuration settings for IMUCalc operator
Figure 4f: Code integration configuration settings for IMUCalc operator
Figure 4g: Graphical Panels configuration settings for IMUCalc operator
Figure 4h: Snapshot to select the option to Connect to Graphical Panels for IMUCalc operator
Figure 4i: Connecting SCADE Suite components to SCADE Display components
Figure 4j: SCADE Suite Components connected to SCADE Display Components
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.
Figure 4k: Folder structure of the .h and .c files copied into the code folder
Figure 4l: Folder structure with the libraries folder copied into the code folder
#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.
int fd;
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;
}
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
Figure 4m: Folder Structure of the code folder on Raspberry Pi
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
Figure 4n: Video of the PFD screen changing its orientation along with the change in IMU orientation