Advertisement
Guest User

Untitled

a guest
May 2nd, 2014
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.99 KB | None | 0 0
  1. #include "IMU_Navdata.h"
  2. #include <stdio.h>   /* Standard input/output definitions */
  3. #include <string.h>  /* String function definitions */
  4. #include <unistd.h>  /* UNIX standard function definitions */
  5. #include <fcntl.h>   /* File control definitions */
  6. #include <errno.h>   /* Error number definitions */
  7. #include <termios.h> /* POSIX terminal control definitions */
  8. #include <stdint.h>
  9. #include <math.h>
  10. #include <stdio.h>
  11.  
  12.  
  13. int IMU_Navdata_init () {
  14.    
  15.         fd = open ("/dev/ttyO1", O_RDWR | O_NOCTTY | O_NONBLOCK | O_NDELAY ) ;
  16.  
  17.         if (fd < 0) {
  18.             printf("Failed to open serial port \n");
  19.             perror("Failed to open serial port");
  20.             return 0 ;
  21.         }
  22.  
  23.                 int flags = fcntl(fd, F_GETFL) ;
  24.        
  25.             fcntl(fd, F_SETFL, flags | O_NONBLOCK); //read calls are non blocking
  26.             //set port options
  27.             struct termios options;
  28.             //Get the current options for the port
  29.             tcgetattr(fd, &options);
  30.             //Set the baud rates to 460800
  31.             cfsetispeed(&options, 460800);
  32.             cfsetospeed(&options, 460800);
  33.  
  34.             options.c_cflag |= (CLOCAL | CREAD); //Enable the receiver and set local mode
  35.             options.c_iflag = 0; //clear input options
  36.             options.c_lflag = 0; //clear local options
  37.             options.c_oflag &= ~OPOST; //clear output options (raw output)
  38.        
  39.    
  40.             //Set the new options for the port
  41.             tcsetattr(fd, TCSANOW, &options);
  42.  
  43.         // stop acquisition
  44.         uint8_t cmd = 0x02;
  45.         write (fd, &cmd, 1);
  46.                
  47.         // read some potential dirt (retry a lot of times)
  48.         char tmp[100];
  49.         for(int i = 0; i < 12; i++) {
  50.             uint16_t dirt = read (fd, tmp, sizeof tmp);
  51.             (void) dirt;
  52.  
  53.             usleep (1000);
  54.         }
  55.                
  56.        
  57. /******************************************************
  58.  *  inline some stuff related to baro calibration
  59.  *
  60.  *******************************************************/
  61.        /*
  62.         uint8_t cmd_1=0x17; // send cmd 23
  63.         write(fd,&cmd_1, 1);
  64.        
  65.            // wait 20ms to retrieve data
  66.         for (int i=0;i<22;i++)
  67.         {
  68.             usleep(1000);
  69.         }
  70.  
  71.                 uint8_t calibBuffer[22];
  72.          
  73.                 size_t baro_byte_count = 0;
  74.  
  75.           while(baro_byte_count < 22)
  76.           {
  77.             ssize_t n = read(fd, calibBuffer + baro_byte_count, 22 - baro_byte_count);
  78.             if (n < 0)
  79.             {
  80.               if (errno == EAGAIN || errno == EWOULDBLOCK)
  81.                 continue;
  82.               return n;
  83.             }
  84.             baro_byte_count += n;
  85.           }
  86.       */
  87.  
  88. /******************************************************
  89.  * End of in-lining baro calibration
  90.  *
  91.  *******************************************************/
  92.         // start acquisition
  93.         cmd = 0x01;
  94.         write (fd, &cmd, 1);
  95.        
  96.        
  97.         port.checksum_errors = 0;
  98.         port.bytesRead = 0;
  99.         port.totalBytesRead = 0;
  100.         port.packetsRead = 0;
  101.         port.isInitialized = 1;
  102.  
  103.      
  104.         isInitialized = 1 ;
  105.  
  106.         return 1 ;
  107.  }
  108.  
  109. int IMU_Navdata_update(unsigned short *y) {
  110.    
  111.     static int last_checksum_wrong = 0 ;
  112.     int checkSumOk = 1 ;
  113.    
  114.     Acquire_Navdata_Bytes();
  115.    
  116.     // while there is something interesting to do...
  117.         while (port.bytesRead >= NAVDATA_PACKET_SIZE) {
  118.          
  119.             if (port.buffer[0] == NAVDATA_START_BYTE) {
  120.                 tcflush(fd,TCIOFLUSH);
  121.                 memcpy (&navdata, port.buffer, NAVDATA_PACKET_SIZE);
  122.  
  123.                 // Calculating the checksum
  124.                 uint16_t checksum = 0;
  125.                 for (int i = 2; i < NAVDATA_PACKET_SIZE - 2; i += 2) {
  126.                     checksum += port.buffer[i] + (port.buffer[i + 1] << 8);
  127.                 }
  128.  
  129.                 // When checksum is incorrect
  130.                 if (navdata.chksum != checksum) {
  131.                     printf ("Checksum error [calculated: %d] [packet: %d] [diff: %d]\n", checksum, navdata.chksum, checksum - navdata.chksum);
  132.                     port.checksum_errors++;
  133.                                         checkSumOk = 0 ;
  134.                 }
  135.  
  136.                 // When we already dropped a packet or checksum is correct
  137.                 if (last_checksum_wrong || navdata.chksum == checksum) {
  138.                     // Invert byte order so that TELEMETRY works better
  139.                     uint8_t tmp;
  140.                     uint8_t* p = (uint8_t*) &(navdata.pressure);
  141.                     tmp = p[0];
  142.                     p[0] = p[1];
  143.                     p[1] = tmp;
  144.                     p = (uint8_t*) &(navdata.temperature_pressure);
  145.                     tmp = p[0];
  146.                     p[0] = p[1];
  147.                     p[1] = tmp;
  148.  
  149.                     //baroUpdateLogic (); //removed this for now
  150.  
  151.                     //imuAvailable = 1 ;  // removed this for now
  152.                     last_checksum_wrong = 0 ;
  153.                     port.packetsRead++;
  154.                 }
  155.  
  156.                 // Crop the buffer
  157.                 cropbuffer (NAVDATA_PACKET_SIZE);
  158.                 tcflush(fd,TCIOFLUSH);
  159.             }
  160.             else {
  161.                 // find start byte, copy all data from startbyte to buffer origin, update bytesread
  162.                 uint8_t * pint;
  163.                 pint = (uint8_t*) memchr (port.buffer, NAVDATA_START_BYTE, port.bytesRead);
  164.  
  165.                 if (pint != NULL) {
  166.                     cropbuffer (pint - port.buffer);
  167.                 }
  168.                 else {
  169.                     // if the start byte was not found, it means there is junk in the buffer
  170.                     port.bytesRead = 0;
  171.                 }
  172.             }
  173.         }
  174.    
  175.    
  176.  
  177.        *y = navdata.ultrasound;
  178. return checkSumOk ;
  179. }
  180.        
  181.  
  182. void IMU_Navdata_stop() {
  183.    
  184.         isInitialized = 0 ;
  185.         close (fd) ;
  186.        
  187.        
  188.        
  189. }
  190.  
  191.  
  192.  
  193. /*-----------------------------------------------------------------------------
  194. *        Other functions
  195. *-----------------------------------------------------------------------------*/
  196.  
  197. void Acquire_Navdata_Bytes () { //original function
  198.         int newbytes = read (fd, port.buffer + port.bytesRead, NAVDATA_BUFFER_SIZE - port.bytesRead);
  199.  
  200.         // because non-blocking read returns -1 when no bytes available
  201.         if (newbytes > 0) {
  202.             port.bytesRead += newbytes;
  203.             port.totalBytesRead += newbytes;
  204.         }
  205.          
  206. }
  207.  
  208.  
  209.  static void cropbuffer (int cropsize) {
  210.  
  211.         if (port.bytesRead - cropsize < 0) {
  212.             // TODO think about why the amount of bytes read minus the cropsize gets below zero
  213.             printf ("BytesRead(=%d) - Cropsize(=%d) may not be below zero. port->buffer=%p\n", port.bytesRead, cropsize, port.buffer);
  214.             return;
  215.         }
  216.  
  217.         memmove (port.buffer, port.buffer + cropsize, NAVDATA_BUFFER_SIZE - cropsize);
  218.         port.bytesRead -= cropsize;
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement