Partage
  • Partager sur Facebook
  • Partager sur Twitter

Utiliser la librairie SDL pour piloter un robot

    19 mars 2016 à 22:01:08

    Bonjour à tous,

    Je suis en train de développer ce script sous code::block dans le but de piloter un robot tout terrain et d'apprendre la programmation, cependant je débute  donc je patauge LOL ... 

    Pour plus de précision:

    - Mon robot sera piloter par une manette de Xbox360 en usb brancher sur mon pc portable.

    - Les éléments qui sont fixer au robot:Une carte raspberry PI B+ (sous linux) et un controller arduino

    - Les informations seront transférer par wifi depuis mon pc à la carte raspberry PI B+ (j'ai choisi cette option car la carte arduino n'a pas de wifi)

      J'avais réussi a récupérer les événements en temps réel dans un fichier, mais impossible d'ouvrir ce fichier (j'avais dans l'idee de recuperer les valeur de ce fichier via un batch est de les envoyée sur le réseau, mais impossible d'utiliser ce fichier tant que l'application SDL fonctionne, donc après quelque recherche j'ai lu qu'il fallait crée un thread pour pouvoir l'exploiter, j'ai absolument aucune idee de comment faire !!!

    Si quelqu’un a des commentaire a faire sur la réalisation de mon projet qu'il n’hésite pas !

    Je me suis meme demander si c'eté la meilleur methode pour réaliser mon projet..

    Pour le moment, ce que je souhaite faire c'est de récupérer les événements de mon joystick dans un fichier, et de les visualiser à l'ecran en même temps via ce script:

    Mais j'ai des erreur, je comprends pas trop, si quelqun peut me corriger, m'indiquer ce qui va pas ,,, je serai raviiiiiiiiiiii  :) :)

     Je me suis inspirer de ce post

    information complementaire sur la librairie SDL qui m'a pas mal aider.

    CODE: 

    #ifdef __cplusplus
        #include <cstdlib>
    #else
        #include <stdlib.h>
    #endif
    
    
    int appel_SDL(int*argc, char**argv);
    void updatevent(void);
    
    int main( int argc, char**argv)
    {
        //code pour la console
        appel_SDL(&argc, argv);
    
        return 0;
    }
    
    #include <SDL/SDL.h>
    int appel_SDL(int*argc, char**argv )
    {
        // initialize SDL video
        if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0 )
        {
            printf( "Unable to init SDL: %s\n", SDL_GetError() );
            return 1;
        }
    
        // make sure SDL cleans up before exit
        atexit(SDL_Quit);
    
        SDL_Joystick *joystick; // on crée le joystick
        joystick = SDL_JoystickOpen(0); // on l'assigne au numéro 0
    
        if (!joystick) // si joystick n'existe pas
        {
            printf("Joystick non connecter \n");
        }
    
        FILE* fichier = NULL;
        fichier=fopen("joystick.txt","w+"); // on crée un fichier Joystick.txt
    
        SDL_JoystickEventState(SDL_ENABLE);
    
        // creation de la fenetre
        SDL_Surface* screen = SDL_SetVideoMode(640, 480, 16,
                                               SDL_HWSURFACE|SDL_DOUBLEBUF);
        if ( !screen )
        {
            printf("impossible de charger l'image: %s\n", SDL_GetError());
            return 1;
        }
    
        // chargement de l'image
        SDL_Surface* bmp = SDL_LoadBMP("6wd.bmp");
        if (!bmp)
        {
            printf("Unable to load bitmap: %s\n", SDL_GetError());
            return 1;
        }
    
        // centrer l'image sur l'ecran
        SDL_Rect dstrect;
        dstrect.x = (screen->w - bmp->w) / 2;
        dstrect.y = (screen->h - bmp->h) / 2;
    
        //JOYSTICKS
        printf("Nombre de joystick connecter %d joysticks\n",SDL_NumJoysticks());
        for(int i=0;i<SDL_NumJoysticks();i++)                                        // tant qu'il y a un joystick non-traité
        printf("Nom du joystick numero %d : %s\n",i,SDL_JoystickName(i)); // on écrit les noms des joysticks
    
        updatevent(); //apelle de la fonction updatevent
    
        //free loaded bitmap
        SDL_Flip(screen);
        SDL_FreeSurface(bmp);
        SDL_JoystickClose(joystick);
    
        // all is well 
        printf("Fin du programme, pressez une touche pour quitter\n");
        return 0;
    }
    
    void updatevent(void)
    {
        // DRAWING STARTS HERE
        bool quitter= false;
    
        while (!quitter)
        {
         // message processing loop
            static SDL_Event event;
            while (SDL_PollEvent(&event))
            {
                // check for messages
                switch (event.type)
                {
                    // exit if the int SDL(int argc, char*argv[])window is closed
                case SDL_QUIT:
                    quitter = 1;
                    break;
    
                    // check for keypresses
                case SDL_KEYDOWN:
                    {
                        // exit if ESCAPE is pressed
                        if (event.key.keysym.sym == SDLK_ESCAPE)
                            quitter = 1;
                        break;
                    }
                case SDL_JOYAXISMOTION:
                    {
                        if(event.jaxis.value < - 7000 )
                        {
                            printf("%d   %d   %d   %d\n",event.jaxis,event.jaxis.which,event.jaxis.axis,event.jaxis.value);
                            //fprintf(fichier,"%d,%d,%d,%d\n",event.jaxis,event.jaxis.which,event.jaxis.axis,event.jaxis.value);
                            fclose(fichier);
                        }
                        if(event.jaxis.value > 7000)
                        {
                            printf("%d   %d   %d   %d\n",event.jaxis,event.jaxis.which,event.jaxis.axis,event.jaxis.value);
                            fprintf(fichier,"%d,%d,%d,%d\n",event.jaxis,event.jaxis.which,event.jaxis.axis,event.jaxis.value);
                            fclose(fichier);
                        }
    
                    }
                    break;
                case SDL_JOYBUTTONDOWN:
                    printf("Bouton : %d\n",event.jbutton.button);
                    switch(event.jbutton.button)
                    {
                        case 7 :
                            quitter = 1;
                        break;
                    }
    
                    break;
                } // end switch
    
            } // end of message processing
         // end main loop
    
        // clear screen
        SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
        // draw bitmap
        SDL_BlitSurface(bmp, 0, screen, &dstrect);
        // DRAWING ENDS HERE
        }
    }
    

    DEBUG

    ||=== Build: Debug in 6wd_SDl_C (compiler: GNU GCC Compiler) ===|

    C:\Users\Nico\Desktop\Projet C\6wd_SDl_C\main.cpp||In function 'int appel_SDL(int*, char**)':|

    C:\Users\Nico\Desktop\Projet C\6wd_SDl_C\main.cpp|40|warning: variable 'fichier' set but not used [-Wunused-but-set-variable]|

    C:\Users\Nico\Desktop\Projet C\6wd_SDl_C\main.cpp|63|warning: variable 'dstrect' set but not used [-Wunused-but-set-variable]|

    C:\Users\Nico\Desktop\Projet C\6wd_SDl_C\main.cpp||In function 'void updatevent()':|

    C:\Users\Nico\Desktop\Projet C\6wd_SDl_C\main.cpp|115|warning: format '%d' expects argument of type 'int', but argument 2 has type 'SDL_JoyAxisEvent' [-Wformat]|

    C:\Users\Nico\Desktop\Projet C\6wd_SDl_C\main.cpp|117|error: 'fichier' was not declared in this scope|

    C:\Users\Nico\Desktop\Projet C\6wd_SDl_C\main.cpp|121|warning: format '%d' expects argument of type 'int', but argument 2 has type 'SDL_JoyAxisEvent' [-Wformat]|

    C:\Users\Nico\Desktop\Projet C\6wd_SDl_C\main.cpp|122|error: 'fichier' was not declared in this scope|

    C:\Users\Nico\Desktop\Projet C\6wd_SDl_C\main.cpp|144|error: 'screen' was not declared in this scope|

    C:\Users\Nico\Desktop\Projet C\6wd_SDl_C\main.cpp|146|error: 'bmp' was not declared in this scope|

    C:\Users\Nico\Desktop\Projet C\6wd_SDl_C\main.cpp|146|error: 'dstrect' was not declared in this scope|

    ||=== Build failed: 5 error(s), 4 warning(s) (0 minute(s), 0 second(s)) ===|

    Merci à celui ou ceux qui feront chauffer leur neurone :-) 

    -
    Edité par Nicolas L'indomptable 19 mars 2016 à 22:33:41

    • Partager sur Facebook
    • Partager sur Twitter
      21 mars 2016 à 0:06:54

      Voila apres correction, mon nouveau code:

      #include <cstdlib>
      #include <stdio.h>
      #include <stdlib.h>
      #include <SDL/SDL.h>
      
      int main(int argc, char**argv)
      {
          // initialize SDL video
          if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0 )
          {
              fprintf(stdout,"Unable to init SDL: %s\n", SDL_GetError());
              return 1;
          }
      
          // make sure SDL cleans up before exit
          atexit(SDL_Quit);
      
          SDL_Joystick *joystick; // on crée le joystick
          joystick = SDL_JoystickOpen(0); // on l'assigne au numéro 0
          //fprinf(fichier,"joystick connecter \n");
      
          if (!joystick) // si joystick n'existe pas
          {
              printf("Joystick non connecter \n");
          }
      
          FILE* fichier = NULL;
          fichier=fopen("joystick.txt","w+"); // on crée un fichier Joystick.txt
      
          SDL_JoystickEventState(SDL_ENABLE);
      
          // creation de la fenetre
          SDL_Surface* screen = SDL_SetVideoMode(640, 480, 16, SDL_HWSURFACE|SDL_DOUBLEBUF);
          if ( !screen )
          {
              printf("impossible de charger l'image: %s\n", SDL_GetError());
              return 1;
          }
      
          // chargement de l'image
          SDL_Surface* bmp = SDL_LoadBMP("6wd.bmp");
          if (!bmp)
          {
              fprintf(fichier,"Unable to load bitmap: %s\n", SDL_GetError());
              return 1;
          }
      
          // centrer l'image sur l'ecran
          SDL_Rect dstrect;
          dstrect.x = (screen->w - bmp->w) / 2;
          dstrect.y = (screen->h - bmp->h) / 2;
      
          //JOYSTICKS
          fprintf(fichier,"Nombre de joystick connecter %d joysticks\n",SDL_NumJoysticks());
          for(int i=0;i<SDL_NumJoysticks();i++)                                        // tant qu'il y a un joystick non-traité
          fprintf(fichier,"Nom du joystick numero %d : %s\n",i,SDL_JoystickName(i)); // on écrit les noms des joysticks
      
           // DRAWING STARTS HERE
          bool quitter= false;
      
          while (!quitter)
          {
           // message processing loop
              static SDL_Event event;
              while (SDL_PollEvent(&event))
              {
                  // check for messages
                  switch (event.type)
                  {
                      // exit if the int SDL(int argc, char*argv[])window is closed
                  case SDL_QUIT:
                      quitter = 1;
                      break;
      
                      // check for keypresses
                  case SDL_KEYDOWN:
                      {
                          // exit if ESCAPE is pressed
                          if (event.key.keysym.sym == SDLK_ESCAPE)
                              quitter = 1;
                          break;
                      }
                  case SDL_JOYAXISMOTION:
                      {
                          if(event.jaxis.value < - 7000 )
                          {
                              fprintf(fichier,"%d:%d:%d:%d \n",event.jaxis,event.jaxis.which,event.jaxis.axis,event.jaxis.value);
                              fclose(fichier);
                          }
                          if(event.jaxis.value > 7000)
                          {
                              fprintf(fichier,"%d:%d:%d:%d\n",event.jaxis,event.jaxis.which,event.jaxis.axis,event.jaxis.value);
                              fclose(fichier);
                          }
                      }
                      break;
                  case SDL_JOYBUTTONDOWN:
                      printf("Bouton : %d\n",event.jbutton.button);
                      switch(event.jbutton.button)
                      {
                          case 7 :
                              quitter = 1;
                          break;
                      }
                      break;
                  } // end switch
              } // end of message processing
           // end main loop
      
          // clear screen
          SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
          // draw bitmap
          SDL_BlitSurface(bmp, 0, screen, &dstrect);
          // DRAWING ENDS HERE
          }
      
          //free loaded bitmap
          SDL_Flip(screen);
          SDL_FreeSurface(bmp);
          SDL_JoystickClose(joystick);
      
          // all is well 
          printf("Fin du programme, pressez une touche pour quitter\n");
          return 0;
      }
      

      Je souhaiterai mettre dans un tableau les valeurs de mes joystick, et boutons.

      Tableau: 1colonne(cordonner joystick1) 2colonne(cornonner joystick2) 3eme(Bouton X:1 ou 0) 4eme(Bouton A:1ou2) 5eme(Bouton B:1ou0) 6eme(bouton Y:1ou0)

      J'ai plus ou moins compris comment faire un tableau mais je n'arrive pas a l'appliquer à mon cas.

      Je voudrai visualiser la valeurs de mes joysticks,et boutons dans un tableau mais il faut que ce tableau soit dans la fenetre que SDL crée ! comment faire ?

      • Partager sur Facebook
      • Partager sur Twitter

      Utiliser la librairie SDL pour piloter un robot

      × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
      × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
      • Editeur
      • Markdown