Mindstorms 3rd Party ROBOTC Drivers RobotC
[Home] [Download] [Submit a bug/suggestion] [ROBOTC Forums] [Blog] [Support this project]

DGPS-driver.h

Go to the documentation of this file.
00001 /*!@addtogroup Dexter_Industries
00002  * @{
00003  * @defgroup dGPS GPS Sensor
00004  * Dexter Industries dGPS Sensor driver
00005  * @{
00006  */
00007 
00008 /*
00009  * $Id: DGPS-driver.h 49 2011-04-27 13:00:05Z xander $
00010  */
00011 
00012 #ifndef __DGPS_H__
00013 #define __DGPS_H__
00014 /** \file DGPS-driver.h
00015  * \brief Dexter Industries GPS Sensor driver
00016  *
00017  * DGPS-driver.h provides an API for the Dexter Industries GPS Sensor.\n
00018  *
00019  * Changelog:
00020  * - 0.1: Initial release
00021  * - 0.2: Added DGPSreadDistToDestination()
00022  * - 0.3: Changed from array structs to typedefs\n
00023  *        Fixed typos and ommissions in commands\n
00024  *
00025  * Credits:
00026  * - Big thanks to Dexter Industries for providing me with the hardware necessary to write and test this.
00027  *
00028  * License: You may use this code as you wish, provided you give credit where its due.
00029  *
00030  * THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 2.00 AND HIGHER.
00031  * \author Xander Soldaat (mightor_at_gmail.com)
00032  * \date 20 February 2011
00033  * \version 0.3
00034  * \example DGPS-test1.c
00035  */
00036 
00037 #pragma systemFile
00038 
00039 #ifndef __COMMON_H__
00040 #include "common.h"
00041 #endif
00042 
00043 #define DGPS_I2C_ADDR   0x06      /*!< Barometric sensor device address */
00044 #define DGPS_CMD_UTC    0x00      /*!< Fetch UTC */
00045 #define DGPS_CMD_STATUS 0x01      /*!< Status of satellite link: 0 no link, 1 link */
00046 #define DGPS_CMD_LAT    0x02      /*!< Fetch Latitude */
00047 #define DGPS_CMD_LONG   0x04      /*!< Fetch Longitude */
00048 #define DGPS_CMD_VELO   0x06      /*!< Fetch velocity in cm/s */
00049 #define DGPS_CMD_HEAD   0x07      /*!< Fetch heading in degrees */
00050 #define DGPS_CMD_DIST   0x08      /*!< Fetch distance to destination */
00051 #define DGPS_CMD_ANGD   0x09      /*!< Fetch angle to destination */
00052 #define DGPS_CMD_ANGR   0x0A      /*!< Fetch angle travelled since last request */
00053 #define DGPS_CMD_SLAT   0x0B      /*!< Set latitude of destination */
00054 #define DGPS_CMD_SLONG  0x0C      /*!< Set longitude of destination */
00055 
00056 
00057 bool DGPSreadStatus(tSensors link);
00058 long DGPSreadUTC(tSensors link);
00059 long DGPSreadLatitude(tSensors link);
00060 long DGPSreadLongitude(tSensors link);
00061 int DGPSreadVelocity(tSensors link);
00062 int DGPSreadHeading(tSensors link);
00063 int DGPSreadRelHeading(tSensors link);
00064 int DGPSreadTravHeading(tSensors link);
00065 bool DGPSsetDestination(tSensors link, long latitude, long longitude);
00066 int DGPSreadDistToDestination(tSensors link);
00067 
00068 tByteArray DGPS_I2CRequest;    /*!< Array to hold I2C command data */
00069 tByteArray DGPS_I2CReply;      /*!< Array to hold I2C reply data */
00070 
00071 long _DGPSreadRegister(tSensors link, unsigned byte command, int replysize) {
00072   memset(DGPS_I2CRequest, 0, sizeof(tByteArray));
00073 
00074   DGPS_I2CRequest[0] = 2;               // Message size
00075   DGPS_I2CRequest[1] = DGPS_I2C_ADDR;   // I2C Address
00076   DGPS_I2CRequest[2] = command;
00077 
00078   if (!writeI2C(link, DGPS_I2CRequest, 4))
00079     return -1;
00080 
00081   if (!readI2C(link, DGPS_I2CReply, 4))
00082     return -1;
00083 
00084   // Reassemble the messages, depending on their expected size.
00085   if (replysize == 4)
00086     return (long)DGPS_I2CReply[3] + ((long)DGPS_I2CReply[2] << 8) + ((long)DGPS_I2CReply[1] << 16) + ((long)DGPS_I2CReply[0] << 24);
00087   else if (replysize == 3)
00088     return (long)DGPS_I2CReply[2] + ((long)DGPS_I2CReply[1] << 8) + ((long)DGPS_I2CReply[0] << 16);
00089   else if (replysize == 2)
00090     return (long)DGPS_I2CReply[1] + ((long)DGPS_I2CReply[0] << 8);
00091   else if (replysize == 1)
00092     return (long)DGPS_I2CReply[0];
00093 
00094   return 0;
00095 }
00096 
00097 
00098 bool DGPSreadStatus(tSensors link) {
00099   return (_DGPSreadRegister(link, DGPS_CMD_STATUS, 1) == 1) ? true : false;
00100 }
00101 
00102 
00103 /**
00104  * Read the time returned by the GPS in UTC.
00105  * @param link the DGPS port number
00106  * @return the time in UTC
00107  */
00108 long DGPSreadUTC(tSensors link) {
00109   return _DGPSreadRegister(link, DGPS_CMD_UTC, 4);
00110 }
00111 
00112 
00113 /**
00114  * Read the current location's latitude in decimal degree format
00115  * @param link the DGPS port number
00116  * @return current latitude
00117  */
00118 long DGPSreadLatitude(tSensors link) {
00119   return _DGPSreadRegister(link, DGPS_CMD_LAT, 4);
00120 }
00121 
00122 
00123 /**
00124  * Read the current location's longitude in decimal degree format
00125  * @param link the DGPS port number
00126  * @return current longitude
00127  */
00128 long DGPSreadLongitude(tSensors link) {
00129   return _DGPSreadRegister(link, DGPS_CMD_LONG, 4);
00130 }
00131 
00132 
00133 /**
00134  * Read the current velocity in cm/s
00135  * @param link the DGPS port number
00136  * @return current velocity in cm/s
00137  */
00138 int DGPSreadVelocity(tSensors link) {
00139   return _DGPSreadRegister(link, DGPS_CMD_VELO, 3);
00140 }
00141 
00142 
00143 /**
00144  * Read the current heading in degrees
00145  * @param link the DGPS port number
00146  * @return current heading in degrees
00147  */
00148 int DGPSreadHeading(tSensors link) {
00149   return _DGPSreadRegister(link, DGPS_CMD_HEAD, 2);
00150 }
00151 
00152 
00153 /**
00154  * Angle to destination
00155  * @param link the DGPS port number
00156  * @return heading in degrees
00157  */
00158 int DGPSreadRelHeading(tSensors link) {
00159   return _DGPSreadRegister(link, DGPS_CMD_ANGD, 2);
00160 }
00161 
00162 
00163 /**
00164  * Angle travelled since last request, resets the request coordinates on the
00165  * GPS sensor, sends the angle of travel since the last call
00166  * @param link the DGPS port number
00167  * @return heading in degrees
00168  */
00169 int DGPSreadTravHeading(tSensors link) {
00170   return _DGPSreadRegister(link, DGPS_CMD_ANGR, 2);
00171 }
00172 
00173 
00174 /**
00175  * Set the destination coordinates
00176  * @param link the DGPS port number
00177  * @param latitude destination's latitude in decimal degrees
00178  * @param longitude destination's longitude in decimal degrees
00179  * @return true if no error occured, false if it did
00180  */
00181 bool DGPSsetDestination(tSensors link, long latitude, long longitude) {
00182   memset(DGPS_I2CRequest, 0, sizeof(tByteArray));
00183 
00184   // First we send the latitude
00185   DGPS_I2CRequest[0] = 2;               // Message size
00186   DGPS_I2CRequest[1] = DGPS_I2C_ADDR;   // I2C Address
00187   DGPS_I2CRequest[2] = DGPS_CMD_SLAT;
00188   DGPS_I2CRequest[3] = (latitude >> 24) & 0xFF;
00189   DGPS_I2CRequest[4] = (latitude >> 16) & 0xFF;
00190   DGPS_I2CRequest[5] = (latitude >>  8) & 0xFF;
00191   DGPS_I2CRequest[6] = (latitude >>  0) & 0xFF;
00192   if (!writeI2C(link, DGPS_I2CRequest, 0))
00193     return false;
00194 
00195   wait1Msec(100);
00196 
00197   // Then send longitude
00198   DGPS_I2CRequest[0] = 2;               // Message size
00199   DGPS_I2CRequest[1] = DGPS_I2C_ADDR;   // I2C Address
00200   DGPS_I2CRequest[2] = DGPS_CMD_SLONG;
00201   DGPS_I2CRequest[3] = (longitude >> 24) & 0xFF;
00202   DGPS_I2CRequest[4] = (longitude >> 16) & 0xFF;
00203   DGPS_I2CRequest[5] = (longitude >>  8) & 0xFF;
00204   DGPS_I2CRequest[6] = (longitude >>  0) & 0xFF;
00205 
00206   return writeI2C(link, DGPS_I2CRequest, 0);
00207 }
00208 
00209 
00210 /**
00211  * Distance to destination in meters
00212  * @param link the DGPS port number
00213  * @return distance to destination in meters
00214  */
00215 int DGPSreadDistToDestination(tSensors link) {
00216   return _DGPSreadRegister(link, DGPS_CMD_DIST, 4);
00217 }
00218 
00219 #endif // __DGPS_H__
00220 
00221 /*
00222  * $Id: DGPS-driver.h 49 2011-04-27 13:00:05Z xander $
00223  */
00224 /* @} */
00225 /* @} */