-
Notifications
You must be signed in to change notification settings - Fork 0
Charger #1
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
Draft
smj-edison
wants to merge
18
commits into
main
Choose a base branch
from
charger
base: main
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.
Draft
Charger #1
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
7b0a9af
charging logic
potter4500 592d9bd
dangerous mode removed
potter4500 fa95cc0
cleaner charging logic
potter4500 d20b5d9
charger states and corrected charger control CAN msg
potter4500 2b73cf1
state of charge msg corrected and charger CAN msg feedback established
potter4500 30e5283
reorganize SOC lookup function and improved charging logic
potter4500 f0ad30f
balance modules on global min cell voltage
potter4500 9a5f0ea
charger temperature logic
potter4500 6517b80
changed CAN data msg send rate
potter4500 fa6ceed
Serial monitor debugging
potter4500 8d0126d
update temperature tolerances per rules
potter4500 ee55b67
charging fault sends msg to stop charger
potter4500 8f4fb6e
update shutdown pin and charge enable pin; reworked charging logic
potter4500 414bd49
charging status response CAN msg serial monitoring debugging
potter4500 cee4e7a
charger shutdown fixed
potter4500 b247a92
Charger debugging ONLY!!!
potter4500 72d12aa
charge enable removed
potter4500 c3f3fdc
debugging tools
potter4500 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| #pragma once | ||
|
|
||
| #include <cstddef> | ||
| #include <cstdint> | ||
|
|
||
| // SOC : State of charge | ||
| // Human readable state of the accumulator because the charging logic | ||
| // uses only the min cell voltage or the max cell voltage in the pack | ||
|
|
||
| namespace soclookuptable { | ||
| constexpr uint8_t kNumLookUpPoints = 100; | ||
|
|
||
| constexpr float kVoltageTable[kNumLookUpPoints] = { | ||
| 3150.0f, 3159.6f, 3169.2f, 3178.8f, 3188.4f, 3198.0f, 3207.6f, 3217.2f, | ||
| 3226.8f, 3236.4f, 3246.0f, 3255.6f, 3265.2f, 3274.7f, 3284.3f, 3293.9f, | ||
| 3303.5f, 3313.1f, 3322.7f, 3332.3f, 3341.9f, 3351.5f, 3361.1f, 3370.7f, | ||
| 3380.3f, 3389.9f, 3399.5f, 3409.1f, 3418.7f, 3428.3f, 3437.9f, 3447.5f, | ||
| 3457.1f, 3466.7f, 3476.3f, 3485.9f, 3495.5f, 3505.1f, 3514.6f, 3524.2f, | ||
| 3533.8f, 3543.4f, 3553.0f, 3562.6f, 3572.2f, 3581.8f, 3591.4f, 3601.0f, | ||
| 3610.6f, 3620.2f, 3629.8f, 3639.4f, 3649.0f, 3658.6f, 3668.2f, 3677.8f, | ||
| 3687.4f, 3697.0f, 3706.6f, 3716.2f, 3725.8f, 3735.4f, 3744.9f, 3754.5f, | ||
| 3764.1f, 3773.7f, 3783.3f, 3792.9f, 3802.5f, 3812.1f, 3821.7f, 3831.3f, | ||
| 3840.9f, 3850.5f, 3860.1f, 3869.7f, 3879.3f, 3888.9f, 3898.5f, 3908.1f, | ||
| 3917.7f, 3927.3f, 3936.9f, 3946.5f, 3956.1f, 3965.7f, 3975.3f, 3984.8f, | ||
| 3994.4f, 4004.0f, 4013.6f, 4023.2f, 4032.8f, 4042.4f, 4052.0f, 4061.6f, | ||
| 4071.2f, 4080.8f, 4090.4f, 4100.0f | ||
| }; | ||
|
|
||
| constexpr float kSocTable[kNumLookUpPoints] = { | ||
| 0.0f, 0.1f, 0.2f, 0.3f, 0.4f, 0.5f, 0.6f, 0.8f, 0.9f, 1.1f, | ||
| 1.3f, 1.5f, 1.7f, 1.9f, 2.1f, 2.4f, 2.7f, 2.9f, 3.3f, 3.6f, | ||
| 4.0f, 4.4f, 4.8f, 5.3f, 5.8f, 6.3f, 6.8f, 7.5f, 8.1f, 8.8f, | ||
| 9.5f, 10.3f, 11.2f, 12.0f, 13.0f, 14.0f, 15.0f, 16.2f, 17.3f, 18.6f, | ||
| 19.9f, 21.3f, 22.7f, 24.2f, 25.8f, 27.4f, 29.1f, 30.8f, 32.6f, 34.5f, | ||
| 36.4f, 38.3f, 40.3f, 42.3f, 44.4f, 46.4f, 48.5f, 50.6f, 52.7f, 54.8f, | ||
| 56.9f, 58.9f, 61.0f, 63.0f, 64.9f, 66.9f, 68.7f, 70.6f, 72.4f, 74.1f, | ||
| 75.8f, 77.4f, 78.9f, 80.4f, 81.8f, 83.1f, 84.4f, 85.6f, 86.8f, 87.9f, | ||
| 88.9f, 89.9f, 90.8f, 91.7f, 92.5f, 93.3f, 94.0f, 94.7f, 95.3f, 95.9f, | ||
| 96.5f, 97.0f, 97.5f, 97.9f, 98.3f, 98.7f, 99.1f, 99.4f, 99.7f, 100.0f, | ||
| }; | ||
|
|
||
| } // State of charge lookup table | ||
|
|
||
| // Maps the first table (voltages) to the second table (percentage) | ||
| float ReadBMS::lookUpSOC(uint16_t cellMv) { | ||
| // check if value is out of range | ||
| if (cellMv <= soclookuptable::kVoltageTable[0]) { | ||
| return soclookuptable::kSocTable[0]; | ||
| } | ||
| if (cellMv >= soclookuptable::kVoltageTable[soclookuptable::kNumLookUpPoints - 1]) { | ||
| return soclookuptable::kSocTable[soclookuptable::kNumLookUpPoints - 1]; | ||
| } | ||
|
|
||
| // linear interpolation to map between the two tables | ||
| for (size_t i=0; i < soclookuptable::kNumLookUpPoints - 1; i++) { | ||
| if (cellMv >= soclookuptable::kVoltageTable[i] && cellMv <= soclookuptable::kVoltageTable[i+1]) { | ||
| float v1 = soclookuptable::kVoltageTable[i]; | ||
| float v2 = soclookuptable::kVoltageTable[i + 1]; | ||
| float soc1 = soclookuptable::kSocTable[i]; | ||
| float soc2 = soclookuptable::kSocTable[i + 1]; | ||
|
|
||
| return static_cast<float>((soc1 * (v1 - cellMv) + soc2 * (cellMv - v2)) / (v1 - v2)); | ||
| } | ||
| } | ||
|
|
||
| // fallback | ||
| return 0.0; | ||
| } | ||
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 |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| #include "BMSControl.h" | ||
| #include "SOCLookUpTable.h" | ||
|
|
||
| const SPISettings ReadBMS::kBmsSpiSettings(1000000, MSBFIRST, SPI_MODE0); | ||
|
|
||
|
|
@@ -77,9 +78,31 @@ void ReadBMS::updateBalancing(bool enabled) { | |
| return; | ||
| } | ||
|
|
||
| // update global pack min cell voltage | ||
| uint16_t lowestPackCellMv = UINT16_MAX; | ||
| for (std::size_t moduleIndex = 0; moduleIndex < kModuleCount; ++moduleIndex) { | ||
| ModuleReadings& module = pollData_.modules[moduleIndex]; | ||
| module.balanceMask = balanceMaskForModule(module, module.balanceMask); | ||
|
|
||
| // pass disconnected cells for now, but we need to look into this | ||
| if (!module.connected || !module.cellDataValid) { | ||
| continue; | ||
| } | ||
|
|
||
| for (uint16_t cellMv : module.cellVoltages) { | ||
| // pass invalid cells for now, but we need to look this later | ||
| if (cellMv == adbms6830::BMSInterface::kInvalidCellValue) { | ||
| continue; | ||
| } | ||
|
|
||
| if (cellMv < lowestPackCellMv) { | ||
| lowestPackCellMv = cellMv; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for (std::size_t moduleIndex = 0; moduleIndex < kModuleCount; ++moduleIndex) { | ||
| ModuleReadings& module = pollData_.modules[moduleIndex]; | ||
| module.balanceMask = balanceMaskForModule(module, module.balanceMask, lowestPackCellMv); | ||
| } | ||
|
|
||
| for (std::size_t moduleIndex = 0; moduleIndex < kModuleCount; ++moduleIndex) { | ||
|
|
@@ -311,15 +334,19 @@ void ReadBMS::logModuleSiliconIds(const LogSnapshot& snapshot, Stream& stream) { | |
| stream.println(); | ||
| } | ||
|
|
||
| uint16_t ReadBMS::balanceMaskForModule(const ModuleReadings& module, uint16_t currentMask) { | ||
| uint16_t ReadBMS::balanceMaskForModule(const ModuleReadings& module, uint16_t currentMask, uint16_t lowestPackCellMv) { | ||
| if (!module.connected || !module.cellDataValid) { | ||
| return 0; | ||
| } | ||
| if (boardThermistorFaulted(module)) { | ||
| return 0; | ||
| } | ||
|
|
||
| uint16_t minCellMv = UINT16_MAX; | ||
| // grab the min cell voltage for the module, this is the balance target | ||
| // thus if the pack min cell voltage is inserted here instead, then each | ||
| // module will balance globally to each other | ||
| #ifdef BALANCE_MODULES_INDEPENDENTLY | ||
| uint16_t targetCellMv = UINT16_MAX; | ||
| for (uint16_t cellMv : module.cellVoltages) { | ||
| if (cellMv == adbms6830::BMSInterface::kInvalidCellValue) { | ||
| return 0; | ||
|
|
@@ -335,11 +362,19 @@ uint16_t ReadBMS::balanceMaskForModule(const ModuleReadings& module, uint16_t cu | |
| if (minCellMv == UINT16_MAX) { | ||
| return 0; | ||
| } | ||
| #else | ||
| // Balance modules to the global min cell voltage | ||
| uint16_t targetCellMv = lowestPackCellMv; | ||
| if (targetCellMv == UINT16_MAX) { | ||
| return 0; | ||
| } | ||
| #endif | ||
|
|
||
| uint16_t desiredMask = 0; | ||
| for (std::size_t cellIndex = 0; cellIndex < module.cellVoltages.size(); ++cellIndex) { | ||
| const uint16_t cellMv = module.cellVoltages[cellIndex]; | ||
| const uint16_t deltaMv = static_cast<uint16_t>(cellMv - minCellMv); | ||
| // calculate delta voltage from the target cell voltage pulled previously | ||
| const uint16_t deltaMv = static_cast<uint16_t>(cellMv - targetCellMv); | ||
| const uint16_t cellBit = static_cast<uint16_t>(1u << cellIndex); | ||
| const bool currentlyBalancing = (currentMask & cellBit) != 0u; | ||
| if (currentlyBalancing) { | ||
|
|
@@ -691,3 +726,45 @@ void ReadBMS::copyModuleReadings(ModuleReadings& destination, | |
| destination.cellVoltages = source.cellVoltages; | ||
| destination.thermistorTempsC = source.thermistorTempsC; | ||
| } | ||
|
|
||
| // Update State of charge struct data | ||
| ReadBMS::StateOfCharge ReadBMS::pollSOC() { | ||
| uint16_t lowestCellMv = UINT16_MAX; | ||
| uint16_t highestCellMv = 0; | ||
| float minStateOfCharge = 0.0f; | ||
| float maxStateOfCharge = 0.0f; | ||
|
|
||
| // get lastest module readings | ||
| updatePollData(); | ||
|
|
||
| for (const ReadBMS::ModuleReadings &module : pollData_.modules) | ||
| { | ||
| if (!module.connected || !module.cellDataValid) { | ||
| continue; | ||
| } | ||
|
Comment on lines
+742
to
+744
Author
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. Is this intentional to ignore bad cells? Might want to add a comment explaining why it's okay to ignore. |
||
|
|
||
| for (uint16_t cellMv : module.cellVoltages) { | ||
| if (cellMv == adbms6830::BMSInterface::kInvalidCellValue) { | ||
| continue; | ||
| } | ||
|
|
||
| if (cellMv < lowestCellMv) { | ||
| lowestCellMv = cellMv; | ||
| } | ||
|
|
||
| if (cellMv > highestCellMv) { | ||
| highestCellMv = cellMv; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| minStateOfCharge = lookUpSOC(lowestCellMv); | ||
| maxStateOfCharge = lookUpSOC(highestCellMv); | ||
|
|
||
| StateOfCharge soc{}; | ||
| soc.minSOC = minStateOfCharge; | ||
| soc.maxSOC = maxStateOfCharge; | ||
| soc.minCellMv = lowestCellMv; | ||
| soc.maxCellMv = highestCellMv; | ||
| return soc; | ||
| } | ||
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
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.