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

Conversation

BrendonBuilds
Copy link

Store the average value of each axis of the last accelerometer run in the Object Model

Store the average value of each axis of the last accelerometer run in the Object Model
@@ -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

{
{
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.

// 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

@@ -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

@@ -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

{
{
nullptr, // no lock needed
[] (const ObjectModel *self, const ObjectExplorationContext& context) noexcept -> size_t { return 3; },
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.

@@ -26,6 +26,7 @@ struct ExpansionBoardData

const char *_ecv_array typeName;
MinCurMax mcuTemp, vin, v12;
float accelerometerLastRunAverages[3];
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

// 2. Average readings from last accelerometer run
{
nullptr, // no lock needed
[] (const ObjectModel *self, const ObjectExplorationContext& context) noexcept -> size_t { return 3; },
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

BrendonBuilds and others added 2 commits November 4, 2022 14:11
Add extra arguments to AddLocalAccelerometerRun instead of a separate function

Move NumAccelerometerAxes to CANLib\RRF3Common.h
@BrendonBuilds
Copy link
Author

The second commit will require the changes in: Duet3D/CANlib#8

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

@@ -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

@@ -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.

@@ -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.


// 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

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants