BLDC Controller phase 6: productisation

In the last post, I’d just got a basic control loop going to control the speed of the motor by varying the throttle. In this post I’ll try to summarise what it took to get from there to something we could use in the robot.

Bench test with 4 motors

Modularisation

Up to now, I’d built the code for controlling the motor in the “main loop” of the program, but I had the goal to control multiple motors from one Pico. To achieve that I refactored the code so that the state variables for each motor were contained in a struct, and I moved the inline code into functions that take a pointer to that struct as first argument (this is a common way of doing “object oriented” programming in C). This gave an API that looked like this:

void motor_init(struct motor_cb *cb, uint pin_a, uint pin_b, uint pin_C, uint pin_pwm_in);
void motor_calibrate(struct motor_cb *cb);
uint16_t motor_get_calibration(struct motor_cb *cb);
...

With that in hand, I could now have four copies of the struct, each configured on different pins. The main loop then just needs to call each motor’s control functions in series to let them each update the state:

...
for (int m_upd_idx = 0; m_upd_idx<4; m_upd_idx++) {
    motor_update(&m[m_upd_idx]);
}
...

Of course, one the code was modularised there was a bunch more tuning to do and I adjusted the control algorithm to use a proper PID control.

Add I2C control

A motor driver isn’t much use if you can’t control it so I used the Pico’s I2C peripheral capability to make it configurable from the main Raspberry Pi. I started with the excellent example from the Pico examples repo and customised it to write 16-bit registers instead of random access.

At time of writing the Pico now has over a dozen registers:

  • An overall control register to enable/disable the motors, trigger calibration, etc.
  • A watchdog timeout register
  • Speed control x4
  • Calibration x4
  • Distance travelled x4
  • Temperature
  • Current
  • Voltage
  • Power

Watchdog

From past experience, I have found that the main Raspberry Pi will crash/reboot/get powered down unexpectedly. It is “really bad” if the motors keep going at full tilt when that happens. To deal with that, I added a watchdog timer; if no commands are received on the I2C bus for a configurable timeout, the Pico shuts down the motors. The main Pi has to “pet the watchdog” a few times a second to stop it from timing out.

DMA

To improve performance, I made use of the RP2040’s DMA controller to shuttle sensor readings from the PIO block to main memory without CPU interaction. I was also able to chain a second DMA to timestamp each sensor reading, which helped a lot with accuracy when runing multiple motors at once. It’s useful to know if the sensor reading is actually from half a millisecond ago. Interrupts were another option but DMA has even lower overhead since it happens without the CPU’s involvement.

INA219

To monitor battery and motor health, I used a second I2C block to connect to an INA219 voltage/current monitor. This is then exposed over I2C.

Temperature

The Pico has a convenient internal temperature probe, I made that available on the I2C bus for monitoring.

Wiring it up

This turned out to be a real challenge. There is a lot to fit into a tiny space:

  • 4 motor driver boards
  • The pico
  • Wiring for the 12 PWM signals
  • 12 motor outputs
  • Power (both motor and pico)
  • I2C
  • 4 sensors, each with 3 wires

I designed a 3D printed module to tame it but it took a lot of careful soldering to squeeze it all in.

A minor disaster

Of course, it didn’t work on the first try; my soldering iron was playing up and I overheated one of the motor driver boards, blowing out a trace. Thankfully I was able to track it down and apply a bodge wire:

Summary

It felt pretty good to hold the finished and working module in my hand after several months of work and it’s working great in the bot (with a bit of tuning still to do to account for the momentum of the bot).

More last-minute redesigns

Its always funny that however much you plan and design digitally, you still find issues when putting it all together.

In this case I’d been working to assemble and test the nerf gun on a fake “bot front” – a duplicate of the front parts of the robot where the accessories connect to it, bolted to a wooden board. It seems the hexagonal axle I’d designed to mount the gun to the robot were not long enough. OK, simple enough fix in CAD, and reprint. The axle halves were then epoxied together and fitted up to the robot. And now we’re not getting enough friction from the press-fit “cup” at the end of the axle to the servo output shaft.

This time the fix was to create a separate cup that screws down onto the servo shaft for a tight and reliable fit, with a hexagonal cutout to put the end of the shaft into. Which fitted perfectly onto the original, too-short axles.

Here’s a shot of the front of the nerf gun on the test stand. The cup is behind the motor cable right at the centre of the shot.

And while we’re here, here’s a side view of the nerf gun on the test stand:

