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

mindsensors-angle.h

Go to the documentation of this file.
00001 /*!@addtogroup mindsensors
00002  * @{
00003  * @defgroup msang Angle Sensor
00004  * GlideWheel-AS Angle Sensor
00005  * @{
00006  */
00007 
00008 /*
00009  * $Id: mindsensors-angle.h 133 2013-03-10 15:15:38Z xander $
00010  */
00011 
00012 #ifndef __MSANG_H__
00013 #define __MSANG_H__
00014 /** \file mindsensors-angle.h
00015  * \brief Mindsensors Angle Sensor driver
00016  *
00017  * mindsensors-angle.h provides an API for the Mindsensors Angle Sensor.
00018  *
00019  * Changelog:
00020  * - 0.1: Initial release
00021  *
00022  * Credits:
00023  * - Big thanks to Mindsensors for providing me with the hardware necessary to write and test this.
00024  *
00025  * License: You may use this code as you wish, provided you give credit where its due.
00026  *
00027  * THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 3.59 AND HIGHER. 
00028 
00029  * \author Xander Soldaat (xander_at_botbench.com)
00030  * \date 20 February 2011
00031  * \version 0.1
00032  * \example mindsensors-angle-test1.c
00033  */
00034 
00035 #pragma systemFile
00036 
00037 #ifndef __COMMON_H__
00038 #include "common.h"
00039 #endif
00040 
00041 #define MSANG_I2C_ADDR         0x30      /*!< HTCS2 I2C device address */
00042 #define MSANG_CMD_REG          0x41      /*!< Command register */
00043 #define MSANG_OFFSET           0x42      /*!< Offset for data registers */
00044 
00045 // Values contained by registers in active mode
00046 #define MSANG_ANG              0x00      /*!< Current angle */
00047 #define MSANG_RAW              0x04      /*!< Current sensor raw value (0.5 deg accuracy) */
00048 #define MSANG_RPM              0x08      /*!< rpms */
00049 
00050 // Various commands
00051 #define MSANG_CMD_RST_ANG      0x72      /*!< Resets 0 position to current shaft angle */
00052 
00053 int MSANGreadAngle(tSensors link);
00054 int MSANGreadRPM(tSensors link);
00055 int MSANGreadRaw(tSensors link);
00056 bool MSANGresetAngle(tSensors link);
00057 
00058 tByteArray MSANG_I2CRequest;             /*!< Array to hold I2C command data */
00059 tByteArray MSANG_I2CReply;               /*!< Array to hold I2C reply data */
00060 
00061 
00062 /**
00063  * Return the current angle
00064  * @param link the MSANG port number
00065  * @return current angle or -1 if an error occurred.
00066  */
00067 int MSANGreadAngle(tSensors link) {
00068   memset(MSANG_I2CRequest, 0, sizeof(tByteArray));
00069 
00070   MSANG_I2CRequest[0] = 2;                         // Message size
00071   MSANG_I2CRequest[1] = MSANG_I2C_ADDR;            // I2C Address
00072   MSANG_I2CRequest[2] = MSANG_OFFSET + MSANG_ANG; // Start Current angle
00073 
00074   if (!writeI2C(link, MSANG_I2CRequest, MSANG_I2CReply, 4))
00075     return -1;
00076 
00077   return MSANG_I2CReply[0] + (MSANG_I2CReply[1] << 8) + (MSANG_I2CReply[2] << 16) + (MSANG_I2CReply[3] << 24);
00078 }
00079 
00080 
00081 /**
00082  * Return the current raw sensor value
00083  * @param link the MSANG port number
00084  * @return current raw value or -1 if an error occurred.
00085  */
00086 int MSANGreadRaw(tSensors link) {
00087   memset(MSANG_I2CRequest, 0, sizeof(tByteArray));
00088 
00089   MSANG_I2CRequest[0] = 2;                         // Message size
00090   MSANG_I2CRequest[1] = MSANG_I2C_ADDR;            // I2C Address
00091   MSANG_I2CRequest[2] = MSANG_OFFSET + MSANG_RAW; // Start Current angle
00092 
00093   if (!writeI2C(link, MSANG_I2CRequest, MSANG_I2CReply, 4))
00094     return -1;
00095 
00096   return MSANG_I2CReply[0] + (MSANG_I2CReply[1] << 8) + (MSANG_I2CReply[2] << 16) + (MSANG_I2CReply[3] << 24);
00097 }
00098 
00099 
00100 /**
00101  * Return the rpm that the shaft is currently rotating at
00102  * @param link the MSANG port number
00103  * @return the current rpm of the shaft or -1 if an error occurred.
00104  */
00105 int MSANGreadRPM(tSensors link) {
00106   memset(MSANG_I2CRequest, 0, sizeof(tByteArray));
00107 
00108   MSANG_I2CRequest[0] = 2;                           // Message size
00109   MSANG_I2CRequest[1] = MSANG_I2C_ADDR;              // I2C Address
00110   MSANG_I2CRequest[2] = MSANG_OFFSET + MSANG_RPM;    // Start of rpm
00111 
00112   if (!writeI2C(link, MSANG_I2CRequest, MSANG_I2CReply, 2))
00113     return -1;
00114 
00115   return (MSANG_I2CReply[1] <<  8) + MSANG_I2CReply[0];
00116 }
00117 
00118 
00119 /**
00120  * Reset the 0 position to the current shaft angle.
00121  * @param link the MSANG port number
00122  * @return true if no error occured, false if it did
00123  */
00124 bool MSANGresetAngle(tSensors link) {
00125   memset(MSANG_I2CRequest, 0, sizeof(tByteArray));
00126 
00127   MSANG_I2CRequest[0] = 3;                 // Message size
00128   MSANG_I2CRequest[1] = MSANG_I2C_ADDR;    // I2C Address
00129   MSANG_I2CRequest[2] = MSANG_CMD_REG;     // Command register
00130   MSANG_I2CRequest[3] = MSANG_CMD_RST_ANG; // Command to be sent
00131 
00132   return writeI2C(link, MSANG_I2CRequest);
00133 }
00134 
00135 
00136 #endif // __MSANG_H__
00137 
00138  /*
00139  * $Id: mindsensors-angle.h 133 2013-03-10 15:15:38Z xander $
00140  */
00141 /* @} */
00142 /* @} */