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

lego-ultrasound.h

Go to the documentation of this file.
00001 /*!@addtogroup lego
00002  * @{
00003  * @defgroup legous Ultrasonic Sensor
00004  * Ultrasonic Sensor
00005  * @{
00006  */
00007 
00008 /*
00009  * $Id: lego-ultrasound.h 133 2013-03-10 15:15:38Z xander $
00010  */
00011 
00012 #ifndef __LEGOUS_H__
00013 #define __LEGOUS_H__
00014 /** \file lego-ultrasound.h
00015  * \brief SMUX driver for the Lego US sensor.
00016  *
00017  * lego-ultrasound.h provides an API for the Lego US driver.
00018  *
00019  * License: You may use this code as you wish, provided you give credit where its due.
00020  * THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 3.59 AND HIGHER. 
00021 
00022  *
00023  * Changelog:
00024  * - 0.1: Initial release
00025  * - 0.2: Added support for additional commands
00026  *
00027  * \author Xander Soldaat (xander_at_botbench.com)
00028  * \date 10 December 2010
00029  * \version 0.2
00030  * \example lego-ultrasound-SMUX-test1.c
00031  */
00032 
00033 #pragma systemFile
00034 
00035 #ifndef __COMMON_H__
00036 #include "common.h"
00037 #endif
00038 
00039 #define LEGOUS_I2C_ADDR    0x02      /*!< Lego US I2C address */
00040 #define LEGOUS_REG_CMD     0x41      /*!< Command register */
00041 #define LEGOUS_REG_DATA    0x42      /*!< Start of measurement data registers */
00042 
00043 #define LEGOUS_CMD_OFF    0x00      /*!< Command to switch US off */
00044 #define LEGOUS_CMD_SSHOT  0x01      /*!< Command to turn on Single Shot mode */
00045 #define LEGOUS_CMD_CONT   0x02      /*!< Command to turn on Continuous Mode */
00046 #define LEGOUS_CMD_ECAPT  0x03      /*!< Command to turn on Event Capture Mode */
00047 #define LEGOUS_CMD_RST    0x04      /*!< Command to request a warm reset */
00048 
00049 // Prototypes
00050 int USreadDist(tSensors link);
00051 bool USreadDistances(tSensors link, tByteArray &distances);
00052 bool _USsendCmd(tSensors link, ubyte command);
00053 bool USsetSingleMode(tSensors link);
00054 bool USsetContinuousMode(tSensors link);
00055 bool USsetOff(tSensors link);
00056 bool USsetEventCapture(tSensors link);
00057 bool USreset(tSensors link);
00058 
00059 #ifdef __HTSMUX_SUPPORT__
00060 int USreadDist(tMUXSensor muxsensor);
00061 
00062 tConfigParams LEGOUS_config = {HTSMUX_CHAN_I2C + HTSMUX_CHAN_9V + HTSMUX_CHAN_I2C_SLOW, 1, 0x02, 0x42}; /*!< Array to hold SMUX config data for sensor */
00063 #endif // __HTSMUX_SUPPORT__
00064 
00065 tByteArray LEGOUS_I2CRequest;
00066 tByteArray LEGOUS_I2CReply;
00067 
00068 /**
00069  * Get the distance value from the sensor
00070  * @param muxsensor the SMUX sensor port number
00071  * @return distance from the sensor or 255 if no valid range has been specified.
00072  */
00073 #ifdef __HTSMUX_SUPPORT__
00074 int USreadDist(tMUXSensor muxsensor) {
00075 
00076   if (HTSMUXSensorTypes[muxsensor] != HTSMUXSensorCustom)
00077     HTSMUXconfigChannel(muxsensor, LEGOUS_config);
00078 
00079 
00080   if (!HTSMUXreadPort(muxsensor, LEGOUS_I2CReply, 1, 0)) {
00081     return 255;
00082   }
00083 
00084   return (int)LEGOUS_I2CReply[0];
00085 }
00086 #endif
00087 
00088 
00089 /**
00090  * Get the distance values from the sensor
00091  * @param link the US port number
00092  * @return distance from the sensor or 255 if no valid range has been specified.
00093  */
00094 int USreadDist(tSensors link) {
00095   memset(LEGOUS_I2CRequest, 0, sizeof(tByteArray));
00096 
00097   LEGOUS_I2CRequest[0] = 2;                // Message size
00098   LEGOUS_I2CRequest[1] = LEGOUS_I2C_ADDR;  // I2C Address
00099   LEGOUS_I2CRequest[2] = LEGOUS_REG_DATA;  // Start direction register
00100 
00101   if (!writeI2C(link, LEGOUS_I2CRequest, LEGOUS_I2CReply, 1))
00102     return -1;
00103 
00104   return LEGOUS_I2CReply[0];
00105 }
00106 
00107 
00108 /**
00109  * Get the distance values from the sensor. The distances to the
00110  * 8 closest objects are returned.
00111  * @param link the US port number
00112  * @param distances array holding data on last 8 echos received
00113  * @return distance from the sensor or 255 if no valid range has been specified.
00114  */
00115 bool USreadDistances(tSensors link, tByteArray &distances) {
00116   memset(LEGOUS_I2CRequest, 0, sizeof(tByteArray));
00117 
00118   LEGOUS_I2CRequest[0] = 2;                // Message size
00119   LEGOUS_I2CRequest[1] = LEGOUS_I2C_ADDR;  // I2C Address
00120   LEGOUS_I2CRequest[2] = LEGOUS_REG_DATA;  // Start direction register
00121 
00122   if (!writeI2C(link, LEGOUS_I2CRequest, LEGOUS_I2CReply, 8))
00123     return false;
00124 
00125   memcpy(distances, LEGOUS_I2CReply, sizeof(tByteArray));
00126   return true;
00127 }
00128 
00129 
00130 /**
00131  * Send a command to the US Sensor
00132  *
00133  * Note: this is an internal function and should not be called directly.
00134  * @param link the US port number
00135  * @param command the command to be sent to the sensor
00136  * @return true if no error occured, false if it did
00137  */
00138 bool _USsendCmd(tSensors link, ubyte command) {
00139   memset(LEGOUS_I2CRequest, 0, sizeof(tByteArray));
00140 
00141   LEGOUS_I2CRequest[0] = 3;                // Message size
00142   LEGOUS_I2CRequest[1] = LEGOUS_I2C_ADDR;  // I2C Address
00143   LEGOUS_I2CRequest[2] = LEGOUS_REG_CMD;   // command register
00144   LEGOUS_I2CRequest[3] = command;          // command
00145 
00146   return writeI2C(link, LEGOUS_I2CRequest);
00147 }
00148 
00149 
00150 /**
00151  * Configure the US sensor for Single Shot mode
00152  * @param link the US port number
00153  * @return true if no error occured, false if it did
00154  */
00155 bool USsetSingleMode(tSensors link) {
00156   return _USsendCmd(link, LEGOUS_CMD_SSHOT);
00157 }
00158 
00159 
00160 /**
00161  * Configure the US sensor for Continuous Mode.  This is the default.
00162  * @param link the US port number
00163  * @return distance from the sensor or 255 if no valid range has been specified.
00164  */
00165 bool USsetContinuousMode(tSensors link) {
00166   return _USsendCmd(link, LEGOUS_CMD_CONT);
00167 }
00168 
00169 
00170 /**
00171  * Turn the sensor off.
00172  * @param link the US port number
00173  * @return true if no error occured, false if it did
00174  */
00175 bool USsetOff(tSensors link){
00176   return _USsendCmd(link, LEGOUS_CMD_OFF);
00177 }
00178 
00179 
00180 /**
00181  * Configure the US sensor for Event Capture mode
00182  * @param link the US port number
00183  * @return true if no error occured, false if it did
00184  */
00185 bool USsetEventCapture(tSensors link) {
00186   return _USsendCmd(link, LEGOUS_CMD_ECAPT);
00187 }
00188 
00189 
00190 /**
00191  * Request a warm reset of the sensor.
00192  * @param link the US port number
00193  * @return true if no error occured, false if it did
00194  */
00195 bool USreset(tSensors link) {
00196   return _USsendCmd(link, LEGOUS_CMD_RST);
00197 }
00198 
00199 #endif // __LEGOSNR_H__
00200 
00201 /*
00202  * $Id: lego-ultrasound.h 133 2013-03-10 15:15:38Z xander $
00203  */
00204 /* @} */
00205 /* @} */