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 2 commits
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
46 changes: 22 additions & 24 deletions src/Accelerometers/Accelerometers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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;
Expand All @@ -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)
Copy link
Collaborator

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

{
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))
{
Expand All @@ -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)))
{
Expand All @@ -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
{
Expand All @@ -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))
{
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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});
Copy link
Collaborator

Choose a reason for hiding this comment

The 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});
Copy link
Collaborator

@dc42 dc42 Nov 5, 2022

Choose a reason for hiding this comment

The 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;
}
Expand Down Expand Up @@ -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});
Copy link
Collaborator

@dc42 dc42 Nov 5, 2022

Choose a reason for hiding this comment

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

}
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});
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.

}
else
{
Expand All @@ -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)
{
Expand Down Expand Up @@ -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)
Copy link
Collaborator

Choose a reason for hiding this comment

The 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);
}
}
}
Expand Down
15 changes: 6 additions & 9 deletions src/CAN/ExpansionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()]); }
}
Expand Down Expand Up @@ -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)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Expand Down
5 changes: 2 additions & 3 deletions src/CAN/ExpansionManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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; }

Expand Down
2 changes: 1 addition & 1 deletion src/Platform/Platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())); }
}
Expand Down