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

HTANG-driver.h

Go to the documentation of this file.
00001 /*!@addtogroup HiTechnic
00002  * @{
00003  * @defgroup htang Angle Sensor
00004  * HiTechnic Angle Sensor
00005  * @{
00006  */
00007 
00008 /*
00009  * $Id: HTANG-driver.h 49 2011-04-27 13:00:05Z xander $
00010  */
00011 
00012 #ifndef __HTANG_H__
00013 #define __HTANG_H__
00014 /** \file HTANG-driver.h
00015  * \brief HiTechnic Angle Sensor driver
00016  *
00017  * HTANG-driver.h provides an API for the HiTechnic Angle Sensor.
00018  *
00019  * Changelog:
00020  * - 0.1: Initial release
00021  * - 0.2: Replaced array structs with typedefs
00022  *
00023  *
00024  * Credits:
00025  * - Big thanks to HiTechnic for providing me with the hardware necessary to write and test this.
00026  *
00027  * License: You may use this code as you wish, provided you give credit where its due.
00028  *
00029  * THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 2.00 AND HIGHER.
00030  * \author Xander Soldaat (mightor_at_gmail.com)
00031  * \date 20 February 2011
00032  * \version 0.2
00033  * \example HTANG-test1.c
00034  * \example HTANG-SMUX-test1.c
00035  */
00036 
00037 #pragma systemFile
00038 
00039 #ifndef __COMMON_H__
00040 #include "common.h"
00041 #endif
00042 
00043 #define HTANG_I2C_ADDR         0x02      /*!< HTCS2 I2C device address */
00044 #define HTANG_CMD_REG          0x41      /*!< Command register */
00045 #define HTANG_OFFSET           0x42      /*!< Offset for data registers */
00046 
00047 // Values contained by registers in active mode
00048 #define HTANG_ANG2             0x00      /*!< Current angle (2 deg increments) */
00049 #define HTANG_ANG1             0x01      /*!< Current angle (1 deg adder) */
00050 #define HTANG_ACC_ANG_B4       0x02      /*!< 32 bit accumulated angle 4th byte */
00051 #define HTANG_ACC_ANG_B3       0x03      /*!< 32 bit accumulated angle 3rd byte */
00052 #define HTANG_ACC_ANG_B2       0x04      /*!< 32 bit accumulated angle 2nd byte */
00053 #define HTANG_ACC_ANG_B1       0x05      /*!< 32 bit accumulated angle 1st byte */
00054 #define HTANG_RPM_H            0x06      /*!< 16 bit rpms, high byte */
00055 #define HTANG_RPM_L            0x07      /*!< 16 bit rpms, low byte */
00056 
00057 // Various commands
00058 #define HTANG_CMD_MEASURE      0x00      /*!< Normal angle measurement mode */
00059 #define HTANG_CMD_RST_ANG      0x43      /*!< Resets 0 position to current shaft angle, non-volatile setting */
00060 #define HTANG_CMD_RST_ACC_ANG  0x52      /*!< Resets the accumulated angle */
00061 
00062 int HTANGreadAngle(tSensors link);
00063 long HTANGreadAccumulatedAngle(tSensors link);
00064 int HTANGreadRPM(tSensors link);
00065 bool HTANGresetAngle(tSensors link);
00066 bool HTANGresetAccumulatedAngle(tSensors link);
00067 bool _HTANGsendCommand(tSensors link, byte command);
00068 
00069 #ifdef __HTSMUX_SUPPORT__
00070 int HTANGreadAngle(tMUXSensor muxsensor);
00071 long HTANGreadAccumulatedAngle(tMUXSensor muxsensor);
00072 int HTANGreadRPM(tMUXSensor muxsensor);
00073 
00074 tConfigParams HTANG_config = {HTSMUX_CHAN_I2C, 8, 0x02, 0x42};  /*!< Array to hold SMUX config data for sensor */
00075 #endif // __HTSMUX_SUPPORT__
00076 
00077 tByteArray HTANG_I2CRequest;             /*!< Array to hold I2C command data */
00078 tByteArray HTANG_I2CReply;               /*!< Array to hold I2C reply data */
00079 
00080 
00081 /**
00082  * Return the current angle
00083  * @param link the HTANG port number
00084  * @return current angle or -1 if an error occurred.
00085  */
00086 int HTANGreadAngle(tSensors link) {
00087   memset(HTANG_I2CRequest, 0, sizeof(tByteArray));
00088 
00089   HTANG_I2CRequest[0] = 2;                         // Message size
00090   HTANG_I2CRequest[1] = HTANG_I2C_ADDR;            // I2C Address
00091   HTANG_I2CRequest[2] = HTANG_OFFSET + HTANG_ANG2; // Start Current angle
00092 
00093   if (!writeI2C(link, HTANG_I2CRequest, 2))
00094     return -1;
00095 
00096   if (!readI2C(link, HTANG_I2CReply, 2))
00097     return -1;
00098 
00099   return HTANG_I2CReply[0] * 2 + HTANG_I2CReply[1];
00100 }
00101 
00102 
00103 /**
00104  * Return the current angle
00105  * @param muxsensor the SMUX sensor port number
00106  * @return current angle or -1 if an error occurred.
00107  */
00108 #ifdef __HTSMUX_SUPPORT__
00109 int HTANGreadAngle(tMUXSensor muxsensor) {
00110         memset(HTANG_I2CRequest, 0, sizeof(tByteArray));
00111 
00112   if (HTSMUXSensorTypes[muxsensor] != HTSMUXSensorCustom)
00113     HTSMUXconfigChannel(muxsensor, HTANG_config);
00114 
00115   if (!HTSMUXreadPort(muxsensor, HTANG_I2CReply, 2, HTANG_ANG2)) {
00116     return -1;
00117   }
00118 
00119   return HTANG_I2CReply[0] * 2 + HTANG_I2CReply[1];
00120 }
00121 #endif // __HTSMUX_SUPPORT__
00122 
00123 
00124 /**
00125  * Return the accumulated angle (signed 32 bit value)
00126  * @param link the HTANG port number
00127  * @return current angle or -1 if an error occurred.
00128  */
00129 long HTANGreadAccumulatedAngle(tSensors link) {
00130   memset(HTANG_I2CRequest, 0, sizeof(tByteArray));
00131 
00132   HTANG_I2CRequest[0] = 2;                                // Message size
00133   HTANG_I2CRequest[1] = HTANG_I2C_ADDR;                   // I2C Address
00134   HTANG_I2CRequest[2] = HTANG_OFFSET + HTANG_ACC_ANG_B4;  // Start accumulated angle
00135 
00136   if (!writeI2C(link, HTANG_I2CRequest, 4))
00137     return -1;
00138 
00139   if (!readI2C(link, HTANG_I2CReply, 4))
00140     return -1;
00141 
00142   return (HTANG_I2CReply[0] << 24) +
00143          (HTANG_I2CReply[1] << 16) +
00144          (HTANG_I2CReply[2] <<  8) +
00145           HTANG_I2CReply[3];
00146 }
00147 
00148 
00149 /**
00150  * Return the accumulated angle (signed 32 bit value)
00151  * @param muxsensor the SMUX sensor port number
00152  * @return current angle or -1 if an error occurred.
00153  */
00154 #ifdef __HTSMUX_SUPPORT__
00155 long HTANGreadAccumulatedAngle(tMUXSensor muxsensor) {
00156         memset(HTANG_I2CRequest, 0, sizeof(tByteArray));
00157 
00158   if (HTSMUXSensorTypes[muxsensor] != HTSMUXSensorCustom)
00159     HTSMUXconfigChannel(muxsensor, HTANG_config);
00160 
00161   if (!HTSMUXreadPort(muxsensor, HTANG_I2CReply, 4, HTANG_ACC_ANG_B4)) {
00162     return -1;
00163   }
00164 
00165   return (HTANG_I2CReply[0] << 24) +
00166          (HTANG_I2CReply[1] << 16) +
00167          (HTANG_I2CReply[2] <<  8) +
00168           HTANG_I2CReply[3];
00169 }
00170 #endif // __HTSMUX_SUPPORT__
00171 
00172 
00173 /**
00174  * Return the rpm that the shaft is currently rotating at
00175  * @param link the HTANG port number
00176  * @return the current rpm of the shaft or -1 if an error occurred.
00177  */
00178 int HTANGreadRPM(tSensors link) {
00179   memset(HTANG_I2CRequest, 0, sizeof(tByteArray));
00180 
00181   HTANG_I2CRequest[0] = 2;                           // Message size
00182   HTANG_I2CRequest[1] = HTANG_I2C_ADDR;              // I2C Address
00183   HTANG_I2CRequest[2] = HTANG_OFFSET + HTANG_RPM_H;  // Start of rpm
00184 
00185   if (!writeI2C(link, HTANG_I2CRequest, 2))
00186     return -1;
00187 
00188   if (!readI2C(link, HTANG_I2CReply, 2))
00189     return -1;
00190 
00191   return (HTANG_I2CReply[0] <<  8) +
00192           HTANG_I2CReply[1];
00193 }
00194 
00195 
00196 /**
00197  * Return the rpm that the shaft is currently rotating at
00198  * @param muxsensor the SMUX sensor port number
00199  * @return the current rpm of the shaft or -1 if an error occurred.
00200  */
00201 #ifdef __HTSMUX_SUPPORT__
00202 int HTANGreadRPM(tMUXSensor muxsensor) {
00203   memset(HTANG_I2CRequest, 0, sizeof(tByteArray));
00204 
00205   if (HTSMUXSensorTypes[muxsensor] != HTSMUXSensorCustom)
00206     HTSMUXconfigChannel(muxsensor, HTANG_config);
00207 
00208   if (!HTSMUXreadPort(muxsensor, HTANG_I2CReply, 2, HTANG_RPM_H)) {
00209     return -1;
00210   }
00211 
00212   return HTANG_I2CReply[0] * 2 + HTANG_I2CReply[1];
00213 
00214   return (HTANG_I2CReply[0] <<  8) +
00215           HTANG_I2CReply[1];
00216 }
00217 #endif // __HTSMUX_SUPPORT__
00218 
00219 
00220 /**
00221  * Reset the 0 position to the current shaft angle.<br>
00222  * Note: this will also reset the accumulated angle counter
00223  * @param link the HTANG port number
00224  * @return true if no error occured, false if it did
00225  */
00226 bool HTANGresetAngle(tSensors link) {
00227   return _HTANGsendCommand(link, HTANG_CMD_RST_ANG);
00228 }
00229 
00230 
00231 /**
00232  * Reset the accumulated angle
00233  * @param link the HTANG port number
00234  * @return true if no error occured, false if it did
00235  */
00236 bool HTANGresetAccumulatedAngle(tSensors link) {
00237   return _HTANGsendCommand(link, HTANG_CMD_RST_ACC_ANG);
00238 }
00239 
00240 
00241 /**
00242  * Send a command to the sensor
00243  *
00244  * Note: this is an internal function and should not be called directly.
00245  * @param link the HTANG port number
00246  * @param command the command to be sent to the sensor
00247  * @return true if no error occured, false if it did
00248  */
00249 bool _HTANGsendCommand(tSensors link, byte command) {
00250   memset(HTANG_I2CRequest, 0, sizeof(tByteArray));
00251 
00252   HTANG_I2CRequest[0] = 3;              // Message size
00253   HTANG_I2CRequest[1] = HTANG_I2C_ADDR; // I2C Address
00254   HTANG_I2CRequest[2] = HTANG_CMD_REG;  // Command register
00255   HTANG_I2CRequest[3] = command;        // Command to be sent
00256 
00257   return writeI2C(link, HTANG_I2CRequest, 0);
00258 }
00259 
00260 
00261 #endif // __HTANG_H__
00262 
00263  /*
00264  * $Id: HTANG-driver.h 49 2011-04-27 13:00:05Z xander $
00265  */
00266 /* @} */
00267 /* @} */