This is the description of the MQTT API bindings for the Servo Bricklet 2.0. General information and technical specifications for the Servo Bricklet 2.0 are summarized in its hardware description.
An installation guide for the MQTT API bindings is part of their general description.
The example code below is Public Domain (CC0 1.0).
Download (example-callback.txt)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | # Change XYZ to the UID of your Servo Bricklet 2.0
setup:
# Use position reached callback to swing back and forth
subscribe to tinkerforge/callback/servo_v2_bricklet/XYZ/position_reached
if a message arives
# message contains the current servo_channel and position as int
if position is 9000
# Position is 90°, going to -90°
publish '{"servo_channel": 0, "position": -9000}' to tinkerforge/request/servo_v2_bricklet/XYZ/set_position
elseif position is -9000
# Position is -90°, going to 90°
publish '{"servo_channel": 0, "position": 9000}' to tinkerforge/request/servo_v2_bricklet/XYZ/set_position
else
# Another program has set the position, print an error
endif
endif
endsubscribe
publish '{"register": true}' to tinkerforge/register/servo_v2_bricklet/XYZ/position_reached # Register position_reached callback
# Enable position reached callback
publish '{"servo_channel": 0, "enabled": true}' to tinkerforge/request/servo_v2_bricklet/XYZ/set_position_reached_callback_configuration
# Set velocity to 100°/s. This has to be smaller or equal to the
# maximum velocity of the servo you are using, otherwise the position
# reached callback will be called too early
publish '{"servo_channel": 0, "velocity": 10000, "acceleration": 500000, "deceleration": 500000}' to tinkerforge/request/servo_v2_bricklet/XYZ/set_motion_configuration
publish '{"servo_channel": 0, "position": 9000}' to tinkerforge/request/servo_v2_bricklet/XYZ/set_position
publish '{"servo_channel": 0, "enable": true}' to tinkerforge/request/servo_v2_bricklet/XYZ/set_enable
cleanup:
# If you are done, run this to clean up
publish '{"servo_channel": 0, "enable": false}' to tinkerforge/request/servo_v2_bricklet/XYZ/set_enable
|
Download (example-configuration.txt)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | # Change XYZ to the UID of your Servo Bricklet 2.0
setup:
# Servo 1: Connected to port 0, period of 19.5ms, pulse width of 1 to 2ms
# and operating angle -100 to 100°
publish '{"servo_channel": 0, "min": -10000, "max": 10000}' to tinkerforge/request/servo_v2_bricklet/XYZ/set_degree
publish '{"servo_channel": 0, "min": 1000, "max": 2000}' to tinkerforge/request/servo_v2_bricklet/XYZ/set_pulse_width
publish '{"servo_channel": 0, "period": 19500}' to tinkerforge/request/servo_v2_bricklet/XYZ/set_period
publish '{"servo_channel": 0, "velocity": 500000, "acceleration": 1000, "deceleration": 1000}' to tinkerforge/request/servo_v2_bricklet/XYZ/set_motion_configuration # Full velocity with slow ac-/deceleration
# Servo 2: Connected to port 5, period of 20ms, pulse width of 0.95 to 1.95ms
# and operating angle -90 to 90°
publish '{"servo_channel": 5, "min": -9000, "max": 9000}' to tinkerforge/request/servo_v2_bricklet/XYZ/set_degree
publish '{"servo_channel": 5, "min": 950, "max": 1950}' to tinkerforge/request/servo_v2_bricklet/XYZ/set_pulse_width
publish '{"servo_channel": 5, "period": 20000}' to tinkerforge/request/servo_v2_bricklet/XYZ/set_period
publish '{"servo_channel": 5, "velocity": 500000, "acceleration": 500000, "deceleration": 500000}' to tinkerforge/request/servo_v2_bricklet/XYZ/set_motion_configuration # Full velocity with full ac-/deceleration
publish '{"servo_channel": 0, "position": 10000}' to tinkerforge/request/servo_v2_bricklet/XYZ/set_position # Set to most right position
publish '{"servo_channel": 0, "enable": true}' to tinkerforge/request/servo_v2_bricklet/XYZ/set_enable
publish '{"servo_channel": 5, "position": -9000}' to tinkerforge/request/servo_v2_bricklet/XYZ/set_position # Set to most left position
publish '{"servo_channel": 5, "enable": true}' to tinkerforge/request/servo_v2_bricklet/XYZ/set_enable
cleanup:
# If you are done, run this to clean up
publish '{"servo_channel": 0, "enable": false}' to tinkerforge/request/servo_v2_bricklet/XYZ/set_enable
publish '{"servo_channel": 5, "enable": false}' to tinkerforge/request/servo_v2_bricklet/XYZ/set_enable
|
All published payloads to and from the MQTT bindings are in JSON format.
If an error occures, the bindings publish a JSON object containing the error message as member _ERROR
.
It is published on the corresponding response topic: .../response/...
for .../request/...
and .../callback/...
for .../register/...
.
Every function of the Servo Brick API that has a servo_channel parameter can
address a servo with the servo channel (0 to 9). If it is a setter function then
multiple servos can be addressed at once with a bitmask for the
servos, if the highest bit is set. For example: 1
will address servo 1,
(1 << 1) | (1 << 5) | (1 << 15)
will address servos 1 and 5.
This allows to set configurations to several
servos with one function call. It is guaranteed that the changes will take
effect in the same PWM period for all servos you specified in the bitmask.
request/
servo_v2_bricklet/
<UID>/
get_status
¶Request: |
|
---|---|
Response: |
|
Returns the status information of the Servo Bricklet 2.0.
The status includes
Please note that the position and the velocity is a snapshot of the current position and velocity of the servo in motion.
request/
servo_v2_bricklet/
<UID>/
set_enable
¶Request: |
|
---|---|
Response: |
|
Enables a servo channel (0 to 9). If a servo is enabled, the configured position, velocity, acceleration, etc. are applied immediately.
request/
servo_v2_bricklet/
<UID>/
get_enabled
¶Request: |
|
---|---|
Response: |
|
Returns true if the specified servo channel is enabled, false otherwise.
request/
servo_v2_bricklet/
<UID>/
set_position
¶Request: |
|
---|---|
Response: |
|
Sets the position in °/100 for the specified servo channel.
The default range of the position is -9000 to 9000, but it can be specified
according to your servo with request/servo_v2_bricklet/<UID>/set_degree
.
If you want to control a linear servo or RC brushless motor controller or
similar with the Servo Brick, you can also define lengths or speeds with
request/servo_v2_bricklet/<UID>/set_degree
.
request/
servo_v2_bricklet/
<UID>/
get_position
¶Request: |
|
---|---|
Response: |
|
Returns the position of the specified servo channel as set by request/servo_v2_bricklet/<UID>/set_position
.
request/
servo_v2_bricklet/
<UID>/
get_current_position
¶Request: |
|
---|---|
Response: |
|
Returns the current position of the specified servo channel. This may not be the
value of request/servo_v2_bricklet/<UID>/set_position
if the servo is currently approaching a
position goal.
request/
servo_v2_bricklet/
<UID>/
get_current_velocity
¶Request: |
|
---|---|
Response: |
|
Returns the current velocity of the specified servo channel. This may not be the
velocity specified by request/servo_v2_bricklet/<UID>/set_motion_configuration
. if the servo is
currently approaching a velocity goal.
request/
servo_v2_bricklet/
<UID>/
set_motion_configuration
¶Request: |
|
---|---|
Response: |
|
Sets the maximum velocity of the specified servo channel in °/100s as well as the acceleration and deceleration in °/100s²
With a velocity of 0 °/100s the position will be set immediately (no velocity).
With an acc-/deceleration of 0 °/100s² the velocity will be set immediately (no acc-/deceleration).
request/
servo_v2_bricklet/
<UID>/
get_motion_configuration
¶Request: |
|
---|---|
Response: |
|
Returns the motion configuration as set by request/servo_v2_bricklet/<UID>/set_motion_configuration
.
request/
servo_v2_bricklet/
<UID>/
set_pulse_width
¶Request: |
|
---|---|
Response: |
|
Sets the minimum and maximum pulse width of the specified servo channel in µs.
Usually, servos are controlled with a PWM, whereby the length of the pulse controls the position of the servo. Every servo has different minimum and maximum pulse widths, these can be specified with this function.
If you have a datasheet for your servo that specifies the minimum and maximum pulse width, you should set the values accordingly. If your servo comes without any datasheet you have to find the values via trial and error.
Both values have a range from 1 to 65535 (unsigned 16-bit integer). The minimum must be smaller than the maximum.
The default values are 1000µs (1ms) and 2000µs (2ms) for minimum and maximum pulse width.
request/
servo_v2_bricklet/
<UID>/
get_pulse_width
¶Request: |
|
---|---|
Response: |
|
Returns the minimum and maximum pulse width for the specified servo channel as set by
request/servo_v2_bricklet/<UID>/set_pulse_width
.
request/
servo_v2_bricklet/
<UID>/
set_degree
¶Request: |
|
---|---|
Response: |
|
Sets the minimum and maximum degree for the specified servo channel (by default given as °/100).
This only specifies the abstract values between which the minimum and maximum
pulse width is scaled. For example: If you specify a pulse width of 1000µs
to 2000µs and a degree range of -90° to 90°, a call of request/servo_v2_bricklet/<UID>/set_position
with 0 will result in a pulse width of 1500µs
(-90° = 1000µs, 90° = 2000µs, etc.).
Possible usage:
request/servo_v2_bricklet/<UID>/set_position
with a resolution of cm/100. Also the velocity will
have a resolution of cm/100s and the acceleration will have a resolution of
cm/100s².request/servo_v2_bricklet/<UID>/set_position
now controls the rpm.Both values have a possible range from -32767 to 32767 (signed 16-bit integer). The minimum must be smaller than the maximum.
The default values are -9000 and 9000 for the minimum and maximum degree.
request/
servo_v2_bricklet/
<UID>/
get_degree
¶Request: |
|
---|---|
Response: |
|
Returns the minimum and maximum degree for the specified servo channel as set by
request/servo_v2_bricklet/<UID>/set_degree
.
request/
servo_v2_bricklet/
<UID>/
set_period
¶Request: |
|
---|---|
Response: |
|
Sets the period of the specified servo channel in µs.
Usually, servos are controlled with a PWM. Different servos expect PWMs with different periods. Most servos run well with a period of about 20ms.
If your servo comes with a datasheet that specifies a period, you should set it accordingly. If you don't have a datasheet and you have no idea what the correct period is, the default value (19.5ms) will most likely work fine.
The minimum possible period is 1µs and the maximum is 1000000µs.
The default value is 19.5ms (19500µs).
request/
servo_v2_bricklet/
<UID>/
get_period
¶Request: |
|
---|---|
Response: |
|
Returns the period for the specified servo channel as set by request/servo_v2_bricklet/<UID>/set_period
.
request/
servo_v2_bricklet/
<UID>/
get_servo_current
¶Request: |
|
---|---|
Response: |
|
Returns the current consumption of the specified servo channel in mA.
request/
servo_v2_bricklet/
<UID>/
set_servo_current_configuration
¶Request: |
|
---|---|
Response: |
|
Sets the averaging duration of the current measurement for the specified servo channel in ms.
request/
servo_v2_bricklet/
<UID>/
get_servo_current_configuration
¶Request: |
|
---|---|
Response: |
|
Returns the servo current configuration for the specified servo channel as set
by request/servo_v2_bricklet/<UID>/set_servo_current_configuration
.
request/
servo_v2_bricklet/
<UID>/
set_input_voltage_configuration
¶Request: |
|
---|---|
Response: |
|
Sets the averaging duration of the input voltage measurement for the specified servo channel in ms.
request/
servo_v2_bricklet/
<UID>/
get_input_voltage_configuration
¶Request: |
|
---|---|
Response: |
|
Returns the input voltage configuration as set by request/servo_v2_bricklet/<UID>/set_input_voltage_configuration
.
request/
servo_v2_bricklet/
<UID>/
get_overall_current
¶Request: |
|
---|---|
Response: |
|
Returns the current consumption of all servos together in mA.
request/
servo_v2_bricklet/
<UID>/
get_input_voltage
¶Request: |
|
---|---|
Response: |
|
Returns the input voltage in mV. The input voltage is given via the black power input connector on the Servo Brick.
request/
servo_v2_bricklet/
<UID>/
set_current_calibration
¶Request: |
|
---|---|
Response: |
|
Sets an offset value (in mA) for each channel.
Note: On delivery the Servo Bricklet 2.0 is already calibrated.
request/
servo_v2_bricklet/
<UID>/
get_current_calibration
¶Request: |
|
---|---|
Response: |
|
Returns the current calibration as set by request/servo_v2_bricklet/<UID>/set_current_calibration
.
request/
servo_v2_bricklet/
<UID>/
get_spitfp_error_count
¶Request: |
|
---|---|
Response: |
|
Returns the error count for the communication between Brick and Bricklet.
The errors are divided into
The errors counts are for errors that occur on the Bricklet side. All Bricks have a similar function that returns the errors on the Brick side.
request/
servo_v2_bricklet/
<UID>/
set_status_led_config
¶Request: |
|
---|---|
Response: |
|
Sets the status LED configuration. By default the LED shows communication traffic between Brick and Bricklet, it flickers once for every 10 received data packets.
You can also turn the LED permanently on/off or show a heartbeat.
If the Bricklet is in bootloader mode, the LED is will show heartbeat by default.
The following symbols are available for this function:
For config:
request/
servo_v2_bricklet/
<UID>/
get_status_led_config
¶Request: |
|
---|---|
Response: |
|
Returns the configuration as set by request/servo_v2_bricklet/<UID>/set_status_led_config
The following symbols are available for this function:
For config:
request/
servo_v2_bricklet/
<UID>/
get_chip_temperature
¶Request: |
|
---|---|
Response: |
|
Returns the temperature 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 bad accuracy. Practically it is only useful as an indicator for temperature changes.
request/
servo_v2_bricklet/
<UID>/
reset
¶Request: |
|
---|---|
Response: |
|
Calling this function will reset the Bricklet. All configurations will be lost.
After a reset you have to create new device objects, calling functions on the existing ones will result in undefined behavior!
request/
servo_v2_bricklet/
<UID>/
get_identity
¶Request: |
|
---|---|
Response: |
|
Returns the UID, the UID where the Bricklet is connected to, the position, the hardware and firmware version as well as the device identifier.
The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port). A Bricklet connected to an Isolator Bricklet is always at position 'z'.
The device identifier numbers can be found here. If symbolic output is not disabled, the device identifier is mapped to the corresponding name in the format used in topics.
The display name contains the Servo 2.0's name in a human readable form.
request/
servo_v2_bricklet/
<UID>/
set_position_reached_callback_configuration
¶Request: |
|
---|---|
Response: |
|
Enable/Disable register/servo_v2_bricklet/<UID>/position_reached
callback.
request/
servo_v2_bricklet/
<UID>/
get_position_reached_callback_configuration
¶Request: |
|
---|---|
Response: |
|
Returns the callback configuration as set by
request/servo_v2_bricklet/<UID>/set_position_reached_callback_configuration
.
Callbacks can be registered to receive
time critical or recurring data from the device. The registration is done
with the corresponding .../register/...
topic and an optional suffix.
This suffix can be used to deregister the callback later.
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.
register/
servo_v2_bricklet/
<UID>/
position_reached
¶Register Request: |
|
---|---|
Callback Response: |
|
A callback can be registered for this event by publishing to the .../register/servo_v2_bricklet/<UID>/position_reached[/<SUFFIX>]
topic with the payload "true".
An added callback can be removed by publishing to the same topic with the payload "false".
To support multiple (de)registrations, e.g. for message filtering, an optional suffix can be used.
If the callback is triggered, a message with it's payload is published under the corresponding .../callback/servo_v2_bricklet/<UID>/position_reached[/<SUFFIX>]
topic for each registered suffix.
This callback is triggered when a position set by request/servo_v2_bricklet/<UID>/set_position
is reached. If the new position matches the current position then the
callback is not triggered, because the servo didn't move.
The callback payload members are the servo and the position that is reached.
You can enable this callback with request/servo_v2_bricklet/<UID>/set_position_reached_callback_configuration
.
Note
Since we can't get any feedback from the servo, this only works if the
velocity (see request/servo_v2_bricklet/<UID>/set_motion_configuration
) is set smaller or equal to the
maximum velocity of the servo. Otherwise the servo will lag behind the
control value and the callback will be triggered too early.
Internal functions are used for maintenance tasks such as flashing a new firmware of changing the UID of a Bricklet. These task should be performed using Brick Viewer instead of using the internal functions directly.
request/
servo_v2_bricklet/
<UID>/
set_bootloader_mode
¶Request: |
|
---|---|
Response: |
|
Sets the bootloader mode and returns the status after the requested mode change was instigated.
You can change from bootloader mode to firmware mode and vice versa. A change from bootloader mode to firmware mode will only take place if the entry function, device identifier and CRC are present and correct.
This function is used by Brick Viewer during flashing. It should not be necessary to call it in a normal user program.
The following symbols are available for this function:
For mode:
For status:
request/
servo_v2_bricklet/
<UID>/
get_bootloader_mode
¶Request: |
|
---|---|
Response: |
|
Returns the current bootloader mode, see request/servo_v2_bricklet/<UID>/set_bootloader_mode
.
The following symbols are available for this function:
For mode:
request/
servo_v2_bricklet/
<UID>/
set_write_firmware_pointer
¶Request: |
|
---|---|
Response: |
|
Sets the firmware pointer for request/servo_v2_bricklet/<UID>/write_firmware
. The pointer has
to be increased by chunks of size 64. The data is written to flash
every 4 chunks (which equals to one page of size 256).
This function is used by Brick Viewer during flashing. It should not be necessary to call it in a normal user program.
request/
servo_v2_bricklet/
<UID>/
write_firmware
¶Request: |
|
---|---|
Response: |
|
Writes 64 Bytes of firmware at the position as written by
request/servo_v2_bricklet/<UID>/set_write_firmware_pointer
before. The firmware is written
to flash every 4 chunks.
You can only write firmware in bootloader mode.
This function is used by Brick Viewer during flashing. It should not be necessary to call it in a normal user program.
request/
servo_v2_bricklet/
<UID>/
write_uid
¶Request: |
|
---|---|
Response: |
|
Writes a new UID into flash. If you want to set a new UID you have to decode the Base58 encoded UID string into an integer first.
We recommend that you use Brick Viewer to change the UID.
request/
servo_v2_bricklet/
<UID>/
read_uid
¶Request: |
|
---|---|
Response: |
|
Returns the current UID as an integer. Encode as Base58 to get the usual string version.