Mundarija:

Raspberry Pi - OpenCV ob'ektlarini kuzatish bilan avtonom Mars Rover: 7 qadam (rasmlar bilan)
Raspberry Pi - OpenCV ob'ektlarini kuzatish bilan avtonom Mars Rover: 7 qadam (rasmlar bilan)

Video: Raspberry Pi - OpenCV ob'ektlarini kuzatish bilan avtonom Mars Rover: 7 qadam (rasmlar bilan)

Video: Raspberry Pi - OpenCV ob'ektlarini kuzatish bilan avtonom Mars Rover: 7 qadam (rasmlar bilan)
Video: Система распознавания OpenCV на Raspberry Pi 3 2024, Noyabr
Anonim
Raspberry Pi - OpenCV ob'ektlarini kuzatish bilan avtonom Mars Rover
Raspberry Pi - OpenCV ob'ektlarini kuzatish bilan avtonom Mars Rover

Raspberry Pi 3, ochiq rezyumeni tanib olish, ultratovushli datchiklar va tishli DC motorlar bilan ishlaydi. Bu rover o'rgatilgan har qanday ob'ektni kuzatishi va istalgan erda harakatlanishi mumkin.

1 -qadam: kirish

Image
Image
Kerakli materiallar va dasturiy ta'minot
Kerakli materiallar va dasturiy ta'minot

Ushbu yo'riqnomada biz Raspberry Pi 3 -da ishlaydigan CV Open dasturidan foydalangan holda ob'ektlarni taniy oladigan va ularni kuzatib boradigan avtonom Mars Roverni qurmoqchimiz, bu veb -kamera yoki original malina pi kamerasidan foydalanish imkoniyatiga ega. U, shuningdek, kamera ishlamaydigan qorong'i muhitda o'z yo'lini kuzatish uchun servoga o'rnatilgan Ultrasonik sensor bilan jihozlangan. Pi dan olingan signallar, PVX quvurlari bilan ishlangan korpusga o'rnatilgan 4x150 RPM doimiy dvigatellarni boshqaruvchi IC (L293D) haydovchisiga yuboriladi.

2 -qadam: Kerakli materiallar va dasturiy ta'minot

Kerakli materiallar va dasturiy ta'minot
Kerakli materiallar va dasturiy ta'minot
Kerakli materiallar va dasturiy ta'minot
Kerakli materiallar va dasturiy ta'minot

Kerakli materiallar

  1. Raspberry Pi (har qanday noldan)
  2. Raspberry PI kamera yoki veb -kamera
  3. L293D dvigatel haydovchi IC
  4. Robot g'ildiraklari (7x4 sm) X 4
  5. Tishli tishli dvigatellar (150 RPM) X 4
  6. Shassi uchun PVX quvurlar

Dasturiy ta'minot talab qilinadi

  1. SSH -ni joylashtirish uchun macun
  2. Ob'ektni aniqlash uchun rezyume oching

3 -qadam: Rover shassisini yaratish

Rover shassisining qurilishi
Rover shassisining qurilishi
Rover shassisining qurilishi
Rover shassisining qurilishi
Rover shassisining qurilishi
Rover shassisining qurilishi

Ushbu PVX shassisni qurish uchun sizga kerak bo'ladi

  • 2 X 8"
  • 2 X 4"
  • 4 ta bo'g'inlar

PVX quvurlarni zinapoyaga o'xshash tuzilishga joylashtiring va T-bo'g'inlarga joylashtiring. Qo'shimchalarni yanada mustahkam qilish uchun siz PVX plomba vositasidan foydalanishingiz mumkin.

Tishli tishli dvigatellar PVX quvur shassisiga qisqichlar yordamida ulanadi, keyin g'ildiraklar dvigatellar bilan vintlar yordamida ulanadi.

4 -qadam: Ultrasonik masofani o'lchash moslamasini yig'ish

