Hi All,
I am using the following equipment:
Arduino Uno R3
240x320 TFT LCD Shield (XC4630 from Jaycar) Using the Adafruit TFTLCD library it works as 0x7575 which is a HX8347G driver.
MPU6050 IMU sensor
This is for an assignment where I am to use arduino hardware and create an artificial horizon. I have it working however the system is running at about 2 fps. Feedback from the instructors is that it needs to be a smooth motion on the screen.
The issue I am finding is that every time I execute the following line
mpu.getEvent(&a, &g, &temp);
The screen goes white. The only way to get the screen on again is running this
tft.begin(0x7575);
tft.begin() takes around 250ms to execute and as a result my loop takes nearly 500ms.
From digging into the libraries it apprears that every time the following function is called the screen goes white.
read(read_buffer, read_len) (from Adafruit_I2CDevice.cpp
Part of the Adafruit_BusIO library). (This function is called in the mpu.getEvent(&a, &g, &temp); line)
Code below (I apologise in advance. It isn't the most refined):
#include <Adafruit_GFX.h>
#include <Adafruit_TFTLCD.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
Adafruit_MPU6050 mpu;
#define LCD_CS A3 // Chip Select goes to Analog 3
#define LCD_CD A2 // Command/Data goes to Analog 2
#define LCD_WR A1 // LCD Write goes to Analog 1
#define LCD_RD A0 // LCD Read goes to Analog 0
Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, A4);
// Assign human-readable names to some common 16-bit color values:
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define BROWN 0xFBE0
float rollold;
float pitchlineold;
void setup(void) {
Serial.begin(9600);
Serial.println("Serial Connected");
Serial.println("Begin IMU setup");
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip");
while (1) {
delay(10);
}
}
Serial.println("MPU6050 Found!");
mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
mpu.setGyroRange(MPU6050_RANGE_500_DEG);
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
tft.reset();
tft.begin(0x7575);
tft.fillScreen(WHITE);
Serial.println("Screen Initialized");
}
void loop() {
//Declare Variables
int beginloop;
int datafromsensor;
int screenreset;
int calculatedata;
int cleardisplay;
int displaydata;
int sendserialdata;
int centreline;
int lower;
int rolldeg;
int pitchdeg;
int pitchline;
float accelx;
float accely;
float accelz;
float gravity;
float pitch;
float roll;
//Pull Data from sensor
beginloop = millis();
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
datafromsensor = millis();
//Re-initialise display
tft.begin(0x7575);
screenreset = millis();
//Process Sensor Data
accelx = a.acceleration.x;
accely = a.acceleration.y;
accelz = a.acceleration.z;
gravity = sqrt(accelx*accelx + accely*accely + accelz*accelz);
pitch = asin(accelx/gravity);
roll = atan(accely/accelz);
rolldeg = (180/3.14)* roll;
pitchdeg = (180/3.14)* pitch;
pitchline = 120 + 60 * tan(pitch);
calculatedata = millis();
//Clear previous line
for(int i = 0; i < 320 ; i++)
{
centreline = i - 160;
lower = centreline * -tan(rollold);
tft.drawPixel(pitchlineold-1-lower,i,WHITE);
tft.drawPixel(pitchlineold-lower,i,WHITE);
tft.drawPixel(pitchlineold+1-lower,i,WHITE);
}
cleardisplay = millis();
//Display new line
for(int i = 0; i < 320 ; i++)
{
centreline = i - 160;
lower = centreline * -tan(roll);
tft.drawPixel(pitchline-1-lower,i,BLACK);
tft.drawPixel(pitchline-lower,i,BLACK);
tft.drawPixel(pitchline+1-lower,i,BLACK);
}
displaydata = millis();
//Store roll and pitch data for clearing next loop
rollold = roll;
pitchlineold = pitchline;
//Send data over Serial
Serial.print("Pitch: ");
Serial.print(pitchdeg);
Serial.print(" Roll: ");
Serial.println(rolldeg);
Serial.print("Data recieved from sensor: ");
Serial.print(datafromsensor-beginloop);
Serial.print("ms Screen Reset: ");
Serial.print(screenreset-datafromsensor);
Serial.print("ms Calculate Data: ");
Serial.print(calculatedata-screenreset);
Serial.print("ms Clear Display: ");
Serial.print(cleardisplay-calculatedata);
Serial.print("ms Display Data: ");
Serial.print(displaydata-cleardisplay);
sendserialdata = millis();
Serial.print("ms Send Serial Data: ");
Serial.print(sendserialdata-displaydata);
Serial.print("ms Screen Time: ");
Serial.print((screenreset-datafromsensor) + (cleardisplay-calculatedata) +( displaydata-cleardisplay));
Serial.print("ms Total Time: ");
Serial.print(sendserialdata-beginloop);
Serial.println("ms");
}
