IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Raspberry Pi Discussion :

Raspbian - bash - Problème avec if [ ] then if [ ] then


Sujet :

Raspberry Pi

  1. #1
    Nouveau Candidat au Club
    Homme Profil pro
    sans emploi
    Inscrit en
    Juillet 2019
    Messages
    35
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 53
    Localisation : France, Loiret (Centre)

    Informations professionnelles :
    Activité : sans emploi

    Informations forums :
    Inscription : Juillet 2019
    Messages : 35
    Points : 1
    Points
    1
    Par défaut Raspbian - bash - Problème avec if [ ] then if [ ] then
    Bonjour, mes conditions secondaires avec "if" ne fonctionne pas. Dois-je passer par "elif" nécessairement où s'agit-il d'un problème de syntaxe ?

    Code bash : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    #!/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

  2. #2
    Nouveau Candidat au Club
    Homme Profil pro
    sans emploi
    Inscrit en
    Juillet 2019
    Messages
    35
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 53
    Localisation : France, Loiret (Centre)

    Informations professionnelles :
    Activité : sans emploi

    Informations forums :
    Inscription : Juillet 2019
    Messages : 35
    Points : 1
    Points
    1
    Par défaut
    le code corrigé et fonctionnel, Pour info pour celles et/ou ceux que ça intéressent, un robot et son programme :
    Par contre j’ai pas bien compris pourquoi « -eq » alors que « = » ne fonctionne pas, la logique des langages…, presque pire que « brainfuck ».

    Robot : PI zero WH + 1 servo à rotation continu + 1 servo 360 + module caméra + module d’alimentation lithium ion 18650 + 1 accus lithium ion 18650

    module d’alimentation 5 volts : ICI ou LA

    servo à rotation continu :
    marron - GPIO pin 6 (GND)
    rouge - GPIO pin 2 (+5 volt)
    orange - GPIO pin 32 - BCM pin 12 (signal PWM)

    servo 360 :
    marron - GPIO pin 6 (GND)
    rouge - GPIO pin 2 (+5 volt)
    orange - GPIO pin 12 - BCM pin 18 (signal PWM)

    module caméra : sur le port caméra
    module d’alimentation : sur le port usb
    batterie lithium 18650 : dans le module d’alimentation

    Le PI zero sous raspbian lite est connecté à ma box en wifi et à l’adresse IP 192.168.0.12
    Avec 1 PI 3B+ (raspbian full) dans la console :
    pi@3bplus:~ $ ssh 192.168.0.12
    pi@3bplus:~ $ mot de passe
    pi@zero:~ $ cd tonio
    pi@zero:~/tonio $ sudo chmod 777 robot
    pi@zero:~/tonio $ ./robot

    Pour lire le streaming j’utilise cvlc avec raspbian (PI 3B+) ou vlc avec windows (plus fluide)
    cvlc tcp/MJPEG://192.168.0.12:1234
    Pour windows j’ai eu des difficultés suivant la version de vlc.

    Le programme n’est pas très optimisé, je l’appel « robot »


    *** robot ***
    Code bash : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    #!/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 4 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 -eq 50 ];then y=128;mouve="arrière lente";
    	elif [ $y -eq 128 ];then y=150;mouve="arrêt";
    	elif [ $y -eq 150 ];then y=170;mouve="avance lente";
    	elif [ $y -eq 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 -eq 240 ];then y=170;mouve="avance lente";
    	elif [ $y -eq 170 ];then y=150;mouve="arrêt";
    	elif [ $y -eq 150 ];then y=128;mouve="arrière lente";
    	elif [ $y -eq 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 -eq 52 ];then x=150;y=150;tourne="tout droit";mouve="arrêt";
    	elif [ $x -eq 150 ];then x=220;y=150;tourne="à droite";mouve="arrêt";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 -eq 220 ];then x=150;y=150;tourne="tout droit";mouve="arrêt";
    	elif [ $x -eq 150 ];then x=52;y=150;tourne="à gauche";mouve="arrêt";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 4 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
     
    done

    Penser à caler les valeurs de x et y.

    Et merci celles et/ou ceux qui ont rendu cela possible.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. %display %window problème avec %if %then
    Par L0007 dans le forum SAS AF et formulaires
    Réponses: 2
    Dernier message: 24/10/2011, 13h03
  2. [Bash] problème avec "perl -e"
    Par Rits dans le forum Linux
    Réponses: 2
    Dernier message: 24/08/2009, 09h32
  3. Problème avec if-then-else
    Par pongping dans le forum Prolog
    Réponses: 6
    Dernier message: 27/10/2008, 23h00
  4. Problème avec IF-THEN
    Par roman67 dans le forum SQL
    Réponses: 3
    Dernier message: 23/09/2008, 16h03
  5. problème avec If then Else
    Par thomas_wagner dans le forum Macros et VBA Excel
    Réponses: 3
    Dernier message: 04/11/2007, 01h32

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo