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

HTMAG-driver.h

Go to the documentation of this file.
00001 /*!@addtogroup HiTechnic
00002  * @{
00003  * @defgroup HTMAG Magnetic Field Sensor
00004  * HiTechnic Magnetic Field Sensor
00005  * @{
00006  */
00007 
00008 /*
00009  * $Id: HTMAG-driver.h 47 2011-01-17 19:47:22Z xander $
00010  */
00011 
00012 #ifndef __HTMAG_H__
00013 #define __HTMAG_H__
00014 /** \file HTMAG-driver.h
00015  * \brief HiTechnic Magnetic Field Sensor driver
00016  *
00017  * HTMAG-driver.h provides an API for the HiTechnic Magnetic Field Sensor.
00018  *
00019  * Changelog:
00020  * - 0.1: Initial release
00021  *
00022  * Credits:
00023  * - Big thanks to HiTechnic 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 2.00 AND HIGHER.
00028  * \author Xander Soldaat (mightor_at_gmail.com)
00029  * \date 27 July 2010
00030  * \version 0.1
00031  * \example HTMAG-test1.c
00032  * \example HTMAG-SMUX-test1.c
00033  */
00034 
00035 #pragma systemFile
00036 
00037 #ifndef __COMMON_H__
00038 #include "common.h"
00039 #endif
00040 
00041 int HTMAGreadVal(tSensors link);
00042 int HTMAGreadRaw(tSensors link);
00043 int HTMAGstartCal(tSensors link);
00044 int HTMAGreadCal(tSensors link);
00045 void HTMAGsetCal(tSensors link, int bias);
00046 
00047 #ifdef __HTSMUX_SUPPORT__
00048 int HTMAGreadVal(tMUXSensor muxsensor);
00049 int HTMAGreadRaw(tMUXSensor muxsensor);
00050 int HTMAGstartCal(tMUXSensor muxsensor);
00051 int HTMAGreadCal(tMUXSensor muxsensor);
00052 void HTMAGsetCal(tMUXSensor muxsensor, int bias);
00053 #endif
00054 
00055 int HTMAG_bias[][] = {{512, 512, 512, 512}, /*!< Array for bias values.  Default is 512 */
00056                           {512, 512, 512, 512},
00057                           {512, 512, 512, 512},
00058                           {512, 512, 512, 512}};
00059 
00060 /**
00061  * Read the value of the Magnetic Field Sensor
00062  * @param link the HTMAG port number
00063  * @return the value of the Magnetic Field Sensor (-200 to +200)
00064  */
00065 int HTMAGreadVal(tSensors link) {
00066   // Make sure the sensor is configured as type sensorRawValue
00067   if (SensorType[link] != sensorRawValue) {
00068     SetSensorType(link, sensorRawValue);
00069     wait1Msec(100);
00070   }
00071 
00072   return (SensorValue[link] - HTMAG_bias[link][0]);
00073 }
00074 
00075 
00076 /**
00077  * Read the value of the Magnetic Field Sensor
00078  * @param muxsensor the SMUX sensor port number
00079  * @return the value of the Magnetic Field Sensor (-200 to +200)
00080  */
00081 #ifdef __HTSMUX_SUPPORT__
00082 int HTMAGreadVal(tMUXSensor muxsensor) {
00083   return HTSMUXreadAnalogue(muxsensor) - HTMAG_bias[SPORT(muxsensor)][MPORT(muxsensor)];
00084 }
00085 #endif // __HTSMUX_SUPPORT__
00086 
00087 
00088 /**
00089  * Read the raw value of the Magnetic Field Sensor
00090  * @param link the HTMAG port number
00091  * @return the value of the Magnetic Field Sensor (approx 300 to 700)
00092  */
00093 int HTMAGreadRaw(tSensors link) {
00094   // Make sure the sensor is configured as type sensorRawValue
00095   if (SensorType[link] != sensorRawValue) {
00096     SetSensorType(link, sensorRawValue);
00097     wait1Msec(100);
00098   }
00099 
00100   return SensorValue[link];
00101 }
00102 
00103 
00104 /**
00105  * Read the raw value of the Magnetic Field Sensor
00106  * @param muxsensor the SMUX sensor port number
00107  * @return the value of the Magnetic Field Sensor (approx 300 to 700)
00108  */
00109 #ifdef __HTSMUX_SUPPORT__
00110 int HTMAGreadRaw(tMUXSensor muxsensor) {
00111   return HTSMUXreadAnalogue(muxsensor);
00112 }
00113 #endif // __HTSMUX_SUPPORT__
00114 
00115 
00116 /**
00117  * Calibrate the sensor by calculating the average bias of 5 raw readings.
00118  * @param link the HTMAG port number
00119  * @return the new bias value for the sensor
00120  */
00121 int HTMAGstartCal(tSensors link) {
00122   int _avgdata = 0;
00123 
00124   // Make sure the sensor is configured as type sensorRawValue
00125   if (SensorType[link] != sensorRawValue) {
00126     SetSensorType(link, sensorRawValue);
00127     wait1Msec(100);
00128   }
00129 
00130   // Take 5 readings and average them out
00131   for (int i = 0; i < 5; i++) {
00132     _avgdata += SensorValue[link];
00133     wait1Msec(50);
00134   }
00135 
00136   // Store new bias
00137   HTMAG_bias[link][0] = (_avgdata / 5);
00138 
00139   // Return new bias value
00140   return HTMAG_bias[link][0];
00141 }
00142 
00143 
00144 /**
00145  * Calibrate the Magnetic Field Sensor by calculating the average bias of 5 raw readings.
00146  * @param muxsensor the SMUX sensor port number
00147  * @return the new bias value for the Magnetic Field Sensor
00148  */
00149 #ifdef __HTSMUX_SUPPORT__
00150 int HTMAGstartCal(tMUXSensor muxsensor) {
00151   int _avgdata = 0;
00152 
00153   // Take 5 readings and average them out
00154   for (int i = 0; i < 5; i++) {
00155     _avgdata += HTSMUXreadAnalogue(muxsensor);
00156     wait1Msec(50);
00157   }
00158 
00159   // Store new bias
00160   HTMAG_bias[SPORT(muxsensor)][MPORT(muxsensor)] = (_avgdata / 5);
00161 
00162   // Return new bias value
00163   return HTMAG_bias[SPORT(muxsensor)][MPORT(muxsensor)];
00164 }
00165 #endif // __HTSMUX_SUPPORT__
00166 
00167 
00168 /**
00169  * Override the current bias for the sensor manually
00170  * @param link the HTMAG port number
00171  * @param bias the new bias to be used
00172  */
00173 void HTMAGsetCal(tSensors link, int bias) {
00174   HTMAG_bias[link][0] = bias;
00175 }
00176 
00177 
00178 /**
00179  * Override the current bias for the sensor manually
00180  * @param muxsensor the SMUX sensor port number
00181  * @param bias the new bias to be used
00182  */
00183 #ifdef __HTSMUX_SUPPORT__
00184 void HTMAGsetCal(tMUXSensor muxsensor, int bias) {
00185   HTMAG_bias[SPORT(muxsensor)][MPORT(muxsensor)] = bias;
00186 }
00187 #endif // __HTSMUX_SUPPORT__
00188 
00189 
00190 /**
00191  * Retrieve the current bias for the sensor
00192  * @param link the HTMAG port number
00193  * @return the bias value for the sensor
00194  */
00195 int HTMAGreadCal(tSensors link) {
00196   return HTMAG_bias[link][0];
00197 }
00198 
00199 
00200 /**
00201  * Retrieve the current bias for the sensor
00202  * @param muxsensor the SMUX sensor port number
00203  * @return the bias value for the sensor
00204  */
00205 #ifdef __HTSMUX_SUPPORT__
00206 int HTMAGreadCal(tMUXSensor muxsensor) {
00207   return HTMAG_bias[SPORT(muxsensor)][MPORT(muxsensor)];
00208 }
00209 #endif // __HTSMUX_SUPPORT__
00210 
00211 
00212 #endif // __HTMAG_H__
00213 
00214 /*
00215  * $Id: HTMAG-driver.h 47 2011-01-17 19:47:22Z xander $
00216  */
00217 /* @} */
00218 /* @} */