void meassurement<T>::update_current() { const T v= sensor; // assure single read if (first) prepare_first(v); current= calculate_current(v); if (first) minimum= maximum= current; if (minimum > current) minimum= current; if (maximum < current) maximum= current; first= false; }
void lpf_work(void) { calculate_target(); calculate_current(); calculate_final(); }
void loop() { long unsigned int current_time = millis(); if(current_time >= sample_clock){ // reset sampling clock sample_clock += SAMPLE_PERIOD; // poll the potentiometer once each sample period // poll_potentiometer(); // dim the LED string using potentiometer as a reference // analogWrite(LED_PWM_PIN, map(dimmer_cts, 0, 1025, 0, 255)); // step through impulse routine // step_pulser(current_time, &pulser); // poll the tachometer encoder //poll_encoder(current_time, &tach_enc); if (pollButton(&button) >= shortPress){ backlight_clock = current_time + backlight_period; } if(current_time >= update_tach_clock){ update_tach_clock = current_time + update_tach_period; // check if tachometer interrupt has been fired if(tach_enc.pin_state){ tach_enc.pin_state = 0; tach_enc.last_period = (current_time - tach_enc.startTime); tach_enc.startTime = current_time; } digitalWrite(LED_PIN, digitalRead(tach_enc.pin_num)); } if(current_time >= calc_tach_clock){ calc_tach_clock = current_time + calc_tach_period; calculate_tach(current_time, &tach_enc); } if(current_time >= calc_current_clock){ calc_current_clock = current_time + calc_current_period; calculate_current(¤t_sensor); calculate_voltage(&volt_sensor); } // if(current_time >= blink_clock){ // led_state = !led_state; // digitalWrite(LED_PIN, led_state); // blink_clock = current_time + blink_period; // } if(current_time >= toggle_display_clock){ toggle_display_clock = current_time + toggle_display_period; toggle_display = !toggle_display; gotoXY(0,2); if(toggle_display == SHOW_RPM) LcdString((char*)"Speed:"); else if(toggle_display == SHOW_WATTS) LcdString((char*)"Power:"); } if(current_time >= update_display_clock){ update_display_clock = current_time + update_display_period; snprintf(outputbuffer, 11,"%d.%02d Amps", (current_sensor.current)/1000, ((abs(current_sensor.current) % 1000) / 10)); gotoXY(6,1); LcdString((char*)" "); gotoXY(6,1); LcdString(outputbuffer); if(toggle_display == SHOW_RPM){ snprintf(outputbuffer,11,"%ld.%01ld RPM", (60*tach_enc.hertz)/1000, ((60*tach_enc.hertz)%1000) / 100); } else if(toggle_display == SHOW_WATTS){ watts = abs(current_sensor.current * volt_sensor.voltage) / 1000; snprintf(outputbuffer,11,"%ld.%01ld Watts", watts/1000, (watts%1000) / 100); } gotoXY(6,3); LcdString((char*)" "); gotoXY(6,3); LcdString(outputbuffer); snprintf(outputbuffer,12,"%ld.%02ld Volts", volt_sensor.voltage/1000, ((volt_sensor.voltage)%1000) / 10); gotoXY(6,5); LcdString((char*)" "); gotoXY(6,5); LcdString(outputbuffer); } // if(current_time >= post_to_serial_clock){ // post_to_serial_clock = current_time + post_to_serial_period; //// Serial.write("<r>"); //// Serial.write(tach_enc.hertz / 1000); //// Serial.write("."); //// Serial.write(tach_enc.hertz % 1000); // sprintf(outputbuffer, "<r>%lu.%03lu\t%ld.%03ld\t%d\t%ld\t%ld.%03ld\t%ld</r>\n", current_time/1000, // current_time % 1000, (60*tach_enc.hertz)/1000, ((60*tach_enc.hertz) % 1000)/10,current_sensor.current, current_sensor.sense_cts, // volt_sensor.voltage / 1000, volt_sensor.voltage % 1000, volt_sensor.sense_cts); // Serial.write(outputbuffer); // } if(current_time <= backlight_clock){ analogWrite(BACKLIGHT_PIN, 35); } else analogWrite(BACKLIGHT_PIN, 0); } }