Line data Source code
1 : #include "AP_TemperatureSensor_config.h" 2 : 3 : #if AP_TEMPERATURE_SENSOR_MLX90614_ENABLED 4 : 5 : #include "AP_TemperatureSensor_MLX90614.h" 6 : 7 : #include <AP_HAL/AP_HAL.h> 8 : #include <AP_HAL/I2CDevice.h> 9 : #include <AP_Math/AP_Math.h> 10 : 11 : 12 : extern const AP_HAL::HAL &hal; 13 : 14 : #define MLX90614_I2CDEFAULTADDR 0x5A // Device default slave address 15 : #define MLX90614_BROADCASTADDR 0 // Device broadcast slave address 16 : 17 : // RAM addresses 18 : #define MLX90614_RAWIR1 0x04 // RAM reg - Raw temperature, source #1 19 : #define MLX90614_RAWIR2 0x05 // RAM reg - Raw temperature, source #2 20 : #define MLX90614_TA 0x06 // RAM reg - Linearized temperature, ambient 21 : #define MLX90614_TOBJ1 0x07 // RAM reg - Linearized temperature, source #1 22 : #define MLX90614_TOBJ2 0x08 // RAM reg - Linearized temperature, source #2 23 : 24 0 : void AP_TemperatureSensor_MLX90614::init() 25 : { 26 0 : _params.bus_address.set_default(MLX90614_I2CDEFAULTADDR); 27 : 28 0 : _dev = hal.i2c_mgr->get_device_ptr(_params.bus, _params.bus_address); 29 0 : if (!_dev) { 30 0 : return; 31 : } 32 : 33 0 : WITH_SEMAPHORE(_dev->get_semaphore()); 34 : 35 0 : _dev->register_periodic_callback(50 * AP_USEC_PER_MSEC, 36 : FUNCTOR_BIND_MEMBER(&AP_TemperatureSensor_MLX90614::_timer, void)); 37 0 : } 38 : 39 : 40 0 : void AP_TemperatureSensor_MLX90614::_timer() 41 : { 42 0 : const uint16_t _crude_value = read_data(MLX90614_TA); 43 : 44 0 : if (_crude_value == 0) { 45 0 : return; 46 : } 47 : 48 0 : WITH_SEMAPHORE(_dev->get_semaphore()); 49 : 50 : // temp * 0.02 - 273.15 = degrees, temp * 0.02 is temperature in kelvin 51 0 : const float tmp = KELVIN_TO_C(_crude_value * 0.02); 52 0 : set_temperature(tmp); 53 0 : } 54 : 55 : 56 : 57 0 : uint16_t AP_TemperatureSensor_MLX90614::read_data(uint8_t cmd) 58 : { 59 : uint8_t val[3]; 60 : 61 0 : if (!_dev->transfer(&cmd, 1, val, 3)) { 62 0 : return 0; 63 : } 64 0 : return UINT16_VALUE(val[1],val[0]); 65 : } 66 : #endif // AP_TEMPERATURE_SENSOR_MLX90614_ENABLED