Skip to content
Draft
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
39 changes: 39 additions & 0 deletions sdk/bare/common/drv/encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ void encoder_set_pulses_per_rev_bits(uint32_t bits)
Xil_Out32(ENCODER_BASE_ADDR + 2 * sizeof(uint32_t), bits);
}

void encoder_get_pulses_per_rev_bits(uint32_t *bits_out)
{
*bits_out = Xil_In32(ENCODER_BASE_ADDR + 2 * sizeof(uint32_t));
}

void encoder_get_steps(int32_t *steps)
{
*steps = Xil_In32(ENCODER_BASE_ADDR);
Expand All @@ -33,6 +38,40 @@ void encoder_get_position(uint32_t *position)
*position = Xil_In32(ENCODER_BASE_ADDR + 1 * sizeof(uint32_t));
}

double encoder_calc_speed(double dt, uint32_t pos_prev)
{
// TODO: implement this
return 0;
}

double encoder_get_theta(void)
{
uint32_t pos;
encoder_get_position(&pos);

double theta = 0;

if (pos != -1) {
uint32_t bits;
encoder_get_pulses_per_rev_bits(&bits);

uint32_t PPR = 1 << bits;

theta = PI2 * ((double)pos / (double)PPR);
}

return theta;
}

double encoder_get_theta_resolution(void)
{
// TODO: implement this
return 0;
}




// ****************
// State Machine which finds z pulse
// ****************
Expand Down
17 changes: 17 additions & 0 deletions sdk/bare/common/drv/encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,27 @@
void encoder_init(void);

void encoder_set_pulses_per_rev_bits(uint32_t bits);
void encoder_get_pulses_per_rev_bits(uint32_t *bits_out);

void encoder_get_steps(int32_t *steps);
void encoder_get_position(uint32_t *position);

// Compute the current speed in mechanical rad/s
//
// This function will subtract the previous position from the current position and divide by delta time.
// Finally, it will use the steps per rev to get the answer in a standardized mechanical rad/s
double encoder_calc_speed(double dt, uint32_t pos_prev);

// Compute the current position in mechanical rad/s
//
// This function simply uses the steps per rev to get the answer in a standardized mechanical radians
double encoder_get_theta(void);

// Compute resolution in mechanical rads
//
// This function returns the resolution of the encoder output in mechanical radians
double encoder_get_theta_resolution(void);

void encoder_find_z(void);

#endif // ENCODER_H