-
Notifications
You must be signed in to change notification settings - Fork 16
Added temp sensor #31
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,19 @@ | ||
| #include "LTC6811.h" | ||
| #include "LTC6810.h" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @meghanabaddipudireddy Instead of renaming/replacing the LTC6811 class with the LTC6810 class, could you keep them both but as separate files? This should be fairly straightforward since you still have the LTC6811 version in the main branch. Reasoning being that we will very likely want to test this TMP1075 code on an old spare fs-3 BMS board and acc board, which uses the LTC6811, but we'll use the LTC6810 for fs-4's bms, so we should be ready to use both at any time. |
||
|
|
||
| #include "mbed.h" | ||
| #include "rtos.h" | ||
|
|
||
| #include "LTC681xParallelBus.h" | ||
|
|
||
| LTC6811::LTC6811(LTC681xBus &bus, uint8_t id) : m_bus(bus), m_id(id) { | ||
| #define TMP1075_I2C_ADDR 0x48 | ||
| #define TMP1075_TEMP_REG 0x00 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These Instead, they should be made parameters to |
||
|
|
||
| LTC6810::LTC6810(LTC681xBus &bus, uint8_t id) : m_bus(bus), m_id(id) { | ||
| m_config = | ||
| Configuration{.gpio5 = GPIOOutputState::kPassive, | ||
| .gpio4 = GPIOOutputState::kPassive, | ||
| .gpio3 = GPIOOutputState::kPassive, | ||
| .gpio2 = GPIOOutputState::kPassive, | ||
| .gpio4 = GPIOOutputState::kHigh, | ||
| .gpio3 = GPIOOutputState::kHigh, | ||
| .gpio2 = GPIOOutputState::kHigh, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||
| .gpio1 = GPIOOutputState::kPassive, | ||
| .referencePowerOff = ReferencePowerOff::kAfterConversions, | ||
| .dischargeTimerEnabled = DischargeTimerEnable::kDisabled, | ||
|
|
@@ -21,7 +24,7 @@ LTC6811::LTC6811(LTC681xBus &bus, uint8_t id) : m_bus(bus), m_id(id) { | |
| .dischargeTimeout = DischargeTimeoutValue::kDisabled}; | ||
| } | ||
|
|
||
| void LTC6811::updateConfig() { | ||
| void LTC6810::updateConfig() { | ||
| // Create configuration data to write | ||
| uint8_t config[6]; | ||
| config[0] = (uint8_t) m_config.gpio5 << 7 | ||
|
|
@@ -45,25 +48,24 @@ void LTC6811::updateConfig() { | |
| m_bus.SendDataCommand(cmd, config); | ||
| } | ||
|
|
||
| LTC6811::Configuration <C6811::getConfig() { return m_config; } | ||
| LTC6810::Configuration <C6810::getConfig() { return m_config; } | ||
|
|
||
| uint16_t *LTC6811::getVoltages() { | ||
| uint16_t *LTC6810::getVoltages() { | ||
| auto cmd = StartCellVoltageADC(AdcMode::k7k, false, CellSelection::kAll); | ||
| m_bus.SendCommand(LTC681xBus::BuildAddressedBusCommand(cmd, m_id)); | ||
|
|
||
| // Wait 2 ms for ADC to finish | ||
| ThisThread::sleep_for(2ms); // TODO: Change | ||
|
|
||
| // 4 * (Register of 6 Bytes + PEC) | ||
| uint8_t rxbuf[8 * 4]; | ||
| uint8_t rxbuf[8 * 2]; | ||
|
|
||
| m_bus.SendReadCommand(LTC681xBus::BuildAddressedBusCommand(ReadCellVoltageGroupA(), m_id), rxbuf); | ||
| m_bus.SendReadCommand(LTC681xBus::BuildAddressedBusCommand(ReadCellVoltageGroupB(), m_id), rxbuf + 8); | ||
| m_bus.SendReadCommand(LTC681xBus::BuildAddressedBusCommand(ReadCellVoltageGroupC(), m_id), rxbuf + 16); | ||
| m_bus.SendReadCommand(LTC681xBus::BuildAddressedBusCommand(ReadCellVoltageGroupD(), m_id), rxbuf + 24); | ||
|
|
||
|
|
||
| // Voltage = val • 100μV | ||
| uint16_t *voltages = new uint16_t[12]; | ||
| uint16_t *voltages = new uint16_t[6]; | ||
| for (unsigned int i = 0; i < sizeof(rxbuf); i++) { | ||
| // Skip over PEC | ||
| if (i % 8 == 6 || i % 8 == 7) continue; | ||
|
|
@@ -78,7 +80,7 @@ uint16_t *LTC6811::getVoltages() { | |
| return voltages; | ||
| } | ||
|
|
||
| uint16_t *LTC6811::getGpio() { | ||
| uint16_t *LTC6810::getGpio() { | ||
| auto cmd = StartGpioADC(AdcMode::k7k, GpioSelection::kAll); | ||
| m_bus.SendCommand(LTC681xBus::BuildAddressedBusCommand(cmd, m_id)); | ||
|
|
||
|
|
@@ -106,7 +108,7 @@ uint16_t *LTC6811::getGpio() { | |
| return voltages; | ||
| } | ||
|
|
||
| uint16_t *LTC6811::getGpioPin(GpioSelection pin) { | ||
| uint16_t *LTC6810::getGpioPin(GpioSelection pin) { | ||
| auto cmd = StartGpioADC(AdcMode::k7k, pin); | ||
| m_bus.SendCommand(LTC681xBus::BuildAddressedBusCommand(cmd, m_id)); | ||
|
|
||
|
|
@@ -132,4 +134,81 @@ uint16_t *LTC6811::getGpioPin(GpioSelection pin) { | |
| } | ||
|
|
||
| return voltages; | ||
| } | ||
| } | ||
|
|
||
| void LTC6810::buildCOMMBytes(uint8_t icom, uint8_t fcom, uint8_t data, uint8_t *commBytes) { | ||
| // COMMn (even byte): Upper 4 bits = ICOM, Lower 4 bits = upper half of data | ||
| commBytes[0] = (icom << 4) | ((data >> 4) & 0x0F); | ||
|
|
||
| // COMMn+1 (odd byte): Upper 4 bits = lower half of data, Lower 4 bits = FCOM | ||
| commBytes[1] = ((data & 0x0F) << 4) | fcom; | ||
| } | ||
|
|
||
| float LTC6810::readTemperatureTMP1075() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Eventually we should probably try to decouple the TMP1075 I2C register logic from the LTC6811 I2C COMM group logic, in case we want to use the I2C for something else. But its alright for now, until we get it working |
||
| uint8_t commData[6]; | ||
| uint8_t rxData[8]; | ||
| uint8_t tempBytes[2]; | ||
|
|
||
| buildCOMMBytes(0x6, 0x0, (TMP1075_I2C_ADDR << 1) | 0x00, tempBytes); | ||
| commData[0] = tempBytes[0]; // COMM0 | ||
| commData[1] = tempBytes[1]; // COMM1 | ||
|
|
||
| buildCOMMBytes(0x0, 0x0, TMP1075_TEMP_REG, tempBytes); | ||
| commData[2] = tempBytes[0]; // COMM2 | ||
| commData[3] = tempBytes[1]; // COMM3 | ||
|
|
||
| buildCOMMBytes(0x1, 0x0, 0x00, tempBytes); | ||
| commData[4] = tempBytes[0]; // COMM4 | ||
| commData[5] = tempBytes[1]; // COMM5 | ||
|
|
||
| auto wrCmd = LTC681xBus::BuildAddressedBusCommand(WriteCommGroup(), m_id); | ||
| m_bus.SendDataCommand(wrCmd, commData); | ||
|
|
||
| auto stCmd = LTC681xBus::BuildAddressedBusCommand(StartComm(), m_id); | ||
| m_bus.SendCommand(stCmd); | ||
|
|
||
| ThisThread::sleep_for(3ms); | ||
|
|
||
| buildCOMMBytes(0x6, 0x0, (TMP1075_I2C_ADDR << 1) | 0x01, tempBytes); | ||
| commData[0] = tempBytes[0]; // COMM0 | ||
| commData[1] = tempBytes[1]; // COMM1 | ||
|
|
||
|
|
||
| buildCOMMBytes(0x0, 0x0, 0xFF, tempBytes); | ||
| commData[2] = tempBytes[0]; // COMM2 | ||
| commData[3] = tempBytes[1]; // COMM3 | ||
|
|
||
| buildCOMMBytes(0x0, 0x9, 0xFF, tempBytes); | ||
| commData[4] = tempBytes[0]; // COMM4 | ||
| commData[5] = tempBytes[1]; // COMM5 | ||
|
|
||
| m_bus.SendDataCommand(wrCmd, commData); | ||
| m_bus.SendCommand(stCmd); | ||
|
|
||
| ThisThread::sleep_for(3ms); | ||
|
|
||
| auto rdCmd = LTC681xBus::BuildAddressedBusCommand(ReadCommGroup(), m_id); | ||
| m_bus.SendReadCommand(rdCmd, rxData); | ||
|
|
||
| uint8_t tempMSB = ((rxData[2] & 0x0F) << 4) | ((rxData[3] >> 4) & 0x0F); | ||
|
|
||
| uint8_t tempLSB = ((rxData[4] & 0x0F) << 4) | ((rxData[5] >> 4) & 0x0F); | ||
|
|
||
| int16_t rawTemp = (tempMSB << 8) | tempLSB; | ||
|
|
||
| float temperature = (rawTemp >> 4) * 0.0625f; | ||
|
|
||
| return temperature; | ||
| } | ||
|
|
||
| bool LTC6810::verifyI2CStatus(uint8_t *rxData) { | ||
| uint8_t data1 = ((rxData[2] & 0x0F) << 4) | ((rxData[3] >> 4) & 0x0F); | ||
| uint8_t data2 = ((rxData[4] & 0x0F) << 4) | ((rxData[5] >> 4) & 0x0F); | ||
|
|
||
| if (data1 == 0xFF && data2 == 0xFF) { | ||
| return false; // No response from sensor | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
|
|
||
| #include <LTC681xParallelBus.h> | ||
|
|
||
| class LTC6811 { | ||
| class LTC6810 { | ||
| public: | ||
| enum class GPIOOutputState : uint8_t { | ||
| // States with pull up resistor | ||
|
|
@@ -20,8 +20,7 @@ class LTC6811 { | |
| enum class AdcModeOption : uint8_t { kDefault = 0, kLowSpeed = 1 }; | ||
| union DischargeState { | ||
| struct { | ||
| uint8_t cell12 : 1, cell11 : 1, cell10 : 1, cell9 : 1, cell8 : 1, | ||
| cell7 : 1, cell6 : 1, cell5 : 1, cell4 : 1, cell3 : 1, cell2 : 1, | ||
| uint8_t cell6 : 1, cell5 : 1, cell4 : 1, cell3 : 1, cell2 : 1, | ||
| cell1 : 1; | ||
| }; | ||
| uint16_t value; | ||
|
|
@@ -63,13 +62,16 @@ class LTC6811 { | |
| DischargeTimeoutValue dischargeTimeout; | ||
| }; | ||
|
|
||
| LTC6811(LTC681xBus &bus, uint8_t id); | ||
| LTC6810(LTC681xBus &bus, uint8_t id); | ||
| Configuration &getConfig(); | ||
| void updateConfig(); | ||
|
|
||
| uint16_t *getVoltages(); | ||
| uint16_t *getGpio(); | ||
| uint16_t *getGpioPin(GpioSelection pin); | ||
| void buildCOMMBytes(uint8_t icom, uint8_t fcom, uint8_t data, uint8_t *commBytes); | ||
| float readTemperatureTMP1075(); | ||
| bool verifyI2CStatus(uint8_t *rxData); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These functions should be |
||
|
|
||
| private: | ||
| LTC681xBus &m_bus; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See my comment below; the change to
GPIOOutputState::kHighgpio configs should be done here (in the constructor of BMSThread, where the LTC6811's are created) rather than in the default constructor of the LTC6811