blender-edutech-tutoriels/labyrinthe/4-arduino_pyserial/4-labyrinthe-imu/4-labyrinthe-imu.ino

149 lines
4.1 KiB
C++

#include "Wire.h"
#include "Grove_LED_Matrix_Driver_HT16K33.h"
#include "MPU6050.h"
/******************************************************************************
* 4-labyrinthe-imu.ino
* @title: Programme pour la carte Arduino de gestion de centrale inertielle (capteur IMU)
* @project: Blender-EduTech - Tutoriel 3 : Labyrinthe à bille - Interfacer avec une carte Arduino par la liaision série
* @lang: fr
* @authors: Philippe Roy <philippe.roy@ac-grenoble.fr>
* @copyright: Copyright (C) 2023 Philippe Roy
* @license: GNU GPL
*
******************************************************************************/
/******************************************************************************
* IMU - I2C
******************************************************************************/
MPU6050 accelgyro;
int16_t ax, ay, az;
int16_t gx, gy, gz;
int16_t mx, my, mz;
float Axyz[3];
float roll;
float pitch;
float roll_deg;
float pitch_deg;
String roll_txt;
String pitch_txt;
/******************************************************************************
* Led Matrix - I2C
******************************************************************************/
Matrix_8x8 matrix;
/******************************************************************************
* Communication serie
******************************************************************************/
String serial_msg = ""; // Message
/* int serial_msg_int; */
bool serial_msg_complet = false; // Flag de message complet
/******************************************************************************
* Initialisation
******************************************************************************/
void setup() {
// Liaison série
Serial.begin(115200); // 7 fps
/* Serial.begin(38400); */ // 6 fps
/* Serial.begin(9600); */ // trop lent 2fps
// IMU
Wire.begin();
accelgyro.initialize();
// Led Matrix
matrix.init();
matrix.setBrightness(0);
matrix.setBlinkRate(BLINK_OFF);
// Ok
// Serial.println("Initialisation des composants I2C.");
}
// uint64_t StrToHex(String str)
// {
// return (strtoul(str, 0, 16));
// }
/******************************************************************************
* Boucle principale
******************************************************************************/
void loop() {
/*****
* Lecture des accelerations
*****/
accelgyro.getMotion9(&ax, &ay, &az, &gx, &gy, &gz, &mx, &my, &mz);
Axyz[0] = (double) ax / 16384;
Axyz[1] = (double) ay / 16384;
Axyz[2] = (double) az / 16384;
roll = asin(-Axyz[0]);
roll_deg = roll*57.3;
roll_txt = String(roll_deg);
/* pitch = asin(Axyz[1]/cos(roll)); */
pitch = -asin(Axyz[1]/cos(roll)); // positio capteur (X vers la gauche, Y vers l'arriere, Z vers le haut)
pitch_deg = pitch*57.3;
pitch_txt = String(pitch_deg);
/*****
* IMU : Arduino -> UPBGE
*****/
//Serial.println("Roll (Rx): "+ roll_txt + " Pitch (Ry): " + pitch_txt);
Serial.print(roll_txt);
Serial.print(",");
Serial.print(pitch_txt);
Serial.println();
/*****
* Led Matrix : UPBGE -> Arduino
*****/
//matrix.writeNumber(0, 1);
//matrix.display();
if (serial_msg_complet) {
//msg_int = serial_msg.toInt();
//matrix.writeNumber(msg_int, 1);
//matrix.writeOnePicture(0x3c4299a581a5423c);
//matrix.writeOnePicture(StrToHex(serial_msg));
//matrix.writeOnePicture(strtoul(serial_msg, 0, 16));
//HexInt = serial_msg.toInt();
//HexInt = serial_msg.parseInt();
/* matrix.writeOnePicture(serial_msg_int); */
//matrix.writeOnePicture(0x8080808080808080);
matrix.display();
//if (serial_msg =="Bp Am R\n") bt_a_m_num=false;
serial_msg = "";
serial_msg_complet = false;
}
}
/******************************************************************************
* Évenement provoqué UPBGE
******************************************************************************/
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
serial_msg += inChar;
if (inChar == '\n') {
serial_msg_complet = true;
}
}
}