-
Notifications
You must be signed in to change notification settings - Fork 535
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 1 commit
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 |
---|---|---|
|
@@ -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 | ||
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. Why not add extra arguments to AddLocalAccelerometerRun instead of adding this function? 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 will do the same thing for |
||
{ | ||
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); | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; }, | ||
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. How would you prefer array sizes are tracked? 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. Move NumAccelerometerAxes (renamed from NumAxis) into an existing common header file so that you can use that value instead of 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; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ struct ExpansionBoardData | |
|
||
const char *_ecv_array typeName; | ||
MinCurMax mcuTemp, vin, v12; | ||
float accelerometerLastRunAverages[3]; | ||
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. How would you prefer array sizes are defined? 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 |
||
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; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; }, | ||
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. How would you prefer array sizes are tracked? 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 earlier comment |
||
[] (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 | ||
|
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.
Is this var name too generic?
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.
NumAccelerometerAxes would be better