Here you can see the Pi, the gun-mounted camera and the PCA9685 servo driver, which we talk to over I2C to set servo positions.

Design for the autonomous challenges

Three years ago we only attempted one challenge autonomously: Nebula, recognising and approaching four coloured balls. This year we’re going for four or possibly five autonomous challenges, and IMO they’re all harder than that one! Eco Disaster in particular is fiendish.

It needs a ton of code, so how do we make it manageable by putting some structure on the problem? I’ve no idea yet if it will work out, but I’ve gone for the following overall design.

  • We have some model of what we think the arena is. E.g., for Escape Route, what we think the ordering of the coloured blocks is. Need to allow for some level of uncertainty or probability here, rather than dealing with certain knowledge.
  • We have an estimate of where the bot is within the arena – again, uncertain rather than exact.
  • On each iteration of performing a challenge, we:
    • Use available sensors to update our beliefs about what the arena is, and where the bot is within it.
    • Decide the next position for the bot to move to.
    • Initiate movement towards that position.

That still sounds pretty general, but I’ve found it helpful so far. We’re mostly just using the camera as our sensor, but we also have an accurate IMU and measurements of wheel rotations, which hopefully will add up to nearly accurate dead reckoning.

Thus far I’ve coded the next level of detail for Escape Route, which boils down to:

  • Working out the colour, and hence the size, of the first block that we need to move past.
  • Generating target positions, in sequence, to move the bot around it.
  • Repeat for the second and third blocks.

We use the camera to work out the colour, and also to check and adjust bot positioning when we think we should be facing the edge of each block. If we manage to get the ToF hooked up, we might be able to use that as well, as an extra check against bumping into things.

Motor failure

Hardware failure is a fact of life with an event like PiWars. This time one of the gun flywheel motors suddenly stopped working. Swapping the ESC from the other motor, I found that the fault stayed with the motor. Putting a multimeter on the windings, it seems that one of the phases has gone open-circuit (which suggests that a wire has snapped somewhere inside the motor).

Fortunately, I have a policy of always buying 1 more of each unique part than we actually need for the robot. That came about after we had a gearbox failure just before a previous piwars.

In this case, that policy was especially handy since these motors (EMax MT2206) have been in use since 2018 (we used them on tigerbot and wall-e), and it turns out you can no longer get them in the UK – we’d originally got them from TME in Poland, and since Brexit, TME will not sell to UK customers.

Anyway, after swapping in the spare, the nerf gun is operational again. Phew!

Remote control

Shaun’s got remote control mode working. There are 2 remote control modes – Direct and drive-by-wire.

In direct mode, the joystick inputs are translated directly into motor speed settings.

In drive-by-wire mode, the robot maintains a model of what the user desires from the robot – e.g. the heading, speed, strafing, etc. And then applies those “set-points” through a PID controller to make the robot’s actual heading, speed, strafing match the desire.

This makes the robot more resilient to disturbances – you’ve probably seen those videos where someone kicks Boston Dynamics Atlas robot and it recovers? Its a bit like that. In our case, the disturbances are likely to come from uneven floors, loose gravel or turntables on the obstacle course, roughness in the mecanum wheel rollers, etc, etc.

Anyway, here’s Shaun driving round the kitchen in direct mode:

Our favourite use of drive-by-wire came from Tigerbot (Piwars 2018), where the “disturbance” was the turntable. The robot maintained the desired heading, no matter what was going on underneath it. Jump to about 20s in to see the good bit.

Ladders

We’ve now got the ladders printed, attached the Time Of Flight sensor and Camera and added the whole thing to the robot:

The time of flight sensor is this one. It gives an 8×8 “image” of distances. Hopefully mounting it with the camera we can combine the image information with depth information.

The way the sensor mount attaches to the ladders is like a pantograph – the mechanical arrangement ensures that the sensor head continues to point at the same angle even when we raise the ladder. We’re hoping that putting the ladder up high means we can see more of the floor for the minesweeper event.

Another mechanical bug was found here – we need the camera cable to be splittable into two because the cable runs through the bodywork. Parts on order

Firebot mechanical parts (4)

Integrating it all!

A robot isn’t really a robot until you combine the parts together. And that’s also the point that you realise that you’ve designed the parts so that its a real pain to assemble the thing…

For example, we found that the Pi covered the holes which attach the mounting rail to the chassis. While there is an ordering which you can use to assemble the robot, once the battery, Pi and mounting rail are all in, its a massive pain if you need to get the Pi out again (for example to put a PCB Hat on it with connectors for all the things that need to plug into it). Shaun had to redesign how the mounting rail fits on the robot to be able to get it in and out easily:

Now the mounting rail screws into semi-circular blobs mounted in the old mounting holes, and we can now get the Pi in and out to work on the connector PCB:

And once you get it all together, you get to find out if it works as a whole. This is the robot performing a pre-programmed test pattern:

Next step is to get the cosmetic parts printed and on, and get to work seriously on the software. And print the attachments, debug the code, etc, etc. So much to do with just 2 weeks to go!

Firebot mechanical parts (3)

The nerf gun!

This is probably the best view to understand the operation of the gun – with the top shell removed.

The large area in the middle is shaped to allow a standard 6-shot nerf magazine to be inserted. Note the magazine release clip on the rear.

The servo with the arc-shaped actuator pushes the dart out of the magazine and into the flywheels at the front. Those are driven by standard RC brushless motors at 10K+ RPM. Once the pusher gets the dart between those, the dart is grabbed and accelerated out of the gun at silly speeds. We need to be careful to limit the speed so that the dart is below the half joule energy limit required to remain classed as a toy (see EN71).

The studs on the front of the gun unit are holes for mounting the Pi Camera to allow autonomous aiming.

The hexagonal axle connects to a servo mounted on the robot’s accessory point to control elevation of the gun. The robot itself will rotate to traverse the gun.

Here’s a video demonstrating the nerf gun in action.

I’m operating the pusher servo by hand (I don’t have it hooked up to the servo driver yet). You can hear the flywheels grinding down the dart tip(!), because I’m too slow to push it in, hopefully the servo will do it faster.

Firebot mechanical parts (2)

In the last post I talked about the design requirements. In this post I’m going to talk about what we’ve ended up with.

So – here are the (more or less) final mechanical parts:

The ladders hold a “sensor block” which mounts the forward-facing camera.

The front “bumpers” are removable with dovetails to lock them in place. This allows for easy changing of the accessories for different events.

The central section of the cab is removable to reveal the channel for storing barrels and mounting the Nerf gun. Here is the robot configured for Eco Disaster:

Those strange keyhole cutouts at the corners are for holding the magnetic rotary encoder PCBs in just the right spot to read the magnet mounted on the brushless motors that Shaun’s worked so hard on.

The front of the chassis and the rear of the chassis are in two parts, with a pivot between them, allowing the front and back wheels to twist relative to each other. This is to ensure that all 4 mecanum wheels touch the ground (for small amounts of unevenness). The pivot is restricted to about 10 degrees of movement. Mecanum wheels (when moving in some directions) rely on the forces produced by opposite wheels to cancel out – this can only happen if they’re all touching the ground. If one wheel isn’t touching the ground, you can get unintended rotations of the robot.

Here’s a view of just the chassis parts, with the pivot point highlighted in yellow. An M5 bolt, washer and nyloc nut go through that pivot to connect the chassis parts.

Firebot mechanical parts

So, while Shaun’s been working hard on the brushless motor controller, I’ve been playing with CAD, specifically Onshape.

An aside – Onshape is awesome – parametric CAD in the browser, so no need to install anything, works on all platforms that can browse the web, etc, etc. There’s even android and iphone apps so you can view models on the move (though with a small screen its a bit of a pain to make edits). I have a “free” account with Onshape, so all my models are freely available for anyone to browse/copy/etc, I’ll post the link in a later post.

We had a lot of fun last time building Wall-E, so we wanted this year’s concept to be “cute”. The overall concept chosen was a low-poly Fire Engine. And in keeping with “cute”, we’re going to base it on the 1910-1930 Leyland Cub, like this:

Acabashi, CC BY-SA 4.0 https://creativecommons.org/licenses/by-sa/4.0, via Wikimedia Commons https://commons.wikimedia.org/wiki/File:1931_Leyland_Cub_fire_engine_at_Hatfield_Heath_Festival_2017_2.jpg

Other choices which impacted the design:

  • we wanted the robot to have a central channel to be able to store barrels. Ideally we’d be able to move 3 barrels at once in Eco Disaster event
  • we wanted to use mecanum wheels – translating in any direction is fun, and particularly handy for the Hindenburg Disaster event
  • most of the events seem to be aimed at camera/image recognition, so we need a camera.
  • we’ve seen people have problems in past PiWars with robots built to the maximum dimensions, so we’re aiming for a little smaller than those