Go Down

Topic: Help needed: PyFyrmata & AccelStepperFirmata (Read 841 times) previous topic - next topic

cncman

Jun 06, 2020, 11:49 pm Last Edit: Jun 06, 2020, 11:55 pm by cncman
Hi,

I'm trying to get RaspberryPi and Arduino due to work with each other via PyFirmata in order to control two synchronized stepper motors. Arduino will be used mainly as a signal generator for stepper motors, with acceleration/deceleration, plus extra IO ports, where RaspPi falls short.
I used firmata builder in order to create Arduino sketch with digital/analog IO and AccelStepperFirmata.
http://firmatabuilder.com/

Communication between RaspberryPi works, I could blink Arduino LED from RaspPi.

Now I'm somewhat stumbled with AccelStepperFirmata, which documentation lacks examples.
Googling AccelStepperFirmata reveals almost nothing, found only this page which is close to the subject but not AccelStepperFirmata.
OK, so each command sent from RaspPi to Arduino looks like this (please correct if I'm wrong here):

Firmata.write(START_SYSEX)
Firmata.write(ACCELSTEPPER_DATA)
Firmata.write(0x09) # set speed
Firmata.write(some commands here)
Firmata.write(END_SYSEX)

Speed and acceleration set in an awkward fashion, instead of plain floats or long ints.
What is human-readable Python formula in order to convert let's say 5, 50, 120, 600 steps/sec to these command bits?
Thanks in advance for any help.

*************

(maxSpeed in steps per sec)
4  maxSpeed, bits 0-6                     
5  maxSpeed, bits 7-13
6  maxSpeed, bits 14-20
7  maxSpeed, bits 21-27

 (acceleration in steps/sec^2)
4  accel, bits 0-6                       
5  accel, bits 7-13
6  accel, bits 14-20
7  accel, bits 21-27

MrY

Take a look at this page that describes the commands and the bit positions to be populated for each command.

There is no simple built-in python construct to help you here. You will have to roll your own.

Welcome to the wonderful world of Firmata :-).

cncman

I read this doc. The way it sets values is really cumbersome to say at least.
And there are no examples of PyFyrmata + AccelStepperFirmata.


MrY

In order to support the feature you are looking for, the Firmata client would need to do so. The wackiness of the protocol normally is hidden by the client.

The only Firmata client that I am aware of that supports the stepper library you wish to use is a JavaScript version.

Being the author of pymata4, I have been asked to support the accelerStepper library but will not do so because:

1. The author does not permit direct contact. It makes it kind of difficult to get support when something breaks.
2. It is published on a website that does not allow users to enter or see issues.
3. It was only recently GPL'ed

cncman

accelStepperFirmata supports AccelStepper in an awkward way I don't understand.
Sequence of commands packed in a byteaaray if I got it correctly (or may be not).

Some numbers like speed and acceleration rate represented as "AccelStepperFirmata's Custom Float Format" according to manual. As there are no examples in Python I don't get into designer's mind.

Anyone can share snippets of python accelStepperFirmata sources? Googling results in nothing, unfortunately.

********
Floats sent and received by accelStepperFirmata are composed of a 23-bit significand (mantissa) and a 4-bit, base 10 exponent (biased -11 with an explicit 1's bit) and a sign bit.
0-20    21    22-25    26-27
least significant bits    sign    exponent    most significant bits
21 bits    1 bit    4 bits    2 bits

These values allow a range from 8.388608*10^-11 to 83886.08. Small enough to represent one step per year and large enough to exceed our max achievable stepper speed.

cncman

Being the author of pymata4, I have been asked to support the accelerStepper library but will not do so because:

1. The author does not permit direct contact. It makes it kind of difficult to get support when something breaks.
2. It is published on a website that does not allow users to enter or see issues.
3. It was only recently GPL'ed
PyMata was in consideration instead of AccelStepperFirmata but I understood from your web site AccelStepper was not supported.  Its a pity since PyMata have really cool features like latches.

cncman

#6
Jun 09, 2020, 10:50 pm Last Edit: Jun 09, 2020, 10:53 pm by cncman
Code: [Select]
Stepper to (absolute move)

Moves a stepper to a desired position based on the number of steps from the zero position. 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)


This is more or less clear.
pos = 5500
pos_ba = pos.to_bytes(pos, 4, sys.byteorder(), signed=True)

So the remaining problem is that custom float format.

MrY

Here is a link to JavaScript client code that performs the custom float encode and decode. It should be fairly easy to port to Python if that is what you are trying to do.

Go Up