Skip to content
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

Add ICM20948 temperature sensor support #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Linux/RTIMULibDrive11/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ DEPS = $(RTIMULIBPATH)/RTMath.h \
$(RTIMULIBPATH)/IMUDrivers/RTHumidityDefs.h \
$(RTIMULIBPATH)/IMUDrivers/RTHumidityHTS221.h \
$(RTIMULIBPATH)/IMUDrivers/RTHumidityHTU21D.h \
$(RTIMULIBPATH)/IMUDrivers/RTHumidityICM20948.h \

OBJECTS = objects/RTIMULibDrive11.o \
objects/RTMath.o \
Expand Down Expand Up @@ -135,7 +136,7 @@ clean:
$(OBJECTS_DIR)%.o : $(RTIMULIBPATH)/%.cpp $(DEPS)
@$(CHK_DIR_EXISTS) objects/ || $(MKDIR) objects/
$(CXX) -c -o $@ $< $(CFLAGS) $(INCPATH)

$(OBJECTS_DIR)%.o : $(RTIMULIBPATH)/IMUDrivers/%.cpp $(DEPS)
@$(CHK_DIR_EXISTS) objects/ || $(MKDIR) objects/
$(CXX) -c -o $@ $< $(CFLAGS) $(INCPATH)
Expand Down
2 changes: 2 additions & 0 deletions Linux/python/PyRTIMU_Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,10 @@ int Unpack_VEC3(const PyObject* val, RTVector3& dest)

static PyGetSetDef RTIMU_Settings_getset[] = {
RTIMU_PARAM_INT(IMUType, m_imuType),
RTIMU_PARAM_INT(HumidityType, m_humidityType),
RTIMU_PARAM_INT(FusionType, m_fusionType),
RTIMU_PARAM_INT(I2CAddress, m_I2CSlaveAddress),
RTIMU_PARAM_INT(HumidityI2CAddress, m_I2CHumidityAddress),
RTIMU_PARAM_INT(I2CBus, m_I2CBus),
RTIMU_PARAM_INT(CompassCalValid, m_compassCalValid),
RTIMU_PARAM_VEC3(CompassCalMin, m_compassCalMin),
Expand Down
3 changes: 2 additions & 1 deletion Linux/python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,12 @@
"IMUDrivers/RTHumidity.cpp",
"IMUDrivers/RTHumidityHTS221.cpp",
"IMUDrivers/RTHumidityHTU21D.cpp",
"IMUDrivers/RTHumidityICM20948.cpp",
]
RTIMU_sourcedir = "../../RTIMULib"

