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

MSDIST-driver.h

Go to the documentation of this file.
00001 /*!@addtogroup mindsensors
00002  * @{
00003  * @defgroup msdist DIST-Nx Sensor
00004  * DIST-Nx Sensor
00005  * @{
00006  */
00007 
00008 /*
00009  * $Id: MSDIST-driver.h 56 2011-05-30 19:47:12Z xander $
00010  */
00011 
00012 #ifndef __MSDIST_H__
00013 #define __MSDIST_H__
00014 /** \file MSDIST-driver.h
00015  * \brief Mindsensors DIST-Nx driver
00016  *
00017  * MSDIST-driver.h provides an API for the Mindsensors DIST-Nx sensor
00018  *
00019  * Changelog:
00020  * - 0.1: Initial release
00021  * - 0.2: More comments
00022  * - 0.3: Sensor now auto-configures the type
00023  * - 0.4: Allow I2C address to be specified as an optional argument
00024  *
00025  * Credits:
00026  * - Big thanks to Mindsensors for providing me with the hardware necessary to write and test this.
00027  *
00028  * License: You may use this code as you wish, provided you give credit where its due.
00029  *
00030  * THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 2.00 AND HIGHER.
00031  * \author Xander Soldaat (mightor_at_gmail.com)
00032  * \date 18 December 2010
00033  * \version 0.4
00034  * \example MSDIST-test1.c
00035  */
00036 
00037 #pragma systemFile
00038 
00039 #ifndef __COMMON_H__
00040 #include "common.h"
00041 #endif
00042 
00043 #define MSDIST_I2C_ADDR   0x02  /*!< MSDIST I2C device address */
00044 
00045 #define MSDIST_CMD        0x41  /*!< MSDIST command register */
00046 
00047 #define MSDIST_DIST       0x42  /*!< MSDIST distance data */
00048 #define MSDIST_VOLT       0x44  /*!< MSDIST voltage data */
00049 #define MSDIST_MOD_TYPE   0x50  /*!< MSDIST Sharp module type */
00050 #define MSDIST_MINDIST    0x52  /*!< MSDIST minimum distance in mm */
00051 #define MSDIST_MAXDIST    0x54  /*!< MSDIST maximum distance in mm */
00052 
00053 #define MSDIST_GP2D12     0x31  /*!< Sharp IR module GP2D12 */
00054 #define MSDIST_GP2D120    0x32  /*!< Sharp IR module GP2D120 */
00055 #define MSDIST_GP2YA21    0x33  /*!< Sharp IR module GP2YA21 */
00056 #define MSDIST_GP2YA02    0x34  /*!< Sharp IR module GP2YA02 */
00057 #define MSDIST_CUSTOM     0x35  /*!< Custom IR module */
00058 
00059 int MSDISTreadDist(tSensors link, ubyte address = MSDIST_I2C_ADDR);
00060 int MSDISTreadVoltage(tSensors link, ubyte address = MSDIST_I2C_ADDR);
00061 int MSDISTreadMinDist(tSensors link, ubyte address = MSDIST_I2C_ADDR);
00062 int MSDISTreadMaxDist(tSensors link, ubyte address = MSDIST_I2C_ADDR);
00063 int MSDISTreadModuleType(tSensors link, ubyte address = MSDIST_I2C_ADDR);
00064 bool MSDISTsendCmd(tSensors link, byte command, ubyte address = MSDIST_I2C_ADDR);
00065 
00066 tByteArray MSDIST_I2CRequest;       /*!< Array to hold I2C command data */
00067 tByteArray MSDIST_I2CReply;         /*!< Array to hold I2C reply data */
00068 
00069 bool MSDISTcalibrated[] = {false, false, false, false};  /*!< Has the sensor been calibrated yet? */
00070 
00071 /**
00072  * Read the distance from the sensor
00073  * @param link the sensor port number
00074  * @param address the I2C address to use, optional, defaults to 0x02
00075  * @return distance to object or -1 if an error occurred
00076  */
00077 int MSDISTreadDist(tSensors link, ubyte address) {
00078 
00079   // Configure the sensor
00080   if (!MSDISTcalibrated[link]) {
00081     if (!MSDISTsendCmd(link, MSDISTreadModuleType(link),address))
00082     // if (!MSDISTsendCmd(link, MSDISTreadModuleType(link)))
00083       return -1;
00084     else
00085       MSDISTcalibrated[link] = true;
00086   }
00087 
00088   memset(MSDIST_I2CRequest, 0, sizeof(tByteArray));
00089 
00090   MSDIST_I2CRequest[0] = 2;               // Number of bytes in I2C command
00091   MSDIST_I2CRequest[1] = address;         // I2C address of sensor
00092   MSDIST_I2CRequest[2] = MSDIST_DIST;     // Set write address to sensor mode register
00093 
00094   if (!writeI2C(link, MSDIST_I2CRequest, 2))
00095     return -1;
00096 
00097   if (!readI2C(link, MSDIST_I2CReply, 2))
00098     return -1;
00099 
00100   return (0x00FF & MSDIST_I2CReply[0]) + ((0x00FF & MSDIST_I2CReply[1]) <<8);
00101 }
00102 
00103 
00104 /**
00105  * Read tilt data from the sensor
00106  * @param link the sensor port number
00107  * @param address the I2C address to use, optional, defaults to 0x02
00108  * @return voltage reading from IR Sensor -1 if an error occurred
00109  */
00110 int MSDISTreadVoltage(tSensors link, ubyte address) {
00111   memset(MSDIST_I2CRequest, 0, sizeof(tByteArray));
00112 
00113   MSDIST_I2CRequest[0] = 2;               // Number of bytes in I2C command
00114   MSDIST_I2CRequest[1] = address;         // I2C address of sensor
00115   MSDIST_I2CRequest[2] = MSDIST_VOLT;     // Set write address to sensor mode register
00116 
00117   if (!writeI2C(link, MSDIST_I2CRequest, 2))
00118     return -1;
00119 
00120   if (!readI2C(link, MSDIST_I2CReply, 2))
00121     return -1;
00122 
00123   // Each result is made up of two bytes.
00124   return (0x00FF & MSDIST_I2CReply[0]) + ((0x00FF & MSDIST_I2CReply[1]) <<8);
00125 }
00126 
00127 
00128 /**
00129  * Read minumum measuring distance from the sensor
00130  * @param link the sensor port number
00131  * @param address the I2C address to use, optional, defaults to 0x02
00132  * @return minumum measuring distance from the sensor -1 if an error occurred
00133  */
00134 int MSDISTreadMinDist(tSensors link, ubyte address) {
00135   memset(MSDIST_I2CRequest, 0, sizeof(tByteArray));
00136 
00137   MSDIST_I2CRequest[0] = 2;               // Number of bytes in I2C command
00138   MSDIST_I2CRequest[1] = address;         // I2C address of sensor
00139   MSDIST_I2CRequest[2] = MSDIST_MINDIST;  // Set write address to sensor mode register
00140 
00141   if (!writeI2C(link, MSDIST_I2CRequest, 2))
00142     return -1;
00143 
00144   if (!readI2C(link, MSDIST_I2CReply, 2))
00145     return -1;
00146 
00147   // Each result is made up of two bytes.
00148   return (0x00FF & MSDIST_I2CReply[0]) + ((0x00FF & MSDIST_I2CReply[1]) <<8);
00149 }
00150 
00151 
00152 /**
00153  * Read maximum measuring distance from the sensor
00154  * @param link the sensor port number
00155  * @param address the I2C address to use, optional, defaults to 0x02
00156  * @return maximum measuring distance from the sensor -1 if an error occurred
00157  */
00158 int MSDISTreadMaxDist(tSensors link, ubyte address) {
00159   memset(MSDIST_I2CRequest, 0, sizeof(tByteArray));
00160 
00161   MSDIST_I2CRequest[0] = 2;               // Number of bytes in I2C command
00162   MSDIST_I2CRequest[1] = address;         // I2C address of sensor
00163   MSDIST_I2CRequest[2] = MSDIST_MAXDIST;     // Set write address to sensor mode register
00164 
00165   if (!writeI2C(link, MSDIST_I2CRequest, 2))
00166     return -1;
00167 
00168   if (!readI2C(link, MSDIST_I2CReply, 2))
00169     return -1;
00170 
00171   // Each result is made up of two bytes.
00172   return (0x00FF & MSDIST_I2CReply[0]) + ((0x00FF & MSDIST_I2CReply[1]) <<8);
00173 }
00174 
00175 
00176 /**
00177  * Read Sharp IR module type from the sensor
00178  * @param link the sensor port number
00179  * @param address the I2C address to use, optional, defaults to 0x02
00180  * @return Sharp IR module type from the sensor -1 if an error occurred
00181  */
00182 int MSDISTreadModuleType(tSensors link, ubyte address) {
00183   memset(MSDIST_I2CRequest, 0, sizeof(tByteArray));
00184 
00185   MSDIST_I2CRequest[0] = 2;               // Number of bytes in I2C command
00186   MSDIST_I2CRequest[1] = address;         // I2C address of sensor
00187   MSDIST_I2CRequest[2] = MSDIST_MOD_TYPE; // Set write address to sensor mode register
00188 
00189   if (!writeI2C(link, MSDIST_I2CRequest, 1))
00190     return -1;
00191 
00192   if (!readI2C(link, MSDIST_I2CReply, 1))
00193     return -1;
00194 
00195   return 0x00FF & MSDIST_I2CReply[0];
00196 }
00197 
00198 
00199 /**
00200  * Send a command to the sensor
00201  * @param link the sensor port number
00202  * @param command the command to be sent
00203  * @param address the I2C address to use, optional, defaults to 0x02
00204  * @return true if no error occured, false if it did
00205  */
00206 bool MSDISTsendCmd(tSensors link, byte command, ubyte address) {
00207   memset(MSDIST_I2CRequest, 0, sizeof(tByteArray));
00208 
00209   MSDIST_I2CRequest[0] = 3;               // Number of bytes in I2C command
00210   MSDIST_I2CRequest[1] = address;         // I2C address of sensor
00211   MSDIST_I2CRequest[2] = MSDIST_CMD;      // Set write address to sensor mode register
00212   MSDIST_I2CRequest[3] = command;         // Command to be sent to the sensor
00213 
00214   return writeI2C(link, MSDIST_I2CRequest, 0);
00215 }
00216 
00217 
00218 #endif //__MSDIST_H__
00219 
00220 /*
00221  * $Id: MSDIST-driver.h 56 2011-05-30 19:47:12Z xander $
00222  */
00223 /* @} */
00224 /* @} */