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

MICC-driver.h

Go to the documentation of this file.
00001 /*!@addtogroup MicroInfinity
00002  * @{
00003  * @defgroup cruizcore CruizCore XG1300L Sensor
00004  * CruizCore XG1300L
00005  * @{
00006  */
00007 
00008 /*
00009  * $Id: MICC-driver.h 56 2011-05-30 19:47:12Z xander $
00010  */
00011 
00012 #ifndef __MICC_H__
00013 #define __MICC_H__
00014 /** \file MICC-driver.h
00015  * \brief MicroInfinity CruizCore XG1300L driver
00016  *
00017  * MICC-driver.h provides an API for the MicroInfinity CruizCore XG1300L sensor.
00018  *
00019  * Changelog:
00020  * - 0.1: Initial release
00021  *
00022  * Credits:
00023  * - Big thanks to MicroInfinity for providing me with the hardware necessary to write and test this.
00024  *
00025  * License: You may use this code as you wish, provided you give credit where its due.
00026  *
00027  * THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 2.00 AND HIGHER.
00028  * \author Xander Soldaat (mightor_at_gmail.com)
00029  * \date 29 May 2011
00030  * \version 0.1
00031  * \example MICC-test1.c
00032  * \example MICC-test2.c
00033  */
00034 
00035 #pragma systemFile
00036 
00037 #ifndef __COMMON_H__
00038 #include "common.h"
00039 #endif
00040 
00041 #define MICC_I2C_ADDR       0x02  /*!< MICC I2C device address */
00042 
00043 #define MICC_ACC_ANG        0x42  /*!< MICC Accumulated angle (2 bytes) */
00044 
00045 #define MICC_TURN_RATE      0x44  /*!< MICC Rate of Turn (2 bytes) */
00046 
00047 #define MICC_X_ACCEL        0x46  /*!< MICC X acceleration data (2 bytes) */
00048 #define MICC_Y_ACCEL        0x48  /*!< MICC Y acceleration data (2 bytes) */
00049 #define MICC_Z_ACCEL        0x4A  /*!< MICC Z acceleration data (2 bytes) */
00050 
00051 #define MICC_CMD_RESET      0x60  /*!< MICC Reset the device */
00052 #define MICC_CMD_RANGE_2G   0x61  /*!< MICC Acceleration up to 2G */
00053 #define MICC_CMD_RANGE_4G   0x62  /*!< MICC Acceleration up to 4G */
00054 #define MICC_CMD_RANGE_8G   0x63  /*!< MICC Acceleration up to 8G */
00055 
00056 int MICCreadRelativeHeading(tSensors link);
00057 int MICCreadTurnRate(tSensors link);
00058 bool MICCreadAccel(tSensors link, int &x_accel, int &y_accel, int &z_accel);
00059 bool MICCsendCmd(tSensors link, ubyte command);
00060 
00061 #define MICCsetRange2G(x) MICCsendCmd(x, MICC_CMD_RANGE_2G) /*!< Macro for setting sensor to 2G range */
00062 #define MICCsetRange4G(x) MICCsendCmd(x, MICC_CMD_RANGE_4G) /*!< Macro for setting sensor to 4G range */
00063 #define MICCsetRange8G(x) MICCsendCmd(x, MICC_CMD_RANGE_8G) /*!< Macro for setting sensor to 8G range */
00064 #define MICCreset(x)      MICCsendCmd(x, MICC_CMD_RESET)    /*!< Macro for resetting sensor */
00065 
00066 tByteArray MICC_I2CRequest;       /*!< Array to hold I2C command data */
00067 tByteArray MICC_I2CReply;         /*!< Array to hold I2C reply data */
00068 
00069 
00070 /**
00071  * Return the current relative heading, value between -179 and 180 degrees.<br>
00072  * Angle is measured in 100th degrees.  So 12899 = 128.99 degrees.
00073  * @return the relative heading
00074  */
00075 int MICCreadRelativeHeading(tSensors link) {
00076   memset(MICC_I2CRequest, 0, sizeof(tByteArray));
00077 
00078   MICC_I2CRequest[0] = 2;               // Number of bytes in I2C command
00079   MICC_I2CRequest[1] = MICC_I2C_ADDR;   // I2C address of accel sensor
00080   MICC_I2CRequest[2] = MICC_ACC_ANG;    // Set write address to sensor mode register
00081 
00082   if (!writeI2C(link, MICC_I2CRequest, 2))
00083     return 0;
00084 
00085   if (!readI2C(link, MICC_I2CReply, 2))
00086     return 0;
00087 
00088   // Each result is made up of two bytes.
00089         return (MICC_I2CReply[1] << 8) + MICC_I2CReply[0];
00090 }
00091 
00092 
00093 /**
00094  * Return the Rate of Turn in degrees per second
00095  * @return the current rate of turn
00096  */
00097 int MICCreadTurnRate(tSensors link) {
00098   memset(MICC_I2CRequest, 0, sizeof(tByteArray));
00099 
00100   MICC_I2CRequest[0] = 2;               // Number of bytes in I2C command
00101   MICC_I2CRequest[1] = MICC_I2C_ADDR;   // I2C address of accel sensor
00102   MICC_I2CRequest[2] = MICC_TURN_RATE;    // Set write address to sensor mode register
00103 
00104   if (!writeI2C(link, MICC_I2CRequest, 2))
00105     return 0;
00106 
00107   if (!readI2C(link, MICC_I2CReply, 2))
00108     return 0;
00109 
00110   // Each result is made up of two bytes.
00111         return (MICC_I2CReply[1] << 8) + MICC_I2CReply[0];
00112 }
00113 
00114 /**
00115  * Read tilt data from the sensor
00116  * @param link the sensor port number
00117  * @param x_accel X acceleration data
00118  * @param y_accel Y acceleration data
00119  * @param z_accel Z acceleration data
00120  * @return true if no error occured, false if it did
00121  */
00122 bool MICCreadAccel(tSensors link, int &x_accel, int &y_accel, int &z_accel) {
00123   memset(MICC_I2CRequest, 0, sizeof(tByteArray));
00124 
00125   MICC_I2CRequest[0] = 2;               // Number of bytes in I2C command
00126   MICC_I2CRequest[1] = MICC_I2C_ADDR;   // I2C address of accel sensor
00127   MICC_I2CRequest[2] = MICC_X_ACCEL;    // Set write address to sensor mode register
00128 
00129   if (!writeI2C(link, MICC_I2CRequest, 6))
00130     return false;
00131 
00132   if (!readI2C(link, MICC_I2CReply, 6))
00133     return false;
00134 
00135   // Each result is made up of two bytes.
00136         x_accel = (MICC_I2CReply[1] << 8) + MICC_I2CReply[0];
00137         y_accel = (MICC_I2CReply[3] << 8) + MICC_I2CReply[2];
00138         z_accel = (MICC_I2CReply[5] << 8) + MICC_I2CReply[4];
00139   return true;
00140 }
00141 
00142 
00143 /**
00144  * Send a command to the sensor
00145  * @param link the sensor port number
00146  * @param command the command to be sent
00147  * @return true if no error occured, false if it did
00148  */
00149 bool MICCsendCmd(tSensors link, ubyte command) {
00150   memset(MICC_I2CRequest, 0, sizeof(tByteArray));
00151 
00152   MICC_I2CRequest[0] = 2;               // Number of bytes in I2C command
00153   MICC_I2CRequest[1] = MICC_I2C_ADDR;   // I2C address of accel sensor
00154   MICC_I2CRequest[2] = command;         // Set write address to sensor mode register
00155 
00156   return writeI2C(link, MICC_I2CRequest, 0);
00157 }
00158 
00159 #endif //__MICC_H__
00160 
00161 /*
00162  * $Id: MICC-driver.h 56 2011-05-30 19:47:12Z xander $
00163  */
00164 /* @} */
00165 /* @} */