2 /// Copyright (c) Titan Robotics Club. All rights reserved.
4 /// <module name="TrcAccel.h" />
7 /// This module contains the definition and implementation of the TrcAccel
12 /// Environment: Wind River C++ for National Instrument cRIO based Robot.
22 #define MOD_ID MOD_ACCEL
26 #define MOD_NAME "TrcAccel"
29 // 50 calibration points at 20ms each will cost us a total of 1 second start
32 #ifndef ACCEL_NUM_CAL_PTS
33 #define ACCEL_NUM_CAL_PTS 50
35 #ifndef ACCEL_CAL_INTERVAL
36 #define ACCEL_CAL_INTERVAL 0.01 //10ms
40 * This class defines and implements the TrcAccel object. The TrcAccel object
41 * inherits the ADXL345_I2C accelerometer object from the WPI library. This
42 * object periodically sample the accelerometer value for the acceleration
43 * value. It also integrates the acceleration value to calculate the velocity
44 * and then integrates the velocity to calculate the distance value.
46 class TrcAccel: public ADXL345_I2C
64 * This function is called periodically by the a timer callback to
65 * process the accelerometer data. It integrates the data with time
66 * to calculate the velocity, integrates it again to calculate
77 CRITICAL_REGION(m_semaphore)
81 UINT32 timeCurr = GetMsecTime();
82 float period = (float)(timeCurr - m_timestamp)/1000.0;
83 m_timestamp = timeCurr;
84 m_accelData = GetAccelerations();
85 m_accelData.XAxis -= m_zeroOffset.XAxis;
86 m_accelData.YAxis -= m_zeroOffset.YAxis;
87 m_accelData.ZAxis -= m_zeroOffset.ZAxis;
88 m_accelData.XAxis = GRAVITY_CONSTANT*
89 DEADBAND(m_accelData.XAxis,
91 m_accelData.YAxis = GRAVITY_CONSTANT*
92 DEADBAND(m_accelData.YAxis,
94 m_accelData.ZAxis = GRAVITY_CONSTANT*
95 DEADBAND(m_accelData.ZAxis,
97 m_xVel += m_accelData.XAxis*period;
98 m_yVel += m_accelData.YAxis*period;
99 m_zVel += m_accelData.ZAxis*period;
100 m_xDist += m_xVel*period;
101 m_yDist += m_yVel*period;
102 m_zDist += m_zVel*period;
103 TSampling(("X(%f,%f,%f), Y(%f,%f,%f), Z(%f,%f,%f)",
104 m_accelData.XAxis*FEET_PER_METER,
105 m_xVel*FEET_PER_METER,
106 m_xDist*FEET_PER_METER,
107 m_accelData.YAxis*FEET_PER_METER,
108 m_yVel*FEET_PER_METER,
109 m_yDist*FEET_PER_METER,
110 m_accelData.ZAxis*FEET_PER_METER,
111 m_zVel*FEET_PER_METER,
112 m_zDist*FEET_PER_METER));
121 * This function is called when the timer expired. It will call the
122 * non-static worker function.
124 * @param timer Points to the TrcAccel object to call the worker function.
133 TEnterMsg(("accel=%p", accel));
134 ((TrcAccel *)accel)->Integrator();
141 * This function is called to calibrate the zero G point and deadband.
142 * It assumes the accelerometer is sitting still at level ground during
143 * the calibration. It samples a number of points on all axes and averages
144 * them to be the zero G point for the axes. Note that the Z-axis is not
145 * really at zero G when it is still. Z-axis should be 1G at level ground.
146 * But for the purpose of measuring relative G's on all axes, we calibrate
147 * zero G for all axes. During calibration, we also determine the floor
148 * and ceiling noise level to form the deadband zone.
150 * @param numCalPts Specifies the number of calibration points.
151 * @param calInterval Specifies the calibration interval in seconds.
155 __in UINT32 numCalPts,
156 __in float calInterval
176 for (UINT32 i = 0; i < numCalPts; i++)
178 data = GetAccelerations();
180 zeroG.XAxis += data.XAxis;
181 zeroG.YAxis += data.YAxis;
182 zeroG.ZAxis += data.ZAxis;
184 if (data.XAxis < min.XAxis)
186 min.XAxis = data.XAxis;
188 else if (data.XAxis > max.XAxis)
190 max.XAxis = data.XAxis;
193 if (data.YAxis < min.YAxis)
195 min.YAxis = data.YAxis;
197 else if (data.YAxis > max.YAxis)
199 max.YAxis = data.YAxis;
202 if (data.ZAxis < min.ZAxis)
204 min.ZAxis = data.ZAxis;
206 else if (data.ZAxis > max.ZAxis)
208 max.ZAxis = data.ZAxis;
214 CRITICAL_REGION(m_semaphore)
216 m_zeroOffset.XAxis = zeroG.XAxis/numCalPts;
217 m_zeroOffset.YAxis = zeroG.YAxis/numCalPts;
218 m_zeroOffset.ZAxis = zeroG.ZAxis/numCalPts;
220 m_deadBand.XAxis = max.XAxis - min.XAxis;
221 m_deadBand.YAxis = max.YAxis - min.YAxis;
222 m_deadBand.ZAxis = max.ZAxis - min.ZAxis;
224 TInfo(("AccelZeroG:x=%f,y=%f,z=%f, AccelDeadBand:x=%f,y=%f,z=%f",
225 m_zeroOffset.XAxis, m_zeroOffset.YAxis, m_zeroOffset.ZAxis,
226 m_deadBand.XAxis, m_deadBand.YAxis, m_deadBand.ZAxis));
236 * Constructor: Create an instance of the TrcAccel object. It initializes
237 * the object and starts the periodic timer.
239 * @param slot Specifies the slot of the digital module on which the I2C
240 * port is used for the accelerometer.
241 * @param period Specifies the sampling time for doing calculations. This
242 * period is used to integrate acceleration into speed and distance
247 __in float period = 0.02
251 TEnterMsg(("slot=%d,period=%f", slot, period));
253 m_semaphore = semBCreate(SEM_Q_PRIORITY, SEM_FULL);
254 m_notifier = new Notifier(TrcAccel::CallIntegrator, this);
255 m_accelData.XAxis = 0.0;
256 m_accelData.YAxis = 0.0;
257 m_accelData.ZAxis = 0.0;
258 m_zeroOffset.XAxis = 0.0;
259 m_zeroOffset.YAxis = 0.0;
260 m_zeroOffset.ZAxis = 0.0;
261 m_deadBand.XAxis = 0.0;
262 m_deadBand.YAxis = 0.0;
263 m_deadBand.ZAxis = 0.0;
271 Calibrate(ACCEL_NUM_CAL_PTS, ACCEL_CAL_INTERVAL);
272 m_notifier->StartPeriodic(period);
278 * Destructor: Destroy an instance of the TrcAccel object.
287 semFlush(m_semaphore);
288 SAFE_DELETE(m_notifier);
294 * This function returns the current acceleration value of the X axis in
295 * the unit of meters per second square.
297 * @return Returns the X acceleration value.
306 TExitMsg(("=%f", m_accelData.XAxis));
307 return m_accelData.XAxis;
311 * This function returns the current acceleration value of the X axis in
312 * the unit of feet per second square.
314 * @return Returns the X acceleration value.
323 double value = m_accelData.XAxis*FEET_PER_METER;
324 TExitMsg(("=%f", value));
329 * This function returns the current acceleration value of the Y axis in
330 * the unit of meters per second square.
332 * @return Returns the Y acceleration value.
341 TExitMsg(("=%f", m_accelData.YAxis));
342 return m_accelData.YAxis;
346 * This function returns the current acceleration value of the Y axis in
347 * the unit of feet per second square.
349 * @return Returns the Y acceleration value.
358 double value = m_accelData.YAxis*FEET_PER_METER;
359 TExitMsg(("=%f", value));
364 * This function returns the current acceleration value of the Z axis in
365 * the unit of meters per second square.
367 * @return Returns the Z acceleration value.
376 TExitMsg(("=%f", m_accelData.ZAxis));
377 return m_accelData.ZAxis;
381 * This function returns the current acceleration value of the Z axis in
382 * the unit of feet per second square.
384 * @return Returns the Z acceleration value.
393 double value = m_accelData.ZAxis*FEET_PER_METER;
394 TExitMsg(("=%f", value));
399 * This function returns the current velocity value of the X axis in
400 * the unit of meters per second.
402 * @return Returns the X velocity value.
411 TExitMsg(("=%f", m_xVel));
416 * This function returns the current velocity value of the X axis in
417 * the unit of feet per second.
419 * @return Returns the X velocity value.
428 double value = m_xVel*FEET_PER_METER;
429 TExitMsg(("=%f", value));
434 * This function returns the current velocity value of the Y axis in
435 * the unit of meters per second.
437 * @return Returns the Y velocity value.
446 TExitMsg(("=%f", m_yVel));
451 * This function returns the current velocity value of the Y axis in
452 * the unit of feet per second.
454 * @return Returns the Y velocity value.
463 double value = m_yVel*FEET_PER_METER;
464 TExitMsg(("=%f", value));
469 * This function returns the current velocity value of the Z axis in
470 * the unit of meters per second.
472 * @return Returns the Z velocity value.
481 TExitMsg(("=%f", m_zVel));
486 * This function returns the current velocity value of the Z axis in
487 * the unit of feet per second.
489 * @return Returns the Z velocity value.
498 double value = m_zVel*FEET_PER_METER;
499 TExitMsg(("=%f", value));
504 * This function returns the current distance value of the X axis in
505 * the unit of meters.
507 * @return Returns the X distance value.
516 TExitMsg(("=%f", m_xDist));
521 * This function returns the current distance value of the X axis in
524 * @return Returns the X distance value.
533 double value = m_xDist*FEET_PER_METER;
534 TExitMsg(("=%f", value));
539 * This function returns the current distance value of the Y axis in
540 * the unit of meters.
542 * @return Returns the Y distance value.
551 TExitMsg(("=%f", m_yDist));
556 * This function returns the current distance value of the Y axis in
559 * @return Returns the Y distance value.
568 double value = m_yDist*FEET_PER_METER;
569 TExitMsg(("=%f", value));
574 * This function returns the current distance value of the Z axis in
575 * the unit of meters.
577 * @return Returns the Z distance value.
586 TExitMsg(("=%f", m_zDist));
591 * This function returns the current distance value of the Z axis in
594 * @return Returns the Z distance value.
603 double value = m_zDist*FEET_PER_METER;
604 TExitMsg(("=%f", value));
609 * This function resets the acceleration, velocity and distance values.
619 CRITICAL_REGION(m_semaphore)
621 m_accelData.XAxis = 0.0;
622 m_accelData.YAxis = 0.0;
623 m_accelData.ZAxis = 0.0;
637 * This function sets the accelerometer to enable or disable state.
639 * @param fEnabled If true, enables the accelerometer, false otherwise.
649 m_fEnabled = fEnabled;
653 m_timestamp = GetMsecTime();
660 #endif //ifndef _TRCACCEL_H