Skip to content

Commit

Permalink
AP_TemperatureSensor: MLX90614 sensor added
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulOCR committed Apr 26, 2024
1 parent c40af01 commit 47e0fd0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 36 deletions.
31 changes: 14 additions & 17 deletions libraries/AP_TemperatureSensor/AP_TemperatureSensor_MLX90614.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,17 @@

extern const AP_HAL::HAL &hal;

#define MLX90614_I2CDEFAULTADDR 0x5A // Device default slave address
#define MLX90614_BROADCASTADDR 0 // Device broadcast slave address

void AP_TemperatureSensor_MLX90614::init()
{
// 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() {

if ((_params.bus_address != MLX90614_I2CDEFAULTADDR) ) {
_params.bus_address.set(MLX90614_I2CDEFAULTADDR);
Expand All @@ -23,35 +31,27 @@ void AP_TemperatureSensor_MLX90614::init()
if (!_dev) {
printf("MLX90614: Device is not ready\n");
return;

}


WITH_SEMAPHORE(_dev->get_semaphore());

_dev->register_periodic_callback(50 * AP_USEC_PER_MSEC,
FUNCTOR_BIND_MEMBER(&AP_TemperatureSensor_MLX90614::_timer, void));

printf("MLX90614: Device is ready\n");
}


void AP_TemperatureSensor_MLX90614::_timer()
{
void AP_TemperatureSensor_MLX90614::_timer() {

_crude_value = read_data(MLX90614_TA);

if(_crude_value > 0)
{
if(_crude_value > 0) {

WITH_SEMAPHORE(_dev->get_semaphore());

// temp * 0.02 - 273.15 = degrees, temp * 0.02 is temperature in kelvin
const float tmp = _crude_value * 0.02 - 273.15;
const float tmp = KELVIN_TO_C(_crude_value) * 0.02;
set_temperature(tmp);
}


}


Expand All @@ -63,9 +63,6 @@ uint16_t AP_TemperatureSensor_MLX90614::read_data(uint8_t cmd) {
{
return 0;
}
hal.scheduler->delay_microseconds(MLX90614_XDLY);

return UINT16_VALUE(val[1],val[0]);
}
#endif // AP_TEMPERATURE_SENSOR_MLX90614_ENABLED

#endif // AP_TEMPERATURE_SENSOR_MLX90614_ENABLED
19 changes: 1 addition & 18 deletions libraries/AP_TemperatureSensor/AP_TemperatureSensor_MLX90614.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,6 @@

#if AP_TEMPERATURE_SENSOR_MLX90614_ENABLED

#define MLX90614_I2CDEFAULTADDR 0x5A // Device default slave address
#define MLX90614_BROADCASTADDR 0 // Device broadcast slave address
#define MLX90614_CRC8POLY 7 // CRC polynomial = X8+X2+X1+1
#define MLX90614_XDLY 25 // Experimentally determined delay to prevent read

// 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


class AP_TemperatureSensor_MLX90614 : public AP_TemperatureSensor_Backend {
using AP_TemperatureSensor_Backend::AP_TemperatureSensor_Backend;
Expand All @@ -24,7 +12,6 @@ class AP_TemperatureSensor_MLX90614 : public AP_TemperatureSensor_Backend {

void update() override {};


private:

// Hold return value in _timer
Expand All @@ -35,10 +22,6 @@ class AP_TemperatureSensor_MLX90614 : public AP_TemperatureSensor_Backend {

uint16_t read_data(uint8_t cmd);


uint16_t read_eeprom(uint8_t address) {return read_data(address | 0x20);};

};


#endif
#endif // AP_TEMPERATURE_SENSOR_MLX90614_ENABLED
2 changes: 1 addition & 1 deletion modules/ChibiOS

0 comments on commit 47e0fd0

Please sign in to comment.