0

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)
CNCman
  • 43
  • 3
  • Your `encode7bit` will return a variable number of bytes instead of the 5 specified by the protocol. No reason why version 2 wouldn't work except that it's kind of inefficient. – Mark Ransom Jun 26 '20 at 21:19
  • Version 2 is skipping some bits - `s[6]` is not included in either `s[0:6]` or `s[7:13]`, for example. Also, you're putting the least significant bits in the byte labelled "bits 28-32", which I would interpret as being the most significant byte. – jasonharper Jun 26 '20 at 21:24
  • corrected array slicing, still doesn't work. cmd = bytearray([acc.ACCELSTEPPER_TO, dev_no, int(s[0:7], 2), int(s[7:14], 2), int(s[14:21], 2), int(s[21:28], 2), int(s[28:33], 2)]) – CNCman Jun 26 '20 at 21:34

0 Answers0