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

HTIRS2-driver.h

Go to the documentation of this file.
00001 /*!@addtogroup HiTechnic
00002  * @{
00003  * @defgroup htirs2 IR Seeker V2
00004  * HiTechnic IR Seeker V2
00005  * @{
00006  */
00007 
00008 /*
00009  * $Id: HTIRS2-driver.h 48 2011-02-13 20:35:38Z xander $
00010  */
00011 
00012 #ifndef __HTIRS2_H__
00013 #define __HTIRS2_H__
00014 /** \file HTIRS2-driver.h
00015  * \brief HiTechnic IR Seeker V2 driver
00016  *
00017  * HTIRS2-driver.h provides an API for the HiTechnic IR Seeker V2.
00018  *
00019  * Changelog:
00020  * - 0.1: Initial release
00021  * - 0.2: Added SMUX functions
00022  * - 0.3: All functions using tIntArray are now pass by reference.<br>
00023  *        HTIRS2_SMUXData removed
00024  * - 0.4: Removed all calls to ubyteToInt()<br>
00025  *        Replaced all functions that used SPORT/MPORT macros
00026  * - 0.5: Driver renamed to HTIRS2
00027  *
00028  * Credits:
00029  * - Big thanks to HiTechnic for providing me with the hardware necessary to write and test this.
00030  *
00031  * License: You may use this code as you wish, provided you give credit where its due.
00032  *
00033  * THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 2.00 AND HIGHER.
00034  * \author Xander Soldaat (mightor_at_gmail.com)
00035  * \date 06 April 2010
00036  * \version 0.5
00037  * \example HTIRS2-test1.c
00038  * \example HTIRS2-SMUX-test1.c
00039  */
00040 
00041 #pragma systemFile
00042 
00043 #ifndef __COMMON_H__
00044 #include "common.h"
00045 #endif
00046 
00047 #define HTIRS2_I2C_ADDR    0x10      /*!< IR Seeker I2C device address */
00048 #define HTIRS2_DSP_MODE    0x41      /*!< AC DSP mode - 0 = 1200Hz, 1 = 600Hz */
00049 #define HTIRS2_OFFSET      0x42      /*!< Offset for data registers */
00050 #define HTIRS2_DC_DIR      0x00      /*!< DC Direction data */
00051 #define HTIRS2_DC_SSTR1    0x01      /*!< DC Sensor 0 signal strength above average */
00052 #define HTIRS2_DC_SSTR2    0x02      /*!< DC Sensor 1 signal strength above average */
00053 #define HTIRS2_DC_SSTR3    0x03      /*!< DC Sensor 2 signal strength above average */
00054 #define HTIRS2_DC_SSTR4    0x04      /*!< DC Sensor 3 signal strength above average */
00055 #define HTIRS2_DC_SSTR5    0x05      /*!< DC Sensor 4 signal strength above average */
00056 #define HTIRS2_DC_SAVG     0x06      /*!< DC sensor signal strength average */
00057 #define HTIRS2_AC_DIR      0x07      /*!< DC Direction data */
00058 #define HTIRS2_AC_SSTR1    0x08      /*!< DC Sensor 0 signal strength above average */
00059 #define HTIRS2_AC_SSTR2    0x09      /*!< DC Sensor 1 signal strength above average */
00060 #define HTIRS2_AC_SSTR3    0x0A      /*!< DC Sensor 2 signal strength above average */
00061 #define HTIRS2_AC_SSTR4    0x0B      /*!< DC Sensor 3 signal strength above average */
00062 #define HTIRS2_AC_SSTR5    0x0C      /*!< DC Sensor 4 signal strength above average */
00063 
00064 
00065 /*!< AC DSP modes */
00066 typedef enum {
00067   DSP_1200 = 0,
00068   DSP_600 = 1
00069 } tHTIRS2DSPMode;
00070 
00071 // ---------------------------- DC Signal processing -----------------------------
00072 int HTIRS2readDCDir(tSensors link);
00073 bool HTIRS2readAllDCStrength(tSensors link, int &dcS1, int &dcS2, int &dcS3, int &dcS4, int &dcS5);
00074 int HTIRS2readDCAverage(tSensors link);
00075 // ---------------------------- AC Signal processing -----------------------------
00076 bool HTIRS2setDSPMode(tSensors link, tHTIRS2DSPMode mode);
00077 int HTIRS2readACDir(tSensors link);
00078 bool HTIRS2readAllACStrength(tSensors link, int &acS1, int &acS2, int &acS3, int &acS4, int &acS5);
00079 
00080 #ifdef __HTSMUX_SUPPORT__
00081 // ---------------------------- DC Signal processing -----------------------------
00082 int HTIRS2readDCDir(tMUXSensor muxsensor);
00083 bool HTIRS2readAllDCStrength(tMUXSensor muxsensor, int &dcS1, int &dcS2, int &dcS3, int &dcS4, int &dcS5);
00084 int HTIRS2readDCAverage(tMUXSensor muxsensor);
00085 // ---------------------------- AC Signal processing -----------------------------
00086 int HTIRS2readACDir(tMUXSensor muxsensor);
00087 bool HTIRS2readAllACStrength(tMUXSensor muxsensor, int &acS1, int &acS2, int &acS3, int &acS4, int &acS5);
00088 
00089 tConfigParams HTIRS2_config = {HTSMUX_CHAN_I2C, 13, 0x10, 0x42}; /*!< Array to hold SMUX config data for sensor */
00090 #endif // __HTSMUX_SUPPORT__
00091 
00092 tByteArray HTIRS2_I2CRequest;    /*!< Array to hold I2C command data */
00093 tByteArray HTIRS2_I2CReply;      /*!< Array to hold I2C reply data */
00094 
00095 // ---------------------------- DC Signal processing -----------------------------
00096 
00097 /**
00098  * Read the value of the DC Direction data register and return it.
00099  * @param link the HTIRS2 port number
00100  * @return value of 0-9, the direction index of the detected IR signal or -1 if an error occurred.
00101  */
00102 int HTIRS2readDCDir(tSensors link) {
00103   memset(HTIRS2_I2CRequest, 0, sizeof(tByteArray));
00104 
00105   HTIRS2_I2CRequest[0] = 2;              // Message size
00106   HTIRS2_I2CRequest[1] = HTIRS2_I2C_ADDR; // I2C Address
00107   HTIRS2_I2CRequest[2] = HTIRS2_OFFSET + HTIRS2_DC_DIR;  // Start direction register
00108 
00109   if (!writeI2C(link, HTIRS2_I2CRequest, 1))
00110     return -1;
00111 
00112   if (!readI2C(link, HTIRS2_I2CReply, 1))
00113     return -1;
00114 
00115   return HTIRS2_I2CReply[0];
00116 }
00117 
00118 
00119 /**
00120  * Read the value of the DC Direction data register and return it.
00121  * @param muxsensor the SMUX sensor port number
00122  * @return value of 0-9, the direction index of the detected IR signal or -1 if an error occurred.
00123  */
00124 #ifdef __HTSMUX_SUPPORT__
00125 int HTIRS2readDCDir(tMUXSensor muxsensor) {
00126         memset(HTIRS2_I2CReply, 0, sizeof(tByteArray));
00127 
00128   if (HTSMUXSensorTypes[muxsensor] != HTSMUXSensorCustom)
00129     HTSMUXconfigChannel(muxsensor, HTIRS2_config);
00130 
00131   if (!HTSMUXreadPort(muxsensor, HTIRS2_I2CReply, 1, HTIRS2_DC_DIR)) {
00132     return -1;
00133   }
00134 
00135   return HTIRS2_I2CReply[0];
00136 }
00137 #endif // __HTSMUX_SUPPORT__
00138 
00139 
00140 /**
00141  * Read the value of the all of the internal DC sensors above average.
00142  * @param link the HTIRS2 port number
00143  * @param dcS1 data from internal sensor nr 1
00144  * @param dcS2 data from internal sensor nr 2
00145  * @param dcS3 data from internal sensor nr 3
00146  * @param dcS4 data from internal sensor nr 4
00147  * @param dcS5 data from internal sensor nr 5
00148  * @return true if no error occured, false if it did
00149  */
00150 bool HTIRS2readAllDCStrength(tSensors link, int &dcS1, int &dcS2, int &dcS3, int &dcS4, int &dcS5) {
00151   memset(HTIRS2_I2CRequest, 0, sizeof(tByteArray));
00152 
00153   HTIRS2_I2CRequest[0] = 2;                      // Message size
00154   HTIRS2_I2CRequest[1] = HTIRS2_I2C_ADDR;         // I2C Address
00155   HTIRS2_I2CRequest[2] = HTIRS2_OFFSET + HTIRS2_DC_SSTR1;         // Sensor 0 signal strength
00156 
00157   if (!writeI2C(link, HTIRS2_I2CRequest, 5))
00158     return false;
00159 
00160   if (!readI2C(link, HTIRS2_I2CReply, 5))
00161     return false;
00162 
00163   dcS1 = HTIRS2_I2CReply[0];
00164   dcS2 = HTIRS2_I2CReply[1];
00165   dcS3 = HTIRS2_I2CReply[2];
00166   dcS4 = HTIRS2_I2CReply[3];
00167   dcS5 = HTIRS2_I2CReply[4];
00168 
00169   return true;
00170 }
00171 
00172 
00173 /**
00174  * Read the value of the all of the internal DC sensors above average.
00175  * @param muxsensor the SMUX sensor port number
00176  * @param dcS1 data from internal sensor nr 1
00177  * @param dcS2 data from internal sensor nr 2
00178  * @param dcS3 data from internal sensor nr 3
00179  * @param dcS4 data from internal sensor nr 4
00180  * @param dcS5 data from internal sensor nr 5
00181  * @return true if no error occured, false if it did
00182  */
00183 #ifdef __HTSMUX_SUPPORT__
00184 bool HTIRS2readAllDCStrength(tMUXSensor muxsensor, int &dcS1, int &dcS2, int &dcS3, int &dcS4, int &dcS5) {
00185   memset(HTIRS2_I2CReply, 0, sizeof(tByteArray));
00186 
00187   if (HTSMUXSensorTypes[muxsensor] != HTSMUXSensorCustom)
00188     HTSMUXconfigChannel(muxsensor, HTIRS2_config);
00189 
00190   if (!HTSMUXreadPort(muxsensor, HTIRS2_I2CReply, 5, HTIRS2_DC_SSTR1)) {
00191     return false;
00192   }
00193 
00194   dcS1 = HTIRS2_I2CReply[0];
00195   dcS2 = HTIRS2_I2CReply[1];
00196   dcS3 = HTIRS2_I2CReply[2];
00197   dcS4 = HTIRS2_I2CReply[3];
00198   dcS5 = HTIRS2_I2CReply[4];
00199 
00200   return true;
00201 }
00202 #endif // __HTSMUX_SUPPORT__
00203 
00204 
00205 /**
00206  * Read the value of the average data register and return it.
00207  * @param link the HTIRS2 port number
00208  * @return value of 0-9, the direction index of the detected IR signal or -1 if an error occurred.
00209  */
00210 int HTIRS2readDCAverage(tSensors link) {
00211   memset(HTIRS2_I2CRequest, 0, sizeof(tByteArray));
00212 
00213   HTIRS2_I2CRequest[0] = 2;              // Message size
00214   HTIRS2_I2CRequest[1] = HTIRS2_I2C_ADDR; // I2C Address
00215   HTIRS2_I2CRequest[2] = HTIRS2_OFFSET + HTIRS2_DC_SAVG;  // DC sensor signal strength average
00216 
00217   if (!writeI2C(link, HTIRS2_I2CRequest, 1))
00218     return -1;
00219 
00220   if (!readI2C(link, HTIRS2_I2CReply, 1))
00221     return -1;
00222 
00223   return HTIRS2_I2CReply[0];
00224 }
00225 
00226 
00227 /**
00228  * Read the value of the average data register and return it.
00229  * @param muxsensor the SMUX sensor port number
00230  * @return value of 0-9, the direction index of the detected IR signal or -1 if an error occurred.
00231  */
00232 #ifdef __HTSMUX_SUPPORT__
00233 int HTIRS2readDCAverage(tMUXSensor muxsensor) {
00234   memset(HTIRS2_I2CReply, 0, sizeof(tByteArray));
00235 
00236   if (HTSMUXSensorTypes[muxsensor] != HTSMUXSensorCustom)
00237     HTSMUXconfigChannel(muxsensor, HTIRS2_config);
00238 
00239   if (!HTSMUXreadPort(muxsensor, HTIRS2_I2CReply, 1, HTIRS2_DC_SAVG)) {
00240     return -1;
00241   }
00242 
00243   return HTIRS2_I2CReply[0];
00244 }
00245 #endif // __HTSMUX_SUPPORT__
00246 
00247 
00248 // ---------------------------- AC Signal processing -----------------------------
00249 
00250 /**
00251  * Set the DSP mode of the AC carrier wave detector.
00252  *
00253  * Mode is one of:
00254  * -DSP_1200
00255  * -DSP_600
00256  * @param link the HTIRS2 port number
00257  * @param mode the frequency that should be detected
00258  * @return true if no error occured, false if it did
00259  */
00260 bool HTIRS2setDSPMode(tSensors link, tHTIRS2DSPMode mode) {
00261   memset(HTIRS2_I2CRequest, 0, sizeof(tByteArray));
00262 
00263   HTIRS2_I2CRequest[0] = 3;              // Message size
00264   HTIRS2_I2CRequest[1] = HTIRS2_I2C_ADDR; // I2C Address
00265   HTIRS2_I2CRequest[2] = HTIRS2_DSP_MODE; // Start direction register
00266   HTIRS2_I2CRequest[3] = (ubyte)mode;
00267 
00268   return writeI2C(link, HTIRS2_I2CRequest, 0);
00269 }
00270 
00271 /**
00272  * Read the value of the AC Direction data register and return it.
00273  * @param link the HTIRS2 port number
00274  * @return value of 0-9, the direction index of the detected IR signal or -1 if an error occurred.
00275  */
00276 int HTIRS2readACDir(tSensors link) {
00277   memset(HTIRS2_I2CRequest, 0, sizeof(tByteArray));
00278 
00279   HTIRS2_I2CRequest[0] = 2;              // Message size
00280   HTIRS2_I2CRequest[1] = HTIRS2_I2C_ADDR; // I2C Address
00281   HTIRS2_I2CRequest[2] = HTIRS2_OFFSET + HTIRS2_AC_DIR;      // Start direction register
00282 
00283   if (!writeI2C(link, HTIRS2_I2CRequest, 1))
00284     return -1;
00285 
00286   if (!readI2C(link, HTIRS2_I2CReply, 1))
00287     return -1;
00288 
00289   return HTIRS2_I2CReply[0];
00290 }
00291 
00292 
00293 /**
00294  * Read the value of the AC Direction data register and return it.
00295  * @param muxsensor the SMUX sensor port number
00296  * @return value of 0-9, the direction index of the detected IR signal or -1 if an error occurred.
00297  */
00298 #ifdef __HTSMUX_SUPPORT__
00299 int HTIRS2readACDir(tMUXSensor muxsensor) {
00300   memset(HTIRS2_I2CReply, 0, sizeof(tByteArray));
00301 
00302   if (HTSMUXSensorTypes[muxsensor] != HTSMUXSensorCustom)
00303     HTSMUXconfigChannel(muxsensor, HTIRS2_config);
00304 
00305   if (!HTSMUXreadPort(muxsensor, HTIRS2_I2CReply, 1, HTIRS2_AC_DIR)) {
00306     return -1;
00307   }
00308 
00309   return HTIRS2_I2CReply[0];
00310 }
00311 #endif // __HTSMUX_SUPPORT__
00312 
00313 
00314 /**
00315  * Read the value of the all of the internal AC sensors and copy into specified buffer.
00316  * @param link the HTIRS2 port number
00317  * @param acS1 data from internal sensor nr 1
00318  * @param acS2 data from internal sensor nr 2
00319  * @param acS3 data from internal sensor nr 3
00320  * @param acS4 data from internal sensor nr 4
00321  * @param acS5 data from internal sensor nr 5
00322  * @return true if no error occured, false if it did
00323  */
00324 bool HTIRS2readAllACStrength(tSensors link, int &acS1, int &acS2, int &acS3, int &acS4, int &acS5) {
00325   memset(HTIRS2_I2CRequest, 0, sizeof(tByteArray));
00326 
00327   HTIRS2_I2CRequest[0] = 2;                      // Message size
00328   HTIRS2_I2CRequest[1] = HTIRS2_I2C_ADDR;         // I2C Address
00329   HTIRS2_I2CRequest[2] = HTIRS2_OFFSET + HTIRS2_AC_SSTR1;         // Sensor 0 signal strength
00330 
00331   if (!writeI2C(link, HTIRS2_I2CRequest, 5))
00332     return false;
00333 
00334   if (!readI2C(link, HTIRS2_I2CReply, 5))
00335     return false;
00336 
00337   acS1 = HTIRS2_I2CReply[0];
00338   acS2 = HTIRS2_I2CReply[1];
00339   acS3 = HTIRS2_I2CReply[2];
00340   acS4 = HTIRS2_I2CReply[3];
00341   acS5 = HTIRS2_I2CReply[4];
00342 
00343   return true;
00344 }
00345 
00346 
00347 /**
00348  * Read the value of the all of the internal AC sensors and copy into specified buffer.
00349  * @param muxsensor the SMUX sensor port number
00350  * @param acS1 data from internal sensor nr 1
00351  * @param acS2 data from internal sensor nr 2
00352  * @param acS3 data from internal sensor nr 3
00353  * @param acS4 data from internal sensor nr 4
00354  * @param acS5 data from internal sensor nr 5
00355  * @return true if no error occured, false if it did
00356  */
00357 #ifdef __HTSMUX_SUPPORT__
00358 bool HTIRS2readAllACStrength(tMUXSensor muxsensor, int &acS1, int &acS2, int &acS3, int &acS4, int &acS5) {
00359   memset(HTIRS2_I2CReply, 0, sizeof(tByteArray));
00360 
00361   if (HTSMUXSensorTypes[muxsensor] != HTSMUXSensorCustom)
00362     HTSMUXconfigChannel(muxsensor, HTIRS2_config);
00363 
00364   if (!HTSMUXreadPort(muxsensor, HTIRS2_I2CReply, 5, HTIRS2_AC_SSTR1)) {
00365     return false;
00366   }
00367 
00368   acS1 = HTIRS2_I2CReply[0];
00369   acS2 = HTIRS2_I2CReply[1];
00370   acS3 = HTIRS2_I2CReply[2];
00371   acS4 = HTIRS2_I2CReply[3];
00372   acS5 = HTIRS2_I2CReply[4];
00373 
00374   return true;
00375 }
00376 #endif // __HTSMUX_SUPPORT__
00377 
00378 #endif // __HTIRS2_H__
00379 
00380 /*
00381  * $Id: HTIRS2-driver.h 48 2011-02-13 20:35:38Z xander $
00382  */
00383 /* @} */
00384 /* @} */