Skip to content

Commit 8be7720

Browse files
authored
[sysid] Fix crash on partially empty raw data (#8572)
Fixes #8570.
1 parent 21b5389 commit 8be7720

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

sysid/src/main/native/cpp/analysis/AnalysisManager.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ static double Lerp(units::second_t time,
5757
*/
5858
static std::vector<PreparedData> ConvertToPrepared(const MotorData& data) {
5959
std::vector<PreparedData> prepared;
60-
// assume we've selected down to a single contiguous run by this point
60+
61+
// Assume we've selected down to a single contiguous run by this point
6162
auto run = data.runs[0];
6263

6364
for (int i = 0; i < static_cast<int>(run.voltage.size()) - 1; ++i) {
@@ -101,7 +102,7 @@ static void CopyRawData(wpi::StringMap<MotorData>* dataset) {
101102
}
102103

103104
/**
104-
* Assigns the combines the various datasets into a single one for analysis.
105+
* Combines the various datasets into a single one for analysis.
105106
*
106107
* @param slowForward The slow forward dataset
107108
* @param slowBackward The slow backward dataset
@@ -127,6 +128,28 @@ void AnalysisManager::PrepareGeneralData() {
127128
// Convert data to PreparedData structs
128129
for (auto& it : m_data.motorData) {
129130
auto key = it.first;
131+
132+
// Assume we've selected down to a single contiguous run by this point
133+
auto run = m_data.motorData[key].runs[0];
134+
135+
// Ensure data has at least two samples in it or linear interpolation within
136+
// ConvertToPrepared() will fail
137+
if (run.voltage.size() < 2) {
138+
throw sysid::InvalidDataError(fmt::format(
139+
"{} data has {} voltage samples and at least 2 are required.", key,
140+
run.voltage.size()));
141+
}
142+
if (run.position.size() < 2) {
143+
throw sysid::InvalidDataError(fmt::format(
144+
"{} data has {} position samples and at least 2 are required.", key,
145+
run.position.size()));
146+
}
147+
if (run.velocity.size() < 2) {
148+
throw sysid::InvalidDataError(fmt::format(
149+
"{} data has {} velocity samples and at least 2 are required.", key,
150+
run.velocity.size()));
151+
}
152+
130153
preparedData[key] = ConvertToPrepared(m_data.motorData[key]);
131154
WPI_INFO(m_logger, "SAMPLES {}", preparedData[key].size());
132155
}

0 commit comments

Comments
 (0)