Ultrasonik masofani o'lchash moslamasini yig'ish
Ultrasonik masofani o'lchash moslamasini yig'ish

Ultrasonik diapazonni aniqlash moslamasi Micro Servo dvigateliga ulangan HC-SR04 ultrasonik sensori yordamida qurilgan. Vintlar yordamida servo dvigatelga ulangan plastik korpusga qo'yishdan oldin kabellar ultratovush sensori bilan oldindan ulanadi.

5 -qadam: sxemalar va elektr aloqalari

Sxemalar va elektr aloqasi
Sxemalar va elektr aloqasi
Sxemalar va elektr aloqasi
Sxemalar va elektr aloqasi

Iltimos, elektr ulanishlarini biriktirilgan sxemaga muvofiq bajaring.

6 -qadam: SSH va ochiq rezyume o'rnatish

SSH va ochiq rezyumelarni o'rnatish
SSH va ochiq rezyumelarni o'rnatish

Endi biz kerakli dasturiy ta'minotni o'rnatish uchun malina pi -ga SSH kiritishimiz kerak. Biz SSHing -dan Raspberry Pi -ga boshlaymiz. Pi sizning shaxsiy kompyuteringiz bilan bir xil yo'riqchiga ulanganligiga ishonch hosil qiling va siz unga yo'riqnoma tomonidan berilgan IP -manzilni bilasiz. Endi, agar siz Windows -da bo'lsangiz, buyruq irodasini yoki PUTTY -ni oching va quyidagi buyruqni bajaring.

ssh [email protected]

Sizning IP -manzilingiz boshqacha bo'lishi mumkin, meniki 192.168.1.6.

Endi standart parolni kiriting - "malina"

Endi sizning Pi -ga SSH o'rnatilgan bo'lsa, keling, ushbu buyruqni yangilashdan boshlaylik.

sudo apt-get update && sudo apt-get upgrade

Keling, kerakli ishlab chiquvchi vositalarini o'rnatamiz, sudo apt-get install build-essential cmake pkg-config

Keyinchalik, biz Pi -ga diskdan har xil tasvir formatlarini olishda yordam beradigan ba'zi rasm kiritish -chiqarish paketlarini o'rnatishimiz kerak.

sudo apt-get libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev ni o'rnating

Endi video olish, jonli translyatsiya va OpenCV ishlashini optimallashtirish uchun ba'zi paketlar

sudo apt-get libavcodec-dev libavformat-dev libswscale-dev libv4l-dev ni o'rnating

sudo apt-get libxvidcore-dev libx264-dev ni o'rnating

sudo apt-get libgtk2.0-dev libgtk-3-dev ni o'rnating

sudo apt-get install libatlas-base-dev gfortran

Bundan tashqari, biz Python 2.7 va Python 3 sarlavhali fayllarni o'rnatishimiz kerak, shunda biz OpenCV -ni python bog'lamalari bilan kompilyatsiya qila olamiz.

sudo apt-get install python2.7-dev python3-dev

OpenCV manba kodini yuklab olish

CD ~

wget -O opencv.zip

opencv.zip -ni oching

Opencv_contrib omborini yuklab olish

wget -O opencv_contrib.zip

opencv_contrib.zip -ni oching

OpenCV -ni o'rnatish uchun virtual muhitdan foydalanish ham tavsiya etiladi.

sudo pip virtualenv virtualenvwrapper -ni o'rnating

sudo rm -rf ~/.cache/pip

Endi virtualenv va virtualenvwrapper o'rnatildi, biz quyidagi satrlarni pastki qismga qo'shish uchun ~/.profile -ni yangilashimiz kerak.

eksport WORKON_HOME = $ HOME/.virtualenvs eksport VIRTUALENVWRAPPER_PYTHON =/usr/bin/python3 source /usr/local/bin/virtualenvwrapper.sh

Python virtual muhitini yarating

mkvirtualenv cv -p python2

