The Pico has a set of PIO co-processors. They are real-time controllers that can execute logic with deterministic timing. Ideal to run strict-timed sequences and state machines. And to implement extra peripherals.
|
Follow up of Stepper Motor Control with Raspberry Pico PI and DRV8711 driver- Part 3: GPIO .
The First 3 posts dealt with anything except the PIO part. Pin assignments and initialisation, SPI setup, DRV8711 driver and communication. This post handles the PIO.
Nothing new here. This post reuses everything from the hackster project PIO Stepper Motor Control - an example that uses the Allegro A4988 stepper driver.
I selected that one because the PIO part is about as simple as it gets: generate a train of pulses. You can tell it the frequency and how many pulses. Then the state machine generates these for you. The pulses are sent to the stepper motor driver, and that takes care that the motor steps.
Here is the very summere PIO script:
.program stepper_1 ;.side_set 1 .define public STD 1 .wrap_target bitloop: out x, 1 ; Side-set still takes place when instruction stalls jmp !x do_zero ; Branch on the bit we shifted out do_one: set pins 1 [STD + 3] set pins 0 [STD] jmp bitloop do_zero: set pins 0 ; Or drive low, for a short pulse .wrap
There is also the usual PIO init code in that file. Check the author's GitHub for the full listing. It's a good example if you are a beginning PIO designer.
All intelligence is in the main C code.
- Set up of your motor configuration (in my case: 200 steps are a full 360°).
- logic to calculate how many steps to take to move from current position to the requested angle (left turn, right turn or shortest path)
- hand over info to the PIO state machine
Here is that C code from the original author. My Pico project, with sources adapted for DRV8711, is attached at the end of this post.
Main differences:
- DRV8711 requires SPI communication to set it up
- I use its microstep capability. I configured it to use 8 microsteps for a full single step
- I run the PIO at 5 times the speed of the example
- my motor has 200 steps per rotation. With the microsteps configuration that becomes 1600 steps. The original uses a stepper with 800 steps.
A lot more of the logic can be implemented inside the PIO, so that it becomes an autonomous motor manager. You could even try to make it perform speed ramp-up and -down.
But then, this post wouldn't be a simple PIO example anymore.
That's it for this series. Enjoy.