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

MSAC-driver.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: MSAC-driver.h 46 2011-01-16 16:56:49Z xander $
00010  */
00011 
00012 #ifndef __MSAC_H__
00013 #define __MSAC_H__
00014 /** \file MSAC-driver.h
00015  * \brief Mindsensors ACCEL-nx driver
00016  *
00017  * MSAC-driver.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 2.00 AND HIGHER.
00030  * \author Xander Soldaat (mightor_at_gmail.com)
00031  * \date 28 November 2009
00032  * \version 0.2
00033  * \example MSAC-test1.c
00034  */
00035 
00036 #pragma systemFile
00037 
00038 #ifndef __COMMON_H__
00039 #include "common.h"
00040 #endif
00041 
00042 #define MSAC_I2C_ADDR       0x02  /*!< MSAC I2C device address */
00043 
00044 #define MSAC_CMD            0x41  /*!< MSAC command register */
00045 
00046 #define MSAC_X_TILT         0x42  /*!< MSAC X tilt data */
00047 #define MSAC_Y_TILT         0x43  /*!< MSAC Y tilt data */
00048 #define MSAC_Z_TILT         0x44  /*!< MSAC Z tilt data */
00049 
00050 #define MSAC_X_ACCEL        0x45  /*!< MSAC Z acceleration data */
00051 #define MSAC_Y_ACCEL        0x47  /*!< MSAC Z acceleration data */
00052 #define MSAC_Z_ACCEL        0x49  /*!< MSAC Z acceleration data */
00053 
00054 #define MSAC_RANGE_2_5      1     /*!< Acceleration up to 2.5G */
00055 #define MSAC_RANGE_3_3      2     /*!< Acceleration up to 3.3G */
00056 #define MSAC_RANGE_6_7      3     /*!< Acceleration up to 6.7G */
00057 #define MSAC_RANGE_10       4     /*!< Acceleration up to 10G */
00058 
00059 bool MSACreadTilt(tSensors link, int &x_tilt, int &y_tilt, int &z_tilt);
00060 bool MSACreadAccel(tSensors link, int &x_accel, int &y_accel, int &z_accel);
00061 bool MSACsendCmd(tSensors link, byte command);
00062 bool MSACsetRange(tSensors link, int range);
00063 
00064 tByteArray MSAC_I2CRequest;       /*!< Array to hold I2C command data */
00065 tByteArray MSAC_I2CReply;         /*!< Array to hold I2C reply data */
00066 
00067 
00068 /**
00069  * Read tilt data from the sensor
00070  * @param link the sensor port number
00071  * @param x_tilt X tilt data
00072  * @param y_tilt Y tilt data
00073  * @param z_tilt Z tilt data
00074  * @return true if no error occured, false if it did
00075  */
00076 bool MSACreadTilt(tSensors link, int &x_tilt, int &y_tilt, int &z_tilt) {
00077   memset(MSAC_I2CRequest, 0, sizeof(tByteArray));
00078 
00079   MSAC_I2CRequest[0] = 2;               // Number of bytes in I2C command
00080   MSAC_I2CRequest[1] = MSAC_I2C_ADDR;   // I2C address of accel sensor
00081   MSAC_I2CRequest[2] = MSAC_X_TILT;     // Set write address to sensor mode register
00082 
00083   if (!writeI2C(link, MSAC_I2CRequest, 3))
00084     return false;
00085 
00086   if (!readI2C(link, MSAC_I2CReply, 3))
00087     return false;
00088 
00089   x_tilt = MSAC_I2CReply[0] - 128;
00090         y_tilt = MSAC_I2CReply[1] - 128;
00091         z_tilt = MSAC_I2CReply[2] - 128;
00092 
00093   return true;
00094 }
00095 
00096 
00097 /**
00098  * Read tilt data from the sensor
00099  * @param link the sensor port number
00100  * @param x_accel X acceleration data
00101  * @param y_accel Y acceleration data
00102  * @param z_accel Z acceleration data
00103  * @return true if no error occured, false if it did
00104  */
00105 bool MSACreadAccel(tSensors link, int &x_accel, int &y_accel, int &z_accel) {
00106   memset(MSAC_I2CRequest, 0, sizeof(tByteArray));
00107 
00108   MSAC_I2CRequest[0] = 2;               // Number of bytes in I2C command
00109   MSAC_I2CRequest[1] = MSAC_I2C_ADDR;   // I2C address of accel sensor
00110   MSAC_I2CRequest[2] = MSAC_X_ACCEL;    // Set write address to sensor mode register
00111 
00112   if (!writeI2C(link, MSAC_I2CRequest, 6))
00113     return false;
00114 
00115   if (!readI2C(link, MSAC_I2CReply, 6))
00116     return false;
00117 
00118   // Each result is made up of two bytes.
00119         x_accel = MSAC_I2CReply[0] + (MSAC_I2CReply[1] << 8);
00120         y_accel = MSAC_I2CReply[2] + (MSAC_I2CReply[3] << 8);
00121         z_accel = MSAC_I2CReply[4] + (MSAC_I2CReply[5] << 8);
00122   return true;
00123 }
00124 
00125 
00126 /**
00127  * Send a command to the sensor
00128  * @param link the sensor port number
00129  * @param command the command to be sent
00130  * @return true if no error occured, false if it did
00131  */
00132 bool MSACsendCmd(tSensors link, byte command) {
00133   memset(MSAC_I2CRequest, 0, sizeof(tByteArray));
00134 
00135   MSAC_I2CRequest[0] = 3;               // Number of bytes in I2C command
00136   MSAC_I2CRequest[1] = MSAC_I2C_ADDR;   // I2C address of accel sensor
00137   MSAC_I2CRequest[2] = MSAC_CMD;        // Set write address to sensor mode register
00138   MSAC_I2CRequest[3] = command;         // Command to be sent to the sensor
00139 
00140   return writeI2C(link, MSAC_I2CRequest, 0);
00141 }
00142 
00143 
00144 /**
00145  * Set sensitivity range of sensor.
00146  * @param link the sensor port number
00147  * @param range 1 = 2.5G, 2 = 3.3G, 3 = 6.7G, 4 = 10G
00148  * @return true if no error occured, false if it did
00149  */
00150 bool MSACsetRange(tSensors link, int range) {
00151   byte command = 0;
00152 
00153   switch (range) {
00154     case 1: command = '1'; break;
00155     case 2: command = '2'; break;
00156     case 3: command = '3'; break;
00157     case 4: command = '4'; break;
00158     default: return false; break;
00159   }
00160   return MSACsendCmd(link, command);
00161 }
00162 
00163 #endif //__MSAC_H__
00164 
00165 /*
00166  * $Id: MSAC-driver.h 46 2011-01-16 16:56:49Z xander $
00167  */
00168 /* @} */
00169 /* @} */