Advertisement
adibdoub

Untitled

Oct 7th, 2015
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.52 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <errno.h>
  3. #include <fcntl.h>
  4. #include <stdio.h>
  5. #include <termios.h>
  6. #include <stdlib.h>
  7. #include <sys/types.h>
  8. #include <sys/mman.h>
  9. #include <sys/stat.h>
  10. #include <ncurses.h>
  11. #include <time.h>
  12.  
  13. #define TRAIL1 54 //C1
  14. #define TRAIL2 55 //C2
  15. #define DIVER 56  //PA
  16. #define VACUUM 57    //PV
  17. #define ARM 58     //AA
  18. #define GRABBER 59 //GA
  19.  
  20. #define HORIZONTALDIVER 60//CP, can be 0, 1, 2 or 3
  21. #define BIN 61
  22. #define TRASH 62
  23. #define CONVEYOR1 63
  24. #define CONVEYOR2 64
  25. struct  ACTUATEURS
  26.                 {
  27.                  unsigned CP : 2;
  28.                  unsigned C1 : 1;
  29.                  unsigned C2 : 1;
  30.                  unsigned PV : 1;
  31.                  unsigned PA : 1;
  32.                  unsigned AA : 1;
  33.                  unsigned GA : 1;
  34.                } ;
  35. union
  36.         {
  37.         struct ACTUATEURS act ;
  38.         unsigned char byte ;
  39.         } u_act ;
  40.  
  41. struct  CAPTEURS
  42.                 {
  43.                  unsigned L1 : 1;
  44.                  unsigned L2 : 1;
  45.                  unsigned T  : 1; /* cablÈ H */
  46.                  unsigned S  : 1;
  47.                  unsigned CS : 1;
  48.                  unsigned AP : 1;
  49.                  unsigned PP : 1;
  50.                  unsigned DE : 1;
  51. /*                 unsigned H1 : 1;
  52.                  unsigned H2 : 1;
  53.                  unsigned H3 : 1;
  54.                  unsigned H4 : 1;
  55.                  unsigned H5 : 1;
  56.                  unsigned H6 : 1;
  57.                  unsigned    : 2;       */
  58.               } ;
  59.  
  60. union
  61.         {
  62.         struct CAPTEURS capt ;
  63.         unsigned char byte ;
  64.         } u_capt ;
  65.  
  66. void AfficheActu(int);
  67. void AfficheCapt(int);
  68. int kbhit(void);
  69.  
  70. void setActu(int actu);
  71. void setHorizontalDiver(int position);
  72.  
  73. int fd_petra_out;
  74. int main ()
  75. {
  76.         int fd_petra_in;
  77.         struct timespec tps;
  78.  
  79.         char action='n';
  80.  
  81.         tps.tv_sec=0;
  82.         tps.tv_nsec=150000000;
  83.  
  84.         u_act.byte = 0x00 ;
  85.  
  86.         fd_petra_out = open ( "/dev/actuateursPETRA", O_WRONLY );
  87.         if ( fd_petra_out == -1 )
  88.         {
  89.                 perror ( "MAIN : Erreur ouverture PETRA_OUT" );
  90.                 return 1;
  91.         }
  92.         else
  93.                 printf ("MAIN: PETRA_OUT opened\n");
  94.  
  95.  
  96.         fd_petra_in = open ( "/dev/capteursPETRA", O_RDONLY );
  97.         if ( fd_petra_in == -1 )
  98.         {
  99.                 perror ( "MAIN : Erreur ouverture PETRA_IN" );
  100.                 return 1;
  101.         }
  102.         else
  103.                 printf ("MAIN: PETRA_IN opened\n");
  104.  
  105.         do
  106.         {
  107.                 printf("DERNIERE TOUCHE %c\n\n\n",action);
  108.                 AfficheCapt(fd_petra_in);
  109.                 AfficheActu(fd_petra_out);
  110.                 nanosleep(&tps, NULL);
  111.                 printf("\033[2J");
  112.                 fflush(stdin);
  113.  
  114.                 if(kbhit())
  115.                 {
  116.                         action=getchar();
  117.  
  118.                         switch(action)
  119.                         {
  120.                                 case '0' :
  121.                                         setHorizontalDiver(BIN);
  122.                                 break;
  123.                                
  124.                                 case '1' :
  125.                                         setHorizontalDiver(CONVEYOR1);
  126.                                 break;
  127.                                
  128.                                 case '2' :
  129.                                         setHorizontalDiver(TRASH);
  130.                                 break;
  131.                                
  132.                                 case '3' :
  133.                                         setHorizontalDiver(CONVEYOR2);
  134.                                 break;
  135.  
  136.                                 case 'e':
  137.                                         setActu(TRAIL1);
  138.                                         break;
  139.  
  140.                                 case 'r':
  141.                                         setActu(TRAIL2);
  142.                                         break;
  143.  
  144.                                 case 't':
  145.                                         setActu(VACUUM);
  146.                                         break;
  147.  
  148.                                 case 'y':
  149.                                         setActu(DIVER);
  150.                                         break;
  151.  
  152.                                 case 'u':
  153.                                         setActu(ARM);
  154.                                         break;
  155.  
  156.                                 case 'i':
  157.                                         setActu(GRABBER);
  158.                                         break;
  159.  
  160.                                 default:
  161.                                         break;
  162.                         };
  163.                 }
  164.         }while(action!='q');
  165.  
  166.         u_act.byte = 0x00;
  167.         write ( fd_petra_out , &u_act.byte ,1 );
  168.         close ( fd_petra_in );
  169.         close ( fd_petra_out );
  170.         return 1;
  171. }
  172.  
  173. void setActu(int actu)
  174. {
  175.         read ( fd_petra_out , &u_act.byte , 1 );
  176.         switch(actu)
  177.         {
  178.                 case TRAIL1 :
  179.                         u_act.act.C1 ^= 1;
  180.                 break;
  181.                 case TRAIL2 :
  182.                         u_act.act.C2 ^= 1;
  183.                 break;
  184.                 case GRABBER :
  185.                         u_act.act.GA ^= 1;
  186.                 break;
  187.                 case DIVER :
  188.                         u_act.act.PA ^=1;
  189.                 break;
  190.                 case VACUUM :
  191.                         u_act.act.PV ^=1;
  192.                 break;
  193.                 case ARM :
  194.                         u_act.act.AA ^= 1;
  195.                 break;
  196.         }
  197.         write ( fd_petra_out , &u_act.byte ,1 );
  198. }
  199. void setHorizontalDiver(int position)
  200. {
  201.         read ( fd_petra_out , &u_act.byte , 1 );
  202.         switch(position)
  203.         {
  204.                 case BIN :
  205.                 u_act.act.CP = 0;
  206.                 break;
  207.                
  208.                 case TRASH :
  209.                 u_act.act.CP = 2;
  210.                 break;
  211.                
  212.                 case CONVEYOR1 :
  213.                 u_act.act.CP = 1;
  214.                 break;
  215.                
  216.                 case CONVEYOR2 :
  217.                 u_act.act.CP = 3;
  218.                 break;
  219.         }
  220.         write ( fd_petra_out , &u_act.byte ,1 );
  221. }
  222. void AfficheActu(int fd_petra_out)
  223. {
  224.         read ( fd_petra_out , &u_act.byte , 1 );
  225.         printf("-----ACTUATEURS-----\n");
  226.         printf(" 0-3: CP : %d\n",u_act.act.CP);
  227.     printf(" e: C1 : %d\n",u_act.act.C1);
  228.     printf(" r: C2 : %d\n",u_act.act.C2);
  229.     printf(" t: PV : %d\n",u_act.act.PV);
  230.     printf(" y: PA : %d\n",u_act.act.PA);
  231.     printf(" u: AA : %d\n",u_act.act.AA);
  232.     printf(" i: GA : %d\n",u_act.act.GA);
  233. }
  234.  
  235. void AfficheCapt(int fd_petra_in)
  236. {
  237.         read ( fd_petra_in , &u_capt.byte , 1 );
  238.         printf("-----CAPTEURS-----\n");
  239.         printf(" L1 : %d\n",u_capt.capt.L1);
  240.     printf(" L2 : %d\n",u_capt.capt.L2);
  241.     printf(" T : %d\n",u_capt.capt.T);
  242.     printf(" S : %d\n",u_capt.capt.S);
  243.     printf(" CS : %d\n",u_capt.capt.CS);
  244.     printf(" AP : %d\n",u_capt.capt.AP);
  245.     printf(" PP : %d\n",u_capt.capt.PP);
  246.     printf(" DE : %d\n",u_capt.capt.DE);
  247. }
  248.  
  249. int kbhit(void)
  250. {
  251.                 struct termios oldt, newt;
  252.                 int ch;
  253.                 int oldf;
  254.  
  255.                 tcgetattr(STDIN_FILENO, &oldt);
  256.                 newt = oldt;
  257.                 newt.c_lflag &= ~(ICANON | ECHO);
  258.                 tcsetattr(STDIN_FILENO, TCSANOW, &newt);
  259.                 oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
  260.                 fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
  261.  
  262.                 ch = getchar();
  263.  
  264.                 tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
  265.                 fcntl(STDIN_FILENO, F_SETFL, oldf);
  266.  
  267.                 if(ch == EOF)
  268.                         return 0;
  269.  
  270.                 ungetc(ch, stdin);
  271.                 return 1;
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement