Wio Terminal Exibindo Leituras do Acelerômetro

Visão geral
Este exemplo demonstra como desenhar um gráfico de linhas no Wio Terminal. Neste exemplo, as leituras do acelerômetro de 3 eixos (3 conjuntos de dados) são plotadas em um único gráfico de linhas para mostrar o posicionamento em tempo real do Wio Terminal!
Recursos
-
Leituras do acelerômetro de 3 eixos exibidas em um único gráfico de linhas
-
Posicionamento em tempo real do Wio Terminal
Bibliotecas Arduino necessárias
-
Acesse Seeed_Arduino_Linechart e baixe todo o repositório para o seu disco local.
-
Instale a biblioteca do acelerômetro on-board
Seeed_Arduino_LIS3DHTR. Visite Seeed_Arduino_LIS3DHTR para mais informações.
Instruções Arduino
-
Baixe o arquivo
AcceratorReadings.inoe faça o upload para o seu Wio Terminal através daArduino IDE. Certifique-se de ter instalado todas as bibliotecas. -
Mova o Wio Terminal em diferentes eixos e veja o que ele exibe; você também pode abrir o
Serial Plotterpara conferir o efeito!
Código
- Para inicializar o LCD e o Acelerômetro
#include"LIS3DHTR.h" //include the accelerator library
#include"seeed_line_chart.h" //include the line chart library
TFT_eSPI tft;
LIS3DHTR<TwoWire> lis;
#define MAX_SIZE 50 //maximum size of data
doubles accelerator_readings[3];
TFT_eSprite spr = TFT_eSprite(&tft); // Sprite
void setup() {
tft.begin();
tft.setRotation(3);
spr.createSprite(TFT_HEIGHT,TFT_WIDTH);
spr.setRotation(3);
lis.begin(Wire1);
lis.setOutputDataRate(LIS3DHTR_DATARATE_25HZ);
lis.setFullScaleRange(LIS3DHTR_RANGE_2G);
Serial.begin(115200);
}
- Lendo dados do Acelerômetro e carregando dados
Consulte Line Charts para mais informações.
void loop() {
spr.fillSprite(TFT_WHITE);
float x_raw = lis.getAccelerationX();
float y_raw = lis.getAccelerationY();
float z_raw = lis.getAccelerationZ();
...
if (accelerator_readings[0].size() == MAX_SIZE) {
for (uint8_t i = 0; i<3; i++){
accelerator_readings[i].pop(); //this is used to remove the first read variable
}
}
accelerator_readings[0].push(x_raw); //store x-axis readings
accelerator_readings[1].push(y_raw); //store y-axis readings
accelerator_readings[2].push(z_raw); //store z-axis readings
...
}
- Configurações de título
Consulte Line Charts para mais informações.
auto header = text(0, 0)
.value("Accelerator Readings")
.align(center)
.valign(vcenter)
.width(spr.width())
.thickness(2);
header.height(header.font_height(&spr) * 2);
header.draw(&spr); // Header height is the twice the height of the font
- Configurações do gráfico de linhas
Para desenhar múltiplas linhas em um único gráfico, você pode passar um array de doubles para o gráfico de linhas como content.value({doubles[0],doubles[1],doubles[2]...}), como mostrado abaixo. Também é possível definir a cor de cada linha usando .color(), onde a ordem das cores corresponderá à ordem de passagem dos dados.
auto content = line_chart(20, header.height()); //(x,ywhere the line graph begins
content
.height(spr.height() - header.height() * 1.5) //actual height of the line chart
.width(spr.width() - content.x() * 2) //actual width of the line chart
.based_on(-2.0) //Starting point of y-axis, must be a float
.show_circle(false) //drawing a cirle at each point, default is on.
.value({accelerator_readings[0],accelerator_readings[1], accelerator_readings[2]}) //passing through the data to line graph
.max_size(MAX_SIZE)
.color(TFT_BLUE, TFT_RED, TFT_GREEN)
.backgroud(TFT_WHITE)
.draw(&spr);
Código completo
#include"LIS3DHTR.h" //include the accelerator library
#include"seeed_line_chart.h" //include the line chart library
TFT_eSPI tft;
LIS3DHTR<TwoWire> lis;
#define MAX_SIZE 50 //maximum size of data
doubles accelerator_readings[3];
TFT_eSprite spr = TFT_eSprite(&tft); // Sprite
void setup() {
tft.begin();
tft.setRotation(3);
spr.createSprite(TFT_HEIGHT,TFT_WIDTH);
spr.setRotation(3);
lis.begin(Wire1);
lis.setOutputDataRate(LIS3DHTR_DATARATE_25HZ);
lis.setFullScaleRange(LIS3DHTR_RANGE_2G);
Serial.begin(115200);
}
void loop() {
spr.fillSprite(TFT_WHITE);
float x_raw = lis.getAccelerationX();
float y_raw = lis.getAccelerationY();
float z_raw = lis.getAccelerationZ();
//This is used to print out on Serial Plotter, check Serial Plotter!
Serial.print(x_raw);
Serial.print(",");
Serial.print(y_raw);
Serial.print(",");
Serial.println(z_raw);
if (accelerator_readings[0].size() == MAX_SIZE) {
for (uint8_t i = 0; i<3; i++){
accelerator_readings[i].pop(); //this is used to remove the first read variable
}
}
accelerator_readings[0].push(x_raw); //read variables and store in data
accelerator_readings[1].push(y_raw);
accelerator_readings[2].push(z_raw);
//Settings for the line graph title
auto header = text(0, 0)
.value("Accelerator Readings")
.align(center)
.valign(vcenter)
.width(spr.width())
.thickness(2);
header.height(header.font_height(&spr) * 2);
header.draw(&spr); // Header height is the twice the height of the font
//Settings for the line graph
auto content = line_chart(20, header.height()); //(x,y) where the line graph begins
content
.height(spr.height() - header.height() * 1.5) //actual height of the line chart
.width(spr.width() - content.x() * 2) //actual width of the line chart
.based_on(-2.0) //Starting point of y-axis, must be a float
.show_circle(false) //drawing a cirle at each point, default is on.
.value({accelerator_readings[0],accelerator_readings[1], accelerator_readings[2]}) //passing through the data to line graph
.max_size(MAX_SIZE)
.color(TFT_BLUE, TFT_RED, TFT_GREEN)
.backgroud(TFT_WHITE)
.draw(&spr);
spr.pushSprite(0, 0);
delay(50);
}