Q: Python problem converting 32-bit signed long to an array of 7-bit values

76 Views Asked by At

I'm struggling to solve a problem with Python and Firmata since it needs a conversion of 32-bit long into an array of 7-bit (midi style) ints. I have 2 variants, none working, looks like both functions send garbage value of "position". Motor starts and continued spinning indefinitely, not required number of steps. Other functions which don't require this working just fine. How to solve this? Thanks in advance.

From documentation:

Stepper to (absolute move)
Moves a stepper to a desired position based on the number of steps from the zero position.
The position is specified as a 32-bit signed long.
0  START_SYSEX                             (0xF0)
1  ACCELSTEPPER_DATA                       (0x62)
2  to command                              (0x03)
3  device number                           (0-9)
4  position, bits 0-6
5  position, bits 7-13
6  position, bits 14-20
7  position, bits 21-27
8  position, bits 28-32
9  END_SYSEX                               (0xF7)

Version No 1

def accStepFmt_MoveTo(brd, dev_no, pos):
    cmd = bytearray([acc.ACCELSTEPPER_TO, dev_no])
    pos7bit = encode7bit(pos)
    cmd.extend(pos7bit)
    brd.send_sysex(acc.ACCELSTEPPER_DATA, cmd)

def encode7bit(v):
    values = [v & 127]
    v >>= 7
    while v:
        values.insert(0, v & 127 | 128)
        v >>= 7
    return values

Version No 2

def accStepFmt_MoveTo(brd, dev_no, pos):
    s = bin(pos)[2:].zfill(32)
    cmd = bytearray([acc.ACCELSTEPPER_TO, dev_no,
                     int(s[0:6], 2), int(s[7:13], 2), int(s[14:20], 2),
                     int(s[21:27], 2), int(s[28:32], 2)])
    brd.send_sysex(acc.ACCELSTEPPER_DATA, cmd)
0

There are 0 best solutions below