Cov txheej txheem:

Kev Lom Zem Gyroscope Nrog Neopixel Nplhaib: 4 Cov Kauj Ruam (nrog Duab)
Kev Lom Zem Gyroscope Nrog Neopixel Nplhaib: 4 Cov Kauj Ruam (nrog Duab)

Video: Kev Lom Zem Gyroscope Nrog Neopixel Nplhaib: 4 Cov Kauj Ruam (nrog Duab)

Video: Kev Lom Zem Gyroscope Nrog Neopixel Nplhaib: 4 Cov Kauj Ruam (nrog Duab)
Video: icu qaug beer lom zem tiag2 official mv 2020 2024, Hlis ntuj nqeg
Anonim
Image
Image

Hauv qhov kev qhia no peb yuav siv MPU6050 gyroscope, lub nplhaib neopixel thiab arduino los tsim cov cuab yeej uas teeb coj coj sib haum rau lub kaum sab xis.

Qhov no yog qhov haujlwm yooj yim thiab lom zem thiab nws yuav sib sau ua ke rau ntawm lub khob cij. Yog tias koj ua raws cov theem koj yuav tsim qhov koj pom hauv video. Nws yog kev qhia zoo rau kev kawm paub txog gyroscope thiab lub nplhaib neopixel.

Kuv tab tom tsim cov ntawv qhia no vim yog qhov kev txaus siab uas kuv pom ntawm kuv thawj qhov kev qhia ntawm no (Gyroscope Led Control With Arduino). Hauv cov lus qhia no kuv tau hloov cov coj yooj yim nrog lub nplhaib neopixel. Lub nplhaib nws yooj yim dua los siv los ntawm Adafruit lub tsev qiv ntawv thiab nws yog qhov zoo nkauj dua.

Yog li yog tias koj muaj cov khoom siv dag nyob ib puag ncig ntawm no yog txoj hauv kev zoo los siv lawv, kuv yuav sim coj koj ib qib zuj zus los ntawm kev tsim lub cuab yeej thiab tseem piav qhia nws ua haujlwm li cas hauv qib kawg.

Kauj Ruam 1: Yam Yuav Tsum Tau

Los ua ke
Los ua ke

Qhov

1. Arduino pro mini 328p (eBay) 2 $

2. Cov qhob cij

3. MPU6050 gyroscope (eBay) 1.2 $

4. 24 neopixel coj lub nplhaib (Adafruit) 17 $

5. 4 x AA roj teeb pob nrog 4 roj teeb

6. U-puab jumper cables (yeem). Kuv tau siv cov jumper cables vim tias lawv saib zoo dua ntawm lub khob cij, thiab cov leds pom ntau dua li no. Koj tuaj yeem pom lub thawv 140 ntawm ebay ntawm kwv yees li 4 $. Yog tias koj tsis muaj cov kab no koj tuaj yeem hloov lawv nrog dupont xov hlau.

Cov Cuab Yeej:

1. USB rau serial FTDI adapter FT232RL rau programm arduino pro mini

2. Arduino IDE

Kev txawj: 1. Soldering, txheeb xyuas qhov kev qhia no

3. Basic programming arduino, cov ntawv qhia no yuav muaj txiaj ntsig

Kauj ruam 2: Sib dhos

Los ua ke
Los ua ke

Kuv tau txuas cov txheej txheem fritzing hauv hom fzz thiab daim duab ntawm nws kom pom tau yooj yim ntawm kev sib txuas

1. Koj yuav tsum tau muab 3 tus txiv neej txuas rau sab nraum qab ntawm lub nplhaib neopixel zoo li qhia hauv daim duab

- solder tus pin zoo

- Looj av

- solder cov ntaub ntawv nkag tus pin

2. Tom qab ntawd 4 x tus tuav lub roj teeb yuav tsum muaj txoj hauv kev txuas mus rau lub khob cij, qhov kev daws teeb meem yooj yim yog txhawm rau txuas ob tus txiv neej dupont xov hlau rau nws lub dav hlau.

3. Npaj lub khob cij.

- tso lub nplhaib neopixel, microcontroller thiab gyroscope rau ntawm lub khob cij zoo li hauv daim duab

