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

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