Subversion Repositories HomeAutomation

Rev

Rev 389 | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. /**
  2.  * CanGPS.
  3.  *
  4.  * Messages: <byte0><byte1>...<byte7>
  5.  * $GPGGA: <GGA_MSG><UTC><Latitude><Latitude dec.><N/S><Longitude><Longitude dec.><E/W>
  6.  * $GPVTG: <VTG_MSG><Course true><Course dec. true><Course magnetic><Course dec. magnetic><Speed km/h><Speed dec. km/h>
  7.  * $GPZDA: <ZDA_MSG><hh><mm><ss><day><month><year>
  8.  *
  9.  * @date    2001-01-09
  10.  * @author  Erik Larsson
  11.  */
  12.  
  13. #define DEBUG           0 /* Prints debug messages to uart, only use this function if output is not connected to GPS */
  14.  
  15. /*-----------------------------------------------------------------------------
  16.  * Includes
  17.  *---------------------------------------------------------------------------*/
  18. /* system files */
  19. #include <avr/io.h>
  20. #include <avr/interrupt.h>
  21. #include <avr/wdt.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. /* lib files */
  25. #include <can.h>
  26. #include <serial.h>
  27. #include <uart.h>
  28. #include <timebase.h>
  29.  
  30. /* defines */
  31. #define GPS_CMD_GET     0x1400
  32. #define GPS_CMD_SEND    0x1401
  33. #define STATUS_SEND_PERIOD 500 /* milliseconds */
  34.  
  35. #define GGA_MSG     0x01
  36. #define VTG_MSG     0x02
  37. #define ZDA_MSG     0x03
  38. #define GGA_MSG_BYTES 8
  39. #define VTG_MSG_BYTES 7
  40. #define ZDA_MSG_BYTES 7
  41.  
  42. #define TRUE 1
  43. #define FALSE !TRUE
  44. #define CR 0x0D
  45. #define LF 0x0A
  46. #define GPS_MSG_DELIMITER ","
  47.  
  48. /*-------------------------------------------------------------------------
  49.  * Global variables
  50.  * -----------------------------------------------------------------------*/
  51.  
  52. /*----------------------------------------------------------------------------
  53.  * Functions
  54.  * -------------------------------------------------------------------------*/
  55.  
  56. /*----------------------------------------------------------------------------
  57.  * Interrupt routines
  58.  * -------------------------------------------------------------------------*/
  59. ISR( PCINT2_vect )
  60. {
  61. }
  62.  
  63. /*-----------------------------------------------------------------------------
  64.  * Main Program
  65.  *---------------------------------------------------------------------------*/
  66. int main(void) {
  67.     uint32_t timeStamp = 0, timeStampTurn = 0;
  68.     uint8_t buffering = FALSE, gpsMsg_received = FALSE, rxGps_element;
  69.     char buffer[100], rxGps;
  70.     /* GPGGA, Global positioning system fix data */
  71.     char gga_utc[7], gga_latitude[9], gga_latitude_NS, gga_longitude[10], gga_longitude_EW;
  72.     /* GPVTG, Course over ground and speed */
  73.     char vtg_course_true[6], vtg_course_magnetic[6], vtg_speed_kmh[6];
  74.     /* GPZDA, Time & Date */
  75.     char zda_time[7], zda_day[3], zda_month[3], zda_year[5];
  76.  
  77.     char *pch;
  78.  
  79.     Timebase_Init();
  80.     Serial_Init();
  81.  
  82.     sei();
  83. #if DEBUG
  84.     printf("\n------------------------------------------------------------\n");
  85.     printf(  "   CAN: GPS\n");
  86.     printf(  "------------------------------------------------------------\n");
  87.  
  88.     printf("CanInit...");
  89.     if (Can_Init() != CAN_OK) {
  90.         printf("FAILED!\n");
  91.     }else{
  92.         printf("OK!\n");
  93.     }
  94. #else
  95.     Can_Init();
  96. #endif
  97.  
  98.     Can_Message_t txMsg;
  99.     Can_Message_t rxMsg;
  100.     txMsg.Id = GPS_CMD_SEND;
  101.     txMsg.RemoteFlag = 0;
  102.     txMsg.ExtendedFlag = 1;
  103.     txMsg.DataLength = 3;
  104.  
  105.     /* main loop */
  106.     while (1) {
  107.         /* service the CAN routines */
  108.         Can_Service();
  109.  
  110.         /* check if any messages have been received */
  111.         while (Can_Receive(&rxMsg) == CAN_OK) {
  112.             /* This node that control a servo is adressed */
  113.             if( rxMsg.Id == GPS_CMD_GET ){
  114.  
  115.             }
  116.         }
  117.  
  118.         if( Timebase_PassedTimeMillis(timeStamp) >= STATUS_SEND_PERIOD ){
  119.             timeStamp = Timebase_CurrentTime();
  120.             /* Send blinds status to CAN */
  121.  
  122.         }
  123.  
  124.         /* Get data from GPS */
  125.         rxGps = uart_getc();
  126.         while( rxGps != UART_NO_DATA ){
  127.             /* '$' indicates start of message, start buffering */
  128.             if( rxGps == '$' ){
  129.                 buffering = TRUE;
  130.                 rxGps_element = 0;
  131.             }else if( rxGps == CR ){
  132.                 /* End of message */
  133.                 buffer[rxGps_element]='\0';
  134.                 buffering = FALSE;
  135.                 gpsMsg_received = TRUE;
  136.             }
  137.  
  138.             if( buffering ){
  139.                 buffer[ rxGps_element ] = rxGps;
  140.                 rxGps_element++;
  141.             }
  142.             /* Get next character */
  143.             rxGps = uart_getc();
  144.         }
  145.  
  146.         if( gpsMsg_received ){
  147.             /* One whole msg is completed, start compare and parse */
  148.             if( !strncmp(buffer, "GPGGA", 5) ){
  149.                 /* Standard NMEA message, $GPGGA */
  150.                 strtok(buffer, GPS_MSG_DELIMITER); /* Throw away "GPGGA" */
  151.  
  152.                 /* Get UTC */
  153.                 pch = strtok(NULL, GPS_MSG_DELIMITER);
  154.                 strcpy(gga_utc, pch);
  155.  
  156.                 /* Get latitude */
  157.                 pch = strtok(NULL, GPS_MSG_DELIMITER);
  158.                 strcpy(gga_latitude, pch);
  159.  
  160.                 /* Get latitude N/S */
  161.                 pch = strtok(NULL, GPS_MSG_DELIMITER);
  162.                 strcpy(gga_latitude_NS, pch);
  163.  
  164.                 /* Get longitude */
  165.                 pch = strtok(NULL, GPS_MSG_DELIMITER);
  166.                 strcpy(gga_longitude, pch);
  167.  
  168.                 /* Get longitude E/W */
  169.                 pch = strtok(NULL, GPS_MSG_DELIMITER);
  170.                 strcpy(gga_longitude_EW, pch);
  171.  
  172.             }else if( !strncmp(buffer, "GPVTG", 5) ){
  173.                 /* Standard NMEA message, $GPVTG */
  174.                 strtok(buffer, GPS_MSG_DELIMITER); /* Throw away "GPVTG" */
  175.  
  176.                 /* Get course over ground, true degree */
  177.                 pch = strtok(NULL, GPS_MSG_DELIMITER);
  178.                 strcpy(vtg_course_true, pch);
  179.  
  180.                 /* Get course over ground, magnetic degree */
  181.                 pch = strtok(NULL, GPS_MSG_DELIMITER);
  182.                 strcpy(vtg_course_magnetic, pch);
  183.  
  184.                 /* Get speed over ground, km/h */
  185.                 pch = strtok(NULL, GPS_MSG_DELIMITER);
  186.                 strcpy(vtg_speed_kmh, pch);
  187.  
  188.             }else if( !strncmp(buffer, "GPZDA", 5) ){
  189.                 /* Standard NMEA message, $GPZDA */
  190.                 strtok(buffer, GPS_MSG_DELIMITER); /* Throw away "GPZDA" */
  191.  
  192.                 /* Get time */
  193.                 pch = strtok(NULL, GPS_MSG_DELIMITER);
  194.                 strcpy(zda_time, pch);
  195.  
  196.                 /* Get day, 0-31 */
  197.                 pch = strtok(NULL, GPS_MSG_DELIMITER);
  198.                 strcpy(zda_day, pch);
  199.  
  200.                 /* Get month, 1-12 */
  201.                 pch = strtok(NULL, GPS_MSG_DELIMITER);
  202.                 strcpy(zda_month, pch);
  203.  
  204.                 /* Get year */
  205.                 pch = strtok(NULL, GPS_MSG_DELIMITER);
  206.                 strcpy(zda_year, pch);
  207.             }
  208.  
  209.             gpsMsg_received = FALSE; /* Get ready for next message */
  210.  
  211.         }
  212.     }
  213.     return 0;
  214. }
  215.  
  216.