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

microinfinity-cruizcore.h

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