-
-
Save hfiennes/0e7f0c14f44578122b830b8a7650fe61 to your computer and use it in GitHub Desktop.
RPM measurement using UART timing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// RPM counter with UART in timing mode | |
// hfiennes@twilio.com 20200611 | |
// | |
// This uses the falling edge of the input signal to trigger a UART receive. | |
// There will be a framing error generated, but we ignore this (and the data) and just | |
// use the timing information. | |
// | |
// The baudrate must be fast enough that the input signal is LOW for at least 1 bit time | |
// and the high time is at least 9 bit times - ie max RPM that can be detected is | |
// 60 * (baudrate/10). | |
// | |
// 115,200bps gives a max RPM detection rate of 691,200rpm, but you want to keep well | |
// below that in general. | |
// Pin J and pin B are connected with a wire; pinJ is the simulated RPM signal | |
uart <- hardware.uartABCD; | |
pwm <- hardware.pinJ; | |
// RPM; we update this every second | |
rpm <- 0; | |
lastrpm <- time(); | |
// Intervals | |
interval_total <- 0; | |
interval_count <- 0; | |
function rx() { | |
// Read out with timing; we don't care about the byte | |
local b = uart.read() >> 8; | |
interval_total += b; | |
interval_count ++; | |
// Print average RPM every second | |
if (time() != lastrpm) { | |
lastrpm = time(); | |
// Avoid a divide by zero! | |
if (interval_count > 0) { | |
// RPM = 60x number of pulses per second | |
rpm = 60.0 * (1000000.0 / (interval_total / interval_count)); | |
// Reset for next time | |
interval_total = interval_count = 0; | |
} else { | |
// No intervals? That's zero RPM | |
rpm = 0; | |
} | |
server.log(format("rpm = %.3f", rpm)); | |
} | |
} | |
// RX only UART, timing mode | |
uart.configure(115200, 8, PARITY_NONE, 1, NO_TX | NO_CTSRTS | TIMING_ENABLED, rx); | |
// Set up PWM to simulate a particular rpm | |
const simulated_rpm = 4250; | |
pwm.configure(PWM_OUT, 1.0/(simulated_rpm/60.0), 0.9); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment