Cov txheej txheem:

Arduino Digital Compass Project: 3 Kauj Ruam
Arduino Digital Compass Project: 3 Kauj Ruam

Video: Arduino Digital Compass Project: 3 Kauj Ruam

Video: Arduino Digital Compass Project: 3 Kauj Ruam
Video: DIY Radar With Ultrasonic Sensor And Chat-GPT Generated Arduino Code | Coders Cafe 2024, Kaum ib hlis
Anonim
Image
Image

Nyob zoo! Hauv cov lus qhia no koj yuav pom yuav ua li cas koj tuaj yeem ua Tus Digital Compass siv Arduino thiab Ua Tus IDE. Qhov no yooj yim heev tab sis nthuav thiab zoo saib Arduino Project.

Koj tuaj yeem saib qhov piv txwv ntawm qhov kev qhia hauv qhov vis dis aus saum toj no. Koj ib txwm tuaj yeem pom cov vis dis aus nthuav tawm zoo li qhov no ntawm kuv tus YouTube channel nrog rau pom ntau yam khoom siv hluav taws xob thiab qhia hauv kuv lub vev xaib, HowToMechatronics.com

Kauj Ruam 1: Qhov Yuav Tsum Tau

Rau txoj haujlwm no koj yuav xav tau tsuas yog Arduino Board thiab MEMS Magnetometer, txhawm rau ntsuas lub ntiaj teb sib nqus. Kuv yuav siv GY - 80 pawg thawj coj uas muaj MC5883L 3 - Axis Magnetometer.

Ua ntej peb txuas ntxiv nrog qhov chaws rau txoj haujlwm Yog tias koj xav tau cov ncauj lus qhia ntxaws li cas MEMS lub tshuab ntsuas hluav taws xob ua haujlwm nrog rau kev txuas thiab siv GY - 80 pawg thawj coj ntawm I2C Kev Sib Txuas Lus koj tuaj yeem tshawb xyuas kuv cov kev qhia tshwj xeeb rau qhov ntawd.

Kauj ruam 2: Arduino Source Code

Dab tsi peb yuav tsum tau ua ua ntej yog xa cov duab kos mus rau Arduino Board uas yuav nyeem cov ntaub ntawv los ntawm lub tshuab ntsuas hluav taws xob thiab nws yuav xa nws mus rau Tus Txheej Txheem IDE. Nov yog Arduino Source Code:

/ * Arduino Compass * * los ntawm Dejan Nedelkovski, * www. HowToMechatronics.com * */

#suav nrog // 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;

ntab nqe lus, nqe lus nqe lus hais txog, nqe lus Filtered, kev poob qis;

ntab Xm, Ym, Zm;

#define Magnetometer 0x1E // I2C 7bit chaw nyob ntawm HMC5883

void teeb tsa () {

// Initialize Serial thiab I2C kev sib txuas lus Serial.begin (115200); Hlau.begin (); ncua (100); Wire.beginTransmission (Magnetometer); Hlau.write (0x02); // Xaiv hom sau npe Wire.write (0x00); // Kev ntsuas ntsuas tsis tu ncua Wire.endTransmission (); }

void voj () {

// ---- X-Axis Wire.beginTransmission (Magnetometer); // xa mus rau ntaus ntawv Wire.write (Magnetometer_mX1); Wire.endTransmission (); Wire.requestFrom (Magnetometer, 1); yog (Wire.available () <= 1) {mX0 = Wire.read (); } Wire.beginTransmission (Magnetometer); // xa mus rau ntaus ntawv Wire.write (Magnetometer_mX0); Wire.endTransmission (); Wire.requestFrom (Magnetometer, 1); yog (Wire.available () <= 1) {mX1 = Wire.read (); }

// ---- Y-Axis

Wire.beginTransmission (Magnetometer); // xa mus rau ntaus ntawv Wire.write (Magnetometer_mY1); Wire.endTransmission (); Wire.requestFrom (Magnetometer, 1); yog (Wire.available () <= 1) {mY0 = Wire.read (); } Wire.beginTransmission (Magnetometer); // xa mus rau ntaus ntawv Wire.write (Magnetometer_mY0); Wire.endTransmission (); Wire.requestFrom (Magnetometer, 1); yog (Wire.available () <= 1) {mY1 = Wire.read (); } // ---- Z-Axis Wire.beginTransmission (Magnetometer); // xa mus rau ntaus ntawv Wire.write (Magnetometer_mZ1); Wire.endTransmission (); Wire.requestFrom (Magnetometer, 1); yog (Wire.available () <= 1) {mZ0 = Wire.read (); } Wire.beginTransmission (Magnetometer); // xa mus rau ntaus ntawv Wire.write (Magnetometer_mZ0); Wire.endTransmission (); Wire.requestFrom (Magnetometer, 1); yog (Wire.available () <= 1) {mZ1 = Wire.read (); } // ---- X-axis mX1 = mX1 << 8; mX_out = mX0+mX1; // Cov ntaub ntawv nyoos // Los ntawm cov ntaub ntawv: 0.92 mG/digit Xm = mX_out*0.00092; // Gauss unit //* Lub ntiaj teb sib nqus thaj tsam ntawm 0.25 txog 0.65 Gauss, yog li cov no yog cov txiaj ntsig uas peb xav tau kom kwv yees.

// ---- Y-Axis

mY1 = mY1 << 8; mY_out = mY0+mY1; Ym = mY_out*0.00092;

// ---- Z-Axis

mZ1 = mZ1 <0.073 rad tsis kam = 0.073; nqe lus += kev poob; // Kho thaum cov paib raug hwm yog tias (nqe lus <0) nqe lus += 2*PI;

// Kho vim qhov ntxiv ntawm lub kaum kev tsis pom zoo

yog (nqe lus> 2*PI) nqe lus -= 2*PI;

HeadDegrees = nqe lus * 180/PI; // Nqe lus hauv Chav Qib

// Ua kom pom lub kaum ntse ntse / Tsawg dhau lim

headingFiltered = nqe lus Filtered*0.85 + nqe lus teev npe*0.15;

// Xa nqe tus nqi los ntawm Serial Port rau Ua IDE

Serial.println (nqe lus Filter);

ncua (50); }

Kauj Ruam 3: Ua IDE Source Code

Tom qab peb tau tshaj tawm cov duab kos Arduino yav dhau los peb yuav tsum tau txais cov ntaub ntawv mus rau hauv Processing IDE thiab kos Digital Compass. Qhov ntsuas hluav taws xob yog tsim los ntawm cov duab keeb kwm yav dhau, cov duab ruaj khov ntawm tus xub, thiab cov duab tig ntawm lub cev ntawm lub koob. Yog li qhov txiaj ntsig rau lub ntiaj teb daim teb sib nqus tau suav nrog Arduino tau siv los tig lub koob.

Nov yog qhov chaws ntawm Kev Ua IDE:

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

Serial myPort;

PImage imgCompass; PImage imgCompassArrow; PImage keeb kwm yav dhau;

Txoj hlua cov ntaub ntawv = "";

ntab hau;

void teeb tsa () {

loj (1920, 1080, P3D); tus (); imgCompass = loadImage ("Compass.png"); imgCompassArrow = loadImage ("CompassArrow.png"); tom qab = loadImage ("Background.png"); myPort = Serial tshiab (qhov no, "COM4", 115200); // pib kev sib txuas lus hauv xov tooj myPort.bufferUntil ('\ n'); }

void kos () {

duab (keeb kwm yav dhau, 0, 0); // Thaub qab cov duab thaub qab pushMatrix (); txhais lus (dav/2, qhov siab/2, 0); // Txhais cov kab ke ua haujlwm rau hauv nruab nrab ntawm lub vijtsam, kom qhov kev sib hloov tshwm sim muaj cai nyob hauv nruab nrab tig Z (radians (-heading)); // Tig lub Compass ncig Z -Axis duab (imgCompass, -960, -540); // Thauj cov duab Compass thiab raws li kev sib koom tes tau hloov chaw peb xav tau teeb tsa cov duab ntawm -960x, -540y (ib nrab ntawm qhov screen loj) popMatrix (); // Ua kom muaj kev sib koom tes rov qab mus rau txoj haujlwm qub 0, 0, 0 duab (imgCompassArrow, 0, 0); // Nkag mus rau CompassArrow cov duab uas tsis cuam tshuam los ntawm kev ua haujlwm rotZ () vim yog popMatrix () muaj nuj nqi textSize (30); text ("Heading:" + nqe lus, 40, 40); // Luam tawm tus nqi ntawm nqe lus ntawm qhov screen

ncua (40);

}

// pib nyeem cov ntaub ntawv los ntawm Serial Port

void serialEvent (Serial myPort) {data = myPort.readStringUntil ('\ n'); // nyeem cov ntaub ntawv los ntawm Serial Port thiab muab tso rau hauv String variable "data". nqe lus = ntab (cov ntaub ntawv); // Hloov pauv txoj hlua tus nqi rau Float tus nqi}

Kuv vam tias koj yuav nyiam txoj haujlwm no. Yog tias yog li koj tseem tuaj yeem mus ntsib kuv lub vev xaib rau cov phiaj xwm txias dua.

Pom zoo: