Has anyone written C++ code to read the temperature sensor on the gyro?

Has anyone written C++ code to read the temperature sensor included as part of the ADW22307 gyro in the 2010 Kit of Parts?

The purpose of the temperature sensor is to allow one to interpret the gyro data while compensating for temperature, since the gyro data varies somewhat depending on the ambient temperature around the gyro itself. We aren’t planning (anytime soon) to implement this temperature compensation for the gyro data (although that would be cool and quite doable…maybe someone will submit that temperature compensation code to WPILib someday).

However, we would still be interested in reading the temperature sensor on the gyro, just to find out the ambient temperature around our electronics inside the robot. We would like to display the robot temperature on our custom dashboard, and if we find that it’s getting hot in there from the Jaguars and such, we can potentially add one of those nifty fans from the Kit of Parts to help cool down the robot electronics.

The gyro temperature sensor is just a standard 3-wire analog sensor, and can apparently be plugged directly into the analog breakout board on the cRIO, just like the gyro itself. However, WPILib doesn’t seem to include any functions that provide a convenient way to read that temperature from the gyro.

Per the datasheet for the ADW22307 gyro, and the FRC 2010 Sensor Manual, the temperature sensor has a nominal output of 2.5V at 25°C (77°F), and the output varies up or down by 9mV per one degree Celsius (5mv per one degree Fahrenheit).

It would not be difficult to write the C++ code to read the temperature sensor on the 2010 gyro. It would involve using the AnalogChannel class in WPILib. We’ve never written code to use AnalogChannel directly before, but we could start with the Gyro.cpp WPILib source code (which also uses AnalogChannel) and modify it accordingly. But if someone else has already written the code to read the temperature sensor, and is interested in sharing it, we wouldn’t have to re-invent the wheel. No big deal either way, we just thought we’d ask.

Many thanks!

I haven’t written code to do this, but probably the easiest thing to do would be to do this:

AnalogChannel* temp = new AnalogChannel(1); // adjust port accordinly
float change;
float temperature;

change = temp->GetVoltage() - 2.5; //Get temperature offset from 77 F
change *= 200 //convert to degrees F (1000 to bring out of millivolts / 5 per degree)
temperature = 77 + change; //apply offset to reference temperature

Change the constants accordingly to make it Celsius (the 200 becomes 1000/9 (not even so leave as equation) and the 77 becomes 25)

I can make a wrapper class for this if you want

Awesome. That looks so easy. I thought it would be a lot more involved. We will work on this tomorrow, starting with your code. Much appreciated!