..
 #############################################################
 # This file was automatically generated on 2015-05-08.      #
 #                                                           #
 # If you have a bugfix for this file and want to commit it, #
 # please fix the bug in the generator. You can find a link  #
 # to the generators git repository on tinkerforge.com       #
 #############################################################


:breadcrumbs: <a href="../../index.html">Home</a> / <a href="../../index.html#software-shell">Software</a> / Shell - Stepper Brick

.. |device_identifier_constant| unicode:: 0xA0
   :trim:

.. _stepper_brick_shell:

Shell - Stepper Brick
=====================

This is the description of the :ref:`Shell API bindings <api_bindings_shell>` for the :ref:`Stepper Brick <stepper_brick>`. General information and technical
specifications for the Stepper Brick are summarized in its :ref:`hardware description
<stepper_brick_description>`.

An :ref:`installation guide <api_bindings_shell_install>` for the Shell API
bindings is part of their general description.

.. _stepper_brick_shell_examples:


Examples
--------

The example code below is `Public Domain (CC0 1.0)
<http://creativecommons.org/publicdomain/zero/1.0/>`__.

Configuration
^^^^^^^^^^^^^

`Download (example-configuration.sh) <https://github.com/Tinkerforge/stepper-brick/raw/master/software/examples/shell/example-configuration.sh>`__

.. literalinclude:: Stepper_Brick_Shell_example-configuration.sh
 :language: bash
 :linenos:
 :tab-width: 4

Callback
^^^^^^^^

`Download (example-callback.sh) <https://github.com/Tinkerforge/stepper-brick/raw/master/software/examples/shell/example-callback.sh>`__

.. literalinclude:: Stepper_Brick_Shell_example-callback.sh
 :language: bash
 :linenos:
 :tab-width: 4

.. _stepper_brick_shell_api:

API
---

Possible exit codes for all ``tinkerforge`` commands are:

* 1: interrupted (ctrl+c)
* 2: syntax error
* 21: Python 2.5 or newer is required
* 22: Python ``argparse`` module is missing
* 23: socket error
* 24: other exception
* 25: invalid placeholder in format string
* 26: authentication error
* 201: timeout occurred
* 209: invalid argument value
* 210: function is not supported
* 211: unknown error



Command Structure
^^^^^^^^^^^^^^^^^

The common options of the ``call`` and ``dispatch`` commands are documented
:ref:`here <ipcon_shell_api>`. The specific command structure is shown below.

.. sh:function:: X Stinkerforge Pcall Nstepper-brick A[<option>..] L<uid> L<function> L[<argument>..]

 :param <uid>: string
 :param <function>: string

 The ``call`` command is used to call a function of the Stepper Brick. It can take several
 options:

 * ``--help`` shows help for the specific ``call`` command and exits
 * ``--list-functions`` shows a list of known functions of the Stepper Brick and exits


.. sh:function:: X Stinkerforge Pdispatch Nstepper-brick A[<option>..] L<uid> L<callback>

 :param <uid>: string
 :param <callback>: string

 The ``dispatch`` command is used to dispatch a callback of the Stepper Brick. It can
 take several options:

 * ``--help`` shows help for the specific ``dispatch`` command and exits
 * ``--list-callbacks`` shows a list of known callbacks of the Stepper Brick and exits


.. sh:function:: X Stinkerforge Scall Pstepper-brick L<uid> N<function> A[<option>..] L[<argument>..]

 :param <uid>: string
 :param <function>: string

 The ``<function>`` to be called can take different options depending of its
 kind. All functions can take the following options:

 * ``--help`` shows help for the specific function and exits

 Getter functions can take the following options:

 * ``--execute <command>`` shell command line to execute for each incoming
   response (see section about :ref:`output formatting <ipcon_shell_output>`
   for details)

 Setter functions can take the following options:

 * ``--expect-response`` requests response and waits for it

 The ``--expect-response`` option for setter functions allows to detect
 timeouts and other error conditions calls of setters as well. The device will
 then send a response for this purpose. If this option is not given for a
 setter function then no response is send and errors are silently ignored,
 because they cannot be detected.


