diff --git a/tests/logique_moteur.py b/tests/logique_moteur.py new file mode 100644 index 0000000..2adf671 --- /dev/null +++ b/tests/logique_moteur.py @@ -0,0 +1,72 @@ +# logique_moteur_eleve.py +# +# Objectif : écrire la logique de contrôle d'un moteur DC +# à partir d'une consigne de vitesse en pourcentage +# +# À compléter zones marquées avec ____ ou TODO + + +def calcul_signaux_moteur(vitesse_pourcent, pwm_max=1023): + + """ + Entrée : + vitesse_pourcent : nombre entre -100 et 100 + > 0 -> marche avant + < 0 -> marche arrière + = 0 -> arrêt + + Sortie : + dictionnaire avec : + "in1" : 0 ou 1 (sens) + "in2" : 0 ou 1 (sens) + "pwm" : valeur entre 0 et pwm_max + """ + + # 1) Limiter la vitesse entre -100 et 100 + # Si vitesse_pourcent > 100, on force à 100 + # Si vitesse_pourcent < -100, on force à -100 + # TODO : compléter les conditions + + if vitesse_pourcent > ____: + vitesse_pourcent = ____ + + if vitesse_pourcent < ____: + vitesse_pourcent = ____ + + # 2) Cas arrêt : si vitesse_pourcent == 0 + # -> in1 = 0, in2 = 0, pwm = 0 + # TODO : compléter la condition et le retour + + if ____ == 0: + return { + "in1": ____, + "in2": ____, + "pwm": ____ + } + + # 3) Détermination du sens + # Si vitesse_pourcent > 0 : avant (in1=1, in2=0) + # Si vitesse_pourcent < 0 : arrière (in1=0, in2=1) + # amplitude = valeur absolue de vitesse_pourcent + + if vitesse_pourcent > 0: + in1 = ____ + in2 = ____ + amplitude = ____ # ici, amplitude = vitesse_pourcent + else: + in1 = ____ + in2 = ____ + amplitude = ____ # ici, amplitude = -vitesse_pourcent + + # 4) Conversion pourcentage -> PWM + # pwm = pwm_max * (amplitude / 100) + # TODO : compléter le calcul + + pwm = int( pwm_max * (____ / 100) ) + + # 5) Retour des valeurs à appliquer sur le pont en H + return { + "in1": in1, + "in2": in2, + "pwm": pwm + } diff --git a/tests/test_logique_moteur.py b/tests/test_logique_moteur.py new file mode 100644 index 0000000..cf9cd23 --- /dev/null +++ b/tests/test_logique_moteur.py @@ -0,0 +1,57 @@ +# Tests unitaires pour la fonction calcul_signaux_moteur(). +# +# IMPORTANT : +# - Aucun résultat attendu n'est donné directement. +# - Les élèves doivent connaître / déduire le comportement attendu +# d'après l'énoncé du TP + +from logique_moteur import calcul_signaux_moteur + + +def test(): + pwm_max = 1000 + + # Test 1 : arrêt + # Vérifier que les trois valeurs (in1, in2, pwm) correspondent à un moteur à l'arrêt + sortie = calcul_signaux_moteur(0, pwm_max) + assert sortie["in1"] == ____ , "Test arrêt : valeur in1 incorrecte" + assert sortie["in2"] == ____ , "Test arrêt : valeur in2 incorrecte" + assert sortie["pwm"] == ____ , "Test arrêt : valeur pwm incorrecte" + + # Test 2 : vitesse maximale en avant + # Vérifier la direction et la valeur maximale du PWM + sortie = calcul_signaux_moteur(100, pwm_max) + assert sortie["in1"] == ____ , "Test avant : in1 incorrect" + assert sortie["in2"] == ____ , "Test avant : in2 incorrect" + assert sortie["pwm"] == ____ , "Test avant : pwm incorrect" + + # Test 3 : vitesse maximale en arrière + # Vérifier la direction inverse et le PWM maximal + sortie = calcul_signaux_moteur(-100, pwm_max) + assert sortie["in1"] == ____ , "Test arrière : in1 incorrect" + assert sortie["in2"] == ____ , "Test arrière : in2 incorrect" + assert sortie["pwm"] == ____ , "Test arrière : pwm incorrect" + + # Test 4 : valeur trop grande positive + # Vérifier que la valeur est bien "clampée" à la valeur maximale + sortie = calcul_signaux_moteur(150, pwm_max) + assert sortie["pwm"] == ____ , "Test clamp positif : pwm incorrect" + + # Test 5 : valeur trop grande négative + # Vérifier que la valeur est bien "clampée" et le sens correct + sortie = calcul_signaux_moteur(-200, pwm_max) + assert sortie["in2"] == ____ , "Test clamp négatif : in2 incorrect" + assert sortie["pwm"] == ____ , "Test clamp négatif : pwm incorrect" + + # Test 6 : valeur intermédiaire (par exemple 50 %) + # Vérifier la direction et la proportion du PWM + sortie = calcul_signaux_moteur(50, pwm_max) + assert sortie["in1"] == ____ , "Test intermédiaire : in1 incorrect" + assert sortie["in2"] == ____ , "Test intermédiaire : in2 incorrect" + assert sortie["pwm"] == ____ , "Test intermédiaire : pwm incorrect" + + print("Tous les tests du moteur sont PASSÉS si vous voyez ce message !") + + +if __name__ == "__main__": + lancer_tests()