-
Notifications
You must be signed in to change notification settings - Fork 17.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AP_TemperatureSensor: new MLX90614 sensor backend driver added
- Loading branch information
Showing
8 changed files
with
105 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
libraries/AP_TemperatureSensor/AP_TemperatureSensor_MLX90614.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#include "AP_TemperatureSensor_config.h" | ||
|
||
#if AP_TEMPERATURE_SENSOR_MLX90614_ENABLED | ||
|
||
#include "AP_TemperatureSensor_MLX90614.h" | ||
|
||
#include <AP_HAL/AP_HAL.h> | ||
#include <AP_HAL/I2CDevice.h> | ||
#include <AP_Math/AP_Math.h> | ||
|
||
|
||
extern const AP_HAL::HAL &hal; | ||
|
||
#define MLX90614_I2CDEFAULTADDR 0x5A // Device default slave address | ||
#define MLX90614_BROADCASTADDR 0 // Device broadcast slave address | ||
|
||
// RAM addresses | ||
#define MLX90614_RAWIR1 0x04 // RAM reg - Raw temperature, source #1 | ||
#define MLX90614_RAWIR2 0x05 // RAM reg - Raw temperature, source #2 | ||
#define MLX90614_TA 0x06 // RAM reg - Linearized temperature, ambient | ||
#define MLX90614_TOBJ1 0x07 // RAM reg - Linearized temperature, source #1 | ||
#define MLX90614_TOBJ2 0x08 // RAM reg - Linearized temperature, source #2 | ||
|
||
void AP_TemperatureSensor_MLX90614::init() | ||
{ | ||
_params.bus_address.set_default(MLX90614_I2CDEFAULTADDR); | ||
|
||
_dev = std::move(hal.i2c_mgr->get_device(_params.bus, _params.bus_address)); | ||
if (!_dev) { | ||
return; | ||
} | ||
|
||
WITH_SEMAPHORE(_dev->get_semaphore()); | ||
|
||
_dev->register_periodic_callback(50 * AP_USEC_PER_MSEC, | ||
FUNCTOR_BIND_MEMBER(&AP_TemperatureSensor_MLX90614::_timer, void)); | ||
} | ||
|
||
|
||
void AP_TemperatureSensor_MLX90614::_timer() | ||
{ | ||
const uint16_t _crude_value = read_data(MLX90614_TA); | ||
|
||
if (_crude_value == 0) { | ||
return; | ||
} | ||
|
||
WITH_SEMAPHORE(_dev->get_semaphore()); | ||
|
||
// temp * 0.02 - 273.15 = degrees, temp * 0.02 is temperature in kelvin | ||
const float tmp = KELVIN_TO_C(_crude_value) * 0.02; | ||
set_temperature(tmp); | ||
} | ||
|
||
|
||
|
||
uint16_t AP_TemperatureSensor_MLX90614::read_data(uint8_t cmd) | ||
{ | ||
uint8_t val[3]; | ||
|
||
if (!_dev->transfer(&cmd, 1, val, 3)) { | ||
return 0; | ||
} | ||
return UINT16_VALUE(val[1],val[0]); | ||
} | ||
#endif // AP_TEMPERATURE_SENSOR_MLX90614_ENABLED |
24 changes: 24 additions & 0 deletions
24
libraries/AP_TemperatureSensor/AP_TemperatureSensor_MLX90614.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#pragma once | ||
|
||
#include "AP_TemperatureSensor_Backend.h" | ||
|
||
#if AP_TEMPERATURE_SENSOR_MLX90614_ENABLED | ||
|
||
|
||
class AP_TemperatureSensor_MLX90614 : public AP_TemperatureSensor_Backend { | ||
using AP_TemperatureSensor_Backend::AP_TemperatureSensor_Backend; | ||
public: | ||
|
||
void init(void) override; | ||
|
||
void update() override {}; | ||
|
||
private: | ||
|
||
// update the temperature, called at 20Hz | ||
void _timer(void); | ||
|
||
uint16_t read_data(uint8_t cmd); | ||
uint16_t read_eeprom(uint8_t address) {return read_data(address | 0x20);}; | ||
}; | ||
#endif // AP_TEMPERATURE_SENSOR_MLX90614_ENABLED |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters