Integer NaN in Rust

Today I want to talk about one cool little thing about Rust that I learned recently.

Rust has a widely used Option type with great language support, and using it prevents a whole class of errors commonly encountered in C++. As a bare metal, cycle-counting aficionado I see these cool features and can’t help but wonder “Sure, but what does it cost?”

It turns out Rust also implements a “null pointer optimization” which allows it to represent an Option of pointer types exactly like you would in C++–as a simple pointer, but with the benefit that the programmer is forced to check it properly. It doesn’t even stop there! Other types with “invalid” encodings can be optimized down to smaller representations, too.

So, how about a common use case for systems using limited communication bandwidth: integer values with a not-a-number canary? It turns out we can get the same optimization to fire!

Read more...

Space Vector Modulation

When implementing control for a sinusoidal permanent magnet synchronous motor, a commonly used element is a hard-switched, two-level, three-phase inteverter. That is, the three motor phases are driven by three half-bridges, and PWM techniques are implemented to drive the required (sinusoidal) output waveforms into each phase.

This post will not attempt to explain the underlying theory of motor control in any great depth–there are plenty of other books, articles, and application notes for that1, some with much prettier graphics–but since I’m in the midst of implementing one such PWM technique (Space Vector Modulation) I thought I’d capture my paper notes into an interactive electronic form. Think of it as a snapshot of my design process, more than an instructive article.

Read more...