Advertisement
Guest User

Untitled

a guest
Mar 9th, 2011
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.15 KB | None | 0 0
  1. component hal_mydro;
  2.  
  3. option userspace;
  4. option singleton yes;
  5. option userinit yes;
  6. option extra_cleanup;
  7.  
  8. pin in float xpos;
  9. pin in float ypos;
  10. pin in float zpos;
  11. license "GPL";
  12. ;;
  13.  
  14. #include <termios.h>
  15. #include <fcntl.h>
  16. #include <stdio.h>
  17. #include <unistd.h>
  18. #include <math.h>
  19.  
  20. #define         B_Z0    5
  21. #define         B_Y0    11
  22. #define         B_X0    16
  23.  
  24.  
  25. //                               0     1     2     3     4     5     6     7     8     9     -     E    FULL
  26. unsigned char decode7seg[13] = {0xEE, 0x82, 0xDC, 0xD6, 0xB2, 0x76, 0x7E, 0xC2, 0xFE, 0xF6, 0x10, 0x7C, 0xff};
  27.  
  28.  
  29. int fd;
  30. unsigned char buffer[64];
  31. struct termios options;
  32. int i,j,l;
  33. double x_val, y_val, z_val, k;
  34. double t;
  35. int ioerror = 0;
  36. char* dev="/dev/ttyACM0";
  37.  
  38. static void aaauser_init(int argc, char **argv) {
  39.   printf ("lol rofl\n");
  40.   printf ("lol =%d\n",argc);
  41. //  printf ("0 =%c\n",argv[0]);
  42.   printf ("lol =%d\n",argc);
  43. }
  44.  
  45. static void user_init(int argc, char **argv) {
  46.  
  47.   printf("hal_mydro: Trying to open (%s)\n",dev);
  48.  
  49.   fd = open(dev, O_RDWR | O_NOCTTY | O_NONBLOCK);
  50.   if (fd <0) {
  51.     printf("hal_mydro: Error opening (%s)\n",dev);
  52.     exit(-EBUSY);
  53.   } else {
  54.     printf("hal_mydro: Open successfull (%s)\n",dev);
  55.     // open successful
  56.     fcntl(fd, F_SETFL, 0);
  57.     /* get the current options */
  58.     tcgetattr(fd, &options);
  59.     cfsetispeed(&options, B57600);
  60.     cfsetospeed(&options, B57600);
  61.     /* set raw input, 1 second timeout */
  62.     options.c_cflag |= (CLOCAL | CREAD);
  63.     options.c_oflag &= ~OPOST;
  64.     options.c_cc[VMIN] = 0;
  65.     options.c_cc[VTIME] = 10;
  66.  
  67.     options.c_cflag &= ~PARENB; /* Clear parity enable */
  68.     options.c_iflag &= ~INPCK; /* Enable parity checking */
  69.     options.c_cflag &= ~CSTOPB;
  70.     options.c_cflag &= ~CSIZE;
  71.     options.c_cflag |= CS8;
  72.     options.c_cflag &= ~CRTSCTS;
  73.     // options.c_iflag &= ~(IXON | IXOFF | IXANY); /* no flow control */
  74.     options.c_oflag &= ~(IXON | IXOFF | IXANY); /* no flow control */
  75.     options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
  76.     options.c_oflag &= ~OPOST; /* No output processing */
  77.     options.c_oflag &= ~ONLCR; /* Don't convert linefeeds */
  78.     /* Miscellaneous stuff */
  79.     options.c_cflag |= (CLOCAL | CREAD); /* Enable receiver, set local */
  80.     /* Linux seems to have problem with the following ??!! */
  81.     options.c_cflag |= (IXON | IXOFF); /* Software flow control */
  82.     options.c_lflag = 0; /* no local flags */
  83.     options.c_cflag |= HUPCL; /* Drop DTR on close */
  84.     /* Setup non blocking, return on 1 character */
  85.     options.c_cc[VMIN] = 0;
  86.     options.c_cc[VTIME] = 1;
  87.     /* Clear the line */
  88.     tcflush(fd,TCIFLUSH);
  89.  
  90.     /* Update the options and do it NOW */
  91.     if (tcsetattr(fd,TCSANOW,&options) != 0) {
  92.       printf("configure port failed\n");
  93.       exit(-EBUSY);
  94.     }
  95.  
  96.     // get version schreiben
  97.     buffer[0] = 0xAA;
  98.     buffer[1] = 0x01;
  99.     buffer[3] = 0x00;
  100.     buffer[2] = buffer[0] ^ buffer[1] ^ buffer[3];
  101.     write(fd,buffer,4);
  102.     usleep(10000);
  103.     // antwort lesen
  104.     i = read(fd,buffer,64);
  105.     printf("hal_mydro: VERSION: Got %d bytes :",i);
  106.     for (j = 0; j<i; j++) {
  107.       printf(" 0x%02x ",buffer[j]);
  108.     }
  109.     printf("\n");
  110.  
  111.  
  112.     // count bytes schreiben
  113.     buffer[0] = 0xAA;
  114.     buffer[1] = 0x13;
  115.     buffer[3] = 0x00;
  116.     buffer[2] = buffer[0] ^ buffer[1] ^ buffer[3];
  117.     write(fd,buffer,4);
  118.     usleep(10000);
  119.  
  120.     i = read(fd,buffer,64);
  121.     printf("hal_mydro: NBYTES: Got %d bytes :",i);
  122.     for (j = 0; j<i; j++) {
  123.       printf(" 0x%02x ",buffer[j]);
  124.     }
  125.     printf("\n");
  126.   }
  127. }
  128.  
  129.  
  130. void user_mainloop() {
  131.   printf("hal_mydro: entered mainloop...\n");
  132.   while (ioerror == 0) {
  133.     /*x_val = (double)xpos;
  134.     y_val = (double)ypos;
  135.     z_val = (double)zpos;*/
  136.     x_val += M_PI;
  137.     y_val = 100-x_val;
  138.     z_val = y_val/3;
  139.  
  140.     // Header
  141.     buffer[0] = 0xAA;
  142.     buffer[1] = 0x10;
  143.     buffer[2] = 0x00;
  144.     buffer[3] = 0x12;
  145.  
  146.     // Z
  147.     l=B_Z0;
  148.     k = fabs(z_val);
  149.     for(i=2; i>-3;i--) {
  150.       t = k;
  151.       j = (int)(k/pow(10,i));
  152.       k = k - j* pow(10,i);
  153.       //printf("Z: i=%d j=%d, t=%4.2f k=%4.2f \n",i,j,t,k);
  154.       if (j > 9) { j = 11;}
  155.       buffer[l++] = decode7seg[j];
  156.     }
  157.     // Minus-Zeichen?
  158.     if (z_val < 0) {
  159.       if (buffer[B_Z0] == decode7seg[0]) {
  160.         buffer[B_Z0] = decode7seg[10];
  161.       } else {
  162.         buffer[B_Z0] = decode7seg[11]; // Error
  163.       }
  164.     }
  165.     buffer[7] += 1;  //Dezimalpunkt
  166.     buffer[4] = 0xff;   //LEDs
  167.  
  168.     // Y
  169.     l=B_Y0;
  170.     k = fabs(y_val);
  171.     for(i=2; i>-3;i--) {
  172.       j = (int)(k/pow(10,i));
  173.       k = k - j* pow(10,i);
  174.       //printf("Y: i=%d j=%d, k=%4.2f \n",i,j,k);
  175.       if (j > 9) { j = 11;}
  176.       buffer[l++] = decode7seg[j];
  177.     }
  178.     // Minus-Zeichen?
  179.     if (y_val < 0) {
  180.       if (buffer[B_Y0] == decode7seg[0]) {
  181.         buffer[B_Y0] = decode7seg[10];
  182.       } else {
  183.         buffer[B_Y0] = decode7seg[11]; // Error
  184.       }
  185.     }
  186.     buffer[13] += 1;  //Dezimalpunkt
  187.     buffer[10] = 0;   //Unbenutzt
  188.  
  189.     // X
  190.     l=B_X0;
  191.     k = fabs(x_val);
  192.     for(i=3; i>-3;i--) {
  193.       j = (int)(k/pow(10,i));
  194.       k = k - j* pow(10,i);
  195.       if (j > 9) { j = 11;}
  196.       //printf("X: i=%d j=%d, k=%4.2f \n",i,j,k);
  197.       buffer[l++] = decode7seg[j];
  198.     }
  199.     // Minus-Zeichen?
  200.     if (x_val < 0) {
  201.       if (buffer[B_X0] == decode7seg[0]) {
  202.         buffer[B_X0] = decode7seg[10];
  203.       } else {
  204.         buffer[B_X0] = decode7seg[11]; // Error
  205.       }
  206.     }
  207.     //Dezimalpunkt
  208.     buffer[19] += 1;
  209.  
  210.  
  211.     // calculate checksum
  212.     buffer[2] = buffer[0] ^ buffer[1];
  213.     for (j=3; j < 22; j++) {
  214.       buffer[2] ^= buffer[j];
  215.     }
  216.  
  217.     // schreiben
  218.     write(fd,buffer,22);
  219.  
  220.     // antwort lesen
  221.     i = read(fd,buffer,64);
  222.     if (i != 4) {
  223.       printf("hal_mydro: Got %d bytes :",i);
  224.       for (j = 0; j<i; j++) {
  225.         printf(" 0x%02x ",buffer[j]);
  226.       }
  227.       printf("\n");
  228.       ioerror = 1;
  229.     }
  230.  
  231.  
  232.   //Delay
  233.   usleep(50*1000);
  234.   } // end while
  235.   printf("hal_mydro: exit mainloop\n");
  236.  
  237. } // end main_loop
  238.  
  239.  
  240. EXTRA_CLEANUP() {
  241.   if (fd >=0) {
  242.     close(fd);
  243.   }
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement