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

hitechnic-gyro.h

Go to the documentation of this file.
00001 /*!@addtogroup HiTechnic
00002  * @{
00003  * @defgroup htgyro Gyroscopic Sensor
00004  * HiTechnic Gyroscopic Sensor
00005  * @{
00006  */
00007 
00008 /*
00009  * $Id: hitechnic-gyro.h 133 2013-03-10 15:15:38Z xander $
00010  */
00011 
00012 #ifndef __HTGYRO_H__
00013 #define __HTGYRO_H__
00014 /** \file hitechnic-gyro.h
00015  * \brief HiTechnic Gyroscopic Sensor driver
00016  *
00017  * hitechnic-gyro.h provides an API for the HiTechnic Gyroscopic Sensor.
00018  *
00019  * Changelog:
00020  * - 0.1: Initial release
00021  * - 0.2: Renamed HTGYROgetCalibration to HTGYROreadCal<br>
00022  *        Renamed HTGYROsetCalibration to HTGYROsetCal<br>
00023  *        Renamed HTGYROcalibrate to HTGYROstartCal<br>
00024  *        Added SMUX functions
00025  * - 0.3: Removed some of the functions requiring SPORT/MPORT macros
00026  * - 0.4: Removed "NW - No Wait" functions\n
00027  *        Replaced array structs with typedefs\n
00028  *
00029  * Credits:
00030  * - Big thanks to HiTechnic for providing me with the hardware necessary to write and test this.
00031  *
00032  * License: You may use this code as you wish, provided you give credit where its due.
00033  *
00034  * THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 3.59 AND HIGHER. 
00035 
00036  * \author Xander Soldaat (xander_at_botbench.com)
00037  * \date 20 February 2011
00038  * \version 0.4
00039  * \example hitechnic-gyro-test1.c
00040  * \example hitechnic-gyro-test2.c
00041  * \example hitechnic-gyro-SMUX-test1.c
00042  */
00043 
00044 #pragma systemFile
00045 
00046 #ifndef __COMMON_H__
00047 #include "common.h"
00048 #endif
00049 
00050 float HTGYROreadRot(tSensors link);
00051 float HTGYROstartCal(tSensors link);
00052 float HTGYROreadCal(tSensors link);
00053 // void HTGYROsetCal(tSensors link, int offset);
00054 
00055 #ifdef __HTSMUX_SUPPORT__
00056 float HTGYROreadRot(tMUXSensor muxsensor);
00057 float HTGYROstartCal(tMUXSensor muxsensor);
00058 float HTGYROreadCal(tMUXSensor muxsensor);
00059 void HTGYROsetCal(tMUXSensor muxsensor, int offset);
00060 #endif // __HTSMUX_SUPPORT__
00061 
00062 float HTGYRO_offsets[][] = {{620.0, 620.0, 620.0, 620.0}, /*!< Array for offset values.  Default is 620 */
00063                           {620.0, 620.0, 620.0, 620.0},
00064                           {620.0, 620.0, 620.0, 620.0},
00065                           {620.0, 620.0, 620.0, 620.0}};
00066 
00067 /**
00068  * Read the value of the gyro
00069  * @param link the HTGYRO port number
00070  * @return the value of the gyro
00071  */
00072 float HTGYROreadRot(tSensors link) {
00073   // Make sure the sensor is configured as type sensorRawValue
00074   if (SensorType[link] != sensorAnalogInactive) {
00075     SetSensorType(link, sensorAnalogInactive);
00076     wait1Msec(100);
00077   }
00078 
00079   return (SensorValue[link] - HTGYRO_offsets[link][0]);
00080 }
00081 
00082 
00083 /**
00084  * Read the value of the gyro
00085  * @param muxsensor the SMUX sensor port number
00086  * @return the value of the gyro
00087  */
00088 #ifdef __HTSMUX_SUPPORT__
00089 float HTGYROreadRot(tMUXSensor muxsensor) {
00090   return HTSMUXreadAnalogue(muxsensor) - HTGYRO_offsets[SPORT(muxsensor)][MPORT(muxsensor)];
00091 }
00092 #endif // __HTSMUX_SUPPORT__
00093 
00094 
00095 /**
00096  * Calibrate the gyro by calculating the average offset of 5 raw readings.
00097  * @param link the HTGYRO port number
00098  * @return the new offset value for the gyro
00099  */
00100 float HTGYROstartCal(tSensors link) {
00101   long _avgdata = 0;
00102 
00103   // Make sure the sensor is configured as type sensorRawValue
00104   if (SensorType[link] != sensorAnalogInactive) {
00105     SetSensorType(link, sensorAnalogInactive);
00106     wait1Msec(100);
00107   }
00108 
00109   // Take 50 readings and average them out
00110   for (int i = 0; i < 50; i++) {
00111     _avgdata += SensorValue[link];
00112     wait1Msec(5);
00113   }
00114 
00115   // Store new offset
00116   HTGYRO_offsets[link][0] = (_avgdata / 50.0);
00117 
00118   // Return new offset value
00119   return HTGYRO_offsets[link][0];
00120 }
00121 
00122 
00123 /**
00124  * Calibrate the gyro by calculating the average offset of 50 raw readings.
00125  * @param muxsensor the SMUX sensor port number
00126  * @return the new offset value for the gyro
00127  */
00128 #ifdef __HTSMUX_SUPPORT__
00129 float HTGYROstartCal(tMUXSensor muxsensor) {
00130   long _avgdata = 0;
00131 
00132   // Take 5 readings and average them out
00133   for (int i = 0; i < 50; i++) {
00134     _avgdata += HTSMUXreadAnalogue(muxsensor);
00135     wait1Msec(50);
00136   }
00137 
00138   // Store new offset
00139   HTGYRO_offsets[SPORT(muxsensor)][MPORT(muxsensor)] = (_avgdata / 50.0);
00140 
00141   // Return new offset value
00142   return HTGYRO_offsets[SPORT(muxsensor)][MPORT(muxsensor)];
00143 }
00144 #endif // __HTSMUX_SUPPORT__
00145 
00146 
00147 /**
00148  * Override the current offset for the gyro manually
00149  * @param link the HTGYRO port number
00150  * @param offset the new offset to be used
00151  */
00152 //#define HTGYROsetCal(link, offset) HTGYRO_offsets[link][0] = offset
00153 void HTGYROsetCal(tSensors link, int offset) {
00154   HTGYRO_offsets[link][0] = offset;
00155 }
00156 
00157 
00158 /**
00159  * Override the current offset for the gyro manually
00160  * @param muxsensor the SMUX sensor port number
00161  * @param offset the new offset to be used
00162  */
00163 #ifdef __HTSMUX_SUPPORT__
00164 //#define HTGYROsetCal(muxsensor, offset) HTGYRO_offsets[SPORT(muxsensor)][MPORT(muxsensor)] = offset
00165 void HTGYROsetCal(tMUXSensor muxsensor, int offset) {
00166   HTGYRO_offsets[SPORT(muxsensor)][MPORT(muxsensor)] = offset;
00167 }
00168 #endif // __HTSMUX_SUPPORT__
00169 
00170 
00171 /**
00172  * Retrieve the current offset for the gyro
00173  * @param link the HTGYRO port number
00174  * @return the offset value for the gyro
00175  */
00176 float HTGYROreadCal(tSensors link) {
00177   return HTGYRO_offsets[link][0];
00178 }
00179 
00180 
00181 /**
00182  * Retrieve the current offset for the gyro
00183  * @param muxsensor the SMUX sensor port number
00184  * @return the offset value for the gyro
00185  */
00186 #ifdef __HTSMUX_SUPPORT__
00187 float HTGYROreadCal(tMUXSensor muxsensor) {
00188   return HTGYRO_offsets[SPORT(muxsensor)][MPORT(muxsensor)];
00189 }
00190 #endif // __HTSMUX_SUPPORT__
00191 
00192 #endif // __HTGYRO_H__
00193 
00194 /*
00195  * $Id: hitechnic-gyro.h 133 2013-03-10 15:15:38Z xander $
00196  */
00197 /* @} */
00198 /* @} */