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

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