thp/psmoveapi — pull request #61 by rhololkeolke (2013-04-18 05:32:46+00:00)
Title: Fix temperature sensor reading

I did some searching to find the chip that the PSMove is using for its temperature sensing. Based on the fact that the code in the psmove.c showed the magnetometer and the temperature sensor were sharing bits I looked up the chip for the magnetometer.

According to [this page](http://www.ps3devwiki.com/wiki/Move_Motion_Controller#AKM_AK8974_.283-Axis_Magnetic_Compass.29) the sensor being used is an AK 8974. However I could not find a datasheet for the AK8974. Instead I picked what appears to be a similar model the AK8973. The datasheet for this chip can be found [here](http://pdf1.alldatasheet.com/datasheet-pdf/view/219477/AKM/AK8973.html)

It says that the range of the temperature sensor is -30 C to 85 C and on page 20 a chart is given converting the 8 bit hex code to Celsius readings. I noticed that in the psmove.c in the PSMove struct that the code is grabbing more than 8 bits which would explain the very large number that is being returned by the get_temperature() method currently. This doesn't match the datasheet and so I modified the code to use only the bits in temphigh.

The modified method gave me hex values in the typical operating range for the AK8973 defined on page 9 of the datasheet and based on the ambient temp of the room I was working in the numbers when converted to Celsius/Fahrenheit seemed reasonable. I would like to test the sensor readings when the controller is being used in a hotter and/or colder environment to see how the sensor varies, but because I am getting reasonable values now it seems to be working.

I also created two new methods get_temperature_in_celsius and get_temperature_in_fahrenheit which return the temperature in Celsius and Fahrenheit respectively. I got the conversions based on a simple linear fit based on the chart on page 20 of the datasheet. I also added the swig bindings for the new methods.

I modified the example.c file to also use the two new methods and the values being outputted look correct. I also tested the values in the python interpreter to make sure the swig bindings were working. They appear to be.

Hopefully this is useful to people. If you have questions about anything or I wasn't clear let me know.