Advertisement
Guest User

Untitled

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