-
Notifications
You must be signed in to change notification settings - Fork 538
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
Store average accelerometer readings in Object Model #590
base: 3.5-dev
Are you sure you want to change the base?
Changes from 2 commits
f4ab904
3a4e5ee
f46e907
a775ab6
b891a99
4d2f10c
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 |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
#include <RTOSIface/RTOSIface.h> | ||
#include <Platform/TaskPriorities.h> | ||
#include <Hardware/Spi/SharedSpiDevice.h> | ||
#include <RRF3Common.h> | ||
|
||
#if SUPPORT_CAN_EXPANSION | ||
# include <CanMessageFormats.h> | ||
|
@@ -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<StringLength50> 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}); | ||
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. Need to pass an array of the correct size here because the compiler doesn't know how many values are expected |
||
} | ||
else | ||
# endif | ||
{ | ||
AddLocalAccelerometerRun(0); | ||
AddLocalAccelerometerRun(0, {0}); | ||
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. Need to pass an array of the correct size here because the compiler doesn't know how many values are expected, or add a new function AddFailedLocalAccelerometerRun that doesn't take the averages parameter |
||
} | ||
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}); | ||
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. Need to pass an array of the correct size here because the compiler doesn't know how many values are expected. Alternatively, add another function AddFailedAccelerometerRun that doesn't take the averages as a parameter and instead sets them to zero, since this appears to be done in several places. |
||
} | ||
return rslt; | ||
} | ||
|
@@ -599,15 +597,15 @@ 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}); | ||
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. See previous comment. |
||
} | ||
else if (msg.axes != expectedRemoteAxes || msg.firstSampleNumber != expectedRemoteSampleNumber || src != expectedRemoteBoardAddress) | ||
{ | ||
f->Write("Received mismatched data\n"); | ||
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}); | ||
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. See previous comment. |
||
} | ||
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) | ||
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. Better to use a range-based for-loop here |
||
{ | ||
accumulatedSamples[axis] /= float(msg.numSamples); | ||
} | ||
reprap.GetExpansion().AddAccelerometerLastRunAverages(src, accumulatedSamples, NumAxis); | ||
|
||
reprap.GetExpansion().AddAccelerometerRun(src, expectedRemoteSampleNumber, accumulatedSamples); | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
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. Could we use memcpyf here? |
||
{ | ||
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(); | ||
} | ||
|
||
|
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.
Suggest use memcpyf here instead of a loop