2 /// Copyright (c) Titan Robotics Club. All rights reserved.
4 /// <module name="gyro.h" />
7 /// This module contains the library functions for the gyro sensor.
11 /// Environment: RobotC for Lego Mindstorms NXT.
18 #include "..\HTDriversV1.6\drivers\HTGYRO-driver.h"
25 #define MOD_ID MOD_GYRO
30 #define GYROF_USER_MASK 0x00ff
31 #define GYROF_INVERSE 0x0001
33 #define GYROF_HTSMUX 0x0080
36 #define GYRO_NUM_CAL_SAMPLES 50
37 #define GYRO_CAL_INTERVAL 10
42 #define GyroGetTurnRate(p) (p.turnRate)
43 #define GyroGetHeading(p) (p.heading)
60 * This function calibrates the gyro for zero offset and deadband.
62 * @param gyro Points to the GYRO structure to be initialized.
63 * @param numSamples Specifies the number of calibration samples.
64 * @param calInterval Specifies the calibration interval in msec.
85 for (i = 0; i < numSamples; i++)
88 turnRate = (gyro.gyroFlags & GYROF_HTSMUX)?
89 HTGYROreadRot((tMUXSensor)gyro.sensorID):
90 HTGYROreadRot((tSensors)gyro.sensorID);
92 turnRate = HTGYROreadRot((tSensors)gyro.sensorID);
94 gyro.zeroOffset += turnRate;
100 else if (turnRate > max)
105 wait1Msec(calInterval);
108 gyro.zeroOffset /= numSamples;
109 gyro.deadBand = max - min;
116 * This function performs the gyro task where it integrates the turn rate
117 * into a heading value.
119 * @param gyro Points to the GYRO structure.
128 TFuncName("GyroTask");
134 gyro.turnRate = (gyro.gyroFlags & GYROF_HTSMUX)?
135 HTGYROreadRot((tMUXSensor)gyro.sensorID):
136 HTGYROreadRot((tSensors)gyro.sensorID);
138 gyro.turnRate = HTGYROreadRot((tSensors)gyro.sensorID);
140 gyro.turnRate -= gyro.zeroOffset;
141 gyro.turnRate = DEADBAND(gyro.turnRate, gyro.deadBand);
142 if (gyro.gyroFlags & GYROF_INVERSE)
144 gyro.turnRate = -gyro.turnRate;
146 gyro.heading += (float)gyro.turnRate*(currTime - gyro.timestamp)/1000;
147 gyro.timestamp = currTime;
154 * This function resets the gyro heading.
156 * @param gyro Points to the GYRO structure to be reset.
163 TFuncName("GyroReset");
175 * This function initializes the gyro sensor.
177 * @param gyro Points to the GYRO structure to be initialized.
178 * @param sensorID Specifies the ID of the gyro sensor.
179 * @param gyroFlags Specifies the gyro flags.
188 TFuncName("GyroInit");
192 gyro.sensorID = sensorID;
193 gyro.gyroFlags = gyroFlags & GYROF_USER_MASK;
195 if (gyro.gyroFlags & GYROF_HTSMUX)
197 HTGYROstartCal((tMUXSensor)sensorID);
201 HTGYROstartCal((tSensors)sensorID);
204 HTGYROstartCal((tSensors)sensorID);
206 GyroCal(gyro, GYRO_NUM_CAL_SAMPLES, GYRO_CAL_INTERVAL);
207 gyro.timestamp = nPgmTime;
214 #endif //ifndef _GYRO_H