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

mindsensors-accelerometer.h

Go to the documentation of this file.
00001 /*!@addtogroup mindsensors
00002  * @{
00003  * @defgroup accelnx ACCEL-nx Sensor
00004  * ACCEL-nx Sensor
00005  * @{
00006  */
00007 
00008 /*
00009  * $Id: mindsensors-accelerometer.h 133 2013-03-10 15:15:38Z xander $
00010  */
00011 
00012 #ifndef __MSAC_H__
00013 #define __MSAC_H__
00014 /** \file mindsensors-accelerometer.h
00015  * \brief Mindsensors ACCEL-nx driver
00016  *
00017  * mindsensors-accelerometer.h provides an API for the Mindsensors ACCEL-nx driver
00018  *
00019  * Changelog:
00020  * - 0.1: Initial release
00021  * - 0.2: Added defines for ranges (MSAC_RANGE_2_5 ... MSAC_RANGE_10)<br>
00022  *        Removed ubyteToInt() calls.
00023  *
00024  * Credits:
00025  * - Big thanks to Mindsensors for providing me with the hardware necessary to write and test this.
00026  *
00027  * License: You may use this code as you wish, provided you give credit where its due.
00028  *
00029  * THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 3.59 AND HIGHER. 
00030 
00031  * \author Xander Soldaat (xander_at_botbench.com)
00032  * \date 28 November 2009
00033  * \version 0.2
00034  * \example mindsensors-accelerometer-test1.c
00035  * \example mindsensors-accelerometer-test2.c
00036  */
00037 
00038 #pragma systemFile
00039 
00040 #ifndef __COMMON_H__
00041 #include "common.h"
00042 #endif
00043 
00044 #define MSAC_I2C_ADDR       0x02  /*!< MSAC I2C device address */
00045 
00046 #define MSAC_CMD            0x41  /*!< MSAC command register */
00047 
00048 #define MSAC_X_TILT         0x42  /*!< MSAC X tilt data */
00049 #define MSAC_Y_TILT         0x43  /*!< MSAC Y tilt data */
00050 #define MSAC_Z_TILT         0x44  /*!< MSAC Z tilt data */
00051 
00052 #define MSAC_X_ACCEL        0x45  /*!< MSAC Z acceleration data */
00053 #define MSAC_Y_ACCEL        0x47  /*!< MSAC Z acceleration data */
00054 #define MSAC_Z_ACCEL        0x49  /*!< MSAC Z acceleration data */
00055 
00056 #define MSAC_RANGE_2_5      1     /*!< Acceleration up to 2.5G */
00057 #define MSAC_RANGE_3_3      2     /*!< Acceleration up to 3.3G */
00058 #define MSAC_RANGE_6_7      3     /*!< Acceleration up to 6.7G */
00059 #define MSAC_RANGE_10       4     /*!< Acceleration up to 10G */
00060 
00061 bool MSACreadTilt(tSensors link, int &x_tilt, int &y_tilt, int &z_tilt);
00062 bool MSACreadAccel(tSensors link, int &x_accel, int &y_accel, int &z_accel);
00063 bool MSACsendCmd(tSensors link, byte command);
00064 bool MSACsetRange(tSensors link, int range);
00065 
00066 tByteArray MSAC_I2CRequest;       /*!< Array to hold I2C command data */
00067 tByteArray MSAC_I2CReply;         /*!< Array to hold I2C reply data */
00068 
00069 
00070 /**
00071  * Read tilt data from the sensor
00072  * @param link the sensor port number
00073  * @param x_tilt X tilt data
00074  * @param y_tilt Y tilt data
00075  * @param z_tilt Z tilt data
00076  * @return true if no error occured, false if it did
00077  */
00078 bool MSACreadTilt(tSensors link, int &x_tilt, int &y_tilt, int &z_tilt) {
00079   memset(MSAC_I2CRequest, 0, sizeof(tByteArray));
00080 
00081   MSAC_I2CRequest[0] = 2;               // Number of bytes in I2C command
00082   MSAC_I2CRequest[1] = MSAC_I2C_ADDR;   // I2C address of accel sensor
00083   MSAC_I2CRequest[2] = MSAC_X_TILT;     // Set write address to sensor mode register
00084 
00085   if (!writeI2C(link, MSAC_I2CRequest, MSAC_I2CReply, 3))
00086     return false;
00087 
00088   x_tilt = MSAC_I2CReply[0] - 128;
00089         y_tilt = MSAC_I2CReply[1] - 128;
00090         z_tilt = MSAC_I2CReply[2] - 128;
00091 
00092   return true;
00093 }
00094 
00095 
00096 /**
00097  * Read tilt data from the sensor
00098  * @param link the sensor port number
00099  * @param x_accel X acceleration data
00100  * @param y_accel Y acceleration data
00101  * @param z_accel Z acceleration data
00102  * @return true if no error occured, false if it did
00103  */
00104 bool MSACreadAccel(tSensors link, int &x_accel, int &y_accel, int &z_accel) {
00105   memset(MSAC_I2CRequest, 0, sizeof(tByteArray));
00106 
00107   MSAC_I2CRequest[0] = 2;               // Number of bytes in I2C command
00108   MSAC_I2CRequest[1] = MSAC_I2C_ADDR;   // I2C address of accel sensor
00109   MSAC_I2CRequest[2] = MSAC_X_ACCEL;    // Set write address to sensor mode register
00110 
00111   if (!writeI2C(link, MSAC_I2CRequest, MSAC_I2CReply, 6))
00112     return false;
00113 
00114   // Each result is made up of two bytes.
00115         x_accel = MSAC_I2CReply[0] + (MSAC_I2CReply[1] << 8);
00116         y_accel = MSAC_I2CReply[2] + (MSAC_I2CReply[3] << 8);
00117         z_accel = MSAC_I2CReply[4] + (MSAC_I2CReply[5] << 8);
00118   return true;
00119 }
00120 
00121 
00122 /**
00123  * Send a command to the sensor
00124  * @param link the sensor port number
00125  * @param command the command to be sent
00126  * @return true if no error occured, false if it did
00127  */
00128 bool MSACsendCmd(tSensors link, byte command) {
00129   memset(MSAC_I2CRequest, 0, sizeof(tByteArray));
00130 
00131   MSAC_I2CRequest[0] = 3;               // Number of bytes in I2C command
00132   MSAC_I2CRequest[1] = MSAC_I2C_ADDR;   // I2C address of accel sensor
00133   MSAC_I2CRequest[2] = MSAC_CMD;        // Set write address to sensor mode register
00134   MSAC_I2CRequest[3] = command;         // Command to be sent to the sensor
00135 
00136   return writeI2C(link, MSAC_I2CRequest);
00137 }
00138 
00139 
00140 /**
00141  * Set sensitivity range of sensor.
00142  * @param link the sensor port number
00143  * @param range 1 = 2.5G, 2 = 3.3G, 3 = 6.7G, 4 = 10G
00144  * @return true if no error occured, false if it did
00145  */
00146 bool MSACsetRange(tSensors link, int range) {
00147   byte command = 0;
00148 
00149   switch (range) {
00150     case 1: command = '1'; break;
00151     case 2: command = '2'; break;
00152     case 3: command = '3'; break;
00153     case 4: command = '4'; break;
00154     default: return false; break;
00155   }
00156   return MSACsendCmd(link, command);
00157 }
00158 
00159 #endif //__MSAC_H__
00160 
00161 /*
00162  * $Id: mindsensors-accelerometer.h 133 2013-03-10 15:15:38Z xander $
00163  */
00164 /* @} */
00165 /* @} */