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

mindsensors-irdist.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: mindsensors-irdist.h 133 2013-03-10 15:15:38Z xander $
00010  */
00011 
00012 #ifndef __MSDIST_H__
00013 #define __MSDIST_H__
00014 /** \file mindsensors-irdist.h
00015  * \brief Mindsensors DIST-Nx driver
00016  *
00017  * mindsensors-irdist.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 3.59 AND HIGHER. 
00031 
00032  * \author Xander Soldaat (xander_at_botbench.com)
00033  * \date 18 December 2010
00034  * \version 0.4
00035  * \example mindsensors-irdist-test1.c
00036  */
00037 
00038 #pragma systemFile
00039 
00040 #ifndef __COMMON_H__
00041 #include "common.h"
00042 #endif
00043 
00044 #define MSDIST_I2C_ADDR   0x02  /*!< MSDIST I2C device address */
00045 
00046 #define MSDIST_CMD        0x41  /*!< MSDIST command register */
00047 
00048 #define MSDIST_DIST       0x42  /*!< MSDIST distance data */
00049 #define MSDIST_VOLT       0x44  /*!< MSDIST voltage data */
00050 #define MSDIST_MOD_TYPE   0x50  /*!< MSDIST Sharp module type */
00051 #define MSDIST_MINDIST    0x52  /*!< MSDIST minimum distance in mm */
00052 #define MSDIST_MAXDIST    0x54  /*!< MSDIST maximum distance in mm */
00053 
00054 #define MSDIST_GP2D12     0x31  /*!< Sharp IR module GP2D12 */
00055 #define MSDIST_GP2D120    0x32  /*!< Sharp IR module GP2D120 */
00056 #define MSDIST_GP2YA21    0x33  /*!< Sharp IR module GP2YA21 */
00057 #define MSDIST_GP2YA02    0x34  /*!< Sharp IR module GP2YA02 */
00058 #define MSDIST_CUSTOM     0x35  /*!< Custom IR module */
00059 
00060 int MSDISTreadDist(tSensors link, ubyte address = MSDIST_I2C_ADDR);
00061 int MSDISTreadVoltage(tSensors link, ubyte address = MSDIST_I2C_ADDR);
00062 int MSDISTreadMinDist(tSensors link, ubyte address = MSDIST_I2C_ADDR);
00063 int MSDISTreadMaxDist(tSensors link, ubyte address = MSDIST_I2C_ADDR);
00064 int MSDISTreadModuleType(tSensors link, ubyte address = MSDIST_I2C_ADDR);
00065 bool MSDISTsendCmd(tSensors link, byte command, ubyte address = MSDIST_I2C_ADDR);
00066 
00067 tByteArray MSDIST_I2CRequest;       /*!< Array to hold I2C command data */
00068 tByteArray MSDIST_I2CReply;         /*!< Array to hold I2C reply data */
00069 
00070 bool MSDISTcalibrated[] = {false, false, false, false};  /*!< Has the sensor been calibrated yet? */
00071 
00072 /**
00073  * Read the distance from the sensor
00074  * @param link the sensor port number
00075  * @param address the I2C address to use, optional, defaults to 0x02
00076  * @return distance to object or -1 if an error occurred
00077  */
00078 int MSDISTreadDist(tSensors link, ubyte address) {
00079 
00080   // Configure the sensor
00081   if (!MSDISTcalibrated[link]) {
00082     if (!MSDISTsendCmd(link, MSDISTreadModuleType(link),address))
00083     // if (!MSDISTsendCmd(link, MSDISTreadModuleType(link)))
00084       return -1;
00085     else
00086       MSDISTcalibrated[link] = true;
00087   }
00088 
00089   memset(MSDIST_I2CRequest, 0, sizeof(tByteArray));
00090 
00091   MSDIST_I2CRequest[0] = 2;               // Number of bytes in I2C command
00092   MSDIST_I2CRequest[1] = address;         // I2C address of sensor
00093   MSDIST_I2CRequest[2] = MSDIST_DIST;     // Set write address to sensor mode register
00094 
00095   if (!writeI2C(link, MSDIST_I2CRequest, MSDIST_I2CReply, 2))
00096     return -1;
00097 
00098   return (0x00FF & MSDIST_I2CReply[0]) + ((0x00FF & MSDIST_I2CReply[1]) <<8);
00099 }
00100 
00101 
00102 /**
00103  * Read tilt data from the sensor
00104  * @param link the sensor port number
00105  * @param address the I2C address to use, optional, defaults to 0x02
00106  * @return voltage reading from IR Sensor -1 if an error occurred
00107  */
00108 int MSDISTreadVoltage(tSensors link, ubyte address) {
00109   memset(MSDIST_I2CRequest, 0, sizeof(tByteArray));
00110 
00111   MSDIST_I2CRequest[0] = 2;               // Number of bytes in I2C command
00112   MSDIST_I2CRequest[1] = address;         // I2C address of sensor
00113   MSDIST_I2CRequest[2] = MSDIST_VOLT;     // Set write address to sensor mode register
00114 
00115   if (!writeI2C(link, MSDIST_I2CRequest, MSDIST_I2CReply, 2))
00116     return -1;
00117 
00118   // Each result is made up of two bytes.
00119   return (0x00FF & MSDIST_I2CReply[0]) + ((0x00FF & MSDIST_I2CReply[1]) <<8);
00120 }
00121 
00122 
00123 /**
00124  * Read minumum measuring distance from the sensor
00125  * @param link the sensor port number
00126  * @param address the I2C address to use, optional, defaults to 0x02
00127  * @return minumum measuring distance from the sensor -1 if an error occurred
00128  */
00129 int MSDISTreadMinDist(tSensors link, ubyte address) {
00130   memset(MSDIST_I2CRequest, 0, sizeof(tByteArray));
00131 
00132   MSDIST_I2CRequest[0] = 2;               // Number of bytes in I2C command
00133   MSDIST_I2CRequest[1] = address;         // I2C address of sensor
00134   MSDIST_I2CRequest[2] = MSDIST_MINDIST;  // Set write address to sensor mode register
00135 
00136   if (!writeI2C(link, MSDIST_I2CRequest, MSDIST_I2CReply, 2))
00137     return -1;
00138 
00139   // Each result is made up of two bytes.
00140   return (0x00FF & MSDIST_I2CReply[0]) + ((0x00FF & MSDIST_I2CReply[1]) <<8);
00141 }
00142 
00143 
00144 /**
00145  * Read maximum measuring distance from the sensor
00146  * @param link the sensor port number
00147  * @param address the I2C address to use, optional, defaults to 0x02
00148  * @return maximum measuring distance from the sensor -1 if an error occurred
00149  */
00150 int MSDISTreadMaxDist(tSensors link, ubyte address) {
00151   memset(MSDIST_I2CRequest, 0, sizeof(tByteArray));
00152 
00153   MSDIST_I2CRequest[0] = 2;               // Number of bytes in I2C command
00154   MSDIST_I2CRequest[1] = address;         // I2C address of sensor
00155   MSDIST_I2CRequest[2] = MSDIST_MAXDIST;     // Set write address to sensor mode register
00156 
00157   if (!writeI2C(link, MSDIST_I2CRequest, MSDIST_I2CReply, 2))
00158     return -1;
00159 
00160   // Each result is made up of two bytes.
00161   return (0x00FF & MSDIST_I2CReply[0]) + ((0x00FF & MSDIST_I2CReply[1]) <<8);
00162 }
00163 
00164 
00165 /**
00166  * Read Sharp IR module type from the sensor
00167  * @param link the sensor port number
00168  * @param address the I2C address to use, optional, defaults to 0x02
00169  * @return Sharp IR module type from the sensor -1 if an error occurred
00170  */
00171 int MSDISTreadModuleType(tSensors link, ubyte address) {
00172   memset(MSDIST_I2CRequest, 0, sizeof(tByteArray));
00173 
00174   MSDIST_I2CRequest[0] = 2;               // Number of bytes in I2C command
00175   MSDIST_I2CRequest[1] = address;         // I2C address of sensor
00176   MSDIST_I2CRequest[2] = MSDIST_MOD_TYPE; // Set write address to sensor mode register
00177 
00178   if (!writeI2C(link, MSDIST_I2CRequest, MSDIST_I2CReply, 1))
00179     return -1;
00180 
00181   return 0x00FF & MSDIST_I2CReply[0];
00182 }
00183 
00184 
00185 /**
00186  * Send a command to the sensor
00187  * @param link the sensor port number
00188  * @param command the command to be sent
00189  * @param address the I2C address to use, optional, defaults to 0x02
00190  * @return true if no error occured, false if it did
00191  */
00192 bool MSDISTsendCmd(tSensors link, byte command, ubyte address) {
00193   memset(MSDIST_I2CRequest, 0, sizeof(tByteArray));
00194 
00195   MSDIST_I2CRequest[0] = 3;               // Number of bytes in I2C command
00196   MSDIST_I2CRequest[1] = address;         // I2C address of sensor
00197   MSDIST_I2CRequest[2] = MSDIST_CMD;      // Set write address to sensor mode register
00198   MSDIST_I2CRequest[3] = command;         // Command to be sent to the sensor
00199 
00200   return writeI2C(link, MSDIST_I2CRequest);
00201 }
00202 
00203 
00204 #endif //__MSDIST_H__
00205 
00206 /*
00207  * $Id: mindsensors-irdist.h 133 2013-03-10 15:15:38Z xander $
00208  */
00209 /* @} */
00210 /* @} */