58 lines
2.3 KiB
Python
58 lines
2.3 KiB
Python
# 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"] == 0 , "Test arrêt : valeur in1 incorrecte"
|
|
assert sortie["in2"] == 0 , "Test arrêt : valeur in2 incorrecte"
|
|
assert sortie["pwm"] == 0 , "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"] == 1 , "Test avant : in1 incorrect"
|
|
assert sortie["in2"] == 0 , "Test avant : in2 incorrect"
|
|
assert sortie["pwm"] == 100 , "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"] == 0 , "Test arrière : in1 incorrect"
|
|
assert sortie["in2"] == 1 , "Test arrière : in2 incorrect"
|
|
assert sortie["pwm"] == -100 , "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"] == 100 , "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"] == 0 , "Test clamp négatif : in2 incorrect"
|
|
assert sortie["pwm"] == -100 , "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"] == 1 , "Test intermédiaire : in1 incorrect"
|
|
assert sortie["in2"] == 0 , "Test intermédiaire : in2 incorrect"
|
|
assert sortie["pwm"] == 50 , "Test intermédiaire : pwm incorrect"
|
|
|
|
print("Tous les tests du moteur sont PASSÉS si vous voyez ce message !")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
test()
|