1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-11-26 23:52:26 +00:00

This should actually be PWM, I think.

I'm just unsure of exactly the proper formula, owing to my ignorance on basic electrical engineering. Research ahoy!
This commit is contained in:
Thomas Harte 2019-07-30 22:02:41 -04:00
parent 2aa308efdd
commit e3f22e5787

View File

@ -25,31 +25,23 @@ void DriveSpeedAccumulator::post_sample(uint8_t sample) {
// Treat 33 as a zero point and count zero crossings; then approximate // Treat 33 as a zero point and count zero crossings; then approximate
// the RPM from the frequency of those. // the RPM from the frequency of those.
size_t first_crossing = 0, last_crossing = 0; int samples_over = 0;
int number_of_crossings = 0;
const uint8_t centre = 33; const uint8_t centre = 33;
bool was_over = samples_[0] > centre; for(size_t c = 0; c < 512; ++c) {
for(size_t c = 1; c < 512; ++c) { if(samples_[c] > centre) ++ samples_over;
bool is_over = samples_[c] > centre;
if(is_over != was_over) {
if(!first_crossing) first_crossing = c;
last_crossing = c;
++number_of_crossings;
}
was_over = is_over;
} }
if(number_of_crossings) { // TODO: if the above is the correct test, do it on sample receipt rather than
// The 1950 multiplier here is a complete guess, based on preliminary // bothering with an intermediate buffer.
// observations of the values supplied and the known RPM selections of
// the 800kb drive. Updated values may be needed. // The below fits for a function like `a + bc`; I'm not sure it's
const float rotation_speed = 1950.0f * float(number_of_crossings-1) / float(last_crossing - first_crossing); const float duty_cycle = float(samples_over) / float(samples_.size());
const float rotation_speed = 392.0f + duty_cycle * 19.95f;
for(int c = 0; c < number_of_drives_; ++c) { for(int c = 0; c < number_of_drives_; ++c) {
drives_[c]->set_rotation_speed(rotation_speed); drives_[c]->set_rotation_speed(rotation_speed);
} }
// printf("RPM: %d crossings => %0.2f\n", number_of_crossings, rotation_speed, min, max); // printf("RPM: %0.2f (%d over)\n", rotation_speed, samples_over);
}
} }
} }