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

hitechnic-accelerometer.h

Go to the documentation of this file.
00001 /*!@addtogroup HiTechnic
00002  * @{
00003  * @defgroup htacc Acceleration Sensor
00004  * HiTechnic Acceleration Sensor
00005  * @{
00006  */
00007 
00008 /*
00009  * $Id: hitechnic-accelerometer.h 133 2013-03-10 15:15:38Z xander $
00010  */
00011 
00012 #ifndef __HTAC_H__
00013 #define __HTAC_H__
00014 /** \file hitechnic-accelerometer.h
00015  * \brief HiTechnic Acceleration Sensor driver
00016  *
00017  * hitechnic-accelerometer.h provides an API for the HiTechnic Acceleration Sensor.
00018  *
00019  * Changelog:
00020  * - 0.1: Initial release
00021  * - 0.2: Fixed bad registers<br>
00022  *        Added HTACreadAllAxes(tSensors link, int &x, int &y, int &z)<br>
00023  *        Removed HTACreadAllAxes(tSensors link, tIntArray &data)<br>
00024  *        Changed HTACreadX, Y, Z to use by reference instead of as return value<br>
00025  * - 0.3: SMUX functions added.
00026  * - 0.4: Removed HTAC_SMUXData, reused HTAC_I2CReply to save memory
00027  * - 0.5: Use new calls in common.h that don't require SPORT/MPORT macros<br>
00028  *        Fixed massive bug in HTACreadAllAxes() in the way values are calculated
00029  * - 0.6: Removed single axis functions
00030  * - 0.7: Replaced array structs with typedefs
00031  *
00032  * Credits:
00033  * - Big thanks to HiTechnic for providing me with the hardware necessary to write and test this.
00034  *
00035  * License: You may use this code as you wish, provided you give credit where its due.
00036  *
00037  * THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 3.59 AND HIGHER. 
00038 
00039  * \author Xander Soldaat (xander_at_botbench.com)
00040  * \date 20 February 2011
00041  * \version 0.7
00042  * \example hitechnic-accelerometer-test1.c
00043  * \example hitechnic-accelerometer-SMUX-test1.c
00044  */
00045 
00046 #pragma systemFile
00047 
00048 #ifndef __COMMON_H__
00049 #include "common.h"
00050 #endif
00051 
00052 #define HTAC_I2C_ADDR  0x02      /*!< IR Seeker I2C device address */
00053 #define HTAC_OFFSET    0x42      /*!< Offset for data registers */
00054 #define HTAC_X_UP      0x00      /*!< X axis upper 8 bits */
00055 #define HTAC_Y_UP      0x01      /*!< Y axis upper 8 bits */
00056 #define HTAC_Z_UP      0x02      /*!< Z axis upper 8 bits */
00057 #define HTAC_X_LOW     0x03      /*!< X axis lower 2 bits */
00058 #define HTAC_Y_LOW     0x04      /*!< Y axis lower 2 bits */
00059 #define HTAC_Z_LOW     0x05      /*!< Z axis lower 2 bits */
00060 
00061 bool HTACreadAllAxes(tSensors link, int &x, int &y, int &z);
00062 
00063 #ifdef __HTSMUX_SUPPORT__
00064 bool HTACreadAllAxes(tMUXSensor muxsensor, int &x, int &y, int &z);
00065 
00066 tConfigParams HTAC_config = {HTSMUX_CHAN_I2C, 6, 0x02, 0x42}; /*!< Array to hold SMUX config data for sensor */
00067 #endif
00068 
00069 tByteArray HTAC_I2CRequest;    /*!< Array to hold I2C command data */
00070 tByteArray HTAC_I2CReply;      /*!< Array to hold I2C reply data */
00071 
00072 /**
00073  * Read the value of all the axes registers return by reference
00074  * @param link the HTAC port number
00075  * @param x x axis
00076  * @param y y axis
00077  * @param z z axis
00078  * @return true if no error occured, false if it did
00079  */
00080 bool HTACreadAllAxes(tSensors link, int &x, int &y, int &z) {
00081   memset(HTAC_I2CRequest, 0, sizeof(tByteArray));
00082 
00083   HTAC_I2CRequest[0] = 2;                       // Message size
00084   HTAC_I2CRequest[1] = HTAC_I2C_ADDR;           // I2C Address
00085   HTAC_I2CRequest[2] = HTAC_OFFSET + HTAC_X_UP; // X axis upper 8 bits register
00086 
00087   if (!writeI2C(link, HTAC_I2CRequest, HTAC_I2CReply, 6))
00088     return false;
00089 
00090   // Convert 2 bytes into a signed 10 bit value.  If the 8 high bits are more than 127, make
00091   // it a signed value before combing it with the lower 2 bits.
00092   // Gotta love conditional assignments!
00093   x = (HTAC_I2CReply[0] > 127) ? (HTAC_I2CReply[0] - 256) * 4 + HTAC_I2CReply[3]
00094                                    : HTAC_I2CReply[0] * 4 + HTAC_I2CReply[3];
00095 
00096   y = (HTAC_I2CReply[1] > 127) ? (HTAC_I2CReply[1] - 256) * 4 + HTAC_I2CReply[4]
00097                                    : HTAC_I2CReply[1] * 4 + HTAC_I2CReply[4];
00098 
00099   z = (HTAC_I2CReply[2] > 127) ? (HTAC_I2CReply[2] - 256) * 4 + HTAC_I2CReply[5]
00100                                    : HTAC_I2CReply[2] * 4 + HTAC_I2CReply[5];
00101 
00102   return true;
00103 }
00104 
00105 
00106 /**
00107  * Read the value of all the axes registers return by reference
00108  * @param muxsensor the SMUX sensor port number
00109  * @param x x axis
00110  * @param y y axis
00111  * @param z z axis
00112  * @return true if no error occured, false if it did
00113  */
00114 #ifdef __HTSMUX_SUPPORT__
00115 bool HTACreadAllAxes(tMUXSensor muxsensor, int &x, int &y, int &z) {
00116   memset(HTAC_I2CReply, 0, sizeof(tByteArray));
00117 
00118   if (HTSMUXSensorTypes[muxsensor] != HTSMUXSensorCustom)
00119     HTSMUXconfigChannel(muxsensor, HTAC_config);
00120 
00121   if (!HTSMUXreadPort(muxsensor, HTAC_I2CReply, 6, HTAC_X_UP)) {
00122     return false;
00123   }
00124 
00125   // Convert 2 bytes into a signed 10 bit value.  If the 8 high bits are more than 127, make
00126   // it a signed value before combing it with the lower 2 bits.
00127   // Gotta love conditional assignments!
00128   x = (HTAC_I2CReply[0] > 127) ? (HTAC_I2CReply[0] - 256) * 4 + HTAC_I2CReply[3]
00129                                    : HTAC_I2CReply[0] * 4 + HTAC_I2CReply[3];
00130 
00131   y = (HTAC_I2CReply[1] > 127) ? (HTAC_I2CReply[1] - 256) * 4 + HTAC_I2CReply[4]
00132                                    : HTAC_I2CReply[1] * 4 + HTAC_I2CReply[4];
00133 
00134   z = (HTAC_I2CReply[2] > 127) ? (HTAC_I2CReply[2] - 256) * 4 + HTAC_I2CReply[5]
00135                                    : HTAC_I2CReply[2] * 4 + HTAC_I2CReply[5];
00136 
00137   return true;
00138 }
00139 #endif // __HTSMUX_SUPPORT__
00140 
00141 #endif // __HTAC_H__
00142 
00143 /*
00144  * $Id: hitechnic-accelerometer.h 133 2013-03-10 15:15:38Z xander $
00145  */
00146 /* @} */
00147 /* @} */