2 /// Copyright (c) Titan Robotics Club. All rights reserved.
4 /// <module name="TrcGyro.h" />
7 /// This module contains the definition and implementation of the TrcGyro
12 /// Environment: Wind River C++ for National Instrument cRIO based Robot.
22 #define MOD_ID MOD_GYRO
26 #define MOD_NAME "TrcGyro"
28 #define PIDMODE_ANGLE 0
29 #define PIDMODE_VELOCITY 1
30 #define PIDMODE_ACCELERATION 2
33 * This class defines and implements the TrcGyro object. This object inherits
34 * the Gyro object from the WPI library. It added the support of providing
35 * angular velocity as well as angular acceleration information which is
36 * missing from the Gyro class in the WPI library.
38 class TrcGyro: public Gyro
45 float m_angularVelocity;
46 float m_angularAcceleration;
50 * This function is called periodically by the a timer callback to process
51 * the gyro data. It differentiate the angle with time to calculate the
52 * angular velocity, differentiates it again to calculate angular
63 CRITICAL_REGION(m_semaphore)
65 float prevAngle = m_angle;
66 float prevVelocity = m_angularVelocity;
68 m_angularVelocity = (m_angle - prevAngle)/m_period;
69 m_angularAcceleration = (m_angularVelocity - prevVelocity)/
71 TSampling(("angle=%f,anglevel=%f,angleaccel=%f",
72 m_angle, m_angularVelocity, m_angularAcceleration));
80 * This function is called when the timer expired. It will call the
81 * non-static worker function.
83 * @param timer Points to the TrcGyro object to call the worker function.
92 TEnterMsg(("gyro=%p", gyro));
93 ((TrcGyro *)gyro)->Differentiator();
95 } //CallDifferentiator
98 * This function does the common initialization of the TrcGyro object.
108 m_semaphore = semBCreate(SEM_Q_PRIORITY, SEM_FULL);
109 m_notifier = new Notifier(TrcGyro::CallDifferentiator, this);
110 m_angle = GetAngle();
111 m_angularVelocity = 0.0;
112 m_angularAcceleration = 0.0;
113 m_modePID = PIDMODE_ANGLE;
114 m_notifier->StartPeriodic(m_period);
121 * Constructor: Create an instance of the TrcGyro object. It initializes
122 * the object and starts the periodic timer.
124 * @param slot Specifies the slot of the analog module.
125 * @param channel Specifies the analog channel.
126 * @param period Specifies the sampling time for doing calculations. This
127 * period is used to differentiate angle into velocity and
133 __in float period = 0.05
134 ): Gyro(slot, channel),
138 TEnterMsg(("slot=%d,channel=%d,period=%f", slot, channel, period));
144 * Constructor: Create an instance of the TrcGyro object. It initializes
145 * the object and starts the periodic timer.
147 * @param channel Specifies the analog channel.
148 * @param period Specifies the sampling time for doing calculations. This
149 * period is used to differentiate angle into velocity and
154 __in float period = 0.05
159 TEnterMsg(("channel=%d,period=%f", channel, period));
165 * Destructor: Destroy an instance of the TrcGyro object.
174 semFlush(m_semaphore);
175 SAFE_DELETE(m_notifier);
181 * This function gets the current angle in degrees.
192 CRITICAL_REGION(m_semaphore)
198 TExitMsg(("=%f", value));
203 * This function gets the current angular velocity in degrees per second.
214 CRITICAL_REGION(m_semaphore)
216 value = m_angularVelocity;
220 TExitMsg(("=%f", value));
222 } //GetAngularVelocity
225 * This function gets the current angular acceleration in degrees per
229 GetAngularAcceleration(
237 CRITICAL_REGION(m_semaphore)
239 value = m_angularAcceleration;
243 TExitMsg(("=%f", value));
245 } //GetAngularAcceleration
248 * This function sets the PID mode so that PIDGet will return
257 TEnterMsg(("mode=%d", mode));
259 CRITICAL_REGION(m_semaphore)
269 * This function is called by the PID controller to get the sensor
281 CRITICAL_REGION(m_semaphore)
289 case PIDMODE_VELOCITY:
290 value = m_angularVelocity;
293 case PIDMODE_ACCELERATION:
294 value = m_angularAcceleration;
300 TExitMsg(("=%f", value));
305 #endif //ifndef _TRCGYRO_H