.. sh:function:: X Stinkerforge Sdispatch Pstepper-brick L<uid> N<callback> A[<option>..]

 :param <uid>: string
 :param <callback>: string

 The ``<callback>`` to be dispatched can take several options:

 * ``--help`` shows help for the specific callback and exits
 * ``--execute <command>`` shell command line to execute for each incoming
   response (see section about :ref:`output formatting <ipcon_shell_output>`
   for details)



Basic Functions
^^^^^^^^^^^^^^^



.. sh:function:: tinkerforge call stepper-brick <uid> set-max-velocity <velocity> 

 :param <velocity>: int

 :noreturn: no output

 Sets the maximum velocity of the stepper motor in steps per second.
 This function does *not* start the motor, it merely sets the maximum
 velocity the stepper motor is accelerated to. To get the motor running use
 either :sh:func:`set-target-position <stepper-brick set-target-position>`, :sh:func:`set-steps <stepper-brick set-steps>`, :sh:func:`drive-forward <stepper-brick drive-forward>` or
 :sh:func:`drive-backward <stepper-brick drive-backward>`.
 
.. sh:function:: tinkerforge call stepper-brick <uid> get-max-velocity  


 :returns velocity: int

 Returns the velocity as set by :sh:func:`set-max-velocity <stepper-brick set-max-velocity>`.
 
.. sh:function:: tinkerforge call stepper-brick <uid> get-current-velocity  


 :returns velocity: int

 Returns the *current* velocity of the stepper motor in steps per second.
 
.. sh:function:: tinkerforge call stepper-brick <uid> set-speed-ramping <acceleration> <deacceleration> 

 :param <acceleration>: int
 :param <deacceleration>: int

 :noreturn: no output

 Sets the acceleration and deacceleration of the stepper motor. The values
 are given in *steps/s²*. An acceleration of 1000 means, that
 every second the velocity is increased by 1000 *steps/s*.
 
 For example: If the current velocity is 0 and you want to accelerate to a
 velocity of 8000 *steps/s* in 10 seconds, you should set an acceleration
 of 800 *steps/s²*.
 
 An acceleration/deacceleration of 0 means instantaneous
 acceleration/deacceleration (not recommended)
 
 The default value is 1000 for both
 
.. sh:function:: tinkerforge call stepper-brick <uid> get-speed-ramping  


 :returns acceleration: int
 :returns deacceleration: int

 Returns the acceleration and deacceleration as set by 
 :sh:func:`set-speed-ramping <stepper-brick set-speed-ramping>`.
 
.. sh:function:: tinkerforge call stepper-brick <uid> full-brake  


 :noreturn: no output

 Executes an active full brake. 
  
 .. warning::
  This function is for emergency purposes,
  where an immediate brake is necessary. Depending on the current velocity and
  the strength of the motor, a full brake can be quite violent.
 
 Call :sh:func:`stop <stepper-brick stop>` if you just want to stop the motor.
 
.. sh:function:: tinkerforge call stepper-brick <uid> set-steps <steps> 

 :param <steps>: int

 :noreturn: no output

 Sets the number of steps the stepper motor should run. Positive values
 will drive the motor forward and negative values backward. 
 The velocity, acceleration and deacceleration as set by
 :sh:func:`set-max-velocity <stepper-brick set-max-velocity>` and :sh:func:`set-speed-ramping <stepper-brick set-speed-ramping>` will be used.
 
.. sh:function:: tinkerforge call stepper-brick <uid> get-steps  


 :returns steps: int

 Returns the last steps as set by :sh:func:`set-steps <stepper-brick set-steps>`.
 
.. sh:function:: tinkerforge call stepper-brick <uid> get-remaining-steps  


 :returns steps: int

 Returns the remaining steps of the last call of :sh:func:`set-steps <stepper-brick set-steps>`.
 For example, if :sh:func:`set-steps <stepper-brick set-steps>` is called with 2000 and 
 :sh:func:`get-remaining-steps <stepper-brick get-remaining-steps>` is called after the motor has run for 500 steps,
 it will return 1500.
 
.. sh:function:: tinkerforge call stepper-brick <uid> drive-forward  


 :noreturn: no output

 Drives the stepper motor forward until :sh:func:`drive-backward <stepper-brick drive-backward>` or
 :sh:func:`stop <stepper-brick stop>` is called. The velocity, acceleration and deacceleration as 
 set by :sh:func:`set-max-velocity <stepper-brick set-max-velocity>` and :sh:func:`set-speed-ramping <stepper-brick set-speed-ramping>` will be used.
 
.. sh:function:: tinkerforge call stepper-brick <uid> drive-backward  


 :noreturn: no output

 Drives the stepper motor backward until :sh:func:`drive-forward <stepper-brick drive-forward>` or
 :sh:func:`stop <stepper-brick stop>` is triggered. The velocity, acceleration and deacceleration as
 set by :sh:func:`set-max-velocity <stepper-brick set-max-velocity>` and :sh:func:`set-speed-ramping <stepper-brick set-speed-ramping>` will be used.
 
.. sh:function:: tinkerforge call stepper-brick <uid> stop  


 :noreturn: no output

 Stops the stepper motor with the deacceleration as set by 
 :sh:func:`set-speed-ramping <stepper-brick set-speed-ramping>`.
 
.. sh:function:: tinkerforge call stepper-brick <uid> set-motor-current <current> 

 :param <current>: int

 :noreturn: no output

 Sets the current in mA with which the motor will be driven.
 The minimum value is 100mA, the maximum value 2291mA and the 
 default value is 800mA.
 
 .. warning::
  Do not set this value above the specifications of your stepper motor.
  Otherwise it may damage your motor.
 
.. sh:function:: tinkerforge call stepper-brick <uid> get-motor-current  


 :returns current: int

 Returns the current as set by :sh:func:`set-motor-current <stepper-brick set-motor-current>`.
 
.. sh:function:: tinkerforge call stepper-brick <uid> enable  


 :noreturn: no output

 Enables the driver chip. The driver parameters can be configured (maximum velocity,
 acceleration, etc) before it is enabled.
 
.. sh:function:: tinkerforge call stepper-brick <uid> disable  


 :noreturn: no output

 Disables the driver chip. The configurations are kept (maximum velocity,
 acceleration, etc) but the motor is not driven until it is enabled again.
 
.. sh:function:: tinkerforge call stepper-brick <uid> is-enabled  


 :returns enabled: bool

 Returns *true* if the driver chip is enabled, *false* otherwise.
 


Advanced Functions
^^^^^^^^^^^^^^^^^^

.. sh:function:: tinkerforge call stepper-brick <uid> set-current-position <position> 

 :param <position>: int

 :noreturn: no output

 Sets the current steps of the internal step counter. This can be used to
 set the current position to 0 when some kind of starting position
 is reached (e.g. when a CNC machine reaches a corner).
 
.. sh:function:: tinkerforge call stepper-brick <uid> get-current-position  


 :returns position: int

 Returns the current position of the stepper motor in steps. On startup
 the position is 0. The steps are counted with all possible driving
 functions (:sh:func:`set-target-position <stepper-brick set-target-position>`, :sh:func:`set-steps <stepper-brick set-steps>`, :sh:func:`drive-forward <stepper-brick drive-forward>` or
 :sh:func:`drive-backward <stepper-brick drive-backward>`). It also is possible to reset the steps to 0 or
 set them to any other desired value with :sh:func:`set-current-position <stepper-brick set-current-position>`.
 
.. sh:function:: tinkerforge call stepper-brick <uid> set-target-position <position> 

 :param <position>: int

 :noreturn: no output

 Sets the target position of the stepper motor in steps. For example,
 if the current position of the motor is 500 and :sh:func:`set-target-position <stepper-brick set-target-position>` is
 called with 1000, the stepper motor will drive 500 steps forward. It will
 use the velocity, acceleration and deacceleration as set by
 :sh:func:`set-max-velocity <stepper-brick set-max-velocity>` and :sh:func:`set-speed-ramping <stepper-brick set-speed-ramping>`.
 
 A call of :sh:func:`set-target-position <stepper-brick set-target-position>` with the parameter *x* is equivalent to
 a call of :sh:func:`set-steps <stepper-brick set-steps>` with the parameter 
 (*x* - :sh:func:`get-current-position <stepper-brick get-current-position>`).
 
.. sh:function:: tinkerforge call stepper-brick <uid> get-target-position  


 :returns position: int

 Returns the last target position as set by :sh:func:`set-target-position <stepper-brick set-target-position>`.
 
.. sh:function:: tinkerforge call stepper-brick <uid> set-step-mode <mode> 

 :param <mode>: int (has symbols)

 :noreturn: no output

 Sets the step mode of the stepper motor. Possible values are:
 
 * Full Step = 1
 * Half Step = 2
 * Quarter Step = 4
 * Eighth Step = 8
 
 A higher value will increase the resolution and
 decrease the torque of the stepper motor.
 
 The default value is 8 (Eighth Step).
 
 The following symbols are available for this function:
 
 * ``full-step`` = 1, for <mode>
 * ``half-step`` = 2, for <mode>
 * ``quarter-step`` = 4, for <mode>
 * ``eighth-step`` = 8, for <mode>
 
.. sh:function:: tinkerforge call stepper-brick <uid> get-step-mode  


 :returns mode: int (has symbols)

 Returns the step mode as set by :sh:func:`set-step-mode <stepper-brick set-step-mode>`.
 
 The following symbols are available for this function:
 
 * ``full-step`` = 1, for mode
 * ``half-step`` = 2, for mode
 * ``quarter-step`` = 4, for mode
 * ``eighth-step`` = 8, for mode
 
.. sh:function:: tinkerforge call stepper-brick <uid> get-stack-input-voltage  


 :returns voltage: int

 Returns the stack input voltage in mV. The stack input voltage is the
 voltage that is supplied via the stack, i.e. it is given by a 
 Step-Down or Step-Up Power Supply.
 
.. sh:function:: tinkerforge call stepper-brick <uid> get-external-input-voltage  


 :returns voltage: int

 Returns the external input voltage in mV. The external input voltage is
 given via the black power input connector on the Stepper Brick. 
  
 If there is an external input voltage and a stack input voltage, the motor
 will be driven by the external input voltage. If there is only a stack 
 voltage present, the motor will be driven by this voltage.
 
 .. warning::
  This means, if you have a high stack voltage and a low external voltage,
  the motor will be driven with the low external voltage. If you then remove
  the external connection, it will immediately be driven by the high
  stack voltage
 
.. sh:function:: tinkerforge call stepper-brick <uid> get-current-consumption  


 :returns current: int

 Returns the current consumption of the motor in mA.
 
.. sh:function:: tinkerforge call stepper-brick <uid> set-decay <decay> 

 :param <decay>: int

 :noreturn: no output

 Sets the decay mode of the stepper motor. The possible value range is
 between 0 and 65535. A value of 0 sets the fast decay mode, a value of
 65535 sets the slow decay mode and a value in between sets the mixed
 decay mode.
 
 Changing the decay mode is only possible if synchronous rectification
 is enabled (see :sh:func:`set-sync-rect <stepper-brick set-sync-rect>`).
 
 For a good explanation of the different decay modes see 
 `this <http://ebldc.com/?p=86/>`__ blog post by Avayan.
 
 A good decay mode is unfortunately different for every motor. The best
 way to work out a good decay mode for your stepper motor, if you can't
 measure the current with an oscilloscope, is to listen to the sound of
 the motor. If the value is too low, you often hear a high pitched 
 sound and if it is too high you can often hear a humming sound.
 
 Generally, fast decay mode (small value) will be noisier but also
 allow higher motor speeds.
 
 The default value is 10000.
 
 .. note::
  There is unfortunately no formula to calculate a perfect decay
  mode for a given stepper motor. If you have problems with loud noises
  or the maximum motor speed is too slow, you should try to tinker with
  the decay value
 
.. sh:function:: tinkerforge call stepper-brick <uid> get-decay  


 :returns decay: int

 Returns the decay mode as set by :sh:func:`set-decay <stepper-brick set-decay>`.
 
.. sh:function:: tinkerforge call stepper-brick <uid> set-sync-rect <sync-rect> 

 :param <sync-rect>: bool

 :noreturn: no output

 Turns synchronous rectification on or off (*true* or *false*).
 
 With synchronous rectification on, the decay can be changed
 (see :sh:func:`set-decay <stepper-brick set-decay>`). Without synchronous rectification fast
 decay is used.
 
 For an explanation of synchronous rectification see 
 `here <http://en.wikipedia.org/wiki/Active_rectification>`__.
 
 .. warning::
  If you want to use high speeds (> 10000 steps/s) for a large 
  stepper motor with a large inductivity we strongly
  suggest that you disable synchronous rectification. Otherwise the
  Brick may not be able to cope with the load and overheat.
 
 The default value is *false*.
 
.. sh:function:: tinkerforge call stepper-brick <uid> is-sync-rect  


 :returns sync-rect: bool

 Returns *true* if synchronous rectification is enabled, *false* otherwise.
 
.. sh:function:: tinkerforge call stepper-brick <uid> set-time-base <time-base> 

 :param <time-base>: int

 :noreturn: no output

 Sets the time base of the velocity and the acceleration of the stepper brick
 (in seconds).
 
 For example, if you want to make one step every 1.5 seconds, you can set 
 the time base to 15 and the velocity to 10. Now the velocity is 
 10steps/15s = 1steps/1.5s.
 
 The default value is 1.
 
.. sh:function:: tinkerforge call stepper-brick <uid> get-time-base  


 :returns time-base: int

 Returns the time base as set by :sh:func:`set-time-base <stepper-brick set-time-base>`.
 
.. sh:function:: tinkerforge call stepper-brick <uid> get-all-data  


 :returns current-velocity: int
 :returns current-position: int
 :returns remaining-steps: int
 :returns stack-voltage: int
 :returns external-voltage: int
 :returns current-consumption: int

 Returns the following parameters: The current velocity,
 the current position, the remaining steps, the stack voltage, the external
 voltage and the current consumption of the stepper motor.
 
 There is also a callback for this function, see :sh:func:`all-data <stepper-brick all-data>`.
 
.. sh:function:: tinkerforge call stepper-brick <uid> enable-status-led  


 :noreturn: no output

 Enables the status LED.
 
 The status LED is the blue LED next to the USB connector. If enabled is is
 on and it flickers if data is transfered. If disabled it is always off.
 
 The default state is enabled.
 
 .. versionadded:: 2.3.1~(Firmware)
 
.. sh:function:: tinkerforge call stepper-brick <uid> disable-status-led  


 :noreturn: no output

 Disables the status LED.
 
 The status LED is the blue LED next to the USB connector. If enabled is is
 on and it flickers if data is transfered. If disabled it is always off.
 
 The default state is enabled.
 
 .. versionadded:: 2.3.1~(Firmware)
 
.. sh:function:: tinkerforge call stepper-brick <uid> is-status-led-enabled  


 :returns enabled: bool

 Returns *true* if the status LED is enabled, *false* otherwise.
 
 .. versionadded:: 2.3.1~(Firmware)
 
.. sh:function:: tinkerforge call stepper-brick <uid> get-protocol1-bricklet-name <port> 

 :param <port>: char

 :returns protocol-version: int
 :returns firmware-version: int,int,int
 :returns name: string

 Returns the firmware and protocol version and the name of the Bricklet for a
 given port.
 
 This functions sole purpose is to allow automatic flashing of v1.x.y Bricklet
 plugins.
 
.. sh:function:: tinkerforge call stepper-brick <uid> get-chip-temperature  


 :returns temperature: int

 Returns the temperature in °C/10 as measured inside the microcontroller. The
 value returned is not the ambient temperature!
 
 The temperature is only proportional to the real temperature and it has an
 accuracy of +-15%. Practically it is only useful as an indicator for
 temperature changes.
 
.. sh:function:: tinkerforge call stepper-brick <uid> reset  


 :noreturn: no output

 Calling this function will reset the Brick. Calling this function
 on a Brick inside of a stack will reset the whole stack.
 
 After a reset you have to create new device objects,
 calling functions on the existing ones will result in
 undefined behavior!
 
.. sh:function:: tinkerforge call stepper-brick <uid> get-identity  


 :returns uid: string
 :returns connected-uid: string
 :returns position: char
 :returns hardware-version: int,int,int
 :returns firmware-version: int,int,int
 :returns device-identifier: int (has symbols)

 Returns the UID, the UID where the Brick is connected to, 
 the position, the hardware and firmware version as well as the
 device identifier.
 
 The position can be '0'-'8' (stack position).
 
 The device identifier numbers can be found :ref:`here <device_identifier>`.
 |device_identifier_constant|
 


Callback Configuration Functions
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^



.. sh:function:: tinkerforge call stepper-brick <uid> set-minimum-voltage <voltage> 

 :param <voltage>: int

 :noreturn: no output

 Sets the minimum voltage in mV, below which the :sh:func:`under-voltage <stepper-brick under-voltage>` callback
 is triggered. The minimum possible value that works with the Stepper Brick is 8V.
 You can use this function to detect the discharge of a battery that is used
 to drive the stepper motor. If you have a fixed power supply, you likely do 
 not need this functionality.
 
 The default value is 8V.
 
.. sh:function:: tinkerforge call stepper-brick <uid> get-minimum-voltage  


 :returns voltage: int

 Returns the minimum voltage as set by :sh:func:`set-minimum-voltage <stepper-brick set-minimum-voltage>`.
 
.. sh:function:: tinkerforge call stepper-brick <uid> set-all-data-period <period> 

 :param <period>: int

 :noreturn: no output

 Sets the period in ms with which the :sh:func:`all-data <stepper-brick all-data>` callback is triggered
 periodically. A value of 0 turns the callback off.
 
.. sh:function:: tinkerforge call stepper-brick <uid> get-all-data-period  


 :returns period: int

 Returns the period as set by :sh:func:`set-all-data-period <stepper-brick set-all-data-period>`.
 


.. _stepper_brick_shell_callbacks:

Callbacks
^^^^^^^^^

Callbacks can be used to receive time critical or recurring data from the
device:

.. code-block:: bash

    tinkerforge dispatch stepper-brick <uid> example

The available callbacks are described below.

.. note::
 Using callbacks for recurring events is *always* preferred
 compared to using getters. It will use less USB bandwidth and the latency
 will be a lot better, since there is no round trip time.

.. sh:function::  tinkerforge dispatch stepper-brick <uid> under-voltage

 :returns voltage: int


 This callback is triggered when the input voltage drops below the value set by
 :sh:func:`set-minimum-voltage <stepper-brick set-minimum-voltage>`. The parameter is the current voltage given
 in mV.
 
.. sh:function::  tinkerforge dispatch stepper-brick <uid> position-reached

 :returns position: int


 This callback is triggered when a position set by :sh:func:`set-steps <stepper-brick set-steps>` or
 :sh:func:`set-target-position <stepper-brick set-target-position>` is reached.
 
 .. note::
  Since we can't get any feedback from the stepper motor, this only works if the
  acceleration (see :sh:func:`set-speed-ramping <stepper-brick set-speed-ramping>`) is set smaller or equal to the
  maximum acceleration of the motor. Otherwise the motor will lag behind the
  control value and the callback will be triggered too early.
 
.. sh:function::  tinkerforge dispatch stepper-brick <uid> all-data

 :returns current-velocity: int
 :returns current-position: int
 :returns remaining-steps: int
 :returns stack-voltage: int
 :returns external-voltage: int
 :returns current-consumption: int


 This callback is triggered periodically with the period that is set by
 :sh:func:`set-all-data-period <stepper-brick set-all-data-period>`. The parameters are: the current velocity,
 the current position, the remaining steps, the stack voltage, the external
 voltage and the current consumption of the stepper motor.
 
.. sh:function::  tinkerforge dispatch stepper-brick <uid> new-state

 :returns state-new: int (has symbols)
 :returns state-previous: int (has symbols)


 This callback is triggered whenever the Stepper Brick enters a new state. 
 It returns the new state as well as the previous state.
 
 Possible states are:
 
 * 1 = Stop
 * 2 = Acceleration
 * 3 = Run
 * 4 = Deacceleration
 * 5 = Direction change to forward
 * 6 = Direction change to backward
 
 The following symbols are available for this function:
 
 * ``stop`` = 1, for state-new and state-previous
 * ``acceleration`` = 2, for state-new and state-previous
 * ``run`` = 3, for state-new and state-previous
 * ``deacceleration`` = 4, for state-new and state-previous
 * ``direction-change-to-forward`` = 5, for state-new and state-previous
 * ``direction-change-to-backward`` = 6, for state-new and state-previous
 


