importer RPi.GPIO comme GPIO
importer temps
de l293d importer L293D
classe Robot():
def __init__(soi, motor_left_pin1=17, motor_left_pin2=27, motor_right_pin1=23, motor_right_pin2=24,
line_follow_pin_left=19, line_follow_pin_right=6 ):
GPIO.mode réglages(GPIO.BCM)
GPIO.setwarnings(Faux)
# modules init
soi.moteur = L293D(motor_left_pin1, motor_left_pin2, motor_right_pin1, motor_right_pin2)
soi.line_follow_pin_left = line_follow_pin_left
soi.line_follow_pin_right = line_follow_pin_right
GPIO.installer(soi.line_follow_pin_left, GPIO.DANS)
GPIO.installer(soi.line_follow_pin_right, GPIO.DANS)
def lineFollowModeOn(soi):
status_left = Faux
status_right = Faux
tandis que Vrai:
status_left = booléen(GPIO.contribution(soi.line_follow_pin_left)) # Faux: pas en ligne / capteur trop éloigné du bas
status_right = booléen(GPIO.contribution(soi.line_follow_pin_right)) # True: en ligne
si status_left et status_right:
# one the line, follow straight on
soi.moteur.vers l’avant()
elif status_left:
# ligne est à gauche, déplacez-vous à gauche (moteur à droite)
soi.moteur.avantDroite()
elif status_right:
# ligne est à droite, déplacez-vous à droite (moteur à gauche)
soi.moteur.avantGauche()
autre:
# se sont égarés, ligne de recherche. d’abord reculer de quelques cm
soi.moteur.en arrière()
temps.dormir(7,5/soi.moteur.DIST_PER_SEC)
soi.moteur.Arrêtez()
# faire pivoter de x degrés pour rechercher la ligne
degrés_to_search = 45,0
soi.moteur.avantDroite()
s = GPIO.wait_for_edge(soi.line_follow_pin_left, GPIO.EN HAUSSE, temps libre=int(1000 * soi.moteur.SEC_PER_TURN / 360,0 * degrés_to_search))
soi.moteur.Arrêtez()
si s est ne pas Aucun:
# ligne trouvée, continuer
continuer
autre:
# rien trouvé, revenir à la position d’origine
soi.moteur.arrièreDroite()
temps.dormir(soi.moteur.SEC_PER_TURN / 360,0 * degrés_to_search)
# recherche de l’autre côté
soi.moteur.avantGauche()
s = GPIO.wait_for_edge(soi.line_follow_pin_right, GPIO.EN HAUSSE, temps libre=int(1000 * soi.moteur.SEC_PER_TURN / 360,0 * degrés_to_search))
soi.moteur.Arrêtez()
si s est ne pas Aucun:
# ligne trouvée, continuer
impression(« fonds »)
continuer
autre:
# ligne introuvable, revenir à la position d’origine, arrêter
soi.moteur.arrièreGauche()
temps.dormir(soi.moteur.SEC_PER_TURN / 360,0 * degrés_to_search)
soi.moteur.Arrêtez()
Pause
temps.dormir(0,001)
-
Freenove Kit de Bras Robotique pour Raspberry Pi, Haute Précision, Moteur Pas à Pas, Structure Métallique, Mode de Serrage, Mode de Dessin, Enregistrement et Replay (Raspberry Pi Non INCLUDÉ)
-
Adeept PiCar Pro V2 Smart Robot Car Kit with 4-DOF Robotic Arm for Raspberry Pi 5/4/3(Pi NOT Included), Programming Educational STEM Project Robotics, Obstacle Avoidance, Line Tracking, USB-C Charging
