On our family holiday to Dorset where we have gone to hunt for fossils on the Jurassic Coast most years since our children were born. By coincidence I find myself working on my robot in the same holiday let conservatory where I worked on my first ever Pi Wars robot back in 2019.
There is another advantage that comes from using PID control, in that you set a target velocity and the PID controller takes the motor to that velocity in a progressive way. So there is some built in dampening which smooths out sudden changes in requested velocity from the driver. This protects the motor gear boxes from excessive strain which can otherwise strip the teeth from cogs in the gearboxes. If you suddenly slam the power joystick from full power forwards to full power reverse, the PID controller starts to adjust the motor power to comply, but does not immediately give the motor full reverse power. It backs off the power a little, measures the effect on the velocity by reading the encoders and re-adjusts accordingly. The driving is still really responsive, but the motors do not break under the strain.
On to the second objective: mechanum driving. I was disappointed to find that the examples for the Motor2040 just set motor speeds to drift left or right, or move forwards or backwards. No omnidirectional drifting. So I looked for information on the internet and eventually found this excellent article with code examples (in C). I managed to reimplement the equations in MicroPython, finding some bugs in their example which had me scratching my head for a few days until I got it all working. Some of my problems where due to me mixing up my motors, and some due to a copy and paste error in the article where they use the wrong variable to adjust for turn input. Now I could move my robot in any direction using one joystick on my controller, while rotating the robot on the spot using the left-right channel from the other joystick. I think this ability to move in 2 dimensions while independently rotating to face in another direction will be really handy in Pi-Noon!
The final piece of the puzzle was to measure the robot heading, so that I could implement field oriented driving. This is where the robot moves in directions relative to the driver position/arena, regardless of which direction it is actually facing. To put it another way, up on the joystick moves the robot up the arena (away from you) and down moves it back towards you. You can spin the robot around to face in any direction and it still moves away/towards you when you push up or down on the joystick. The magic of mechanum wheels in conjunction with matching wheel velocities due to the velocity controlled encoder motor driving, allowing the robot to move in any direction while pointing in any unrelated direction. The robot just needs to be able to measure it's heading, which requires an IMU. Luckily I had been learning how to use the BNO01 IMU chips on my other small robot project (see my earlier blog entry for Piwars 2024). This IMU chip has on-board sensor fusion, where it combines the 3 axis compass, 3 axis accelerometer and 3 axis gyro readings to calculate a 3 dimensional orientation vector for the robot. Using this you can determine which direction the robot is facing at any point in time, and subtract that angle from the direction the mechanum drive equations are being given to move the robot. This provides field oriented driving, which should be very useful in several of the challenges.