Show HN: A FPV Lego car, controlled in Rust

dystroy1 pts0 comments

Mastodon-->

Fearless Embedded Rust: A FPV Lego car

Table of Contents

Introduction

Features

Hardware parts

All the parts of the previous Picomobile

Smaller breadboard

Arducam

Buck Converter

Schottky diode

The software

Embedded code

Backend/GUI code

Step by Step

Just an LED

Video stream & Rodent detector

Lego Power

Motor and servo

Lego car

Conclusion

In the previous article I built a wireless car, with Lego servo and Lego motor, powered by a Lego power pack.<br>The code, both the embedded one and the backend one, was in Rust.

Now, I'll make it FPV with an arducam, still controlled in Rust, and still Lego powered.

If you're here to learn, I suggest you really start by reading the previous article.

Features

This is the FPV Picomobile:

real-time video feed from the embedded cam (7 to 8 FPS in 640x480)

arrow keys in the Web GUI control the car, as you'd do in any game

100% of the power comes from the Lego power pack (holding 6 AA 1.5V batteries)

standard Lego servo

standard Lego motor

no_std embedded Rust with multiple async tasks running in parallel to receive commands, drive the car, control the camera, send the image stream, and more

Rust backend communicating with the car in TCP over Wi-Fi and with the browser in HTTP

no separate probe: debugging and installation are done with the USB cable

rodent motion detector (yes, seriously)

Here's what the control GUI looks like:

Hardware parts

All the parts of the previous Picomobile

See here for the Raspberry Pico WH, the Kitronik 5331, small components, and the Lego parts.<br>You'll also see how to cut flat Lego cables.

Smaller breadboard

With a lot of experience in software and almost none in hardware, my initial strategy was to wire the components onto a large breadboard, with a lot of room, and additional jumps to ease refactoring.

Electronics, and especially high-frequency buses such as SPI, really don't like that.

Long wires inside the breadboard behave like tiny antennas and capacitors.

The builds (four of them) I made on large breadboards were very unreliable, driving wasn't possible without crashing, the arducam usually wasn't even responding.

Prototypes I made on small breadboards with as short as possible communication cables have SPI and I2C perfectly working.

So, rather than enduring what I did, start with a small breadboard and short cables.<br>And don't make power paths bigger than necessary, don't use the second power rail to avoid introducing parasitic capacitance or ground loops.

Arducam

I went with the Mini module Camera Arducam Shield OV2640 2MP Plus , which is high resolution, fast enough, can be controlled by I2C (which the Pico has) and can send JPEG-encoded images on a SPI bus (which the Pico has).

That's by far the most expensive part, costing about 30€.

Buck Converter

Writing the software was surprisingly almost straightforward.

Electronics... Not so much.

The biggest challenge, after the inductive coupling and capacitor ones I mentioned in the breadboard chapter, was powering everything, that is the Pico, the Arducam, the Servo, the motor, from the Lego power pack.

Actually, this is the reason I had to build half a dozen different circuits on the small breadboard before finding one which works well. In most of them the power was lacking which made parts, most often the servo, very unreliable.

The Kitronik 5331 is able to provide about 100mA at 3.3V.

It's enough to power the Pico and, with a capacitor added, support the short spikes which are assumed to be at 200mA due to the Wi-Fi (according to the official documentation, which states that "due to the additional current draw of a Pico W's wireless features it is not recommended to use the wireless functionality with this board" this shouldn't be OK, but in my experience there's no problem).

But it proved not enough to also power the Arducam, which itself asks for about 150mA.

I tried first with linear converters, that is components which basically convert the excess voltage into heat, but that didn't work well.

What works is to use a buck converter: those converters waste almost no power.

Take one which can convert from 9V to 5V. They usually have a LED screen to display both the input and the output voltage.

Such converter costs about 3€.

Schottky diode

Sometimes you'll have to plug the USB cable, for example to deploy a new version of the embedded software.

Doing it with no protection of the buck converter would probably fry it.

A Schottky diode, which costs a few cents protects the buck converter from being backfed by the USB's 5V power (VBUS).

I took a 1N5819, which has a low voltage drop (~0.3V to 0.5V) and handles up to 1A of current.

The software

You can fetch it at https://github.com/Canop/picomobile

It's the software for the previous picomobile, for this one, and for the intermediate steps. It works for all.

The repository contains 2 programs:

picomobile-embedded, which runs on the Pico

picomobile-gui, which runs on a...

lego power embedded picomobile breadboard arducam

Related Articles