doppelhub / MuddersMIMA

Mudders Take on Manual IMA Control in the G1 Honda Insight
GNU General Public License v3.0
2 stars 4 forks source link

Measuring vehicle speed using VSS #1

Closed Hurricos closed 9 months ago

Hurricos commented 9 months ago

Context:

Measuring vehicle speed is generally useful for implementing vehicle-speed-dependent behavior.

Assumptions:

There are multiple strategies to produce a frequency value from a square wave input.

1) Pin change interrupts: LiControl's parsing strategy for NEP, done by linking the PIN_NEP (D8) to an ISR

2) Polling pin value

On the Nano, D7 does not support interrupt generation on level change; this leaves only polling strategies.

We cannot create a hardware timer to call a polling function, because we currently use all three timers - timer 0 for millis(), timer 1 for PWM on pin 9 (PIN_CMDPWR_MCM), and timer 2 for PWM on pin 3 (PIN_MAMODE1_MCM).

Outside of a separate timer, we could poll VSS once per event loop, but that defaults to 10ms -- workable (if inaccurate) for low speeds, but at higher speeds, this polling rate is too slow. We can speed up the polling rate, but we still have the issue of accuracy. We could improve this by keeping a rolling-average of detected pulse widths using a ring-buffer, but this gives us some lag in speed measurement.

Personally, I'm tempted to replace the busy-wait part of the control loop with a VSS monitoring segment.

Hurricos commented 9 months ago

On the Nano, D7 does not support interrupt generation on level change; this leaves only polling strategies.

ChatGPT has screwed me here. D7 supports interrupt generation without issue, c.f. https://dronebotworkshop.com/interrupts/ -- this pin is on port D.

Hurricos commented 9 months ago

Implemented by calling a new ISR from a pin-change interrupt in PR #3; closing.