Étape 18 : OpenCV : Centre de gravité moyenne et génération de cibles
#//// Move Bot //////////////////////////////////////#Don't start moving until things are ready.if iFrame >=160: #This compares the bot's heading with the target angle. It must#be +-30 for the bot to move forward, otherwise it will turn.if intHeadingDeg <= (targetDegs +30) and intHeadingDeg >+ (targetDegs -30): tranx =3 motorDuration =10#I'll use later#Forwardelse: if intHeadingDeg < targetDegs: if1< (targetDegs - intHeadingDeg): #abs(intHeadingDeg - targetDegs) >= 180: tranx =2 motorDuration =10print (intHeadingDeg - targetDegs) print"Right 1"elif1> (targetDegs - intHeadingDeg): #abs(intHeadingDeg - targetDegs) < 180: tranx =4 motorDuration =10print (intHeadingDeg - targetDegs) print"Left 1"elif intHeadingDeg >= targetDegs: if1< (targetDegs - intHeadingDeg): #abs(intHeadingDeg - targetDegs) <= 180: tranx =2 motorDuration =10print (intHeadingDeg - targetDegs) print"Right 2"elif1> (targetDegs - intHeadingDeg): #abs(intHeadingDeg - targetDegs) > 180: tranx =4 motorDuration =10print (intHeadingDeg - targetDegs) print"Left 2"#//// End Move Bot ////////////////////////////////// | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
142-145: ici la cible, ou "dot," pour le robot à courir est générée aléatoirement. Comme vous pouvez avis que j’ai limité la zone de génération des points vers le centre de ma webcam du champ de vision. C’est parce que je suis sale et points allaient où le petit robot n’a pas pu obtenir.
147-153 : Il s’agit d’une fonction de détection de collision rugueux. Fondamentalement, si le robot alors se rapproche de la cible (45px) puis il a considéré avoir "mangé" le point. Dans l’affirmative, puis la variable dot est incrémentée indiquant le montant global montant qu’il a fait a mangé et la variable de chaîne newTarget est renversée alors il peut générer une nouvelle cible de la nouvelle course à travers.