yaratilgan virtual muhitga o'tish

manba ~/.profil

workon cv

NumPy -ni o'rnatish

pip o'rnatish numpy

OpenCV -ni kompilyatsiya qilish va o'rnatish

cd ~/opencv-3.3.0/

mkdir qurish

CD yaratish

cmake -D CMAKE_BUILD_TYPE = RELEASE / -D CMAKE_INSTALL_PREFIX =/usr/local / -D INSTALL_PYTHON_EXAMPLES = ON / -D OPENCV_EXTRA_MODULES_PATH = ~/opencv_contrib --DD_PUL = 3.3.0/

Nihoyat OpenCV -ni kompilyatsiya qiling

qilish -j4

Bu buyruq bajarilgandan so'ng. Buning uchun uni o'rnatish kifoya.

sudo konfiguratsiya qilish

sudo ldconfig

7 -qadam: Rover uchun Python kodini ishga tushirish

Image
Image

Tracker.py deb nomlangan Python faylini yarating va unga quyidagi kodni qo'shing.

sudo nano tracker.py

kod:-

#ASAR dasturi

#Bu dastur qizil to'pni kuzatadi va unga malina pi ga rioya qilishni buyuradi. syspath.append syspath.append ('/usr/local/lib/python2.7/site-packages') import cv2 import numpy sifatida np import os import RPi. GPIO sifatida IO IO.setmode (IO. BOARD) IO.setup (7, IO. OUT) IO.setup (15, IO. OUT) IO.setup (13, IO. OUT) IO.setup (21, IO. OUT) IO.setup (22, IO. OUT) def fwd (): IO. chiqish (21, 1)#Chap motor oldinga IO. chiqish (22, 0) IO. chiqish (13, 1)#O'ng dvigatel oldinga chiqish IO. chiqish (15, 0) def bac (): IO. chiqish (21, 0)#Chap motor orqaga IO. chiqishi (22, 1) IO. chiqishi (13, 0)#O'ng dvigatel orqaga IO chiqishi (15, 1) def ryt (): IO. chiqishi (21, 0) #Chap motor orqaga IO. chiqish (22, 1) IO. chiqish (13, 1)#O'ng dvigatel oldinga chiqish IO. chiqish (15, 0) def lft (): IO. chiqish (21, 1)#Chap motor oldinga IO.chiqish (22, 0) IO. chiqish (13, 0)#O'ng dvigatel orqaga IO. chiqish (15, 1) def stp (): IO. chiqish (21, 0)#Chap motor to'xtash IO. chiqish (22, 0) IO. chiqish (13, 0)#O'ng dvigatel to'xtashi IO. chiqish (15, 0) ############################ ###################################################################################### ##################### def main (): capWebcam = cv2. VideoCapture (0)#e'lon qilish VideoCapture obyekti va veb -kameraga ulanish, 0 => birinchi veb -kameradan foydalaning # asl o'lchamini ko'rsatish "standart o'lchamlari =" + str (capWebcam.get (cv2. CAP_PROP_FRAME_WIDTH)) + "x" + str (capWebcam.get (cv2. CAP_PROP_FRAME_HEIGHT)) capWebcam.set (cv2. CAP_PROP_FRAME_WIDTH, 320.0) # tezroq ishlov berish uchun piksellar sonini 320x240 ga o'zgartiring (cv2. CAP_PROP_FRAME_HEIGHT, 240.0) # yangilangan piksellar sonini ko'rsatish "yangilangan piksellar sonini" " + str (capWebcam.get (cv2_FRAME)) + "x" + str (capWebcam.get (cv2. CAP_PROP_FRAME_HEIGHT)) agar capWebcam.isOpened () == False: # VideoCapture obyekti veb -kamera bilan muvaffaqiyatli bog'langanligini tekshiring "xatosi: capWebcam -ga muvaffaqiyatli kirilmadi / n / n" # bo'lmasa, xato xabarini chop eting os.system ("pauza"))! = 27 va capWebcam.isOpened (): # Esc tugmasi bosilmaguncha yoki veb -kamera aloqasi yo'qolguncha blnFrameReadSuccessf ully, imgOriginal = capWebcam.read () # blnFrameReadSuccessfully bo'lmasa keyingi ramkani o'qing yoki imgOriginal Yo'q: # agar kadr muvaffaqiyatli o'qilmasa "xato: kadr veb -kameradan o'qilmadi / n" # os.systemni o'chirish uchun chop etish xato xabari ("pauza") # tugmachani bosmaguncha # pauza, shuning uchun foydalanuvchi xato xabarini ko'rishi mumkin # chiqish paytida tsikldan chiqish (dasturdan chiqadi) # end agar imgHSV = cv2.cvtColor (imgOriginal, cv2. COLOR_BGR2HSV) imgThreshLow = cv2.inRange (imgHSV), np.array ([0, 135, 135]), np.array ([18, 255, 255])) imgThreshHigh = cv2.inRange (imgHSV, np.array ([165, 135, 135]), np. qator ([179, 255, 255])) imgThresh = cv2.add (imgThreshLow, imgThreshHigh) imgThresh = cv2. GaussianBlur (imgThresh, (3, 3), 2) imgThresh = cv2.dilate (imgThresh (np) 5, 5), np.uint8)) imgThresh = cv2.erode (imgThresh, np.ones ((5, 5), np.uint8)) intRows, intColumns = imgThresh.shape doiralari = cv2. HoughCircles (imgThresh, cv2. HOUGH_GRADIENT, 5, intRows / 4) # o'zgaruvchan doiralarni ishlov berilgan rasmdagi barcha doiralar bilan to'ldiring Yo'q: # bu satr, agar aylanalar topilmasa, keyingi satrda dasturning ishdan chiqishiga yo'l qo'ymaslik uchun zarur [0]: # har bir doira uchun x, y, radius = doira # uchun IO.output (7, 1). x, y va radiusli chop eting "to'p holati x =" + str (x) + ", y =" + str (y) + ", radius =" + str (radius) # to'p to'pi holati va radiusi obRadius = int (radius) xAxis = int (x) if obRadius> 0 & obRadius100 & xAxis180: print ("O'ngga siljish") ryt () elif xAxis <100: print ("Chapga siljish") lft () boshqa: stp () boshqa: stp () cv2.circle (imgOriginal, (x, y), 3, (0, 255, 0), -1) # aniqlangan ob'ektning markazida kichik yashil doira chizish cv2.circle (imgOriginal, (x, y), radius, (0, 0, 255), 3) # aniqlangan ob'ekt atrofida # oxirigacha qizil doira chizish # agar boshqa bo'lsa: IO.output (7, 0) cv2.namedWindow ("imgOriginal", cv2. WINDOW_AUTOSIZE) # oynalar yaratish, belgilangan oyna o'lchamlari uchun WINDOW_AUTOSIZE dan foydalaning cv2.namedWindow ("imgThresh", cv2. WINDOW_AUTOSIZE) # yoki oynaning o'lchamini o'zgartirish uchun WINDOW_NORMAL -dan foydalaning cv2.imshow ("imgOriginal", imgOri) ginal)#oynalarni ko'rsatish cv2.imshow ("imgThresh", imgThresh)#end while cv2.destroyAllWindows ()#oynalarni xotiradan qaytarish ####################### ######################################################################################## ############################# agar _name_ == "_main_": main ()

Endi dasturni ishga tushirishgina qoldi

python tracker.py

Tabriklaymiz! sizning o'ziyurar mashinangiz tayyor! Ultrasonik sensorga asoslangan navigatsiya qismi tez orada tugaydi va men bu ko'rsatmalarni yangilayman.

O'qiganingiz uchun tashakkur!

Tavsiya: