Emulated Rotary Encoder

Overview

While consulting on a large multidisciplinary project in the early proof-of-concept phase, we offered support to another team beyond our initial remit.

Here, we investigated a misbehaving multi-axis motion control system, delivering a working solution despite harsh constraints.

Situation

ODrive S1 controllers were employed, and would frequently disengage due to an error detecting the axis position.

We established that the controller was connected to a remote encoder via approximately 1.5 metres of individual thin wires.

Approach

Electromagnetic Interference (EMI) was the prime suspect, with the main aggressor likely to be long motor phase wires running in parallel to the encoder signals.

Due to the tight design and extreme time constraints, we proposed switching to RS-485, emulating the supported AMT21 series encoders, and shortening the SPI link to less than 10cm.

We ordered new components for next-morning delivery, and began the design and implementation.

Hidden Requirement

With the parts assembled and initial firmware loaded, we discovered that the ODrive was throwing an immediate MISSING_ESTIMATE error.

Unfortunately, we had overlooked the AMT21’s very tight 3 µs turnaround time - had we spotted it earlier, we might have selected a faster MCU.

Solution

The ATmega4809 running at 20 MHz has no chance of calculating and sending the response in time, let alone querying the SPI encoder too (3 µs is just 60 CPU cycles).

To resolve this, we rewrote the firmware with careful scheduling and aggressive optimisations.

Result

The result was a very robust encoder, which fully resolved the issues experienced by our colleagues, meeting the deadline without requiring new wiring or integration of a different encoder.

The time elapsed from agreeing a plan of action to delivering a solution was under 24 hours.

Technical Journey (hide)

The original setup had the AS5048A encoder connected directly to the ODrive's SPI interface over ~1.5 metres of individual thin wires. In this state, the drive was regularly presenting the MISSING_ESTIMATE error. Being a single-ended bus designed for internal communication, SPI has no inherent rejection of external noise. We measured the clock at ~1.56 MHz with fast edges, and observed reflections on the signals. Reducing the clock speed and adding termination improved the situation while the motors were idle, but did not result in more reliable operation once the motion control system was active.

The AMT21 encoders come in a couple of options - 2 Mbaud or "adjustable data rate". Only the former variants are supported by ODrive, as the adjustable models are limited to 115,200 baud - a response would take more than two of the ODrive's 8 kHz control cycle periods. An ATmega4809 can only reach 2 Mbaud with the main clock prescaler disabled (CLK_PER at the full 20 MHz) and the CLK2X flag set in the USART peripheral (2.5 Mbaud is the absolute fastest).

Initial testing of the emulator found that the ODrive enforces a hard deadline just beyond the 3 µs turnaround specification, requiring a workaround. We allow the ODrive's first request after power-up to go unanswered and start a timer. When the timeout occurs, we retrieve the current position from the SPI encoder (yellow trace below), validate the parity and error flags, and pre-calculate the emulated AMT21 response. Finally, when the next request is received from the ODrive (blue trace), we immediately transmit the ready-and-waiting response from the ISR (red trace), before re-triggering the timer. This allows us to reduce the sample latency, while still meeting the tight turnaround specification.

In the end, we produced remarkably simple firmware that runs almost entirely from interrupt routines. The coordinated operation results in a sample being retrieved just ~17 µs before the ODrive's next request arrives, and the implementation was dependable and error-free during many hours of operation.

← Previous Story | Next Story →

Can we help with your project?

Contact us!