Cov txheej txheem:
- Kauj Ruam 1: Sib Sau Ua Ke
- Kauj Ruam 2: Txhim Kho OpenCV ntawm Raspberry Pi thiab teeb tsa Cov Chaw Taws Teeb
- Kauj Ruam 3: Txuas Qhov Chaw Ua Ke
- Kauj Ruam 4: Thawj Xeem
- Kauj Ruam 5: Txheeb Xyuas Txoj Kab Kab thiab Xam Kab Kab Lus
- Kauj Ruam 6: Ua Ntawv Thov PD
- Kauj Ruam 7: Cov txiaj ntsig
Video: Autonomous Lane-Keeping Car Siv Raspberry Pi thiab OpenCV: 7 Cov Kauj Ruam (nrog Duab)
2024 Tus sau: John Day | [email protected]. Kawg hloov kho: 2024-01-30 09:26
Hauv cov lus qhia no, txoj kab kev tswj hwm tus kheej yuav siv tus neeg hlau thiab yuav hla cov theem hauv qab no:
- Sib Sau Ua Ke
- Txhim kho software ua ntej yuav tsum tau ua
- Khoos phis tawj sib dhos
- Kev Xeem Thawj
- Txheeb xyuas kab kab thiab nthuav tawm txoj kab kev qhia siv openCV
- Ua raws li PD tus tswj hwm
- Cov txiaj ntsig
Kauj Ruam 1: Sib Sau Ua Ke
Cov duab saum toj no qhia tag nrho cov khoom siv hauv txoj haujlwm no:
- Tsheb RC: Kuv tau txais kuv li los ntawm cov khw hauv ib cheeb tsam hauv kuv lub tebchaws. Nws tau nruab nrog 3 lub cev muaj zog (2 rau lub qhov rooj thiab 1 rau txoj hlua). Qhov tsis zoo ntawm lub tsheb no yog tias lub kauj tsheb tau txwv ntawm "tsis muaj lub kauj" thiab "lub kauj puv". Hauv lwm lo lus, nws tsis tuaj yeem txav ntawm lub kaum ntse ntse tshwj xeeb, tsis zoo li servo-steering RC tsheb. Koj tuaj yeem pom cov khoom siv tsheb zoo sib xws tsim tshwj xeeb rau raspberry pi los ntawm no.
- Raspberry pi 3 qauv b+: qhov no yog lub hlwb ntawm lub tsheb uas yuav lis ntau theem ua. Nws yog ua raws plaub quad core 64-ntsis processor moos ntawm 1.4 GHz. Kuv tau txais kuv los ntawm no.
- Raspberry pi 5 mp lub koob yees duab module: Nws txhawb 1080p @ 30 fps, 720p @ 60 fps, thiab 640x480p 60/90 kaw. Nws kuj tseem txhawb nqa ntu txuas uas tuaj yeem txuas ncaj qha rau hauv raspberry pi. Nws tsis yog qhov kev xaiv zoo tshaj plaws rau kev ua duab daim ntawv thov tab sis nws txaus rau qhov haujlwm no ntxiv nrog rau nws pheej yig heev. Kuv tau txais kuv los ntawm no.
- Tsav Tsheb: Siv los tswj cov lus qhia thiab nrawm ntawm DC motors. Nws txhawb kev tswj ntawm 2 dc lub cev muaj zog hauv 1 lub rooj tsavxwm thiab tuaj yeem tiv taus 1.5 A.
- Power Bank (Yeem Xaiv): Kuv siv lub txhab nyiaj fais fab (ntsuas ntawm 5V, 3A) txhawm rau txhawb lub zog raspberry sib cais. Ib kauj ruam qis hloov pauv (phaw hloov pauv: 3A tso tawm tam sim no) yuav tsum siv txhawm rau txhawm rau txhawb lub zog raspberry pi los ntawm 1 qhov chaw.
- 3s (12 V) LiPo roj teeb: Cov roj teeb Lithium Polymer tau paub txog lawv qhov ua tau zoo hauv kev siv neeg hlau. Nws yog siv los txhawb lub zog tsav tsheb. Kuv yuav kuv li ntawm no.
- Txiv neej rau txiv neej thiab poj niam rau poj niam jumper xov hlau.
- Daim kab xev ob tog: Siv los txhim kho cov khoom ntawm lub tsheb RC.
- Daim kab xev xiav: Qhov no yog ib feem tseem ceeb ntawm txoj haujlwm no, nws tau siv los ua ob txoj kab kab uas lub tsheb yuav tsav nruab nrab. Koj tuaj yeem xaiv cov xim twg koj xav tau tab sis kuv pom zoo xaiv cov xim sib txawv dua li cov nyob ib puag ncig.
- Zip ties thiab ntoo tuav.
- Ntsia hlau tsav tsheb.
Kauj Ruam 2: Txhim Kho OpenCV ntawm Raspberry Pi thiab teeb tsa Cov Chaw Taws Teeb
Cov kauj ruam no yog qhov nyuab me ntsis thiab yuav siv qee lub sijhawm.
OpenCV (Qhib qhov Khoos Phis Tawj Lub Zeem Muag) yog lub khoos phis tawj qhib lub zeem muag thiab tshuab kev kawm software lub tsev qiv ntawv. Lub tsev qiv ntawv muaj ntau dua 2500 qhov ua tau zoo ntawm cov txheej txheem. Ua raws li phau ntawv qhia no yooj yim heev rau nruab openCV ntawm koj tus raspberry pi ntxiv rau txhim kho raspberry pi OS (yog tias koj tseem tsis tau ua). Thov nco ntsoov tias txheej txheem ntawm kev tsim lub openCV yuav siv sijhawm kwv yees li 1.5 teev nyob rau hauv chav txias txias (txij li lub tshuab ua haujlwm kub yuav nce siab heev!) Yog li muaj qee cov tshuaj yej thiab ua siab ntev: D.
Rau cov chaw taws teeb pom kev, tseem ua raws li phau ntawv qhia no teeb tsa kev nkag mus rau koj lub raspberry pi los ntawm koj lub Windows/Mac ntaus ntawv.
Kauj Ruam 3: Txuas Qhov Chaw Ua Ke
Cov duab saum toj no qhia txog kev sib txuas ntawm raspberry pi, lub koob yees duab module thiab tsav tsheb. Thov nco ntsoov tias lub cav uas kuv siv nqus 0.35 A ntawm 9 V txhua qhov uas ua rau nws muaj kev nyab xeeb rau tus tsav tsheb tsav 3 lub cav tib lub sijhawm. Thiab txij li kuv xav tswj 2 lub cav nrawm nrawm (1 nraub qaum thiab 1 pem hauv ntej) zoo ib yam nkaus, kuv txuas lawv mus rau tib lub chaw nres nkoj. Kuv ntsia tus tsav tsheb ntawm sab xis ntawm lub tsheb siv ob kab xev. Raws li rau lub koob yees duab module, Kuv tau ntxig rau zip khi ntawm cov ntsia hlau qhov raws li cov duab saum toj no qhia. Tom qab ntawd, Kuv haum lub koob yees duab mus rau ntoo ntoo kom kuv tuaj yeem kho txoj haujlwm ntawm lub koob yees duab raws li kuv xav tau. Sim nruab lub koob yees duab nyob nruab nrab ntawm lub tsheb kom ntau li ntau tau. Kuv pom zoo tso lub koob yees duab yam tsawg 20 cm saum toj no hauv av yog li qhov pom ntawm lub hauv ntej ntawm lub tsheb yuav zoo dua. Fritzing schematic yog txuas hauv qab no.
Kauj Ruam 4: Thawj Xeem
Kev Xeem Lub Koob Yees Duab:
Thaum lub koob yees duab tau teeb tsa, thiab qhib lub tsev qiv ntawv openCV, nws yog lub sijhawm los sim peb thawj daim duab! Peb yuav yees duab los ntawm pi cam thiab khaws nws li "original.jpg". Nws tuaj yeem ua tiav hauv 2 txoj hauv kev:
1. Siv Terminal Commands:
Qhib lub davhlau ya nyob twg qhov rai tshiab thiab ntaus cov lus txib hauv qab no:
raspistill -o original.jpg
Qhov no yuav siv cov duab tseem thiab khaws nws hauv "/pi/original.jpg" cov npe.
2. Siv ib qho nab nab IDE (Kuv siv IDLE):
Qhib tus duab kos tshiab thiab sau cov cai hauv qab no:
ua cv2
video = cv2. VideoCapture (0) thaum muaj tseeb: ret, thav duab = video.read () thav duab = cv2.flip (thav duab, -1) # siv los tig cov duab ntsug cv2.imshow ('thawj', thav duab) cv2. imwrite ('thawj.jpg', thav duab) qhov tseem ceeb = cv2.waitKey (1) yog qhov tseem ceeb == 27: so video.release () cv2.destroyAllWindows ()
Cia saib dab tsi tshwm sim hauv txoj cai no. Thawj kab yog ntshuam peb lub tsev qiv ntawv openCV siv tag nrho nws cov haujlwm. VideoCapture (0) muaj nuj nqi pib tshaj tawm cov vis dis aus los ntawm qhov chaw txiav txim los ntawm txoj haujlwm no, qhov no nws yog 0 uas txhais tau tias yog lub koob yees duab raspi. yog tias koj muaj ntau lub koob yees duab, yuav tsum muaj tus lej sib txawv. video.read () yuav nyeem txhua kab ntawv los ntawm lub koob yees duab thiab khaws nws hauv qhov sib txawv hu ua "thav duab". flip () muaj nuj nqi yuav tig cov duab nrog kev hwm rau y-axis (ntsug) txij li kuv tau teeb tsa kuv lub koob yees duab rov qab. imshow () yuav tso tawm peb cov kab taub hau los ntawm lo lus "thawj" thiab imwrite () yuav txuag peb daim duab li qub.jpg. waitKey (1) yuav tos 1 ms rau txhua lub keyboard keyboard kom raug nias thiab xa rov qab nws ASCII code. yog tias khiav dim (esc) khawm raug nias, tus lej lej ntawm 27 tau rov qab los thiab yuav ua txhaum lub voj raws li. video.release () yuav tso tseg kaw thiab rhuav tshem AllWindows () yuav kaw txhua daim duab qhib los ntawm imshow () muaj nuj nqi.
Kuv pom zoo ntsuas koj lub yees duab nrog txoj hauv kev thib ob kom paub txog kev ua haujlwm openCV. Qhov duab tau khaws tseg hauv "/pi/original.jpg" cov npe. Thawj daim duab uas kuv lub koob yees duab thaij tau qhia rau saud.
Kuaj Cov Tsav Tsheb:
Cov kauj ruam no yog qhov tseem ceeb los txiav txim siab kev taw qhia ntawm kev sib hloov ntawm txhua lub cav. Ua ntej, cia peb muaj cov lus qhia luv luv ntawm txoj cai ua haujlwm ntawm tus tsav tsheb. Cov duab saum toj no qhia tus pin tsav tsheb. Pab kom A, Cov tswv yim 1 thiab Cov tswv yim 2 cuam tshuam nrog lub cav A tswj. Pab kom B, Cov tswv yim 3 thiab cov tswv yim 4 cuam tshuam nrog kev tswj lub cev B. Kev tswj kev taw qhia tau tsim los ntawm "Kev nkag mus" ntu thiab kev tswj nrawm tau tsim los ntawm "Pab" Txhawm rau tswj cov kev taw qhia ntawm lub cev A piv txwv, teeb tsa Lub Tswv Yim 1 rau HIGH (3.3 V hauv qhov no vim tias peb tab tom siv raspberry pi) thiab teeb tsa Input 2 mus rau LOW, lub cav yuav tig rau hauv qhov kev qhia tshwj xeeb thiab los ntawm kev teeb tsa qhov sib txawv rau Kev Tawm Tswv Yim 1 thiab Kev Nkag Siab 2, lub cav yuav tig rov qab. Yog tias Tswv Yim 1 = Tswv Yim 2 = (HIGH lossis LOW), lub cav yuav tsis tig. Pab kom pins siv Pulse Width Modulation (PWM) cov teeb liab tawm los ntawm raspberry (0 txog 3.3 V) thiab khiav lub cav kom haum. Piv txwv li, 100% PWM teeb liab txhais tau tias peb tab tom ua haujlwm ceev tshaj plaws thiab 0% PWM teeb liab txhais tau tias lub cav tsis tig. Cov cai hauv qab no yog siv los txiav txim cov tshuab taw qhia thiab ntsuas lawv qhov nrawm.
lub sij hawm ntshuam
ntshuam RPi. GPIO li GPIO GPIO.setwarnings (Cuav) # Lub Cev Tsav Pins steering_enable = 22 # Lub cev Pin 15 in1 = 17 # Lub cev Pin 11 in2 = 27 # Lub Cev Pin 13 #Throttle Motors Pins throttle_enable = 25 # Lub Cev Pin 22 in3 = 23 # Lub Cev Pin 16 in4 = 24 # Lub Cev Pin 18 GPIO.setmode (GPIO. BCM) # Siv GPIO tus lej tsis siv tus lej lub cev GPIO.setup (hauv 1, GPIO.out) GPIO.setup (hauv 2, GPIO.out) GPIO. teeb tsa (hauv 3, GPIO.out) GPIO.setup (hauv 4, GPIO.out) GPIO.setup (throttle_enable, GPIO.out) GPIO.setup (steering_enable, GPIO.out) # Steering Motor Control GPIO.output (in1, GPIO. HIGH) GPIO.output (in2, GPIO. LOW) kauj = GPIO. PWM (steering_enable, 1000) # teeb tsa qhov hloov pauv mus rau 1000 Hz steering.stop () # Lub Cav Hluav Taws Xob Tswj GPIO.output (in3, GPIO. HIGH) GPIO.output (in4, GPIO. LOW) throttle = GPIO. PWM (throttle_enable, 1000) # teeb tsa qhov hloov pauv mus rau 1000 Hz throttle.stop () time.sleep (1) throttle.start (25) # pib lub cav ntawm 25 % PWM teeb liab-> (0.25 * roj teeb Voltage) - tsav tsheb poob steering.start (100) # pib lub cav ntawm 100% PWM teeb liab-> (1 * Roj Teeb Hluav Taws Xob) - tus tsav tsheb poob lub sijhawm. pw tsaug zog (3) throttle.stop () steering.stop ()
Txoj cai no yuav siv lub tshuab ua kom lub cav thiab lub cav tsav rau 3 vib nas this thiab tom qab ntawd yuav nres lawv. Qhov (tus neeg tsav tsheb poob) tuaj yeem txiav txim siab siv lub voltmeter. Piv txwv li, peb paub tias 100% PWM teeb liab yuav tsum muab lub roj teeb tag nrho ntawm lub cav lub dav hlau. Tab sis, los ntawm kev teeb tsa PWM rau 100%, Kuv pom tias tus tsav tsheb ua rau 3 V poob thiab lub cav tau txais 9 V hloov 12 V (raws nraim qhov kuv xav tau!). Qhov poob tsis yog linear piv txwv li poob ntawm 100% yog qhov sib txawv heev ntawm qhov poob ntawm 25%. Tom qab khiav cov cai saum toj no, kuv cov txiaj ntsig tau raws li hauv qab no:
Throttling Results: yog tias in3 = HIGH thiab in4 = LOW, lub cav siv lub cav yuav muaj Clock-Wise (CW) kev sib hloov piv txwv li lub tsheb yuav txav mus tom ntej. Txwv tsis pub, lub tsheb yuav txav rov qab.
Cov Ntsiab Lus Taw Qhia: yog in1 = HIGH thiab in2 = LOW, lub kauj tsheb yuav tig ntawm nws qhov siab tshaj plaws xws li lub tsheb yuav tsav mus rau sab laug. Txwv tsis pub, lub tsheb yuav tsav txoj cai. Tom qab qee qhov kev sim, kuv pom tias lub kauj tsheb yuav tig tsis tau yog tias PWM teeb liab tsis yog 100% (piv txwv li lub cav yuav tsav ncaj nraim mus rau sab xis lossis sab laug).
Kauj Ruam 5: Txheeb Xyuas Txoj Kab Kab thiab Xam Kab Kab Lus
Hauv theem no, cov txheej txheem uas yuav tswj hwm lub tsheb lub zog yuav piav qhia. Thawj daim duab qhia tag nrho cov txheej txheem. Cov tswv yim ntawm lub kaw lus yog cov duab, cov zis yog theta (lub kaum sab xis hauv qib). Nco ntsoov tias, kev ua tiav ntawm 1 daim duab thiab yuav rov ua dua ntawm txhua daim duab.
Lub Koob Yees Duab:
Lub koob yees duab yuav pib kaw cov vis dis aus nrog (320 x 240) daws teeb meem. Kuv pom zoo txo qhov kev daws teeb meem yog li koj tuaj yeem tau txais tus lej zoo dua (fps) txij li fps poob yuav tshwm sim tom qab thov ua cov txheej txheem rau txhua tus ncej. Cov cai hauv qab no yuav yog lub ntsiab lus ntawm txoj haujlwm thiab yuav ntxiv txhua kauj ruam hla txoj cai no.
ua cv2
import numpy as np video = cv2. VideoCapture (0) video.set (cv2. CAP_PROP_FRAME_WIDTH, 320) # teeb tsa qhov dav rau 320 p video.set (cv2. CAP_PROP_FRAME_HEIGHT, 240) # teeb tsa qhov siab rau 240 p # Lub voj thaum Tseeb: ret, thav duab = video.read () thav duab = cv2.flip (thav duab, -1) cv2.imshow ("thawj", thav duab) qhov tseem ceeb = cv2.waitKey (1) yog qhov tseem ceeb == 27: tawg video.release () cv2.destroyAllWindows ()
Cov cai ntawm no yuav qhia thawj daim duab uas tau txais hauv qib 4 thiab tau qhia hauv cov duab saum toj no.
Hloov mus rau HSV Xim Chaw:
Tam sim no tom qab siv cov vis dis aus kaw ua cov thav duab los ntawm lub koob yees duab, cov kauj ruam tom ntej yog hloov pauv txhua lub thav duab rau hauv Hawj Hawj, Saturation, thiab Tus Nqi (HSV) qhov chaw xim. Qhov txiaj ntsig tseem ceeb ntawm kev ua li ntawd yog kom muaj peev xwm sib txawv ntawm cov xim los ntawm lawv qib kev ci ntsa iab. Thiab ntawm no yog cov lus piav qhia zoo ntawm HSV xim qhov chaw. Kev hloov pauv mus rau HSV tau ua tiav ntawm cov haujlwm hauv qab no:
def hloov_to_HSV (thav duab):
hsv = cv2.cvtColor (frame, cv2. COLOR_BGR2HSV) cv2.imshow ("HSV", hsv) rov hsv
Lub luag haujlwm no yuav raug hu los ntawm lub voj loj thiab yuav rov qab thav duab hauv HSV qhov chaw xim. Lub thav duab tau txais los ntawm kuv hauv qhov chaw xim HSV tau qhia saum toj no.
Ntes Xiav Xim thiab Ntug:
Tom qab hloov cov duab mus rau qhov chaw xim HSV, nws yog lub sijhawm los tshuaj xyuas tsuas yog cov xim uas peb txaus siab rau (piv txwv li xim xiav vim nws yog xim kab kab). Txhawm rau rho tawm xim xiav los ntawm HSV thav duab, ntau yam ntawm cov xim zas, saturation thiab tus nqi yuav tsum tau teev tseg. xa mus rau ntawm no kom muaj lub tswv yim zoo ntawm HSV qhov txiaj ntsig. Tom qab qee qhov kev sim, sab saud thiab qis dua ntawm cov xim xiav tau qhia hauv cov cai hauv qab no. Thiab txhawm rau txo qhov kev cuam tshuam tag nrho hauv txhua kab ntawv, cov npoo tau pom tsuas yog siv cov nplaim hluav taws xob ntes. Xav paub ntau ntxiv txog canny ntug tau pom ntawm no. Txoj cai ntawm tus ntiv tes xoo yog xaiv qhov tsis muaj peev xwm ntawm Canny () ua haujlwm nrog qhov sib piv ntawm 1: 2 lossis 1: 3.
def detect_edges (thav duab):
lower_blue = np.array ([90, 120, 0], dtype = "uint8") # qis txwv ntawm cov xim xiav upper_blue = np.array ([150, 255, 255], dtype = "uint8") # txwv tsis pub tshaj daim npog ntsej muag xiav = cv2.inRange (hsv, qis_blue, Upper_blue) # daim npog no yuav lim tawm txhua yam tab sis xiav # pom cov npoo ntug = cv2. Canny (daim npog, 50, 100) cv2.imshow ("ntug", ntug) rov qab
Lub luag haujlwm no tseem yuav raug hu los ntawm lub voj tseem ceeb uas siv raws li qhov ntsuas ntawm HSV xim qhov chaw thav duab thiab rov ua cov kab rov tav. Cov kab uas kuv tau txais yog pom saum toj no.
Xaiv cheeb tsam uas txaus siab (ROI):
Xaiv thaj tsam ntawm kev txaus siab yog qhov tseem ceeb kom tsom mus rau tsuas yog 1 cheeb tsam ntawm tus ncej. Hauv qhov no, Kuv tsis xav kom lub tsheb pom ntau yam khoom hauv ib puag ncig. Kuv tsuas xav kom lub tsheb tsom mus rau kab kab thiab tsis quav ntsej lwm yam. PS: kev sib koom ua ke (x thiab y axes) pib los ntawm kaum sab laug. Hauv lwm lo lus, lub ntsiab lus (0, 0) pib los ntawm kaum sab saud sab saud. y-axis yog qhov siab thiab x-axis yog qhov dav. Cov cai hauv qab no xaiv thaj tsam ntawm kev txaus siab los tsom mus rau qis ib nrab ntawm tus ncej.
def cheeb tsam_of_interest (ntug):
qhov siab, dav = npoo.shape # rho tawm qhov siab thiab dav ntawm cov npoo daim npog = np.zeros_like (ntug) # ua qhov khoob khoob nrog tib qhov ntev ntawm cov npoo thav duab # tsuas yog tsom qis ib nrab ntawm lub vijtsam # qhia qhov chaw ua haujlwm ntawm 4 ntsiab lus (sab laug, sab laug, sab xis, sab xis) polygon = np.array (
Lub luag haujlwm no yuav siv cov txheej txheem edged ua qhov ntsuas thiab kos lub ntsej muag nrog 4 cov ntsiab lus ua ntej. Nws yuav tsuas yog tsom mus rau dab tsi hauv lub polygon thiab tsis quav ntsej txhua yam sab nraud nws. Kuv cheeb tsam ntawm kev txaus siab thav duab tau qhia saum toj no.
Ntes Kab Kab:
Hough hloov pauv tau siv los txheeb xyuas kab ntu los ntawm cov kab ntug. Hough hloov pauv yog txheej txheem txhawm rau txheeb xyuas txhua yam duab hauv cov lej. Nws tuaj yeem txheeb xyuas yuav luag txhua yam khoom txawm tias nws cuam tshuam raws li qee tus lej pov npav. cov ntaub ntawv zoo rau Hough hloov pauv tau qhia ntawm no. Rau daim ntawv thov no, cv2. HoughLinesP () muaj nuj nqi tau siv los tshuaj xyuas kab hauv txhua kab ntawv. Qhov tseem ceeb tsis ua haujlwm no yog:
cv2. HoughLinesP (thav duab, rho, theta, min_threshold, minLineLength, maxLineGap)
- Ncej: yog thav duab peb xav pom kab hauv.
- rho: Nws yog qhov kev ncua deb qhov tseeb hauv pixels (feem ntau nws yog = 1)
- theta: angular precision hauv radians (ib txwm = np.pi/180 ~ 1 degree)
- min_threshold: qhov pov npav tsawg kawg nws yuav tsum tau txais rau nws los txiav txim siab ua kab
- minLineLength: yam tsawg kawg ntawm kab hauv pixels. Ib kab twg luv dua tus lej no tsis suav tias yog kab.
- maxLineGap: qhov sib txawv ntau kawg hauv pixels ntawm 2 kab kom raug kho raws li 1 kab. (Nws tsis siv rau hauv kuv cov ntaub ntawv txij li txoj kab kab kuv tau siv tsis muaj qhov sib txawv).
Txoj haujlwm no rov ua qhov kawg ntawm kab. Cov haujlwm hauv qab no yog hu los ntawm kuv lub voj tseem ceeb txhawm rau txheeb xyuas kab siv Hough hloov pauv:
def detect_line_segments (cropped_edges):
rho = 1 theta = np.pi / 180 min_threshold = 10 line_segments = cv2. HoughLinesP (cropped_edges, rho, theta, min_threshold, np.array (), minLineLength = 5, maxLineGap = 0) return line_segments
Qhov nruab nrab nqes hav thiab cuam tshuam (m, b):
nco qab tias qhov sib npaug ntawm kab muab los ntawm y = mx + b. Qhov twg m yog txoj kab nqes ntawm kab thiab b yog y-cuam tshuam. Hauv qhov no, qhov nruab nrab ntawm txoj kab nqes thiab cuam tshuam ntawm kab ntu uas pom siv Hough hloov pauv yuav raug suav. Ua ntej ua qhov no, cia saib ntawm daim duab thav duab qub uas tau hais los saum no. Txoj kab sab laug zoo li yuav nce mus ntxiv yog li nws muaj qhov nqes hav tsis zoo (nco qab qhov kev sib koom tes pib qhov pib?). Hauv lwm lo lus, txoj kab kab sab laug muaj x1 <x2 thiab y2 x1 thiab y2> y1 uas yuav muab qhov nqes hav zoo. Yog li, txhua kab nrog txoj kab nqes hav zoo raug txiav txim siab txoj kab kab. Yog cov kab ntsug (x1 = x2), txoj kab nqes yuav tsis muaj qhov kawg. Hauv qhov no, peb yuav hla txhua txoj kab ntsug kom tiv thaiv kom tau txais qhov yuam kev. Txhawm rau ntxiv qhov tseeb rau qhov ntsuas pom no, txhua tus ncej tau muab faib ua ob cheeb tsam (sab xis thiab sab laug) mus txog 2 kab ciam teb. Tag nrho cov ntsiab lus dav (x-axis ntsiab lus) ntau dua li txoj kab ciam teb raug cai, cuam tshuam nrog txoj kab txoj cai suav. Thiab yog tias txhua qhov dav cov ntsiab lus tsawg dua kab sab laug, lawv cuam tshuam nrog txoj kab sab laug. Cov haujlwm hauv qab no siv lub thav duab raws li kev ua thiab kab ntu pom pom siv Hough hloov pauv thiab rov qab nruab nrab txoj kab nqes thiab cuam tshuam ntawm ob txoj kab kab.
def nruab nrab_slope_intercept (thav duab, kab_segments):
lane_lines = yog line_segments Tsis muaj: luam tawm ("tsis muaj kab ntu pom") rov qab kab kab qhov siab, dav, _ = thav duab. dav * ciam rau kab_segment hauv kab_segments: rau x1, y1, x2, y2 hauv kab_segment: yog x1 == x2: print ("hla kab ntsug (nqes hav = infinity)") txuas ntxiv haum = np.polyfit ((x1, x2), (y1, y2), 1) nqes hav = (y2 - y1) / (x2 - x1) cuam tshuam = y1 - (nqes hav * x1) yog nqes hav <0: yog x1 <left_region_boundary thiab x2 right_region_boundary thiab x2> right_region_boundary: right_fit. append ((txoj kab nqes, cuam tshuam)) left_fit_average = np.average (left_fit, axis = 0) if len (left_fit)> 0: lane_lines.append (make_points (frame, left_fit_average)) right_fit_average = np.average (right_fit, axis = 0)) yog tias len (right_fit)> 0: lane_lines.append (make_points (thav duab, right_fit_average)) # txoj kab_lines yog 2-D array suav nrog txoj haujlwm ntawm txoj kab thiab txoj kab kab sab laug # piv txwv: lan e_lines =
make_points () yog tus pab ua haujlwm rau nruab nrab_slope_intercept () muaj nuj nqi uas yuav xa rov qab cov kev tswj hwm ntawm txoj kab kab (los ntawm hauv qab mus rau nruab nrab ntawm tus ncej).
def make_points (thav duab, kab):
qhov siab, dav, _ = thav duab. Qhov nqes hav, cuam tshuam = kab y1 = qhov siab # hauv qab ntawm tus ncej y2 = int (y1 / 2) # ua cov ntsiab lus los ntawm nruab nrab ntawm tus ncej nqes yog nqes hav == 0: nqes hav = 0.1 x1 = int ((y1 - cuam tshuam) / nqes hav) x2 = int ((y2 - cuam tshuam) / nqes hav) rov qab
Txhawm rau tiv thaiv kev faib ua 0, ib qho xwm txheej tau nthuav tawm. Yog tias nqes hav = 0 uas txhais tau tias y1 = y2 (kab rov tav), muab txoj kab nqes tus nqi ze 0. Qhov no yuav tsis cuam tshuam rau kev ua haujlwm ntawm cov txheej txheem ntxiv nrog rau nws yuav tiv thaiv tsis tau cov ntaub ntawv (faib ua 0).
Txhawm rau tso tawm kab kab ntawm kab kab, cov haujlwm hauv qab no tau siv:
def display_lines (kab, kab, kab_color = (0, 255, 0), kab_width = 6): # kab xim (B, G, R)
line_image = np. line_width) line_image = cv2.addWeighted (thav duab, 0.8, line_image, 1, 1) rov line_image
cv2.addWeighted () muaj nuj nqi siv cov ntsuas hauv qab no thiab nws tau siv los ua ke ob daim duab tab sis nrog rau muab ib qho hnyav.
cv2.addWeighted (duab 1, alpha, duab 2, beta, gamma)
Thiab suav cov duab tso tawm siv cov zauv hauv qab no:
cov zis = alpha * image1 + beta * image2 + gamma
Xav paub ntau ntxiv txog cv2.addWeighted () muaj nuj nqi tau muab los ntawm no.
Xam thiab Zaub Lub Npe Kab:
Qhov no yog cov kauj ruam kawg ua ntej peb thov nrawm rau peb lub cav. Cov kab kab lus yog lub luag haujlwm muab lub kauj tsheb rau qhov kev taw qhia uas nws yuav tsum tig thiab muab lub cav tso rau qhov nrawm uas lawv yuav ua haujlwm. Xam cov kab lus kab lus yog qhov tseeb trigonometry, tan thiab atan (tan^-1) trigonometric functions tau siv. Qee qhov xwm txheej hnyav yog thaum lub koob yees duab pom tsuas yog ib txoj kab kab lossis thaum nws tsis pom kab twg. Txhua qhov xwm txheej no tau qhia hauv cov haujlwm hauv qab no:
def tau_steering_angle (thav duab, kab_lines):
qhov siab, dav, _ = frame.shape yog len (lane_lines) == 2: # yog tias ob txoj kab kab raug kuaj pom _, _, left_x2, _ = lane_lines [0] [0] # rho tawm x2 ntawm lane_lines array _, _, right_x2, _ = lane_lines [1] [0] # rho tawm txoj cai x2 los ntawm lane_lines array nruab nrab = int (dav / 2) x_offset = (left_x2 + right_x2) / 2 - nruab nrab y_offset = int (qhov siab / 2) elif len (txoj kab_lines) == 1: # yog tsuas pom ib kab nkaus xwb x1, _, x2, _ = lane_lines [0] [0] x_offset = x2 - x1 y_offset = int (qhov siab / 2) elif len (lane_lines) == 0: # yog tias tsis pom kab x_offset = 0 y_offset = int (qhov siab / 2) angle_to_mid_radian = lej.atan (x_offset / y_offset) angle_to_mid_deg = int (angle_to_mid_radian * 180.0 / math.pi) steering_angle = angle_to_mid_deg + 90 rov qab steering_angle
x_offset hauv thawj kis yog qhov nruab nrab ntau npaum li cas ((txoj cai x2 + sab laug x2) / 2) txawv ntawm nruab nrab ntawm lub vijtsam. y_offset ib txwm coj los ua qhov siab / 2. Cov duab kawg saum toj no qhia piv txwv ntawm kab lus. angle_to_mid_radians zoo ib yam li "theta" qhia hauv daim duab kawg saum toj no. Yog hais tias steering_angle = 90, nws txhais tau tias lub tsheb muaj kab kab sib dhos rau "qhov siab / 2" kab thiab lub tsheb yuav txav mus tom ntej yam tsis muaj lub kauj. Yog hais tias steering_angle> 90, lub tsheb yuav tsum tsav mus rau txoj cai txwv tsis pub nws yuav tsum mus rau sab laug. Txhawm rau tso tawm kab kab lus, siv cov haujlwm hauv qab no:
def display_heading_line (thav duab, steering_angle, line_color = (0, 0, 255), line_width = 5)
head_image = np.zeros_like (thav duab) qhov siab, dav, _ = frame.shape steering_angle_radian = steering_angle / 180.0 * math.pi x1 = int (width / 2) y1 = height x2 = int (x1 - height / 2 / math.tan (steering_angle_radian)) y2 = int (qhov siab / 2) cv2.line (nqe lus _image, (x1, y1), (x2, y2), kab_color, kab_width) nqe lus_image = cv2.addWeighted (thav duab, 0.8, nqe lus_image, 1, 1) rov qab head_image
Cov haujlwm saum toj no yuav siv tus ncej uas kab kab lus yuav tau kos rau thiab lub kaum sab xis raws li kev nkag. Nws rov qab cov duab ntawm kab lus. Cov kab lus kab lus coj los hauv kuv rooj plaub yog qhia hauv daim duab saum toj no.
Ua ke Txhua Txoj Cai Ua Ke:
Txoj cai tam sim no tau npaj los sib sau ua ke. Cov cai hauv qab no qhia txog lub ntsiab lus tseem ceeb ntawm txoj haujlwm hu rau txhua txoj haujlwm:
ua cv2
import numpy as np video = cv2. VideoCapture (0) video.set (cv2. CAP_PROP_FRAME_WIDTH, 320) video.set (cv2. CAP_PROP_FRAME_HEIGHT, 240) thaum muaj tseeb: ret, thav duab = video.read () thav duab = cv2.flip (thav duab, -1) #Calling the functions hsv = convert_to_HSV (thav duab) npoo = detect_edges (hsv) roi = region_of_interest (ntug) line_segments = detect_line_segments (roi) lane_lines = nruab nrab_slope_intercept (thav duab, kab_segments) lane_lines_image = display_lines (kab lus) = get_steering_angle (thav duab, kab_lines) nqe lus_image = display_heading_line (kab ntawv_ kab_image, steering_angle) qhov tseem ceeb = cv2.waitKey (1) yog qhov tseem ceeb == 27: so video.release () cv2.destroyAllWindows ()
Kauj Ruam 6: Ua Ntawv Thov PD
Tam sim no peb muaj peb lub kaum sab xis npaj tau pub rau cov motors. Raws li tau hais ua ntej, yog lub kaum sab xis loj dua 90, lub tsheb yuav tsum tig sab xis yog li nws yuav tsum tig sab laug. Kuv tau thov cov cai yooj yim uas tig lub kauj tsheb kom yog yog lub kaum sab xis siab dua 90 thiab tig nws sab laug yog lub kaum sab xis qis dua 90 ntawm qhov nrawm nrawm ntawm (10% PWM) tab sis kuv tau txais ntau qhov yuam kev. Qhov yuam kev tseem ceeb uas kuv tau txais yog thaum lub tsheb txav mus rau ib qho twg, lub kauj tsheb ua haujlwm ncaj qha tab sis lub tshuab ua kom lub cav tsoo. Kuv tau sim ua kom lub nrawm nrawm dua (20% PWM) ntawm qhov tig tab sis xaus nrog tus neeg hlau tawm ntawm txoj kab. Kuv xav tau qee yam uas ua rau kom nrawm nrawm ntau yog tias lub kaum sab xis tig loj heev thiab ua kom nrawm dua me ntsis yog tias lub kaum sab xis tsis loj npaum li ntawd yuav txo qhov nrawm mus rau tus nqi pib thaum lub tsheb mus txog 90 degrees (txav ncaj). Txoj kev daws teeb meem yog siv tus tswj hwm PD.
PID maub los sawv rau Kev Sib Piv, Kev Sib Txuas thiab Kev Tswj Derivative. Hom kev tswj kab no tau siv dav hauv kev siv neeg hlau. Cov duab saum toj no qhia txog PID cov lus tawm tswv yim tswj lub voj. Lub hom phiaj ntawm tus tswj hwm no yog kom mus txog "teeb tsa" nrog txoj kev ua tau zoo tshaj plaws tsis zoo li "rau - tawm" cov tswj uas qhib lossis tua cov nroj tsuag raws li qee qhov xwm txheej. Qee cov ntsiab lus yuav tsum paub:
- Setpoint: yog tus nqi xav tau uas koj xav kom koj lub cev mus txog.
- Tus nqi tiag: yog tus nqi tiag tiag tau hnov los ntawm lub sensor.
- Yuam kev: yog qhov sib txawv ntawm setpoint thiab tus nqi tiag (yuam kev = Setpoint - Tus nqi tiag).
- Kev tswj hwm sib txawv: los ntawm nws lub npe, qhov sib txawv koj xav tswj.
- Kp: Qhov sib piv tsis tu ncua.
- Ki: Ib qho tsis tu ncua.
- Kd: Derivative qhov tsis tu ncua.
Hauv ntej, PID kev tswj hwm lub voj ua haujlwm raws li hauv qab no:
- Tus neeg siv piav qhia qhov teeb tsa xav tau rau lub kaw lus kom mus txog.
- Qhov yuam kev yog xam (yuam kev = setpoint - qhov tseeb).
- P tus tswj hwm tsim qhov kev ua haujlwm sib luag rau qhov ua yuam kev tus nqi. (yuam kev nce, P kev nqis tes kuj nce ntxiv)
- Kuv tus tswj hwm yuav suav nrog qhov ua yuam kev dhau lub sijhawm uas tshem tawm cov kab ke hauv lub xeev tsis raug cai tab sis ua rau nws nrawm dua.
- D maub los tsuas yog lub sijhawm los ntawm qhov ua yuam kev. Hauv lwm lo lus, nws yog txoj kab nqes ntawm qhov ua yuam kev. Nws ua qhov kev nqis tes ua piv rau qhov ua yuam kev. Tus tswj hwm no ua kom muaj kev ruaj ntseg ntawm lub kaw lus.
- Cov zis ntawm tus tswj hwm yuav yog qhov sib npaug ntawm peb tus tswj. Tus tswj qhov tso tawm yuav dhau 0 yog qhov ua yuam kev 0.
Cov lus piav qhia zoo ntawm PID tus tswj hwm tuaj yeem pom ntawm no.
Rov qab mus rau txoj kab kev khaws tsheb, kuv qhov kev tswj hwm sib txawv tau ua kom nrawm nrawm (txij li lub kauj tsheb tsuas muaj ob lub xeev yog txoj cai lossis sab laug). Tus tswj hwm PD tau siv rau lub hom phiaj no txij li D nqis tes ua kom nrawm nrawm ntau yog tias qhov kev hloov pauv yuam kev loj heev (piv txwv tias muaj qhov sib txawv loj) thiab ua rau lub tsheb qeeb qeeb yog qhov yuam kev hloov pauv mus 0. Kuv tau ua cov hauv qab no txhawm rau siv PD maub los:
- Teem lub teeb taw tes rau 90 degrees (Kuv ib txwm xav kom lub tsheb txav ncaj)
- Xam qhov sib txawv ntawm lub kaum sab xis
- Qhov sib txawv muab ob yam ntaub ntawv: qhov loj npaum li cas qhov ua yuam kev yog (qhov loj ntawm qhov sib txawv) thiab txoj kev taw qhia lub kauj tsheb yuav tsum ua li cas (kos npe ntawm qhov sib txawv). Yog tias qhov sib txawv yog qhov zoo, lub tsheb yuav tsum tsav txoj cai txwv tsis pub nws yuav tsum txav mus sab laug.
- Txij li qhov sib txawv yog qhov tsis zoo lossis qhov zoo, qhov "yuam kev" sib txawv tau piav qhia thiab ib txwm sib npaug rau qhov muaj txiaj ntsig ntawm qhov sib txawv.
- Qhov ua yuam kev yog sib npaug los ntawm qhov tsis tu ncua Kp.
- Qhov ua yuam kev dhau lub sijhawm sib txawv thiab sib tw los ntawm qhov tsis tu ncua Kd.
- Lub cav tau hloov kho tshiab thiab lub voj pib dua.
Cov cai hauv qab no yog siv nyob rau hauv lub voj tseem ceeb los tswj lub cav nrawm nrawm:
speed = 10 # operating speed in % PWM
#Variables yuav tsum tau hloov kho txhua lub voj lastTime = 0 lastError = 0 # PD tsis tu ncua Kp = 0.4 Kd = Kp * 0.65 Thaum Tseeb: tam sim no = time.time () # lub sijhawm tam sim no sib txawv dt = tam sim no - lastTime sib txawv = steering_angle - 90 # sib npaug rau angle_to_mid_deg qhov txawv txav yuam kev = abs (deviation) yog qhov sib txawv -5: # tsis txhob tsav yog tias muaj 10 -degree kev ua yuam kev ntau yam sib txawv = 0 yuam kev = 0 GPIO.output (hauv 1, GPIO. LOW) GPIO.output (in2, GPIO. LOW) steering.stop () elif deviation> 5: # txiav txoj cai yog tias qhov sib txawv yog qhov zoo GPIO.output (in1, GPIO. LOW) GPIO.output (in2, GPIO. HIGH) steering.start (100) elif deviation < -5: # txiav tawm sab laug yog qhov txawv txav tsis zoo GPIO.output (in1, GPIO. HIGH) GPIO.output (in2, GPIO. LOW) steering.start (100) derivative = kd * (error - lastError) / dt proportional = kp * yuam kev PD = int (nrawm + derivative + proportional) spd = abs (PD) yog spd> 25: spd = 25 throttle.start (spd) lastError = yuam kev lastTime = time.time ()
Yog tias qhov ua yuam kev loj heev (qhov sib txawv ntawm qhov nruab nrab yog siab), qhov sib piv thiab cov txiaj ntsig tau ua yog siab uas ua rau muaj qhov nrawm nrawm. Thaum qhov ua yuam kev ze 0 (qhov sib txawv ntawm qhov nruab nrab qis), qhov kev txiav txim los ntawm kev ua haujlwm rov qab (txoj kab nqes tsis zoo) thiab qhov nrawm nrawm tau qis los tswj kev ruaj ntseg ntawm lub system. Cov cai tag nrho yog txuas hauv qab no.
Kauj Ruam 7: Cov txiaj ntsig
Cov vis dis aus saum toj no qhia cov txiaj ntsig uas kuv tau txais. Nws xav tau kev kho dua thiab hloov kho ntxiv. Kuv tab tom txuas raspberry pi rau kuv lub vijtsam LCD vim tias cov vis dis aus hla kuv lub network muaj latency siab thiab ua rau muaj kev ntxhov siab heev ua haujlwm nrog, yog vim li cas thiaj muaj cov xov hlau txuas nrog raspberry pi hauv video. Kuv siv cov laug cam los kos cov kab ntawm.
Kuv tos kom hnov koj cov lus pom zoo kom ua txoj haujlwm no zoo dua! Raws li kuv vam tias phau ntawv qhia no zoo txaus los muab qee cov ntaub ntawv tshiab rau koj.
Pom zoo:
Raspberry Pi - Autonomous Mars Rover Nrog OpenCV Lub Hom Phiaj Nrhiav: 7 Cov Kauj Ruam (nrog Duab)
Raspberry Pi - Autonomous Mars Rover Nrog OpenCV Lub Hom Phiaj Saib Xyuas: Txhawb nqa los ntawm Raspberry Pi 3, Qhib CV cov khoom paub, Ultrasonic sensors thiab ntsuas DC motors. Lub rover no tuaj yeem taug qab txhua yam khoom nws tau kawm rau thiab txav mus rau txhua qhov av
Txawb Xov Tooj Siv Yooj Yim Siv STM32F407 Cov Khoom Siv Tshawb Pom thiab GSM A6 Module: 14 Cov Kauj Ruam (nrog Duab)
Kev Siv Xov Tooj Ntawm Lub Xov Tooj Siv STM32F407 Cov Khoom Siv Tshawb Pom thiab GSM A6 Module: Koj puas tau xav tsim qhov kev ua kom txias txias? Yog tias yog, ua li cas txog kev tsim ib qho ntawm cov neeg nyiam tshaj plaws thiab txhua tus nyiam cov khoom siv xws li Lub Xov Tooj Txawb !!!. Hauv Cov Lus Qhia no, Kuv yuav qhia koj yuav tsim lub xov tooj txawb yooj yim siv STM li cas
Cov Duab Duab Duab thiab Cov Duab 1: 4 Cov Kauj Ruam (nrog Duab)
Cov Duab Duab Cubes thiab Cov Duab 1: Thaum sim nrog qee cov ntoo ntoo thiab cov duab los qhia, kuv pom ob peb txoj hauv kev los ua cov voos thiab lwm yam duab los ntawm cov khoom siv yooj yim. Los ntawm kev muab cov no los ua Cov Ntawv Qhia, Kuv vam tias yuav txhawb nqa kev ua si thiab kev kawm. Kev hloov pauv ntawm qhov kev qhia no
8 Relay Tswj Nrog NodeMCU thiab IR Txais Txais Siv WiFi thiab IR Chaw Taws Teeb thiab Android App: 5 Kauj Ruam (nrog Duab)
8 Relay Tswj Nrog NodeMCU thiab IR Txais Txais Siv WiFi thiab IR Chaw Taws Teeb thiab Android App: Tswj 8 qhov hloov pauv hloov siv nodemcu thiab ir tus txais tshaj wifi thiab ir chaw taws teeb thiab hauv app.the ir tej thaj chaw deb ua haujlwm ywj pheej ntawm wifi txuas. NTUJ
Cov Cuab Yeej Siv Hluav Taws Xob thiab Cov Cuam Tshuam Xov: Ua LED Zaub thiab Npuag Hluav Taws Xob uas Rolls Up: 7 Cov Kauj Ruam (nrog Duab)
Cov Cuab Yeej Siv Hluav Taws Xob thiab Cov Cuam Tshuam: Ua kom pom LED thiab Cov Khoom Siv Hluav Taws Xob uas Rolls Up: Ua rau koj tus kheej cov ntaub ua hluav taws xob, xov, kua nplaum, thiab kab xev, thiab siv lawv los ua lub zog ntsuas hluav taws xob, tiv thaiv, hloov pauv, LED cov lus qhia thiab siv hluav taws xob. thiab cov xov hluav taws xob koj tuaj yeem ua cov lus qhia LED thiab cov kab hluav taws xob ntawm ib qho ntaub uas hloov pauv tau