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

dexterind-gps.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: dexterind-gps.h 133 2013-03-10 15:15:38Z xander $
00010  */
00011 
00012 #ifndef __DGPS_H__
00013 #define __DGPS_H__
00014 /** \file dexterind-gps.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 3.59 AND HIGHER. 
00031 
00032  * \author Xander Soldaat (xander_at_botbench.com)
00033  * \date 20 February 2011
00034  * \version 0.3
00035  * \example dexterind-gps-test1.c
00036  */
00037 
00038 #pragma systemFile
00039 
00040 #ifndef __COMMON_H__
00041 #include "common.h"
00042 #endif
00043 
00044 #define DGPS_I2C_ADDR   0x06      /*!< Barometric sensor device address */
00045 #define DGPS_CMD_UTC    0x00      /*!< Fetch UTC */
00046 #define DGPS_CMD_STATUS 0x01      /*!< Status of satellite link: 0 no link, 1 link */
00047 #define DGPS_CMD_LAT    0x02      /*!< Fetch Latitude */
00048 #define DGPS_CMD_LONG   0x04      /*!< Fetch Longitude */
00049 #define DGPS_CMD_VELO   0x06      /*!< Fetch velocity in cm/s */
00050 #define DGPS_CMD_HEAD   0x07      /*!< Fetch heading in degrees */
00051 #define DGPS_CMD_DIST   0x08      /*!< Fetch distance to destination */
00052 #define DGPS_CMD_ANGD   0x09      /*!< Fetch angle to destination */
00053 #define DGPS_CMD_ANGR   0x0A      /*!< Fetch angle travelled since last request */
00054 #define DGPS_CMD_SLAT   0x0B      /*!< Set latitude of destination */
00055 #define DGPS_CMD_SLONG  0x0C      /*!< Set longitude of destination */
00056 
00057 
00058 bool DGPSreadStatus(tSensors link);
00059 long DGPSreadUTC(tSensors link);
00060 long DGPSreadLatitude(tSensors link);
00061 long DGPSreadLongitude(tSensors link);
00062 int DGPSreadVelocity(tSensors link);
00063 int DGPSreadHeading(tSensors link);
00064 int DGPSreadRelHeading(tSensors link);
00065 int DGPSreadTravHeading(tSensors link);
00066 bool DGPSsetDestination(tSensors link, long latitude, long longitude);
00067 int DGPSreadDistToDestination(tSensors link);
00068 
00069 tByteArray DGPS_I2CRequest;    /*!< Array to hold I2C command data */
00070 tByteArray DGPS_I2CReply;      /*!< Array to hold I2C reply data */
00071 
00072 long _DGPSreadRegister(tSensors link, unsigned byte command, int replysize) {
00073   memset(DGPS_I2CRequest, 0, sizeof(tByteArray));
00074 
00075   DGPS_I2CRequest[0] = 2;               // Message size
00076   DGPS_I2CRequest[1] = DGPS_I2C_ADDR;   // I2C Address
00077   DGPS_I2CRequest[2] = command;
00078 
00079   if (!writeI2C(link, DGPS_I2CRequest, DGPS_I2CReply, 4))
00080     return -1;
00081 
00082   // Reassemble the messages, depending on their expected size.
00083   if (replysize == 4)
00084     return (long)DGPS_I2CReply[3] + ((long)DGPS_I2CReply[2] << 8) + ((long)DGPS_I2CReply[1] << 16) + ((long)DGPS_I2CReply[0] << 24);
00085   else if (replysize == 3)
00086     return (long)DGPS_I2CReply[2] + ((long)DGPS_I2CReply[1] << 8) + ((long)DGPS_I2CReply[0] << 16);
00087   else if (replysize == 2)
00088     return (long)DGPS_I2CReply[1] + ((long)DGPS_I2CReply[0] << 8);
00089   else if (replysize == 1)
00090     return (long)DGPS_I2CReply[0];
00091 
00092   return 0;
00093 }
00094 
00095 
00096 bool DGPSreadStatus(tSensors link) {
00097   return (_DGPSreadRegister(link, DGPS_CMD_STATUS, 1) == 1) ? true : false;
00098 }
00099 
00100 
00101 /**
00102  * Read the time returned by the GPS in UTC.
00103  * @param link the DGPS port number
00104  * @return the time in UTC
00105  */
00106 long DGPSreadUTC(tSensors link) {
00107   return _DGPSreadRegister(link, DGPS_CMD_UTC, 4);
00108 }
00109 
00110 
00111 /**
00112  * Read the current location's latitude in decimal degree format
00113  * @param link the DGPS port number
00114  * @return current latitude
00115  */
00116 long DGPSreadLatitude(tSensors link) {
00117   return _DGPSreadRegister(link, DGPS_CMD_LAT, 4);
00118 }
00119 
00120 
00121 /**
00122  * Read the current location's longitude in decimal degree format
00123  * @param link the DGPS port number
00124  * @return current longitude
00125  */
00126 long DGPSreadLongitude(tSensors link) {
00127   return _DGPSreadRegister(link, DGPS_CMD_LONG, 4);
00128 }
00129 
00130 
00131 /**
00132  * Read the current velocity in cm/s
00133  * @param link the DGPS port number
00134  * @return current velocity in cm/s
00135  */
00136 int DGPSreadVelocity(tSensors link) {
00137   return _DGPSreadRegister(link, DGPS_CMD_VELO, 3);
00138 }
00139 
00140 
00141 /**
00142  * Read the current heading in degrees
00143  * @param link the DGPS port number
00144  * @return current heading in degrees
00145  */
00146 int DGPSreadHeading(tSensors link) {
00147   return _DGPSreadRegister(link, DGPS_CMD_HEAD, 2);
00148 }
00149 
00150 
00151 /**
00152  * Angle to destination
00153  * @param link the DGPS port number
00154  * @return heading in degrees
00155  */
00156 int DGPSreadRelHeading(tSensors link) {
00157   return _DGPSreadRegister(link, DGPS_CMD_ANGD, 2);
00158 }
00159 
00160 
00161 /**
00162  * Angle travelled since last request, resets the request coordinates on the
00163  * GPS sensor, sends the angle of travel since the last call
00164  * @param link the DGPS port number
00165  * @return heading in degrees
00166  */
00167 int DGPSreadTravHeading(tSensors link) {
00168   return _DGPSreadRegister(link, DGPS_CMD_ANGR, 2);
00169 }
00170 
00171 
00172 /**
00173  * Set the destination coordinates
00174  * @param link the DGPS port number
00175  * @param latitude destination's latitude in decimal degrees
00176  * @param longitude destination's longitude in decimal degrees
00177  * @return true if no error occured, false if it did
00178  */
00179 bool DGPSsetDestination(tSensors link, long latitude, long longitude) {
00180   memset(DGPS_I2CRequest, 0, sizeof(tByteArray));
00181 
00182   // First we send the latitude
00183   DGPS_I2CRequest[0] = 2;               // Message size
00184   DGPS_I2CRequest[1] = DGPS_I2C_ADDR;   // I2C Address
00185   DGPS_I2CRequest[2] = DGPS_CMD_SLAT;
00186   DGPS_I2CRequest[3] = (latitude >> 24) & 0xFF;
00187   DGPS_I2CRequest[4] = (latitude >> 16) & 0xFF;
00188   DGPS_I2CRequest[5] = (latitude >>  8) & 0xFF;
00189   DGPS_I2CRequest[6] = (latitude >>  0) & 0xFF;
00190   if (!writeI2C(link, DGPS_I2CRequest))
00191     return false;
00192 
00193   wait1Msec(100);
00194 
00195   // Then send longitude
00196   DGPS_I2CRequest[0] = 2;               // Message size
00197   DGPS_I2CRequest[1] = DGPS_I2C_ADDR;   // I2C Address
00198   DGPS_I2CRequest[2] = DGPS_CMD_SLONG;
00199   DGPS_I2CRequest[3] = (longitude >> 24) & 0xFF;
00200   DGPS_I2CRequest[4] = (longitude >> 16) & 0xFF;
00201   DGPS_I2CRequest[5] = (longitude >>  8) & 0xFF;
00202   DGPS_I2CRequest[6] = (longitude >>  0) & 0xFF;
00203 
00204   return writeI2C(link, DGPS_I2CRequest);
00205 }
00206 
00207 
00208 /**
00209  * Distance to destination in meters
00210  * @param link the DGPS port number
00211  * @return distance to destination in meters
00212  */
00213 int DGPSreadDistToDestination(tSensors link) {
00214   return _DGPSreadRegister(link, DGPS_CMD_DIST, 4);
00215 }
00216 
00217 #endif // __DGPS_H__
00218 
00219 /*
00220  * $Id: dexterind-gps.h 133 2013-03-10 15:15:38Z xander $
00221  */
00222 /* @} */
00223 /* @} */