ArdEV3
Use EV3 peripherals from Arduino microcontrollers.
Loading...
Searching...
No Matches
Encoder Hacks

Why are ev3Motor_EncoderGlobal() and ev3Motor_SetupEncoder() needed? In short, because of architecture limitations.

The encoder tracking works by setting an interrupt on the encoder pin: when the voltage is rising (and thus a new wave is starting), the encoder tick count of the motor is increased by one. Since you might have multiple motors, though, the interrupt needs to track which encoder value should be increased - thus, needing a this pointer to the motor. In modern C++, and with 32-bit Arduinos, this is possible through the <FunctionalInterrupt.h> builtin library, which allows you to attach bound functions to an interrupt. Unfortunately, though, on the 8-bit Arduino Uno, memory limitations make it impossible to include this part of the C++ standard library. Thus, in order to attach an interrupt, you have to have an unbound function. ev3Motor_EncoderGlobal(motor) creates one, with a name that's highly unlikely to collide with anything else, for the motor you pass it. ev3Motor_SetupEncoder(motor) then attaches that function to the interrupt.