mod = Extension('RTIMU',
sources = ['PyRTIMU.cpp', 'PyRTIMU_Settings.cpp', 'PyRTIMU_RTIMU.cpp',
sources = ['PyRTIMU.cpp', 'PyRTIMU_Settings.cpp', 'PyRTIMU_RTIMU.cpp',
'PyRTIMU_RTPressure.cpp', 'PyRTIMU_RTHumidity.cpp'] +
[ os.path.join(RTIMU_sourcedir, sr) for sr in RTIMU_sources],
include_dirs = [RTIMU_sourcedir],
Expand Down
1 change: 1 addition & 0 deletions RTIMULib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ SET(LIBRTIMU_SRCS
IMUDrivers/RTHumidityHTS221.cpp
IMUDrivers/RTHumidityHTU21D.cpp
IMUDrivers/RTIMUHMC5883LADXL345.cpp
IMUDrivers/RTHumidityICM20498.cpp
)

IF(WIN32 AND QT5)
Expand Down
4 changes: 4 additions & 0 deletions RTIMULib/IMUDrivers/RTHumidity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include "RTHumidityHTS221.h"
#include "RTHumidityHTU21D.h"
#include "RTHumidityICM20948.h"

RTHumidity *RTHumidity::createHumidity(RTIMUSettings *settings)
{
Expand All @@ -36,6 +37,9 @@ RTHumidity *RTHumidity::createHumidity(RTIMUSettings *settings)
case RTHUMIDITY_TYPE_HTU21D:
return new RTHumidityHTU21D(settings);

case RTHUMIDITY_TYPE_ICM20948:
return new RTHumidityICM20948(settings);

case RTHUMIDITY_TYPE_AUTODISCOVER:
if (settings->discoverHumidity(settings->m_humidityType, settings->m_I2CHumidityAddress)) {
settings->saveSettings();
Expand Down
17 changes: 17 additions & 0 deletions RTIMULib/IMUDrivers/RTHumidityDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#define RTHUMIDITY_TYPE_NULL 1 // if no physical hardware
#define RTHUMIDITY_TYPE_HTS221 2 // HTS221
#define RTHUMIDITY_TYPE_HTU21D 3 // HTU21D
#define RTHUMIDITY_TYPE_ICM20948 4 // ICM20948

//----------------------------------------------------------
//
Expand Down Expand Up @@ -79,4 +80,20 @@
#define HTU21D_READ_USER_REG 0xe7
#define HTU21D_CMD_SOFT_RESET 0xfe

// ICM20948 I2C Slave Addresses

#define ICM20948_ADDRESS0 0x68
#define ICM20948_ADDRESS1 0x69


// Register map

#define ICM20948_TEMP_OUT_H 0x39
#define ICM20948_TEMP_OUT_L 0x3A

// Constants

#define ICM20948_ROOM_TEMP_OFFSET 0
#define ICM20948_TEMP_SENSITIVITY 333.87

#endif // _RTHUMIDITYDEFS_H
67 changes: 67 additions & 0 deletions RTIMULib/IMUDrivers/RTHumidityICM20948.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
////////////////////////////////////////////////////////////////////////////
//
// This file is part of RTIMULib
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
// Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#include "RTHumidityICM20948.h"
#include "RTHumidityDefs.h"

RTHumidityICM20948::RTHumidityICM20948(RTIMUSettings *settings) : RTHumidity(settings)
{
m_temperatureValid = false;
}

RTHumidityICM20948::~RTHumidityICM20948()
{
}

bool RTHumidityICM20948::humidityInit()
{
m_humidityAddr = m_settings->m_I2CSlaveAddress;

return true;
}


bool RTHumidityICM20948::humidityRead(RTIMU_DATA& data)
{
unsigned char temp_out_h, temp_out_l;
uint16_t temp_out;


data.humidityValid = false;
data.humidity = 0;

if (!m_settings->HALRead(m_humidityAddr, ICM20948_TEMP_OUT_H, 1, &temp_out_h, "Failed to read ICM20948 temperature high"))
return false;

if (!m_settings->HALRead(m_humidityAddr, ICM20948_TEMP_OUT_L, 1, &temp_out_l, "Failed to read ICM20948 temperature low"))
return false;

temp_out = (temp_out_h << 8) | (temp_out_l);


m_temperature = ((temp_out - ICM20948_ROOM_TEMP_OFFSET) / ICM20948_TEMP_SENSITIVITY) + 21.0;
m_temperatureValid = true;

data.temperatureValid = m_temperatureValid;
data.temperature = m_temperature;

return true;
}
49 changes: 49 additions & 0 deletions RTIMULib/IMUDrivers/RTHumidityICM20948.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
////////////////////////////////////////////////////////////////////////////
//
// This file is part of RTIMULib
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
// Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#ifndef _RTHUMIDITYICM20948_H_
#define _RTHUMIDITYICM20948_H_

#include "RTHumidity.h"

class RTIMUSettings;

class RTHumidityICM20948 : public RTHumidity
{
public:
RTHumidityICM20948(RTIMUSettings *settings);
~RTHumidityICM20948();

virtual const char *humidityName() { return "ICM20948"; }
virtual int humidityType() { return RTHUMIDITY_TYPE_ICM20948; }
virtual bool humidityInit();
virtual bool humidityRead(RTIMU_DATA& data);

private:
unsigned char m_humidityAddr; // I2C address

RTFLOAT m_temperature; // the current temperature
bool m_temperatureValid;

};

#endif // _RTHUMIDITYICM20948_H_

1 change: 1 addition & 0 deletions RTIMULib/RTIMULib.pri
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ HEADERS += $$PWD/RTIMULib.h \
$$PWD/IMUDrivers/RTHumidityDefs.h \
$$PWD/IMUDrivers/RTHumidityHTS221.h \
$$PWD/IMUDrivers/RTHumidityHTU21D.h \
$$PWD/IMUDrivers/RTHumidityICM20948.h \

SOURCES += $$PWD/RTMath.cpp \
$$PWD/RTIMUHal.cpp \
Expand Down
19 changes: 17 additions & 2 deletions RTIMULib/RTIMUSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

#include "IMUDrivers/RTHumidityHTS221.h"
#include "IMUDrivers/RTHumidityHTU21D.h"
#include "IMUDrivers/RTHumidityICM20948.h"

#define RATE_TIMER_INTERVAL 2

Expand Down Expand Up @@ -549,6 +550,20 @@ bool RTIMUSettings::discoverHumidity(int& humidityType, unsigned char& humidityA
return true;
}

if (Detect_ICM20948(ICM20948_ADDRESS0)) {
humidityType = RTHUMIDITY_TYPE_ICM20948;
humidityAddress = ICM20948_ADDRESS0;
HAL_INFO("Detected ICM20948 at standard address\n");
return true;
}

if (Detect_ICM20948(ICM20948_ADDRESS1)) {
humidityType = RTHUMIDITY_TYPE_ICM20948;
humidityAddress = ICM20948_ADDRESS1;
HAL_INFO("Detected ICM20948 at option address\n");
return true;
}

}
HAL_ERROR("No humidity sensor detected\n");
return false;
Expand Down Expand Up @@ -705,7 +720,7 @@ void RTIMUSettings::setDefaults()

m_LSM9DS1CompassSampleRate = LSM9DS1_COMPASS_SAMPLERATE_20;
m_LSM9DS1CompassFsr = LSM9DS1_COMPASS_FSR_4;

// BMX055 defaults

m_BMX055GyroSampleRate = BMX055_GYRO_SAMPLERATE_100_32;
Expand Down Expand Up @@ -2048,7 +2063,7 @@ bool RTIMUSettings::Detect_ICM20948(uint8_t slaveAddress)
uint8_t bank;
if (!HALRead(slaveAddress, ICM20948_REG_BANK_SEL, 1, &bank, ""))
return false;

if(bank != ICM20948_BANK0) {
// wrong bank.. We have to write the bank 0 here, so far I don't know any
// alternate devices which use 0x7f address for anything meaningful, but if
Expand Down