Practicas con Arduino
Simulación con giroscopio (MPU 6050).
Realizar una simulación del movimiento del un modelo de avión en 3D, utilizando el módulo MPU 6050 que dispone de un acelerómetro y un giroscopio, mediante Arduino. El sensor enviará los datos por el puerto serie a un programa en Processing ejecutado en el ordenador, que orientará un modelo 3D básico de un avión según las coordenadas que reciba del sensor.
Vamos a ver el resultado de la demostración en el siguiente vídeo:
ESQUEMA:
ARDUINO | MPU 6050 |
5V | VCC |
GND | GND |
A4 | SDA |
A5 | SCL |
PROGRAMA A CARGAR EN ARDUINO:
#include <MPU6050_tockn.h> #include <Wire.h> MPU6050 mpu6050(Wire); void setup() { Serial.begin(9600); Wire.begin(); mpu6050.begin(); mpu6050.calcGyroOffsets(true); } void loop() { mpu6050.update(); Serial.print(mpu6050.getAngleX()); Serial.print("*"); Serial.print(mpu6050.getAngleY()); Serial.print("*"); Serial.println(mpu6050.getAngleZ()); }
PROGRAMA A EJECUTAR EN PROCESSING (PC / LAPTOP):
//Arduino Gyro - Processing airplane yaw-pitch-roll demo. //www.practicasconarduino.com //3D model and demo based in: //6/21/2012 by Jeff Rowberg <jeff@rowberg.net> //Updates should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib //Created for teaching purposes. //2020 Jorge Muñoz Rodenas //Licensed under GPL v3 import processing.serial.*; Serial puertoArduino; void setup(){ size(400, 300, P3D); println(Serial.list()); String portName = Serial.list()[1]; puertoArduino = new Serial(this, portName, 9600); text("WAIT FEW SECONDS FOR SYNCHRONIZATION... DO NOT TOUCH THE GYRO", 30, 100); } void draw(){ background(0); pushMatrix(); translate(width/2, height/2, 0); String inString = puertoArduino.readStringUntil('\n'); if (inString != null){ inString = trim(inString); String[] d = splitTokens(inString, "*"); if(d.length == 3){ println(d[1]);//roll println(d[0]);//pitch println(d[2]);//yaw rotateZ(radians(-float(d[1]))); //roll rotateX(radians(float(d[0])));//pitch rotateY(radians(float(d[2]))); //yaw } } fill(255, 0, 0, 200); box(10, 10, 200); fill(0, 255, 0, 200); beginShape(TRIANGLES); vertex(-100, 2, 30); vertex(0, 2, -80); vertex(100, 2, 30); // wing top layer vertex(-100, -2, 30); vertex(0, -2, -80); vertex(100, -2, 30); // wing bottom layer vertex(-2, 0, 98); vertex(-2, -30, 98); vertex(-2, 0, 70); // tail left layer vertex( 2, 0, 98); vertex( 2, -30, 98); vertex( 2, 0, 70); // tail right layer endShape(); beginShape(QUADS); vertex(-100, 2, 30); vertex(-100, -2, 30); vertex( 0, -2, -80); vertex( 0, 2, -80); vertex( 100, 2, 30); vertex( 100, -2, 30); vertex( 0, -2, -80); vertex( 0, 2, -80); vertex(-100, 2, 30); vertex(-100, -2, 30); vertex(100, -2, 30); vertex(100, 2, 30); vertex(-2, 0, 98); vertex(2, 0, 98); vertex(2, -30, 98); vertex(-2, -30, 98); vertex(-2, 0, 98); vertex(2, 0, 98); vertex(2, 0, 70); vertex(-2, 0, 70); vertex(-2, -30, 98); vertex(2, -30, 98); vertex(2, 0, 70); vertex(-2, 0, 70); endShape(); popMatrix(); }