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

HTGYRO-driver.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: HTGYRO-driver.h 49 2011-04-27 13:00:05Z xander $
00010  */
00011 
00012 #ifndef __HTGYRO_H__
00013 #define __HTGYRO_H__
00014 /** \file HTGYRO-driver.h
00015  * \brief HiTechnic Gyroscopic Sensor driver
00016  *
00017  * HTGYRO-driver.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 2.00 AND HIGHER.
00035  * \author Xander Soldaat (mightor_at_gmail.com)
00036  * \date 20 February 2011
00037  * \version 0.4
00038  * \example HTGYRO-test1.c
00039  * \example HTGYRO-SMUX-test1.c
00040  */
00041 
00042 #pragma systemFile
00043 
00044 #ifndef __COMMON_H__
00045 #include "common.h"
00046 #endif
00047 
00048 int HTGYROreadRot(tSensors link);
00049 int HTGYROstartCal(tSensors link);
00050 int HTGYROreadCal(tSensors link);
00051 void HTGYROsetCal(tSensors link, int offset);
00052 
00053 #ifdef __HTSMUX_SUPPORT__
00054 int HTGYROreadRot(tMUXSensor muxsensor);
00055 int HTGYROstartCal(tMUXSensor muxsensor);
00056 int HTGYROreadCal(tMUXSensor muxsensor);
00057 void HTGYROsetCal(tMUXSensor muxsensor, int offset);
00058 #endif // __HTSMUX_SUPPORT__
00059 
00060 int HTGYRO_offsets[][] = {{620, 620, 620, 620}, /*!< Array for offset values.  Default is 620 */
00061                           {620, 620, 620, 620},
00062                           {620, 620, 620, 620},
00063                           {620, 620, 620, 620}};
00064 
00065 /**
00066  * Read the value of the gyro
00067  * @param link the HTGYRO port number
00068  * @return the value of the gyro
00069  */
00070 int HTGYROreadRot(tSensors link) {
00071   // Make sure the sensor is configured as type sensorRawValue
00072   if (SensorType[link] != sensorAnalogInactive) {
00073     SetSensorType(link, sensorAnalogInactive);
00074     wait1Msec(100);
00075   }
00076 
00077   return (SensorValue[link] - HTGYRO_offsets[link][0]);
00078 }
00079 
00080 
00081 /**
00082  * Read the value of the gyro
00083  * @param muxsensor the SMUX sensor port number
00084  * @return the value of the gyro
00085  */
00086 #ifdef __HTSMUX_SUPPORT__
00087 int HTGYROreadRot(tMUXSensor muxsensor) {
00088   return HTSMUXreadAnalogue(muxsensor) - HTGYRO_offsets[SPORT(muxsensor)][MPORT(muxsensor)];
00089 }
00090 #endif // __HTSMUX_SUPPORT__
00091 
00092 
00093 /**
00094  * Calibrate the gyro by calculating the average offset of 5 raw readings.
00095  * @param link the HTGYRO port number
00096  * @return the new offset value for the gyro
00097  */
00098 int HTGYROstartCal(tSensors link) {
00099   long _avgdata = 0;
00100 
00101   // Make sure the sensor is configured as type sensorRawValue
00102   if (SensorType[link] != sensorAnalogInactive) {
00103     SetSensorType(link, sensorAnalogInactive);
00104     wait1Msec(100);
00105   }
00106 
00107   // Take 50 readings and average them out
00108   for (int i = 0; i < 50; i++) {
00109     _avgdata += SensorValue[link];
00110     wait1Msec(5);
00111   }
00112 
00113   // Store new offset
00114   HTGYRO_offsets[link][0] = (_avgdata / 50);
00115 
00116   // Return new offset value
00117   return HTGYRO_offsets[link][0];
00118 }
00119 
00120 
00121 /**
00122  * Calibrate the gyro by calculating the average offset of 5 raw readings.
00123  * @param muxsensor the SMUX sensor port number
00124  * @return the new offset value for the gyro
00125  */
00126 #ifdef __HTSMUX_SUPPORT__
00127 int HTGYROstartCal(tMUXSensor muxsensor) {
00128   int _avgdata = 0;
00129 
00130   // Take 5 readings and average them out
00131   for (int i = 0; i < 5; i++) {
00132     _avgdata += HTSMUXreadAnalogue(muxsensor);
00133     wait1Msec(50);
00134   }
00135 
00136   // Store new offset
00137   HTGYRO_offsets[SPORT(muxsensor)][MPORT(muxsensor)] = (_avgdata / 5);
00138 
00139   // Return new offset value
00140   return HTGYRO_offsets[SPORT(muxsensor)][MPORT(muxsensor)];
00141 }
00142 #endif // __HTSMUX_SUPPORT__
00143 
00144 
00145 /**
00146  * Override the current offset for the gyro manually
00147  * @param link the HTGYRO port number
00148  * @param offset the new offset to be used
00149  */
00150 void HTGYROsetCal(tSensors link, int offset) {
00151   HTGYRO_offsets[link][0] = offset;
00152 }
00153 
00154 
00155 /**
00156  * Override the current offset for the gyro manually
00157  * @param muxsensor the SMUX sensor port number
00158  * @param offset the new offset to be used
00159  */
00160 #ifdef __HTSMUX_SUPPORT__
00161 void HTGYROsetCal(tMUXSensor muxsensor, int offset) {
00162   HTGYRO_offsets[SPORT(muxsensor)][MPORT(muxsensor)] = offset;
00163 }
00164 #endif // __HTSMUX_SUPPORT__
00165 
00166 
00167 /**
00168  * Retrieve the current offset for the gyro
00169  * @param link the HTGYRO port number
00170  * @return the offset value for the gyro
00171  */
00172 int HTGYROreadCal(tSensors link) {
00173   return HTGYRO_offsets[link][0];
00174 }
00175 
00176 
00177 /**
00178  * Retrieve the current offset for the gyro
00179  * @param muxsensor the SMUX sensor port number
00180  * @return the offset value for the gyro
00181  */
00182 #ifdef __HTSMUX_SUPPORT__
00183 int HTGYROreadCal(tMUXSensor muxsensor) {
00184   return HTGYRO_offsets[SPORT(muxsensor)][MPORT(muxsensor)];
00185 }
00186 #endif // __HTSMUX_SUPPORT__
00187 
00188 #endif // __HTGYRO_H__
00189 
00190 /*
00191  * $Id: HTGYRO-driver.h 49 2011-04-27 13:00:05Z xander $
00192  */
00193 /* @} */
00194 /* @} */