Manufactura industrial
Internet industrial de las cosas | Materiales industriales | Mantenimiento y reparación de equipos | Programación industrial |
home  MfgRobots >> Manufactura industrial >  >> Manufacturing Technology >> Proceso de manufactura

Cómo hacer una brújula usando Arduino y Processing IDE

En este Proyecto Arduino veremos cómo podemos hacer esta brújula de aspecto genial usando un Arduino, un magnetómetro MEMS y el IDE de procesamiento. Aquí hay un video de demostración de la brújula:

Resumen

Todo lo que necesitamos para este proyecto es un magnetómetro MEMS, para medir el campo magnético terrestre, una placa Arduino y algunos cables de puente. Como ejemplo, utilizaré el HMC5883L, un magnetómetro de 3 ejes integrado en la placa de conexiones GY–80.

Cómo funciona la brújula

Pieza de Arduino

Primero necesitamos obtener los datos del sensor usando la placa Arduino a través del protocolo I2C. Luego, utilizando los valores del eje X y del eje Y del sensor, calcularemos el rumbo y enviaremos su valor al IDE de procesamiento a través del puerto serie. El siguiente código hará ese trabajo:

/*   Arduino Compass 
 *      
 *  by Dejan Nedelkovski, 
 *  www.HowToMechatronics.com
 *  
 */

#include <Wire.h> //I2C Arduino Library

#define Magnetometer_mX0 0x03  
#define Magnetometer_mX1 0x04  
#define Magnetometer_mZ0 0x05  
#define Magnetometer_mZ1 0x06  
#define Magnetometer_mY0 0x07  
#define Magnetometer_mY1 0x08  


int mX0, mX1, mX_out;
int mY0, mY1, mY_out;
int mZ0, mZ1, mZ_out;

float heading, headingDegrees, headingFiltered, declination;

float Xm,Ym,Zm;


#define Magnetometer 0x1E //I2C 7bit address of HMC5883

void setup(){
  //Initialize Serial and I2C communications
  Serial.begin(115200);
  Wire.begin();
  delay(100);
  
  Wire.beginTransmission(Magnetometer); 
  Wire.write(0x02); // Select mode register
  Wire.write(0x00); // Continuous measurement mode
  Wire.endTransmission();
}

void loop(){
 
  //---- X-Axis
  Wire.beginTransmission(Magnetometer); // transmit to device
  Wire.write(Magnetometer_mX1);
  Wire.endTransmission();
  Wire.requestFrom(Magnetometer,1); 
  if(Wire.available()<=1)   
  {
    mX0 = Wire.read();
  }
  Wire.beginTransmission(Magnetometer); // transmit to device
  Wire.write(Magnetometer_mX0);
  Wire.endTransmission();
  Wire.requestFrom(Magnetometer,1); 
  if(Wire.available()<=1)   
  {
    mX1 = Wire.read();
  }

  //---- Y-Axis
  Wire.beginTransmission(Magnetometer); // transmit to device
  Wire.write(Magnetometer_mY1);
  Wire.endTransmission();
  Wire.requestFrom(Magnetometer,1); 
  if(Wire.available()<=1)   
  {
    mY0 = Wire.read();
  }
  Wire.beginTransmission(Magnetometer); // transmit to device
  Wire.write(Magnetometer_mY0);
  Wire.endTransmission();
  Wire.requestFrom(Magnetometer,1); 
  if(Wire.available()<=1)   
  {
    mY1 = Wire.read();
  }
  
  //---- Z-Axis
  Wire.beginTransmission(Magnetometer); // transmit to device
  Wire.write(Magnetometer_mZ1);
  Wire.endTransmission();
  Wire.requestFrom(Magnetometer,1); 
  if(Wire.available()<=1)   
  {
    mZ0 = Wire.read();
  }
  Wire.beginTransmission(Magnetometer); // transmit to device
  Wire.write(Magnetometer_mZ0);
  Wire.endTransmission();
  Wire.requestFrom(Magnetometer,1); 
  if(Wire.available()<=1)   
  {
    mZ1 = Wire.read();
  }
  
  //---- X-Axis
  mX1=mX1<<8;
  mX_out =mX0+mX1; // Raw data
  // From the datasheet: 0.92 mG/digit
  Xm = mX_out*0.00092; // Gauss unit
  //* Earth magnetic field ranges from 0.25 to 0.65 Gauss, so these are the values that we need to get approximately.

  //---- Y-Axis
  mY1=mY1<<8;
  mY_out =mY0+mY1;
  Ym = mY_out*0.00092;

  //---- Z-Axis
  mZ1=mZ1<<8;
  mZ_out =mZ0+mZ1;
  Zm = mZ_out*0.00092;
  // ==============================
  //Calculating Heading
  heading = atan2(Ym, Xm);
 
  // Correcting the heading with the declination angle depending on your location
  // You can find your declination angle at: https://www.ngdc.noaa.gov/geomag-web/
  // At my location it's 4.2 degrees => 0.073 rad
  declination = 0.073; 
  heading += declination;
  
  // Correcting when signs are reveresed
  if(heading <0) heading += 2*PI;

  // Correcting due to the addition of the declination angle
  if(heading > 2*PI)heading -= 2*PI;

  headingDegrees = heading * 180/PI; // The heading in Degrees unit

  // Smoothing the output angle / Low pass filter 
  headingFiltered = headingFiltered*0.85 + headingDegrees*0.15;

  //Sending the heading value through the Serial Port to Processing IDE
  Serial.println(headingFiltered);

  
  delay(50);
}Code language: Arduino (arduino)