- tso tag nrho cov xov tooj tsis zoo: rau lub microcontroller, neopixel ring, gyro

- tso tag nrho cov xov hlau zoo: rau lub microcontroller, neopixel ring, gyro

- tso tag nrho cov xov hlau xov:

* SDA thiab SCL los ntawm lub microcontroller mus rau gyro

* tus pin D6 ntawm lub microcontroller mus rau lub nplhaib neopixel

- ob zaug txheeb xyuas txhua qhov kev sib txuas ua ntej lub zog

- xaiv tau siv cov kab xev kaw, kaw lub roj teeb pob rau sab nraum qab ntawm daim bradboard los tuav nws hauv qhov chaw thiab ua kom nws txav tau yooj yim dua.

Kauj Ruam 3: Txoj Cai thiab Calibration

Ua ntej koj yuav tsum rub tawm thiab teeb tsa ob lub tsev qiv ntawv:

1. Adafruit neopixel lub tsev qiv ntawv fir tswj lub neopixel

2. MPU6050 lub tsev qiv ntawv rau lub gyroscope

3. I2CDev lub tsev qiv ntawv qhov chaw

Lawv yog ob lub tsev qiv ntawv zoo uas yuav ua rau lub nra hnyav!

Xav paub ntau ntxiv txog neopixels ntawm no

Tom qab ntawd rub tawm thiab teeb tsa kuv lub tsev qiv ntawv los ntawm no lossis luam nws los ntawm hauv qab no:

#suav nrog "I2Cdev.h"

#include #include "MPU6050_6Axis_MotionApps20.h" #include "Wire.h" #define NEOPIXED_CONTROL_PIN 6 #define NUM_LEDS 24 const int MAX_ANGLE = 45; const int LED_OFFSET = 12; MPU6050 mpu; Adafruit_NeoPixel strip = Adafruit_NeoPixel (NUM_LEDS, NEOPIXED_CONTROL_PIN, NEO_RBG + NEO_KHZ800); unsigned ntev lastPrintTime = 0; bool initialization = tsis tseeb; // teeb tsa qhov tseeb yog tias DMP pib ua tiav uint8_t mpuIntStatus; // tuav qhov xwm txheej cuam tshuam los ntawm MPU uint8_t devStatus; // rov qab xwm txheej tom qab txhua lub cuab yeej ua haujlwm (0 = ua tiav,! 0 = yuam kev) uint16_t packetSize; // xav tias DMP pob ntawv loj (lub neej ntawd yog 42 bytes) uint16_t fifoCount; // suav ntawm txhua bytes tam sim no hauv FIFO uint8_t fifoBuffer [64]; // FIFO cia tsis Quaternion q; // [w, x, y, z] quaternion ntim VectorFloat lub ntiajteb txawj nqus; // [x, y, z] lub ntiajteb txawj nqus vector ntab ypr [3]; // [yaw, pitch, roll] yaw/pitch/roll container and gravity vector volatile bool mpuInterrupt = cuav; // qhia seb MPU cuam tshuam tus pin tau nce siab

void teeb tsa ()

