-
Notifications
You must be signed in to change notification settings - Fork 21.2k
AP_TemperatureSensor: Add support for MCP9808 I2C temperature sensor #33746
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lavleshpoyrekar
wants to merge
20
commits into
ArduPilot:master
Choose a base branch
from
lavleshpoyrekar:ap-temp-mcp9808
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
d549da9
AP_TemperatureSensor: add MCP9808 driver skeleton
e04821b
AP_TemperatureSensor: add MCP9808 temperature sensor driver
c271fce
AP_TemperatureSensor: add MCP9808 sensor header
ef2d714
AP_TemperatureSensor: add MCP9808 enable option
a55dd22
AP_TemperatureSensor: add MCP9808 backend registration
2259218
AP_TemperatureSensor: add MCP9808 sensor type parameter
2b3c598
AP_TemperatureSensor: Reformat MCP9808 backend
c9348b6
AP_TemperatureSensor: Remove unused GCS include
ed332cf
AP_TemperatureSensor: Add MCP9808 backend declaration
a011577
AP_TemperatureSensor: Include AP_TemperatureSensor_config.h
fe4552b
AP_TemperatureSensor: Add nullptr Check
935775a
AP_TemperatureSensor: Fix MCP9808 conversion timing
7e48b06
Tools: Add MCP9808 build option
8589dbf
SITL: add MCP9808 temperature sensor simulator
7bca473
SITL: add MCP9808 temperature sensor support
f34dd2a
Tools: add MCP9808 temperature sensor build option
00c639e
autotest: add MCP9808 temperature sensor autotest
4801419
AP_TemperatureSensor: add MCP9808 driver
2a48edd
Tools: fix build options whitespace
f9d28ee
ci: retrigger build
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
100 changes: 100 additions & 0 deletions
100
libraries/AP_TemperatureSensor/AP_TemperatureSensor_MCP9808.cpp
This file contains hidden or 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,100 @@ | ||
| /* | ||
| This program is free software: you can redistribute it and/or modify | ||
| it under the terms of the GNU General Public License as published by | ||
| the Free Software Foundation, either version 3 of the License, or | ||
| (at your option) any later version. | ||
|
|
||
| This program is distributed in the hope that it will be useful, | ||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| GNU General Public License for more details. | ||
|
|
||
| You should have received a copy of the GNU General Public License | ||
| along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| #include "AP_TemperatureSensor_config.h" | ||
|
|
||
| #if AP_TEMPERATURE_SENSOR_MCP9808_ENABLED | ||
|
|
||
| #include "AP_TemperatureSensor_MCP9808.h" | ||
| #include <AP_HAL/AP_HAL.h> | ||
| #include <AP_HAL/I2CDevice.h> | ||
| #include <AP_Math/AP_Math.h> | ||
|
|
||
| extern const AP_HAL::HAL &hal; | ||
|
|
||
| // MCP9808 register addresses | ||
| #define MCP9808_REG_AMBIENT_TEMP 0x05 // ambient temperature register | ||
| #define MCP9808_REG_RESOLUTION 0x08 // resolution register | ||
| // Resolution register - 0.25°C resolution (65ms conversion time) | ||
| #define MCP9808_RESOLUTION_VALUE 0x01 | ||
|
|
||
| void AP_TemperatureSensor_MCP9808::init() | ||
| { | ||
| _params.bus_address.set_default(AP_TEMPERATURE_SENSOR_MCP9808_DEFAULT_I2C_ADDR); | ||
| _dev = hal.i2c_mgr->get_device_ptr( _params.bus, _params.bus_address); | ||
| if (_dev == nullptr) { | ||
| return; | ||
| } | ||
| WITH_SEMAPHORE(_dev->get_semaphore()); | ||
| // Increase retries during startup | ||
| _dev->set_retries(10); | ||
| // Configure sensor resolution | ||
| if (!write_register8(MCP9808_REG_RESOLUTION, MCP9808_RESOLUTION_VALUE)) { | ||
| return; | ||
| } | ||
| // Reduce retries during normal operation | ||
| _dev->set_retries(3); | ||
| // Poll temperature at 10Hz (100ms) | ||
| _dev->register_periodic_callback(100 * AP_USEC_PER_MSEC, FUNCTOR_BIND_MEMBER(&AP_TemperatureSensor_MCP9808::_timer, void)); | ||
| } | ||
|
|
||
| void AP_TemperatureSensor_MCP9808::_timer(void) | ||
| { | ||
| uint16_t raw; | ||
| if (!read_registers(MCP9808_REG_AMBIENT_TEMP, raw)) { | ||
| return; | ||
| } | ||
| // MCP9808 temperature register format: | ||
| // bit15 : Alert flag | ||
| // bit14-13 : Temperature alert status | ||
| // bit12 : Sign bit | ||
| // bit11-0 : Temperature data | ||
| // | ||
| // Temperature register LSB is always 0.0625°C | ||
| int16_t temp_raw = raw & 0x1FFF; | ||
| // Sign extend negative temperature values | ||
| if (temp_raw & 0x1000) { | ||
| temp_raw |= 0xE000; | ||
| } | ||
| float temp = temp_raw * 0.0625f; | ||
| set_temperature(temp); | ||
| } | ||
|
|
||
| bool AP_TemperatureSensor_MCP9808::read_registers(uint8_t reg, uint16_t &value) const | ||
| { | ||
| uint8_t val[2]; | ||
| if (!_dev->transfer(®, 1, val, sizeof(val))) { | ||
| return false; | ||
| } | ||
| // MCP9808 registers are 16-bit big endian | ||
| value = UINT16_VALUE(val[0], val[1]); | ||
| return true; | ||
| } | ||
|
|
||
| bool AP_TemperatureSensor_MCP9808::write_register8(uint8_t reg, uint8_t value) const | ||
| { | ||
| // Registers are 8-bit big endian | ||
| uint8_t buf[2] = {reg, value}; | ||
| return _dev->transfer(buf, sizeof(buf), nullptr, 0); | ||
| } | ||
|
|
||
| bool AP_TemperatureSensor_MCP9808::write_register(uint8_t reg, uint16_t value) const | ||
| { | ||
| // Registers are 16-bit big endian | ||
| uint8_t buf[3] {reg, uint8_t(value >> 8), uint8_t(value & 0xFF)}; | ||
| return _dev->transfer(buf, sizeof(buf), nullptr, 0); | ||
| } | ||
|
|
||
| #endif // AP_TEMPERATURE_SENSOR_MCP9808_ENABLED | ||
55 changes: 55 additions & 0 deletions
55
libraries/AP_TemperatureSensor/AP_TemperatureSensor_MCP9808.h
This file contains hidden or 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,55 @@ | ||
| /* | ||
| This program is free software: you can redistribute it and/or modify | ||
| it under the terms of the GNU General Public License as published by | ||
| the Free Software Foundation, either version 3 of the License, or | ||
| (at your option) any later version. | ||
|
|
||
| This program is distributed in the hope that it will be useful, | ||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| GNU General Public License for more details. | ||
|
|
||
| You should have received a copy of the GNU General Public License | ||
| along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| /* | ||
| * I2C driver for Microchip MCP9808 high-accuracy digital temperature sensor. | ||
| * | ||
| * The MCP9808 is a digital temperature sensor with 0.0625°C resolution. | ||
| * It communicates over I2C and provides a 16-bit ambient temperature register. | ||
| */ | ||
|
|
||
| #pragma once | ||
| #include "AP_TemperatureSensor_Backend.h" | ||
|
|
||
| #if AP_TEMPERATURE_SENSOR_MCP9808_ENABLED | ||
|
|
||
| #ifndef AP_TEMPERATURE_SENSOR_MCP9808_DEFAULT_I2C_ADDR | ||
| #define AP_TEMPERATURE_SENSOR_MCP9808_DEFAULT_I2C_ADDR 0x18 | ||
| #endif | ||
|
|
||
|
|
||
| class AP_TemperatureSensor_MCP9808 : public AP_TemperatureSensor_Backend { | ||
| using AP_TemperatureSensor_Backend::AP_TemperatureSensor_Backend; | ||
| public: | ||
|
|
||
| __INITFUNC__ void init(void) override; | ||
|
|
||
| void update() override {}; | ||
|
|
||
| private: | ||
| // update the temperature, called at 20Hz | ||
| void _timer(void); | ||
|
|
||
| // read a 16-bit big-endian register | ||
| bool read_registers(uint8_t reg, uint16_t &value) const; | ||
|
|
||
| // write a 16-bit big-endian register | ||
| bool write_register(uint8_t reg, uint16_t value) const; | ||
|
|
||
| // write a 8-bit big-endian register | ||
| bool write_register8(uint8_t reg, uint8_t value) const; | ||
| }; | ||
|
|
||
| #endif // AP_TEMPERATURE_SENSOR_MCP9808_ENABLED |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,84 @@ | ||
| /* | ||
| * SITL simulation for MCP9808 temperature sensor | ||
| */ | ||
|
|
||
| #include "SIM_config.h" | ||
|
|
||
| #if AP_SIM_TEMPERATURE_MCP9808_ENABLED | ||
|
|
||
| #include "SIM_Temperature_MCP9808.h" | ||
| #include <AP_HAL/AP_HAL.h> | ||
| #include <AP_Math/AP_Math.h> | ||
| using namespace SITL; | ||
|
|
||
| void MCP9808::init() | ||
| { | ||
| set_debug(true); | ||
| // Configuration register | ||
| add_register("CONFIG", MCP9808DevReg::CONFIG, 2, I2CRegisters::RegMode::RDWR); | ||
| set_register(MCP9808DevReg::CONFIG, uint16_t(htobe16(0x0000))); | ||
| // Ambient temperature register | ||
| add_register("AMBIENT_TEMP", MCP9808DevReg::AMBIENT_TEMP, 2, I2CRegisters::RegMode::RDONLY); | ||
| // Resolution register | ||
| add_register("RESOLUTION", MCP9808DevReg::RESOLUTION, 1, I2CRegisters::RegMode::RDWR); | ||
| // Default power-up resolution | ||
| // 0x03 = 0.0625°C, 250 ms | ||
| set_register(MCP9808DevReg::RESOLUTION, uint8_t(0x03)); | ||
| } | ||
|
|
||
| void MCP9808::update(const Aircraft &aircraft) | ||
| { | ||
| const uint32_t now_ms = AP_HAL::millis(); | ||
| // Read current resolution selected by ArduPilot | ||
| uint8_t resolution; | ||
| get_reg_value(MCP9808DevReg::RESOLUTION, resolution); | ||
|
|
||
| uint32_t conversion_time_ms = 250; | ||
|
|
||
| switch (resolution) | ||
| { | ||
| case 0x00: | ||
| conversion_time_ms = 30; | ||
| break; | ||
|
|
||
| case 0x01: | ||
| conversion_time_ms = 65; | ||
| break; | ||
|
|
||
| case 0x02: | ||
| conversion_time_ms = 130; | ||
| break; | ||
|
|
||
| case 0x03: | ||
| default: | ||
| conversion_time_ms = 250; | ||
| break; | ||
| } | ||
|
|
||
| if ((now_ms - last_temperature_update_ms) < conversion_time_ms) { | ||
| return; | ||
| } | ||
|
|
||
| last_temperature_update_ms = now_ms; | ||
|
|
||
| const float period_s = 10.0f; // One complete cycle every 10 sec | ||
| const float phase = (2.0f * M_PI * (now_ms * 0.001f)) / period_s; | ||
| const float temperature = 60.0f + 35.0f * sinf(phase); | ||
|
|
||
| // MCP9808 resolution = 0.0625°C | ||
| int16_t raw_temperature = int16_t(temperature / 0.0625f); | ||
| uint16_t value = uint16_t(raw_temperature & 0x0FFF); | ||
|
|
||
| if (temperature < 0) { | ||
| value |= (1 << 12); | ||
| } | ||
| set_register( | ||
| MCP9808DevReg::AMBIENT_TEMP, uint16_t(htobe16(value))); | ||
| } | ||
|
|
||
| int MCP9808::rdwr(I2C::i2c_rdwr_ioctl_data *&data) | ||
| { | ||
| return I2CRegisters_ConfigurableLength::rdwr(data); | ||
| } | ||
|
|
||
| #endif // AP_SIM_TEMPERATURE_MCP9808_ENABLED |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.