Si necesita más detalles sobre cómo funciona el magnetómetro MEMS y cómo obtener los datos de él, puede consultar mi Tutorial de sensores MEMS.

Procesamiento de parte IDE

Aquí primero necesitamos recibir los valores de rumbo provenientes del puerto serie. Para obtener más detalles sobre cómo se hace esto, puede consultar mi Tutorial de procesamiento y Arduino.

La brújula es en realidad una imagen, o más precisamente, se compone de múltiples imágenes transparentes cargadas en Processing IDE. Las imágenes tienen que estar ubicadas en el directorio de trabajo del boceto. Después de definir los objetos de las imágenes en la sección dibujar() usando la función image(), cargamos la imagen de fondo (que es opcional, puede usar solo un color simple para el fondo). Luego, se carga la imagen de la brújula, que mediante la función de rotación() se gira con los valores del encabezado. Encima de ellos se carga la imagen de la flecha de la brújula.

Aquí está el código IDE de procesamiento:

/*   Arduino Compass 
 *      
 *  by Dejan Nedelkovski, 
 *  www.HowToMechatronics.com
 *  
 */
 
import processing.serial.*;
import java.awt.event.KeyEvent;
import java.io.IOException;

Serial myPort;
PImage imgCompass;
PImage imgCompassArrow;
PImage background;

String data="";
float heading;

void setup() {
  size (1920, 1080, P3D);
  smooth();
  imgCompass = loadImage("Compass.png");
  imgCompassArrow = loadImage("CompassArrow.png");
  background = loadImage("Background.png");
  
  myPort = new Serial(this, "COM4", 115200); // starts the serial communication
  myPort.bufferUntil('\n');
}

void draw() {
  
  image(background,0, 0); // Loads the Background image
    
  pushMatrix();
  translate(width/2, height/2, 0); // Translates the coordinate system into the center of the screen, so that the rotation happen right in the center
  rotateZ(radians(-heading)); // Rotates the Compass around Z - Axis 
  image(imgCompass, -960, -540); // Loads the Compass image and as the coordinate system is relocated we need need to set the image at -960x, -540y (half the screen size)
  popMatrix(); // Brings coordinate system is back to the original position 0,0,0
  
  image(imgCompassArrow,0, 0); // Loads the CompassArrow image which is not affected by the rotateZ() function because of the popMatrix() function
  textSize(30);
  text("Heading: " + heading,40,40); // Prints the value of the heading on the screen

  delay(40);
  
}

// starts reading data from the Serial Port
 void serialEvent (Serial myPort) { 
  
   data = myPort.readStringUntil('\n');// reads the data from the Serial Port and puts it into the String variable "data".
  
  heading = float(data); // Convering the the String value into Float value
}
Code language: Arduino (arduino)

Aquí puedes descargar los archivos del proyecto, las imágenes y los códigos fuente:


Proceso de manufactura

  1. Hacer Monitor Ambilight usando Arduino
  2. Máquina de LEVITACIÓN ULTRASÓNICA usando ARDUINO
  3. Control remoto universal usando Arduino, 1Sheeld y Android
  4. Voltímetro de bricolaje con Arduino y un teléfono inteligente
  5. Medición de frecuencia y ciclo de trabajo con Arduino
  6. Cómo hacer un botón de teclado perforable personalizable
  7. Sonda usando arduino y visualización en procesamiento IDE
  8. Contador de autos usando Arduino + Procesamiento + PHP
  9. Control del brillo del LED usando Bolt y Arduino
  10. Brazo robótico simple e inteligente con Arduino
  11. Cómo crear un sitio web comunicando Arduino usando PHP