Onward! Let’s make this thing move! (Part 2 – It’s moving, how do I steer this thing?)
I had never been familiar with nor worked with stepper motors. After working with the simplistic drive motor, I was in for a rude awakening as I tried to figure out how to drive this motor. For a quick description on why there are so many wires, I’m not sure I can put it any better than Wikipedia:
Brushed DC motors rotate continuously when DC voltage is applied to their terminals. The stepper motor is known by its property of converting a train of input pulses (typically square wave pulses) into a precisely defined increment in the shaft position. Each pulse moves the shaft through a fixed angle.
Stepper motors effectively have multiple “toothed” electromagnets arranged around a central gear-shaped piece of iron. The electromagnets are energized by an external driver circuit or a micro controller. To make the motor shaft turn, first, one electromagnet is given power, which magnetically attracts the gear’s teeth. When the gear’s teeth are aligned to the first electromagnet, they are slightly offset from the next electromagnet. This means that when the next electromagnet is turned on and the first is turned off, the gear rotates slightly to align with the next one. From there the process is repeated. Each of those rotations is called a “step”, with an integer number of steps making a full rotation. In that way, the motor can be turned by a precise angle.
The circular arrangement of electromagnets is divided into groups, each group called a phase, and there is an equal number of electromagnets per group. The number of groups is chosen by the designer of the stepper motor. The electromagnets of each group are interleaved with the electromagnets of other groups to form a uniform pattern of arrangement. For example, if the stepper motor has two groups identified as A or B, and ten electromagnets in total, then the grouping pattern would be ABABABABAB.
Electromagnets within the same group are all energized together. Because of this, stepper motors with more phases typically have more wires (or leads) to control the motor.
Fortunately, it turns out that Arduino has a library already for handling this complexity. Setting the number of steps was challenging as most of the details on the motor were no longer visible from age. After some trial and error, I settled on 300 steps (for the constructor call to “Stepper”), and with a centered wheel, stepping it, 300 would bring it to exactly 45 degrees. This means any angle could be accommodated by multiplying the desired angle by 6.6 repeating.
The next part was to build a re-calibration mechanism. The issue is that a stepper has no idea where it is. In other words, you can always tell a stepper to turn 90 degrees, and without any knowledge about it’s starting position, it can increment 90 degrees. When you have a bias of what 0 degrees is in relation to the wheel (let’s call that straight), suddenly there is context to saying turn to 90 degrees as opposed to turn 90 degrees. Without knowing where the wheel currently is, it can’t know how to get to 90 degrees in that example. This means that step one is to re-calibrate and center the wheel and call that 0. Fortunately, HERO has a peg that only allows the wheels to turn up to 90 degrees. This means if you tell the wheel to turn 90, then -90, you will then know where the wheel is. Finally, you can set it to 0 and know the wheel is pointing straight.
With this knowledge, I set up the Trinket Pro for I2C communication and created a rudimentary protocol as follows:
– PROTOCOL –
All Responses first byte is OPCODE that it is responding to
First byte received is the opcode:
0x00: STATUS
OPERAND:
none
RESPONSE:
STATUS_READY 0x01
STATUS_MOVING_NONBLOCKED 0x02
STATUS_MOVING 0xF2
STATUS_RECALIBRATING 0xF3
STATUS_FAULT 0xFF
0x01: WHEEL POSITION
OPERAND:EnableSerial
1 BYTE
From MSB:
Angle: 0 – 180 where 0 = full left
0x02: DRIVE
OPERAND:
2 BYTES
From MSB:
Direction (byte)
Speed (byte)0x03: RECALIBRATE
——
0xFE: ABORT
OPERAND:
none
RESPONSE:
none