0

A raspberry pi is connected to three stepper motor controllers. Each controller needs two gpios: one for direction (high is clockwise, low is counterclockwise), and one for a step signal.

Setting a GPIO high and low again will trigger a step of the stepper motor.

My current code controls the steppers "directly" though python's RPi.GPIO library by switching the appropriate GPIOs as required. As it turns out, this method is not precise enough.

The steppers have to follow a rather complex set of instructions, that are currently calculated on the fly. I want to switch to a method, where all instructions for the whole run are calculated in advance and stored in a file.

My idea is that I end up with a binary file, that looks something like this:

0x00110100
0x00100000
0x01110100
0x00100000
0x00110100
0x01100000
etc...

Each byte (8 bits) contain all information needed for all stepper motors to take their respective actions. Let me explain in detail:

The first two bits of the first byte are 00. This indicates that the direction of stepper 1 is set to counterclockwise (first 0) and that no step has to be taken (second 0). The second pair of bits are 11, so stepper 2 is set to clockwise and a step has to be taken. Bits 5 and 6 are for stepper 3, bits 7 and 8 are for stepper 4 (the 4th one is not needed at the moment, but it's good to have room for expansion).

I considered something like this as a start:

with open("stepperdata", "rb") as f:
while (byte := f.read(1)):
    # send the bits from this byte to the GPIOs.

Each such byte should now be sent to the pins of the connected stepper controllers once. The process has to be repeated 1600 times a second. This means, that each stepper can - at maximum - receive 800 step commands per second (in the form of alternating 1s and 0s on its step pin).

So 1600 times a second a new byte is read, split into bits, and distributed to the gpio pins.

The 1600 Hz is basically my sampling frequency for the stepper motion. I would like to go even higher to 3200 Hz or 6400 Hz to reduce jitter at speeds that approach the sampling rate (those are indeed noticeable), but I'd be perfectly happy to stay at 1600 Hz.

My problem: I don't know how to send such a byte stream to the pins using python. I'm aware of the pigpio library, that should be able to do something like this, but have not found a way to accomplish it.

Any help would be greatly appreciated.

PS: I know how to do something similar just using a RP2040 and a shift register - but in this case the data files are just to big to be handled by the RP2040.

Nuke
  • 19
  • 1

0 Answers0