raspbian (pios) - bash - if then - problème avec des conditions multiples
Posté : ven. 23 avr. 2021 23:44
Bonjour, je n'arrive pas à régler un problème de conditions multiples avec "if then" dans mon code, je ne sais pas si je dois utiliser "elif" ou si c'est une erreur de syntaxe.
#!/bin/bash
clear
x=150
y=150
mouve="arrêt"
tourne="tout droit"
#robot à l'arrêt (pin12 bcm : 150), direction devant (pin 18 bcm: 150)
gpio -g mode 12 pwm;gpio pwm-ms;gpio pwmc 192;gpio pwmr 2000;gpio -g pwm 12 $x
gpio -g mode 18 pwm;gpio pwm-ms;gpio pwmc 192;gpio pwmr 2000;gpio -g pwm 18 $y
#lancement du streaming
tput cup 1 20;echo "... lancement du streaming ..."
tput cup 2 20;echo "lire avec : cvlc tcp/MJPEG://192.168.0.12:1234"
printf "$(raspivid -n -t 0 -w 640 -h 480 -vf -hf -cd MJPEG -l -o tcp://0.0.0.0:1234)" &
#lire coté client avec : "cvlc tcp/MJPEG://192.168.0.12:1234" (MJPEG en majuscule)
sleep 2
tput cup 3 20;printf "qqq ou Ctrl+c pour arréter"
tput cup 5 2;echo $mouve
tput cup 6 2;echo "position de la roue :" $tourne
while true
do
read -rsn3 -d '' TOUCHE
case ${TOUCHE:2} in
A) TOUCHE="↑" ;;
B) TOUCHE="↓" ;;
C) TOUCHE="→" ;;
D) TOUCHE="←" ;;
q) TOUCHE="q" ;;
esac
if [ $TOUCHE = "↑" ];then
if [ $y=50 ];then y=128;mouve=arrière lente;fi
if [ $y=128 ];then y=150;mouve=arrêt;fi
if [ $y=150 ];then y=170;mouve=avance lente;fi
if [ $y=170 ];then y=240;mouve=avance rapide;fi
gpio -g mode 18 in;gpio -g mode 12 pwm;gpio pwm-ms;gpio pwmc 192;gpio pwmr 2000;gpio -g pwm 12 $y
fi
if [ $TOUCHE = "↓" ];then
if [ $y=240 ];then y=170;mouve=avance lente;fi
if [ $y=170 ];then y=150;mouve=arrêt;fi
if [ $y=150 ];then y=128;mouve=arrière lente;fi
if [ $y=128 ];then y=50;mouve=arrière rapide;fi
gpio -g mode 18 in;gpio -g mode 12 pwm;gpio pwm-ms;gpio pwmc 192;gpio pwmr 2000;gpio -g pwm 12 $y
fi
if [ $TOUCHE = "→" ];then
if [ $x=52 ];then x=105;tourne=tout droit;fi
if [ $x=105 ];then x=220;tourne=à droite;fi
gpio -g mode 12 in;gpio -g mode 18 pwm;gpio pwm-ms;gpio pwmc 192;gpio pwmr 2000;gpio -g pwm 18 $x
fi
if [ $TOUCHE = "←" ];then
if [ x=220 ];then x=105;tourne=tout droit;fi
if [ x=105 ];then x=52;tourne=à gauche;fi
gpio -g mode 12 in;gpio -g mode 18 pwm;gpio pwm-ms;gpio pwmc 192;gpio pwmr 2000;gpio -g pwm 18 $x
fi
if [ $TOUCHE = "q" ];then killall raspivid;gpio -g mode 12 in; gpio -g mode 18 in;exit
fi
clear
tput cup 3 20;printf "qqq ou Ctrl+c pour arréter"
tput cup 5 2;echo $mouve
tput cup 6 2;echo "position de la roue :" $tourne
#tput cup 8 5
#echo "La flèche est \"${TOUCHE}\""
done
Merci pour votre aide.
#!/bin/bash
clear
x=150
y=150
mouve="arrêt"
tourne="tout droit"
#robot à l'arrêt (pin12 bcm : 150), direction devant (pin 18 bcm: 150)
gpio -g mode 12 pwm;gpio pwm-ms;gpio pwmc 192;gpio pwmr 2000;gpio -g pwm 12 $x
gpio -g mode 18 pwm;gpio pwm-ms;gpio pwmc 192;gpio pwmr 2000;gpio -g pwm 18 $y
#lancement du streaming
tput cup 1 20;echo "... lancement du streaming ..."
tput cup 2 20;echo "lire avec : cvlc tcp/MJPEG://192.168.0.12:1234"
printf "$(raspivid -n -t 0 -w 640 -h 480 -vf -hf -cd MJPEG -l -o tcp://0.0.0.0:1234)" &
#lire coté client avec : "cvlc tcp/MJPEG://192.168.0.12:1234" (MJPEG en majuscule)
sleep 2
tput cup 3 20;printf "qqq ou Ctrl+c pour arréter"
tput cup 5 2;echo $mouve
tput cup 6 2;echo "position de la roue :" $tourne
while true
do
read -rsn3 -d '' TOUCHE
case ${TOUCHE:2} in
A) TOUCHE="↑" ;;
B) TOUCHE="↓" ;;
C) TOUCHE="→" ;;
D) TOUCHE="←" ;;
q) TOUCHE="q" ;;
esac
if [ $TOUCHE = "↑" ];then
if [ $y=50 ];then y=128;mouve=arrière lente;fi
if [ $y=128 ];then y=150;mouve=arrêt;fi
if [ $y=150 ];then y=170;mouve=avance lente;fi
if [ $y=170 ];then y=240;mouve=avance rapide;fi
gpio -g mode 18 in;gpio -g mode 12 pwm;gpio pwm-ms;gpio pwmc 192;gpio pwmr 2000;gpio -g pwm 12 $y
fi
if [ $TOUCHE = "↓" ];then
if [ $y=240 ];then y=170;mouve=avance lente;fi
if [ $y=170 ];then y=150;mouve=arrêt;fi
if [ $y=150 ];then y=128;mouve=arrière lente;fi
if [ $y=128 ];then y=50;mouve=arrière rapide;fi
gpio -g mode 18 in;gpio -g mode 12 pwm;gpio pwm-ms;gpio pwmc 192;gpio pwmr 2000;gpio -g pwm 12 $y
fi
if [ $TOUCHE = "→" ];then
if [ $x=52 ];then x=105;tourne=tout droit;fi
if [ $x=105 ];then x=220;tourne=à droite;fi
gpio -g mode 12 in;gpio -g mode 18 pwm;gpio pwm-ms;gpio pwmc 192;gpio pwmr 2000;gpio -g pwm 18 $x
fi
if [ $TOUCHE = "←" ];then
if [ x=220 ];then x=105;tourne=tout droit;fi
if [ x=105 ];then x=52;tourne=à gauche;fi
gpio -g mode 12 in;gpio -g mode 18 pwm;gpio pwm-ms;gpio pwmc 192;gpio pwmr 2000;gpio -g pwm 18 $x
fi
if [ $TOUCHE = "q" ];then killall raspivid;gpio -g mode 12 in; gpio -g mode 18 in;exit
fi
clear
tput cup 3 20;printf "qqq ou Ctrl+c pour arréter"
tput cup 5 2;echo $mouve
tput cup 6 2;echo "position de la roue :" $tourne
#tput cup 8 5
#echo "La flèche est \"${TOUCHE}\""
done
Merci pour votre aide.