{Serial.begin (9600); Serial.println ("Pib qhov program"); initialization = initializeGyroscope (); strip.begin (); } void loop () {yog (! initialization) {rov qab; } mpuInterrupt = tsis tseeb; mpuIntStatus = mpu.getIntStatus (); fifoCount = mpu.getFIFOCount (); yog (hasFifoOverflown (mpuIntStatus, fifoCount)) {mpu.resetFIFO (); rov qab; } yog (mpuIntStatus & 0x02) {thaum (fifoCount <packetSize) {fifoCount = mpu.getFIFOCount (); } mpu.getFIFOBytes (fifoBuffer, packetSize); fifoCount -= packetSize; mpu.dmpGetQuaternion (& q, fifoBuffer); mpu.dmpGetGravity (& lub ntiajteb txawj nqus, & q); mpu.dmpGetYawPitchRoll (ypr, & q, & lub ntiajteb txawj nqus); redrawLeds (ypr [0] * 180/M_PI, ypr [1] * 180/M_PI, ypr [2] * 180/M_PI); }} boolean hasFifoOverflown (int mpuIntStatus, int fifoCount) {xa mpuIntStatus & 0x10 || fifoCount == 1024; } void redrawLeds (int x, int y, int z) {x = txwv (x, -1 * MAX_ANGLE, MAX_ANGLE); y = txwv (y, -1 * MAX_ANGLE, MAX_ANGLE); yog (y 0) {lightLeds (y, z, 0, 5, 0, 89); } lwm yog (y <0 thiab z 0 thiab z 0 thiab z> 0) {lightLeds (y, z, 20, 24, 89, 0); }} void lightLeds (int x, int y, int fromLedPosition, int toLedPosition, int fromAngle, int toAngle) {ob lub kaum sab xis = (atan ((ob npaug) abs (x) / (ob npaug) abs (y)) * 4068) / 71; int ledNr = daim ntawv qhia (kaum sab xis, los ntawm Angle, toAngle, fromLedPosition, toLedPosition); printDebug (x, y, ledNr, lub kaum sab xis); uint32_t xim; rau (int i = 0; kuv txoj hauj lwm + LED_OFFSET) {txoj hauj lwm rov qab + LED_OFFSET; } rov qab txoj haujlwm + LED_OFFSET - NUM_LEDS; } void printDebug (int y, int z, int lightLed, int angle) {yog (millis () - lastPrintTime <500) {rov qab; } Serial.print ("a ="); Serial.print (lub kaum sab xis); Serial.print (";"); Serial.print ("ll ="); Serial.print (lightLed); Serial.print (";"); Serial.print ("y ="); Serial.print (y); Serial.print (";"); Serial.print ("z ="); Serial.print (z); Serial.println (";"); lastPrintTime = millis (); } bool initializeGyroscope () {Wire.begin (); TWB = 24; mpu.initialize (); Serial.println (mpu.testConnection ()? F ("MPU6050 kev sib txuas ua tiav"): F ("MPU6050 kev sib txuas tsis tau tiav")); Serial.println (F ("Pib DMP …")); devStatus = mpu.dmpInitialize (); mpu.setXGyroOffset (220); mpu.setYGyroOffset (76); mpu.setZGyroOffset (-85); mpu.setZAccelOffset (1788); yog (devStatus! = 0) {Serial.print (F ("DMP Initialization ua tsis tau tejyam (chaws")); Serial.println (devStatus); rov qab tsis tseeb;} mpu.setDMPEnabled (tseeb); Serial.println (F ("Enabling cuam tshuam kev txheeb xyuas (Arduino sab nraud cuam tshuam 0) … ")); attachInterrupt (0, dmpDataReady, RISING); mpuIntStatus = mpu.getIntStatus (); Serial.println (F (" DMP npaj tau! Tos thawj qhov cuam tshuam … ")); packetSize = mpu.dmpGetFIFOPacketSize (); rov muaj tseeb;} tsis muaj dab tsi dmpDataReady () {mpuInterrupt = muaj tseeb;}

Upload cov cai:

Siv FTDI adapter xa cov cai rau arduino.

Txuas lub hwj huam (roj teeb)

Kev ntsuas:

Qhov tseem ceeb tshaj plaws los ntsuas ntawm no yog "LED_OFFSET" tas li. Hauv kuv qhov piv txwv yog 12. Koj yuav tsum tau kho qhov no los ntawm 0 txog 23 kom tom qab ua kom lub rooj tsavxwm muaj zog, cov coj yuav ci nyob rau hauv qhov kev taw qhia koj qaij lub rooj tsavxwm.

Yog tias koj xav nrhiav kom paub ntau ntxiv txog nws ua haujlwm li cas txheeb xyuas cov kauj ruam kawg

Kauj Ruam 4: Nws Ua Haujlwm Li Cas (xaiv tau)

Nws Ua Haujlwm Li Cas (xaiv tau)
Nws Ua Haujlwm Li Cas (xaiv tau)

Ua ntej me ntsis cov ntaub ntawv hais txog MPU6050 gyroscope. Nov yog MEMS gyroscope (MEMS sawv rau Microelectromechanical systems).

