From f4ab904d499929028d6a39b91f3d8bad2ef24be6 Mon Sep 17 00:00:00 2001 From: BrendonBuilds Date: Wed, 2 Nov 2022 06:42:15 -0700 Subject: [PATCH 1/4] Store average accelerometer readings in Object Model Store the average value of each axis of the last accelerometer run in the Object Model --- src/Accelerometers/Accelerometers.cpp | 39 +++++++++++++++++++++++++++ src/Accelerometers/Accelerometers.h | 1 + src/CAN/ExpansionManager.cpp | 24 ++++++++++++++++- src/CAN/ExpansionManager.h | 4 ++- src/Platform/Platform.cpp | 10 ++++++- 5 files changed, 75 insertions(+), 3 deletions(-) diff --git a/src/Accelerometers/Accelerometers.cpp b/src/Accelerometers/Accelerometers.cpp index 7b1835a8b5..dcfa3a1b41 100644 --- a/src/Accelerometers/Accelerometers.cpp +++ b/src/Accelerometers/Accelerometers.cpp @@ -68,6 +68,8 @@ static volatile uint32_t numSamplesRequested; static uint8_t resolution = DefaultResolution; static uint8_t orientation = 20; // +Z -> +Z, +X -> +X static volatile uint8_t axesRequested; +constexpr size_t NumAxis = 3; +static float lastRunAverages[NumAxis] = {0.0f, 0.0f, 0.0f}; static FileStore* volatile accelerometerFile = nullptr; // this is non-null when the accelerometer is running, null otherwise static unsigned int numLocalRunsCompleted = 0; static unsigned int lastRunNumSamplesReceived = 0; @@ -87,6 +89,15 @@ static void AddLocalAccelerometerRun(unsigned int numDataPoints) noexcept reprap.BoardsUpdated(); } +static void AddLocalAccelerometerAverages(float averages[]) noexcept +{ + for(unsigned int axis = 0;axis < NumAxis;++axis) + { + lastRunAverages[axis] = averages[axis]; + } + reprap.BoardsUpdated(); +} + static uint8_t TranslateAxes(uint8_t axes) noexcept { uint8_t rslt = 0; @@ -115,6 +126,7 @@ static uint8_t TranslateAxes(uint8_t axes) noexcept const uint16_t mask = (1u << resolution) - 1; const int decimalPlaces = GetDecimalPlaces(resolution); bool recordFailedStart = false; + float accumulatedSamples[NumAxis] = {0.0f, 0.0f, 0.0f}; if (accelerometer->StartCollecting(TranslateAxes(axesRequested))) { @@ -180,6 +192,9 @@ static uint8_t TranslateAxes(uint8_t axes) noexcept // Append it to the buffer temp.catf(",%.*f", decimalPlaces, (double)fVal); + + // accumulate the values so they can be averaged later + accumulatedSamples[axis] += fVal; } } @@ -216,6 +231,13 @@ static uint8_t TranslateAxes(uint8_t axes) noexcept f->Truncate(); // truncate the file in case we didn't write all the preallocated space f->Close(); AddLocalAccelerometerRun(samplesWritten); + + // find the average value for each axis + for(unsigned int axis = 0;axis < NumAxis;++axis) + { + accumulatedSamples[axis] /= float(samplesWritten); + } + AddLocalAccelerometerAverages(accumulatedSamples); } accelerometer->StopCollecting(); @@ -532,6 +554,11 @@ bool Accelerometers::HasLocalAccelerometer() noexcept return accelerometer != nullptr; } +float Accelerometers::GetLocalAccelerometerLastRunAverage(const int &axis) noexcept +{ + return lastRunAverages[axis]; +} + unsigned int Accelerometers::GetLocalAccelerometerDataPoints() noexcept { return lastRunNumSamplesReceived; @@ -592,6 +619,8 @@ void Accelerometers::ProcessReceivedData(CanAddress src, const CanMessageAcceler const unsigned int receivedResolution = msg.bitsPerSampleMinusOne + 1; const uint16_t mask = (1u << receivedResolution) - 1; const int decimalPlaces = GetDecimalPlaces(receivedResolution); + float accumulatedSamples[NumAxis] = {0.0f, 0.0f, 0.0f}; + if (msg.overflowed) { ++numRemoteOverflows; @@ -632,6 +661,9 @@ void Accelerometers::ProcessReceivedData(CanAddress src, const CanMessageAcceler // Append it to the buffer temp.catf(",%.*f", decimalPlaces, (double)fVal); + + // accumulate the values so they can be averaged later + accumulatedSamples[axis] += fVal; } temp.cat('\n'); @@ -648,6 +680,13 @@ void Accelerometers::ProcessReceivedData(CanAddress src, const CanMessageAcceler f->Close(); accelerometerFile = nullptr; reprap.GetExpansion().AddAccelerometerRun(src, expectedRemoteSampleNumber); + + // find the average value for each axis + for (unsigned int axis = 0; axis < NumAxis; ++axis) + { + accumulatedSamples[axis] /= float(msg.numSamples); + } + reprap.GetExpansion().AddAccelerometerLastRunAverages(src, accumulatedSamples, NumAxis); } } } diff --git a/src/Accelerometers/Accelerometers.h b/src/Accelerometers/Accelerometers.h index bc45a42af3..9c3c6d98e0 100644 --- a/src/Accelerometers/Accelerometers.h +++ b/src/Accelerometers/Accelerometers.h @@ -23,6 +23,7 @@ class CanMessageAccelerometerData; namespace Accelerometers { bool HasLocalAccelerometer() noexcept; + float GetLocalAccelerometerLastRunAverage(const int& axis) noexcept; unsigned int GetLocalAccelerometerRuns() noexcept; unsigned int GetLocalAccelerometerDataPoints() noexcept; GCodeResult ConfigureAccelerometer(GCodeBuffer& gb, const StringRef& reply) THROWS(GCodeException); diff --git a/src/CAN/ExpansionManager.cpp b/src/CAN/ExpansionManager.cpp index 8e82cb0427..106948a0a9 100644 --- a/src/CAN/ExpansionManager.cpp +++ b/src/CAN/ExpansionManager.cpp @@ -24,6 +24,18 @@ #define OBJECT_MODEL_FUNC(...) OBJECT_MODEL_FUNC_BODY(ExpansionManager, __VA_ARGS__) #define OBJECT_MODEL_FUNC_IF(...) OBJECT_MODEL_FUNC_IF_BODY(ExpansionManager, __VA_ARGS__) +constexpr ObjectModelArrayTableEntry ExpansionManager::objectModelArrayTable[] = +{ + { + nullptr, // no lock needed + [] (const ObjectModel *self, const ObjectExplorationContext& context) noexcept -> size_t { return 3; }, + [] (const ObjectModel *self, ObjectExplorationContext& context) noexcept -> ExpressionValue + { return ExpressionValue(((const ExpansionManager*)self)->FindIndexedBoard(context.GetIndex(1)).accelerometerLastRunAverages[context.GetLastIndex()]); } + } +}; + +DEFINE_GET_OBJECT_MODEL_ARRAY_TABLE(ExpansionManager) + constexpr ObjectModelTableEntry ExpansionManager::objectModelTable[] = { // 0. boards[] members @@ -59,6 +71,7 @@ constexpr ObjectModelTableEntry ExpansionManager::objectModelTable[] = { "min", OBJECT_MODEL_FUNC(self->FindIndexedBoard(context.GetLastIndex()).v12.minimum, 1), ObjectModelEntryFlags::none }, // 4. accelerometer members + { "lastRunAverages", OBJECT_MODEL_FUNC_ARRAY(0), ObjectModelEntryFlags::none }, { "points", OBJECT_MODEL_FUNC((int32_t)self->FindIndexedBoard(context.GetLastIndex()).accelerometerLastRunDataPoints), ObjectModelEntryFlags::none }, { "runs", OBJECT_MODEL_FUNC((int32_t)self->FindIndexedBoard(context.GetLastIndex()).accelerometerRuns), ObjectModelEntryFlags::none }, @@ -74,7 +87,7 @@ constexpr uint8_t ExpansionManager::objectModelTableDescriptor[] = 3, // section 1: mcuTemp 3, // section 2: vIn 3, // section 3: v12 - 2, // section 4: accelerometer + 3, // section 4: accelerometer 2 // section 5: closed loop }; @@ -294,6 +307,15 @@ void ExpansionManager::UpdateFailed(CanAddress address) noexcept UpdateBoardState(address, BoardState::flashFailed); } +void ExpansionManager::AddAccelerometerLastRunAverages(CanAddress address, float averages[], int32_t numAxis) noexcept +{ + for(int32_t i = 0;i < numAxis;++i) + { + boards[address].accelerometerLastRunAverages[i] = averages[i]; + } + reprap.BoardsUpdated(); +} + void ExpansionManager::AddAccelerometerRun(CanAddress address, unsigned int numDataPoints) noexcept { boards[address].accelerometerLastRunDataPoints = numDataPoints; diff --git a/src/CAN/ExpansionManager.h b/src/CAN/ExpansionManager.h index a204627477..aca3d728df 100644 --- a/src/CAN/ExpansionManager.h +++ b/src/CAN/ExpansionManager.h @@ -26,6 +26,7 @@ struct ExpansionBoardData const char *_ecv_array typeName; MinCurMax mcuTemp, vin, v12; + float accelerometerLastRunAverages[3]; uint32_t accelerometerLastRunDataPoints; uint32_t closedLoopLastRunDataPoints; UniqueId uniqueId; @@ -59,13 +60,14 @@ class ExpansionManager INHERIT_OBJECT_MODEL void UpdateFinished(CanAddress address) noexcept; void UpdateFailed(CanAddress address) noexcept; void AddAccelerometerRun(CanAddress address, unsigned int numDataPoints) noexcept; + void AddAccelerometerLastRunAverages(CanAddress address, float averages[], int32_t numAxis) noexcept; void AddClosedLoopRun(CanAddress address, unsigned int numDataPoints) noexcept; bool IsFlashing() const noexcept { return numBoardsFlashing != 0; } void EmergencyStop() noexcept; protected: - DECLARE_OBJECT_MODEL + DECLARE_OBJECT_MODEL_WITH_ARRAYS private: const ExpansionBoardData& FindIndexedBoard(unsigned int index) const noexcept; diff --git a/src/Platform/Platform.cpp b/src/Platform/Platform.cpp index 72ebaa4f05..ad8c060a3c 100644 --- a/src/Platform/Platform.cpp +++ b/src/Platform/Platform.cpp @@ -219,6 +219,13 @@ constexpr ObjectModelArrayTableEntry Platform::objectModelArrayTable[] = [] (const ObjectModel *self, const ObjectExplorationContext& context) noexcept -> size_t { return NumCoordinateSystems; }, [] (const ObjectModel *self, ObjectExplorationContext& context) noexcept -> ExpressionValue { return ExpressionValue(reprap.GetGCodes().GetWorkplaceOffset(context.GetIndex(1), context.GetLastIndex()), 3); } + }, + // 2. Average readings from last accelerometer run + { + nullptr, // no lock needed + [] (const ObjectModel *self, const ObjectExplorationContext& context) noexcept -> size_t { return 3; }, + [] (const ObjectModel *self, ObjectExplorationContext& context) noexcept -> ExpressionValue + { return ExpressionValue((float)Accelerometers::GetLocalAccelerometerLastRunAverage(context.GetLastIndex())); } } }; @@ -357,6 +364,7 @@ constexpr ObjectModelTableEntry Platform::objectModelTable[] = #if SUPPORT_ACCELEROMETERS // 9. boards[0].accelerometer members + { "lastRunAverages", OBJECT_MODEL_FUNC_ARRAY(2), ObjectModelEntryFlags::none }, { "points", OBJECT_MODEL_FUNC_NOSELF((int32_t)Accelerometers::GetLocalAccelerometerDataPoints()), ObjectModelEntryFlags::none }, { "runs", OBJECT_MODEL_FUNC_NOSELF((int32_t)Accelerometers::GetLocalAccelerometerRuns()), ObjectModelEntryFlags::none }, #endif @@ -393,7 +401,7 @@ constexpr uint8_t Platform::objectModelTableDescriptor[] = 2, // section 7: move.axes[].microstepping 2, // section 8: move.extruders[].microstepping #if SUPPORT_ACCELEROMETERS - 2, // section 9: boards[0].accelerometer + 3, // section 9: boards[0].accelerometer #else 0, #endif From 3a4e5ee4f7a94603647f027690f3edc34b3d9283 Mon Sep 17 00:00:00 2001 From: BrendonBuilds Date: Fri, 4 Nov 2022 14:11:13 -0700 Subject: [PATCH 2/4] Adjustments to accelerometer averaging Add extra arguments to AddLocalAccelerometerRun instead of a separate function Move NumAccelerometerAxes to CANLib\RRF3Common.h --- src/Accelerometers/Accelerometers.cpp | 46 +++++++++++++-------------- src/CAN/ExpansionManager.cpp | 15 ++++----- src/CAN/ExpansionManager.h | 5 ++- src/Platform/Platform.cpp | 2 +- 4 files changed, 31 insertions(+), 37 deletions(-) diff --git a/src/Accelerometers/Accelerometers.cpp b/src/Accelerometers/Accelerometers.cpp index dcfa3a1b41..45df8a4409 100644 --- a/src/Accelerometers/Accelerometers.cpp +++ b/src/Accelerometers/Accelerometers.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #if SUPPORT_CAN_EXPANSION # include @@ -68,8 +69,7 @@ static volatile uint32_t numSamplesRequested; static uint8_t resolution = DefaultResolution; static uint8_t orientation = 20; // +Z -> +Z, +X -> +X static volatile uint8_t axesRequested; -constexpr size_t NumAxis = 3; -static float lastRunAverages[NumAxis] = {0.0f, 0.0f, 0.0f}; +static float lastRunAverages[NumAccelerometerAxes] = {0.0f, 0.0f, 0.0f}; static FileStore* volatile accelerometerFile = nullptr; // this is non-null when the accelerometer is running, null otherwise static unsigned int numLocalRunsCompleted = 0; static unsigned int lastRunNumSamplesReceived = 0; @@ -82,26 +82,23 @@ static IoPort spiCsPort; static IoPort irqPort; // Add a local accelerometer run -static void AddLocalAccelerometerRun(unsigned int numDataPoints) noexcept +static void AddLocalAccelerometerRun(unsigned int numDataPoints, float averages[]) noexcept { lastRunNumSamplesReceived = numDataPoints; ++numLocalRunsCompleted; - reprap.BoardsUpdated(); -} -static void AddLocalAccelerometerAverages(float averages[]) noexcept -{ - for(unsigned int axis = 0;axis < NumAxis;++axis) + for(unsigned int axis = 0;axis < NumAccelerometerAxes;++axis) { lastRunAverages[axis] = averages[axis]; } + reprap.BoardsUpdated(); } static uint8_t TranslateAxes(uint8_t axes) noexcept { uint8_t rslt = 0; - for (unsigned int i = 0; i < 3; ++i) + for (unsigned int i = 0; i < NumAccelerometerAxes; ++i) { if (axes & (1u << i)) { @@ -126,7 +123,7 @@ static uint8_t TranslateAxes(uint8_t axes) noexcept const uint16_t mask = (1u << resolution) - 1; const int decimalPlaces = GetDecimalPlaces(resolution); bool recordFailedStart = false; - float accumulatedSamples[NumAxis] = {0.0f, 0.0f, 0.0f}; + float accumulatedSamples[NumAccelerometerAxes] = {0.0f, 0.0f, 0.0f}; if (accelerometer->StartCollecting(TranslateAxes(axesRequested))) { @@ -145,7 +142,7 @@ static uint8_t TranslateAxes(uint8_t axes) noexcept f->Truncate(); // truncate the file in case we didn't write all the preallocated space f->Close(); f = nullptr; - AddLocalAccelerometerRun(0); + AddLocalAccelerometerRun(0, {0}); } else { @@ -170,7 +167,7 @@ static uint8_t TranslateAxes(uint8_t axes) noexcept String temp; temp.printf("%u", samplesWritten); - for (unsigned int axis = 0; axis < 3; ++axis) + for (unsigned int axis = 0; axis < NumAccelerometerAxes; ++axis) { if (axesRequested & (1u << axis)) { @@ -230,14 +227,15 @@ static uint8_t TranslateAxes(uint8_t axes) noexcept { f->Truncate(); // truncate the file in case we didn't write all the preallocated space f->Close(); - AddLocalAccelerometerRun(samplesWritten); // find the average value for each axis - for(unsigned int axis = 0;axis < NumAxis;++axis) + for(unsigned int axis = 0;axis < NumAccelerometerAxes;++axis) { accumulatedSamples[axis] /= float(samplesWritten); } - AddLocalAccelerometerAverages(accumulatedSamples); + + AddLocalAccelerometerRun(samplesWritten, accumulatedSamples); + } accelerometer->StopCollecting(); @@ -479,12 +477,12 @@ GCodeResult Accelerometers::StartAccelerometer(GCodeBuffer& gb, const StringRef& # if SUPPORT_CAN_EXPANSION if (device.IsRemote()) { - reprap.GetExpansion().AddAccelerometerRun(device.boardAddress, 0); + reprap.GetExpansion().AddAccelerometerRun(device.boardAddress, 0, {0}); } else # endif { - AddLocalAccelerometerRun(0); + AddLocalAccelerometerRun(0, {0}); } return GCodeResult::error; } @@ -515,7 +513,7 @@ GCodeResult Accelerometers::StartAccelerometer(GCodeBuffer& gb, const StringRef& accelerometerFile->Close(); accelerometerFile = nullptr; MassStorage::Delete(accelerometerFileName.c_str(), false); - reprap.GetExpansion().AddAccelerometerRun(device.boardAddress, 0); + reprap.GetExpansion().AddAccelerometerRun(device.boardAddress, 0, {0}); } return rslt; } @@ -599,7 +597,7 @@ void Accelerometers::ProcessReceivedData(CanAddress src, const CanMessageAcceler f->Truncate(); // truncate the file in case we didn't write all the preallocated space f->Close(); accelerometerFile = nullptr; - reprap.GetExpansion().AddAccelerometerRun(src, 0); + reprap.GetExpansion().AddAccelerometerRun(src, 0, {0}); } else if (msg.axes != expectedRemoteAxes || msg.firstSampleNumber != expectedRemoteSampleNumber || src != expectedRemoteBoardAddress) { @@ -607,7 +605,7 @@ void Accelerometers::ProcessReceivedData(CanAddress src, const CanMessageAcceler f->Truncate(); // truncate the file in case we didn't write all the preallocated space f->Close(); accelerometerFile = nullptr; - reprap.GetExpansion().AddAccelerometerRun(src, 0); + reprap.GetExpansion().AddAccelerometerRun(src, 0, {0}); } else { @@ -619,7 +617,7 @@ void Accelerometers::ProcessReceivedData(CanAddress src, const CanMessageAcceler const unsigned int receivedResolution = msg.bitsPerSampleMinusOne + 1; const uint16_t mask = (1u << receivedResolution) - 1; const int decimalPlaces = GetDecimalPlaces(receivedResolution); - float accumulatedSamples[NumAxis] = {0.0f, 0.0f, 0.0f}; + float accumulatedSamples[NumAccelerometerAxes] = {0.0f, 0.0f, 0.0f}; if (msg.overflowed) { @@ -679,14 +677,14 @@ void Accelerometers::ProcessReceivedData(CanAddress src, const CanMessageAcceler f->Truncate(); // truncate the file in case we didn't write all the preallocated space f->Close(); accelerometerFile = nullptr; - reprap.GetExpansion().AddAccelerometerRun(src, expectedRemoteSampleNumber); // find the average value for each axis - for (unsigned int axis = 0; axis < NumAxis; ++axis) + for (unsigned int axis = 0; axis < NumAccelerometerAxes; ++axis) { accumulatedSamples[axis] /= float(msg.numSamples); } - reprap.GetExpansion().AddAccelerometerLastRunAverages(src, accumulatedSamples, NumAxis); + + reprap.GetExpansion().AddAccelerometerRun(src, expectedRemoteSampleNumber, accumulatedSamples); } } } diff --git a/src/CAN/ExpansionManager.cpp b/src/CAN/ExpansionManager.cpp index 106948a0a9..dee48a90be 100644 --- a/src/CAN/ExpansionManager.cpp +++ b/src/CAN/ExpansionManager.cpp @@ -28,7 +28,7 @@ constexpr ObjectModelArrayTableEntry ExpansionManager::objectModelArrayTable[] = { { nullptr, // no lock needed - [] (const ObjectModel *self, const ObjectExplorationContext& context) noexcept -> size_t { return 3; }, + [] (const ObjectModel *self, const ObjectExplorationContext& context) noexcept -> size_t { return NumAccelerometerAxes; }, [] (const ObjectModel *self, ObjectExplorationContext& context) noexcept -> ExpressionValue { return ExpressionValue(((const ExpansionManager*)self)->FindIndexedBoard(context.GetIndex(1)).accelerometerLastRunAverages[context.GetLastIndex()]); } } @@ -307,19 +307,16 @@ void ExpansionManager::UpdateFailed(CanAddress address) noexcept UpdateBoardState(address, BoardState::flashFailed); } -void ExpansionManager::AddAccelerometerLastRunAverages(CanAddress address, float averages[], int32_t numAxis) noexcept +void ExpansionManager::AddAccelerometerRun(CanAddress address, unsigned int numDataPoints, float averages[]) noexcept { - for(int32_t i = 0;i < numAxis;++i) + boards[address].accelerometerLastRunDataPoints = numDataPoints; + ++boards[address].accelerometerRuns; + + for(int32_t i = 0;i < NumAccelerometerAxes;++i) { boards[address].accelerometerLastRunAverages[i] = averages[i]; } - reprap.BoardsUpdated(); -} -void ExpansionManager::AddAccelerometerRun(CanAddress address, unsigned int numDataPoints) noexcept -{ - boards[address].accelerometerLastRunDataPoints = numDataPoints; - ++boards[address].accelerometerRuns; reprap.BoardsUpdated(); } diff --git a/src/CAN/ExpansionManager.h b/src/CAN/ExpansionManager.h index aca3d728df..9044b19dd0 100644 --- a/src/CAN/ExpansionManager.h +++ b/src/CAN/ExpansionManager.h @@ -26,7 +26,7 @@ struct ExpansionBoardData const char *_ecv_array typeName; MinCurMax mcuTemp, vin, v12; - float accelerometerLastRunAverages[3]; + float accelerometerLastRunAverages[NumAccelerometerAxes]; uint32_t accelerometerLastRunDataPoints; uint32_t closedLoopLastRunDataPoints; UniqueId uniqueId; @@ -59,8 +59,7 @@ class ExpansionManager INHERIT_OBJECT_MODEL void UpdateFinished(CanAddress address) noexcept; void UpdateFailed(CanAddress address) noexcept; - void AddAccelerometerRun(CanAddress address, unsigned int numDataPoints) noexcept; - void AddAccelerometerLastRunAverages(CanAddress address, float averages[], int32_t numAxis) noexcept; + void AddAccelerometerRun(CanAddress address, unsigned int numDataPoints, float averages[]) noexcept; void AddClosedLoopRun(CanAddress address, unsigned int numDataPoints) noexcept; bool IsFlashing() const noexcept { return numBoardsFlashing != 0; } diff --git a/src/Platform/Platform.cpp b/src/Platform/Platform.cpp index ad8c060a3c..cd3b309be5 100644 --- a/src/Platform/Platform.cpp +++ b/src/Platform/Platform.cpp @@ -223,7 +223,7 @@ constexpr ObjectModelArrayTableEntry Platform::objectModelArrayTable[] = // 2. Average readings from last accelerometer run { nullptr, // no lock needed - [] (const ObjectModel *self, const ObjectExplorationContext& context) noexcept -> size_t { return 3; }, + [] (const ObjectModel *self, const ObjectExplorationContext& context) noexcept -> size_t { return NumAccelerometerAxes; }, [] (const ObjectModel *self, ObjectExplorationContext& context) noexcept -> ExpressionValue { return ExpressionValue((float)Accelerometers::GetLocalAccelerometerLastRunAverage(context.GetLastIndex())); } } From b891a991745ff820c685de7dc65e385c00731569 Mon Sep 17 00:00:00 2001 From: BrendonBuilds Date: Sat, 5 Nov 2022 09:16:09 -0700 Subject: [PATCH 3/4] Add Add(Local)FailedAccelerometerRun --- src/Accelerometers/Accelerometers.cpp | 40 ++++++++++++++------------- src/CAN/ExpansionManager.cpp | 16 ++++++++--- src/CAN/ExpansionManager.h | 1 + 3 files changed, 34 insertions(+), 23 deletions(-) diff --git a/src/Accelerometers/Accelerometers.cpp b/src/Accelerometers/Accelerometers.cpp index 45df8a4409..fec6998807 100644 --- a/src/Accelerometers/Accelerometers.cpp +++ b/src/Accelerometers/Accelerometers.cpp @@ -87,10 +87,19 @@ static void AddLocalAccelerometerRun(unsigned int numDataPoints, float averages[ lastRunNumSamplesReceived = numDataPoints; ++numLocalRunsCompleted; - for(unsigned int axis = 0;axis < NumAccelerometerAxes;++axis) - { - lastRunAverages[axis] = averages[axis]; - } + memcpyf(lastRunAverages, averages, ARRAY_SIZE(averages)); + + reprap.BoardsUpdated(); +} + +// Add a local failed accelerometer run +static void AddLocalFailedAccelerometerRun() noexcept +{ + lastRunNumSamplesReceived = 0; + ++numLocalRunsCompleted; + + // reset the last run averages + for (float& f : lastRunAverages) { f = 0.0f; } reprap.BoardsUpdated(); } @@ -142,7 +151,7 @@ static uint8_t TranslateAxes(uint8_t axes) noexcept f->Truncate(); // truncate the file in case we didn't write all the preallocated space f->Close(); f = nullptr; - AddLocalAccelerometerRun(0, {0}); + AddLocalFailedAccelerometerRun(); } else { @@ -229,13 +238,9 @@ static uint8_t TranslateAxes(uint8_t axes) noexcept f->Close(); // find the average value for each axis - for(unsigned int axis = 0;axis < NumAccelerometerAxes;++axis) - { - accumulatedSamples[axis] /= float(samplesWritten); - } + for (float& f : accumulatedSamples) { f /= float(samplesWritten); } AddLocalAccelerometerRun(samplesWritten, accumulatedSamples); - } accelerometer->StopCollecting(); @@ -477,12 +482,12 @@ GCodeResult Accelerometers::StartAccelerometer(GCodeBuffer& gb, const StringRef& # if SUPPORT_CAN_EXPANSION if (device.IsRemote()) { - reprap.GetExpansion().AddAccelerometerRun(device.boardAddress, 0, {0}); + reprap.GetExpansion().AddFailedAccelerometerRun(device.boardAddress); } else # endif { - AddLocalAccelerometerRun(0, {0}); + AddLocalFailedAccelerometerRun(); } return GCodeResult::error; } @@ -513,7 +518,7 @@ GCodeResult Accelerometers::StartAccelerometer(GCodeBuffer& gb, const StringRef& accelerometerFile->Close(); accelerometerFile = nullptr; MassStorage::Delete(accelerometerFileName.c_str(), false); - reprap.GetExpansion().AddAccelerometerRun(device.boardAddress, 0, {0}); + reprap.GetExpansion().AddFailedAccelerometerRun(device.boardAddress); } return rslt; } @@ -597,7 +602,7 @@ void Accelerometers::ProcessReceivedData(CanAddress src, const CanMessageAcceler f->Truncate(); // truncate the file in case we didn't write all the preallocated space f->Close(); accelerometerFile = nullptr; - reprap.GetExpansion().AddAccelerometerRun(src, 0, {0}); + reprap.GetExpansion().AddFailedAccelerometerRun(src); } else if (msg.axes != expectedRemoteAxes || msg.firstSampleNumber != expectedRemoteSampleNumber || src != expectedRemoteBoardAddress) { @@ -605,7 +610,7 @@ void Accelerometers::ProcessReceivedData(CanAddress src, const CanMessageAcceler f->Truncate(); // truncate the file in case we didn't write all the preallocated space f->Close(); accelerometerFile = nullptr; - reprap.GetExpansion().AddAccelerometerRun(src, 0, {0}); + reprap.GetExpansion().AddFailedAccelerometerRun(src); } else { @@ -679,10 +684,7 @@ void Accelerometers::ProcessReceivedData(CanAddress src, const CanMessageAcceler accelerometerFile = nullptr; // find the average value for each axis - for (unsigned int axis = 0; axis < NumAccelerometerAxes; ++axis) - { - accumulatedSamples[axis] /= float(msg.numSamples); - } + for (float& f : accumulatedSamples) { f /= float(msg.numSamples); } reprap.GetExpansion().AddAccelerometerRun(src, expectedRemoteSampleNumber, accumulatedSamples); } diff --git a/src/CAN/ExpansionManager.cpp b/src/CAN/ExpansionManager.cpp index dee48a90be..e84e66d332 100644 --- a/src/CAN/ExpansionManager.cpp +++ b/src/CAN/ExpansionManager.cpp @@ -312,10 +312,18 @@ void ExpansionManager::AddAccelerometerRun(CanAddress address, unsigned int numD boards[address].accelerometerLastRunDataPoints = numDataPoints; ++boards[address].accelerometerRuns; - for(int32_t i = 0;i < NumAccelerometerAxes;++i) - { - boards[address].accelerometerLastRunAverages[i] = averages[i]; - } + memcpyf(boards[address].accelerometerLastRunAverages, averages, ARRAY_SIZE(averages)); + + reprap.BoardsUpdated(); +} + +void ExpansionManager::AddFailedAccelerometerRun(CanAddress address) noexcept +{ + boards[address].accelerometerLastRunDataPoints = 0; + ++boards[address].accelerometerRuns; + + // reset the averages to 0.0f + for (float& f : boards[address].accelerometerLastRunAverages) { f = 0.0f; } reprap.BoardsUpdated(); } diff --git a/src/CAN/ExpansionManager.h b/src/CAN/ExpansionManager.h index 9044b19dd0..667ac196c8 100644 --- a/src/CAN/ExpansionManager.h +++ b/src/CAN/ExpansionManager.h @@ -60,6 +60,7 @@ class ExpansionManager INHERIT_OBJECT_MODEL void UpdateFinished(CanAddress address) noexcept; void UpdateFailed(CanAddress address) noexcept; void AddAccelerometerRun(CanAddress address, unsigned int numDataPoints, float averages[]) noexcept; + void AddFailedAccelerometerRun(CanAddress address) noexcept; void AddClosedLoopRun(CanAddress address, unsigned int numDataPoints) noexcept; bool IsFlashing() const noexcept { return numBoardsFlashing != 0; } From 4d2f10c6f94bbb0daa494f0d39bcfb667ebcc9cb Mon Sep 17 00:00:00 2001 From: BrendonBuilds Date: Sat, 5 Nov 2022 09:35:10 -0700 Subject: [PATCH 4/4] fix compiler warnings --- src/Accelerometers/Accelerometers.cpp | 6 +++--- src/CAN/ExpansionManager.cpp | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Accelerometers/Accelerometers.cpp b/src/Accelerometers/Accelerometers.cpp index fec6998807..ea91b66ccd 100644 --- a/src/Accelerometers/Accelerometers.cpp +++ b/src/Accelerometers/Accelerometers.cpp @@ -87,7 +87,7 @@ static void AddLocalAccelerometerRun(unsigned int numDataPoints, float averages[ lastRunNumSamplesReceived = numDataPoints; ++numLocalRunsCompleted; - memcpyf(lastRunAverages, averages, ARRAY_SIZE(averages)); + memcpyf(lastRunAverages, averages, NumAccelerometerAxes); reprap.BoardsUpdated(); } @@ -238,7 +238,7 @@ static uint8_t TranslateAxes(uint8_t axes) noexcept f->Close(); // find the average value for each axis - for (float& f : accumulatedSamples) { f /= float(samplesWritten); } + for (float& sample : accumulatedSamples) { sample /= float(samplesWritten); } AddLocalAccelerometerRun(samplesWritten, accumulatedSamples); } @@ -684,7 +684,7 @@ void Accelerometers::ProcessReceivedData(CanAddress src, const CanMessageAcceler accelerometerFile = nullptr; // find the average value for each axis - for (float& f : accumulatedSamples) { f /= float(msg.numSamples); } + for (float& sample : accumulatedSamples) { sample /= float(msg.numSamples); } reprap.GetExpansion().AddAccelerometerRun(src, expectedRemoteSampleNumber, accumulatedSamples); } diff --git a/src/CAN/ExpansionManager.cpp b/src/CAN/ExpansionManager.cpp index e84e66d332..c0d6a848db 100644 --- a/src/CAN/ExpansionManager.cpp +++ b/src/CAN/ExpansionManager.cpp @@ -312,7 +312,7 @@ void ExpansionManager::AddAccelerometerRun(CanAddress address, unsigned int numD boards[address].accelerometerLastRunDataPoints = numDataPoints; ++boards[address].accelerometerRuns; - memcpyf(boards[address].accelerometerLastRunAverages, averages, ARRAY_SIZE(averages)); + memcpyf(boards[address].accelerometerLastRunAverages, averages, NumAccelerometerAxes); reprap.BoardsUpdated(); }