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

50 encoder velocity calculation #51

Merged
merged 3 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ All versions in this changelog have two entries: ``driver`` and ``firmware``. Th
have the same version, as communication protocol might change between versions. In the firmware/driver there
is a safeguard to prevent miscommunication.

Under development
=================

These are the features which are currently under development, but not yet released.

* ``driver``:

* ``encoder``: calculation of average speed has been correctd (#50).


Version 1.0.2
=============

Expand Down
7 changes: 3 additions & 4 deletions src/litexcnc/driver/modules/litexcnc_encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,18 +302,17 @@ int litexcnc_encoder_process_read(void *module, uint8_t **data, int period) {
// running average is not modified when an index-pulse is received, as this
// means there is a large jump in position and thus to a large theoretical
// speed.
size_t velocity_pointer = 0;
if (!(*(instance->hal.pin.index_pulse))) {
// Replace the element in the array
instance->memo.velocity[velocity_pointer] = (*(instance->hal.pin.position) - position_old) * encoder->data.recip_dt;
instance->memo.velocity[encoder->memo.velocity_pointer] = (*(instance->hal.pin.position) - position_old) * encoder->data.recip_dt;
// Sum the array and divide by the size of the array
float average = 0.0;
for (size_t j=0; j < LITEXCNC_ENCODER_POSITION_AVERAGE_SIZE; j++) {average += instance->memo.velocity[j];};
*(instance->hal.pin.velocity) = average * 0.125;
*(instance->hal.pin.velocity) = average * LITEXCNC_ENCODER_POSITION_AVERAGE_RECIP;
*(instance->hal.pin.velocity_rpm) = *(instance->hal.pin.velocity) * 60.0;
// Increase the pointer to the next element, revert to the beginning of
// the array
if (velocity_pointer++ >= sizeof(instance->memo.velocity)) {velocity_pointer=0;};
if (encoder->memo.velocity_pointer++ >= LITEXCNC_ENCODER_POSITION_AVERAGE_SIZE) {encoder->memo.velocity_pointer=0;};
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/litexcnc/driver/modules/litexcnc_encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
* into account that at these low speeds the resolution of the speed is detoriated.
*/
#define LITEXCNC_ENCODER_POSITION_AVERAGE_SIZE 8
#define LITEXCNC_ENCODER_POSITION_AVERAGE_RECIP 0.125

/** The ID of the component, only used when the component is used as stand-alone */
int comp_id;
Expand Down Expand Up @@ -145,6 +146,7 @@ typedef struct {
/** This struct holds data from previous cycles, so changes can be detected */
struct {
long period; /** period of a single cycle */
size_t velocity_pointer;
} memo;
} litexcnc_encoder_t;

Expand Down