Skip to content
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

Open
wants to merge 6 commits into
base: 3.5-dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/Accelerometers/Accelerometers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Author

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?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NumAccelerometerAxes would be better

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;
Expand All @@ -87,6 +89,15 @@ static void AddLocalAccelerometerRun(unsigned int numDataPoints) noexcept
reprap.BoardsUpdated();
}

static void AddLocalAccelerometerAverages(float averages[]) noexcept
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not add extra arguments to AddLocalAccelerometerRun instead of adding this function?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will do the same thing for AddAccelerometerRun as well

{
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;
Expand Down Expand Up @@ -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)))
{
Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand All @@ -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);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Accelerometers/Accelerometers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
24 changes: 23 additions & 1 deletion src/CAN/ExpansionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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; },
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would you prefer array sizes are tracked?

Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -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 },

Expand All @@ -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
};

Expand Down Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/CAN/ExpansionManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ struct ExpansionBoardData

const char *_ecv_array typeName;
MinCurMax mcuTemp, vin, v12;
float accelerometerLastRunAverages[3];
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would you prefer array sizes are defined?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See previous comment

uint32_t accelerometerLastRunDataPoints;
uint32_t closedLoopLastRunDataPoints;
UniqueId uniqueId;
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 9 additions & 1 deletion src/Platform/Platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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; },
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would you prefer array sizes are tracked?

Copy link
Collaborator

Choose a reason for hiding this comment

The 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())); }
}
};

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down