Txhua hom MEMs gyroscope muaj qee daim ntawv oscillating tiv thaiv los ntawm qhov ua kom pom tseeb, thiab yog li kev hloov pauv, tuaj yeem kuaj pom. Qhov no yog vim tias, raws li kev khaws cia ntawm txoj cai lij choj, cov khoom vibrating nyiam txuas ntxiv mus hauv tib lub dav hlau, thiab ib qho kev sib txawv ntawm qhov sib txawv tuaj yeem siv los ua kom muaj kev hloov pauv.

Lub gyro tseem muaj microcontroller ntawm nws tus kheej los suav qhov yob, suab thiab yaw los ntawm qee qhov kev ua lej.

Tab sis cov ntaub ntawv raw gyro raug kev txom nyem los ntawm suab nrov thiab drift, yog li peb siv lub tsev qiv ntawv sab nraud los ua kom cov khoom du thiab muab cov ntaub ntawv huv rau peb.

Neopixel yog RGB coj tus kheej tuaj yeem hais qhia tau thiab txuas rau hauv pawg thiab nplhaib. Lawv ua haujlwm ntawm 5V thiab lawv muaj lawv tus kheej Circuitry yog li koj tsuas xav tau lub zog neopixels thiab sib txuas lus nrog lawv siv cov kab ntawv. Kev sib txuas lus tau ua tiav nrog ib kab ntawv cov ntaub ntawv uas muaj cov moos thiab cov ntaub ntawv (xav paub ntau ntxiv ntawm no). Adafruit muab lub tsev qiv ntawv huv rau kev cuam tshuam nrog cov nplhaib neopixel.

Cov cai

Sab hauv lub l oop () ua haujlwm MPU6050_6Axis_MotionApps20 lub tsev qiv ntawv hu ua. Thaum lub tsev qiv ntawv muaj cov ntaub ntawv tshiab los ntawm gyroscpe nws hu redrawLeds (x, y, z) nrog 3 kev sib cav sawv cev yaw, suab thiab yob

Sab hauv redrawLeds ():

- Peb tab tom tsom mus rau ob txoj kab: y, z

- peb tab tom txwv ob qho axys los ntawm -MAX_ANGLE mus rau +MAX_ANGLE, peb tau txhais lub kaum ntse ntse siab txog 45 thiab nws tuaj yeem hloov pauv

- peb tab tom faib 360 degreeds rau hauv 4 quadrants thiab hu rau lightLeds () ua haujlwm rau txhua tus raws li hauv qab no:

* y qhov tsis zoo, z qhov ua tau zoo thawj zaug yuav tswj tus coj ntawm 0 txog 5, lub kaum sab xis yuav yog 0 txog 89

* y tsis zoo, z tsis zoo thib ob kev tswj hwm coj los ntawm 6 txog 12, lub kaum sab xis yuav yog los ntawm 89 txog 0

* … lwm

- sab hauv lub teeb ua haujlwm LED

* Kuv tab tom xam lub kaum sab xis raws li ob txoj kev siv arctangent (kos daim duab txuas)

* Kuv tab tom suav qhov coj mus qhia siv arduino daim ntawv qhia ua haujlwm

* Kuv tab tom rov kho cov hlua coj tag nrho tab sis ob tus coj, ib qho coj mus rau txoj haujlwm coj uas kuv tau suav ua ntej thiab ua txoj haujlwm ua ntej (los qhia qhov ua kom ploj)

* Kuv tab tom siv lub luag haujlwm hu ua normalizeLedPosition () coj mus rau hauv tus account neopixel calibration. Kev ntsuas ntsuas tau muaj txiaj ntsig vim tias lub nplhaib neopixel tuaj yeem hloov pauv raws li qhov txaus siab, thiab yuav tsum ua kom haum nrog lub gyroscope

* Kuv kuj tseem luam tawm lub axis cab, dab tsi coj tau lub teeb thiab lub kaum ntse ntse

Lej

Kuv tau txuas daim duab nrog lub nplhaib coj thiab lub luag haujlwm trigonometric siv los txiav txim lub kaum sab xis.

Pom zoo: