This is the description of the MATLAB/Octave API bindings for the Joystick Bricklet. General information and technical specifications for the Joystick Bricklet are summarized in its hardware description.
An installation guide for the MATLAB/Octave API bindings is part of their general description.
The example code below is Public Domain (CC0 1.0).
Download (matlab_example_simple.m)
1function matlab_example_simple()
2 import com.tinkerforge.IPConnection;
3 import com.tinkerforge.BrickletJoystick;
4
5 HOST = 'localhost';
6 PORT = 4223;
7 UID = 'XYZ'; % Change XYZ to the UID of your Joystick Bricklet
8
9 ipcon = IPConnection(); % Create IP connection
10 j = handle(BrickletJoystick(UID, ipcon), 'CallbackProperties'); % Create device object
11
12 ipcon.connect(HOST, PORT); % Connect to brickd
13 % Don't use device before ipcon is connected
14
15 % Get current position
16 position = j.getPosition();
17
18 fprintf('Position [X]: %i\n', position.x);
19 fprintf('Position [Y]: %i\n', position.y);
20
21 input('Press key to exit\n', 's');
22 ipcon.disconnect();
23end
Download (matlab_example_callback.m)
1function matlab_example_callback()
2 import com.tinkerforge.IPConnection;
3 import com.tinkerforge.BrickletJoystick;
4
5 HOST = 'localhost';
6 PORT = 4223;
7 UID = 'XYZ'; % Change XYZ to the UID of your Joystick Bricklet
8
9 ipcon = IPConnection(); % Create IP connection
10 j = handle(BrickletJoystick(UID, ipcon), 'CallbackProperties'); % Create device object
11
12 ipcon.connect(HOST, PORT); % Connect to brickd
13 % Don't use device before ipcon is connected
14
15 % Register pressed callback to function cb_pressed
16 set(j, 'PressedCallback', @(h, e) cb_pressed(e));
17
18 % Register released callback to function cb_released
19 set(j, 'ReleasedCallback', @(h, e) cb_released(e));
20
21 input('Press key to exit\n', 's');
22 ipcon.disconnect();
23end
24
25% Callback function for pressed callback
26function cb_pressed(e)
27 fprintf('Pressed\n');
28end
29
30% Callback function for released callback
31function cb_released(e)
32 fprintf('Released\n');
33end
Download (matlab_example_find_borders.m)
1function matlab_example_find_borders()
2 import com.tinkerforge.IPConnection;
3 import com.tinkerforge.BrickletJoystick;
4
5 HOST = 'localhost';
6 PORT = 4223;
7 UID = 'XYZ'; % Change XYZ to the UID of your Joystick Bricklet
8
9 ipcon = IPConnection(); % Create IP connection
10 j = handle(BrickletJoystick(UID, ipcon), 'CallbackProperties'); % Create device object
11
12 ipcon.connect(HOST, PORT); % Connect to brickd
13 % Don't use device before ipcon is connected
14
15 % Get threshold callbacks with a debounce time of 0.2 seconds (200ms)
16 j.setDebouncePeriod(200);
17
18 % Register position reached callback to function cb_position_reached
19 set(j, 'PositionReachedCallback', @(h, e) cb_position_reached(e));
20
21 % Configure threshold for position "outside of -99, -99 to 99, 99"
22 j.setPositionCallbackThreshold('o', -99, 99, -99, 99);
23
24 input('Press key to exit\n', 's');
25 ipcon.disconnect();
26end
27
28% Callback function for position reached callback
29function cb_position_reached(e)
30 if e.y == 100
31 fprintf('Top\n');
32 elseif e.y == -100
33 fprintf('Bottom\n');
34 end
35 if e.x == 100
36 fprintf('Right\n');
37 elseif e.x == -100
38 fprintf('Left\n');
39 end
40 fprintf('\n');
41end
Download (octave_example_simple.m)
1function octave_example_simple()
2 more off;
3
4 HOST = "localhost";
5 PORT = 4223;
6 UID = "XYZ"; % Change XYZ to the UID of your Joystick Bricklet
7
8 ipcon = javaObject("com.tinkerforge.IPConnection"); % Create IP connection
9 j = javaObject("com.tinkerforge.BrickletJoystick", UID, ipcon); % Create device object
10
11 ipcon.connect(HOST, PORT); % Connect to brickd
12 % Don't use device before ipcon is connected
13
14 % Get current position
15 position = j.getPosition();
16
17 fprintf("Position [X]: %d\n", java2int(position.x));
18 fprintf("Position [Y]: %d\n", java2int(position.y));
19
20 input("Press key to exit\n", "s");
21 ipcon.disconnect();
22end
23
24function int = java2int(value)
25 if compare_versions(version(), "3.8", "<=")
26 int = value.intValue();
27 else
28 int = value;
29 end
30end
Download (octave_example_callback.m)
1function octave_example_callback()
2 more off;
3
4 HOST = "localhost";
5 PORT = 4223;
6 UID = "XYZ"; % Change XYZ to the UID of your Joystick Bricklet
7
8 ipcon = javaObject("com.tinkerforge.IPConnection"); % Create IP connection
9 j = javaObject("com.tinkerforge.BrickletJoystick", UID, ipcon); % Create device object
10
11 ipcon.connect(HOST, PORT); % Connect to brickd
12 % Don't use device before ipcon is connected
13
14 % Register pressed callback to function cb_pressed
15 j.addPressedCallback(@cb_pressed);
16
17 % Register released callback to function cb_released
18 j.addReleasedCallback(@cb_released);
19
20 input("Press key to exit\n", "s");
21 ipcon.disconnect();
22end
23
24% Callback function for pressed callback
25function cb_pressed(e)
26 fprintf("Pressed\n");
27end
28
29% Callback function for released callback
30function cb_released(e)
31 fprintf("Released\n");
32end
Download (octave_example_find_borders.m)
1function octave_example_find_borders()
2 more off;
3
4 HOST = "localhost";
5 PORT = 4223;
6 UID = "XYZ"; % Change XYZ to the UID of your Joystick Bricklet
7
8 ipcon = javaObject("com.tinkerforge.IPConnection"); % Create IP connection
9 j = javaObject("com.tinkerforge.BrickletJoystick", UID, ipcon); % Create device object
10
11 ipcon.connect(HOST, PORT); % Connect to brickd
12 % Don't use device before ipcon is connected
13
14 % Get threshold callbacks with a debounce time of 0.2 seconds (200ms)
15 j.setDebouncePeriod(200);
16
17 % Register position reached callback to function cb_position_reached
18 j.addPositionReachedCallback(@cb_position_reached);
19
20 % Configure threshold for position "outside of -99, -99 to 99, 99"
21 j.setPositionCallbackThreshold("o", -99, 99, -99, 99);
22
23 input("Press key to exit\n", "s");
24 ipcon.disconnect();
25end
26
27% Callback function for position reached callback
28function cb_position_reached(e)
29 x = java2int(e.x);
30 y = java2int(e.y);
31
32 if y == 100
33 fprintf("Top\n");
34 elseif y == -100
35 fprintf("Bottom\n");
36 end
37 if x == 100
38 fprintf("Right\n");
39 elseif x ==-100
40 fprintf("Left\n");
41 end
42 fprintf("\n");
43end
44
45function int = java2int(value)
46 if compare_versions(version(), "3.8", "<=")
47 int = value.intValue();
48 else
49 int = value;
50 end
51end
Generally, every method of the MATLAB bindings that returns a value can
throw a TimeoutException. This exception gets thrown if the
device did not respond. If a cable based connection is used, it is
unlikely that this exception gets thrown (assuming nobody unplugs the
device). However, if a wireless connection is used, timeouts will occur
if the distance to the device gets too big.
Beside the TimeoutException there is also a NotConnectedException that
is thrown if a method needs to communicate with the device while the
IP Connection is not connected.
Since the MATLAB bindings are based on Java and Java does not support multiple return values and return by reference is not possible for primitive types, we use small classes that only consist of member variables. The member variables of the returned objects are described in the corresponding method descriptions.
The package for all Brick/Bricklet bindings and the IP Connection is
com.tinkerforge.*
All methods listed below are thread-safe.
| Parameters: |
|
|---|---|
| Returns: |
|
Creates an object with the unique device ID uid.
In MATLAB:
import com.tinkerforge.BrickletJoystick;
joystick = BrickletJoystick('YOUR_DEVICE_UID', ipcon);
In Octave:
joystick = java_new("com.tinkerforge.BrickletJoystick", "YOUR_DEVICE_UID", ipcon);
This object can then be used after the IP Connection is connected.
| Return Object: |
|
|---|
Returns the position of the joystick. The middle position of the joystick is x=0, y=0.
The returned values are averaged and calibrated (see calibrate()).
If you want to get the position periodically, it is recommended to use the
PositionCallback callback and set the period with
setPositionCallbackPeriod().
| Returns: |
|
|---|
Returns true if the button is pressed and false otherwise.
It is recommended to use the PressedCallback and ReleasedCallback callbacks
to handle the button.
| Return Object: |
|
|---|
Returns the values as read by a 12-bit analog-to-digital converter.
Note
The values returned by getPosition() are averaged over several samples
to yield less noise, while getAnalogValue() gives back raw
unfiltered analog values. The only reason to use getAnalogValue() is,
if you need the full resolution of the analog-to-digital converter.
If you want the analog values periodically, it is recommended to use the
AnalogValueCallback callback and set the period with
setAnalogValueCallbackPeriod().
Calibrates the middle position of the joystick. If your Joystick Bricklet does not return x=0 and y=0 in the middle position, call this function while the joystick is standing still in the middle position.
The resulting calibration will be saved on the EEPROM of the Joystick Bricklet, thus you only have to calibrate it once.
| Return Object: |
|
|---|
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. There is also a constant for the device identifier of this Bricklet.
| Parameters: |
|
|---|
Sets the period with which the PositionCallback callback is triggered
periodically. A value of 0 turns the callback off.
The PositionCallback callback is only triggered if the position has changed since the
last triggering.
| Returns: |
|
|---|
Returns the period as set by setPositionCallbackPeriod().
| Parameters: |
|
|---|
Sets the period with which the AnalogValueCallback callback is triggered
periodically. A value of 0 turns the callback off.
The AnalogValueCallback callback is only triggered if the analog values have
changed since the last triggering.
| Returns: |
|
|---|
Returns the period as set by setAnalogValueCallbackPeriod().
| Parameters: |
|
|---|
Sets the thresholds for the PositionReachedCallback callback.
The following options are possible:
Option |
Description |
|---|---|
'x' |
Callback is turned off |
'o' |
Callback is triggered when the position is outside the min and max values |
'i' |
Callback is triggered when the position is inside the min and max values |
'<' |
Callback is triggered when the position is smaller than the min values (max is ignored) |
'>' |
Callback is triggered when the position is greater than the min values (max is ignored) |
The following constants are available for this function:
For option:
BrickletJoystick.THRESHOLD_OPTION_OFF = 'x'
BrickletJoystick.THRESHOLD_OPTION_OUTSIDE = 'o'
BrickletJoystick.THRESHOLD_OPTION_INSIDE = 'i'
BrickletJoystick.THRESHOLD_OPTION_SMALLER = '<'
BrickletJoystick.THRESHOLD_OPTION_GREATER = '>'
| Return Object: |
|
|---|
Returns the threshold as set by setPositionCallbackThreshold().
The following constants are available for this function:
For option:
BrickletJoystick.THRESHOLD_OPTION_OFF = 'x'
BrickletJoystick.THRESHOLD_OPTION_OUTSIDE = 'o'
BrickletJoystick.THRESHOLD_OPTION_INSIDE = 'i'
BrickletJoystick.THRESHOLD_OPTION_SMALLER = '<'
BrickletJoystick.THRESHOLD_OPTION_GREATER = '>'
| Parameters: |
|
|---|
Sets the thresholds for the AnalogValueReachedCallback callback.
The following options are possible:
Option |
Description |
|---|---|
'x' |
Callback is turned off |
'o' |
Callback is triggered when the analog values are outside the min and max values |
'i' |
Callback is triggered when the analog values are inside the min and max values |
'<' |
Callback is triggered when the analog values are smaller than the min values (max is ignored) |
'>' |
Callback is triggered when the analog values are greater than the min values (max is ignored) |
The following constants are available for this function:
For option:
BrickletJoystick.THRESHOLD_OPTION_OFF = 'x'
BrickletJoystick.THRESHOLD_OPTION_OUTSIDE = 'o'
BrickletJoystick.THRESHOLD_OPTION_INSIDE = 'i'
BrickletJoystick.THRESHOLD_OPTION_SMALLER = '<'
BrickletJoystick.THRESHOLD_OPTION_GREATER = '>'
| Return Object: |
|
|---|
Returns the threshold as set by setAnalogValueCallbackThreshold().
The following constants are available for this function:
For option:
BrickletJoystick.THRESHOLD_OPTION_OFF = 'x'
BrickletJoystick.THRESHOLD_OPTION_OUTSIDE = 'o'
BrickletJoystick.THRESHOLD_OPTION_INSIDE = 'i'
BrickletJoystick.THRESHOLD_OPTION_SMALLER = '<'
BrickletJoystick.THRESHOLD_OPTION_GREATER = '>'
| Parameters: |
|
|---|
Sets the period with which the threshold callbacks
are triggered, if the thresholds
keep being reached.
| Returns: |
|
|---|
Returns the debounce period as set by setDebouncePeriod().
Callbacks can be registered to receive time critical or recurring data from the device. The registration is done with "set" function of MATLAB. The parameters consist of the IP Connection object, the callback name and the callback function. For example, it looks like this in MATLAB:
function my_callback(e)
fprintf('Parameter: %s\n', e.param);
end
set(device, 'ExampleCallback', @(h, e) my_callback(e));
Due to a difference in the Octave Java support the "set" function cannot be used in Octave. The registration is done with "add*Callback" functions of the device object. It looks like this in Octave:
function my_callback(e)
fprintf("Parameter: %s\n", e.param);
end
device.addExampleCallback(@my_callback);
It is possible to add several callbacks and to remove them with the corresponding "remove*Callback" function.
The parameters of the callback are passed to the callback function as fields of
the structure e, which is derived from the java.util.EventObject class.
The available callback names with corresponding structure fields 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.
| Event Object: |
|
|---|
This callback is triggered periodically with the period that is set by
setPositionCallbackPeriod(). The parameter is the position of the
joystick.
The PositionCallback callback is only triggered if the position has changed since the
last triggering.
In MATLAB the set() function can be used to register a callback function
to this callback.
In Octave a callback function can be added to this callback using the
addPositionCallback() function. An added callback function can be removed with
the removePositionCallback() function.
| Event Object: |
|
|---|
This callback is triggered periodically with the period that is set by
setAnalogValueCallbackPeriod(). The parameters are the
analog values of the joystick.
The AnalogValueCallback callback is only triggered if the values have changed
since the last triggering.
In MATLAB the set() function can be used to register a callback function
to this callback.
In Octave a callback function can be added to this callback using the
addAnalogValueCallback() function. An added callback function can be removed with
the removeAnalogValueCallback() function.
| Event Object: |
|
|---|
This callback is triggered when the threshold as set by
setPositionCallbackThreshold() is reached.
The parameters are the position of the joystick.
If the threshold keeps being reached, the callback is triggered periodically
with the period as set by setDebouncePeriod().
In MATLAB the set() function can be used to register a callback function
to this callback.
In Octave a callback function can be added to this callback using the
addPositionReachedCallback() function. An added callback function can be removed with
the removePositionReachedCallback() function.
| Event Object: |
|
|---|
This callback is triggered when the threshold as set by
setAnalogValueCallbackThreshold() is reached.
The parameters are the analog values of the joystick.
If the threshold keeps being reached, the callback is triggered periodically
with the period as set by setDebouncePeriod().
In MATLAB the set() function can be used to register a callback function
to this callback.
In Octave a callback function can be added to this callback using the
addAnalogValueReachedCallback() function. An added callback function can be removed with
the removeAnalogValueReachedCallback() function.
| Event Object: |
|
|---|
This callback is triggered when the button is pressed.
In MATLAB the set() function can be used to register a callback function
to this callback.
In Octave a callback function can be added to this callback using the
addPressedCallback() function. An added callback function can be removed with
the removePressedCallback() function.
| Event Object: |
|
|---|
This callback is triggered when the button is released.
In MATLAB the set() function can be used to register a callback function
to this callback.
In Octave a callback function can be added to this callback using the
addReleasedCallback() function. An added callback function can be removed with
the removeReleasedCallback() function.
Virtual functions don't communicate with the device itself, but operate only on the API bindings device object. They can be called without the corresponding IP Connection object being connected.
| Return Object: |
|
|---|
Returns the version of the API definition implemented by this API bindings. This is neither the release version of this API bindings nor does it tell you anything about the represented Brick or Bricklet.
| Parameters: |
|
|---|---|
| Returns: |
|
Returns the response expected flag for the function specified by the function ID parameter. It is true if the function is expected to send a response, false otherwise.
For getter functions this is enabled by default and cannot be disabled,
because those functions will always send a response. For callback configuration
functions it is enabled by default too, but can be disabled by
setResponseExpected(). For setter functions it is disabled by default
and can be enabled.
Enabling the response expected flag for a setter function allows to detect timeouts and other error conditions calls of this setter as well. The device will then send a response for this purpose. If this flag is disabled for a setter function then no response is sent and errors are silently ignored, because they cannot be detected.
The following constants are available for this function:
For functionId:
BrickletJoystick.FUNCTION_CALIBRATE = 4
BrickletJoystick.FUNCTION_SET_POSITION_CALLBACK_PERIOD = 5
BrickletJoystick.FUNCTION_SET_ANALOG_VALUE_CALLBACK_PERIOD = 7
BrickletJoystick.FUNCTION_SET_POSITION_CALLBACK_THRESHOLD = 9
BrickletJoystick.FUNCTION_SET_ANALOG_VALUE_CALLBACK_THRESHOLD = 11
BrickletJoystick.FUNCTION_SET_DEBOUNCE_PERIOD = 13
| Parameters: |
|
|---|
Changes the response expected flag of the function specified by the function ID parameter. This flag can only be changed for setter (default value: false) and callback configuration functions (default value: true). For getter functions it is always enabled.
Enabling the response expected flag for a setter function allows to detect timeouts and other error conditions calls of this setter as well. The device will then send a response for this purpose. If this flag is disabled for a setter function then no response is sent and errors are silently ignored, because they cannot be detected.
The following constants are available for this function:
For functionId:
BrickletJoystick.FUNCTION_CALIBRATE = 4
BrickletJoystick.FUNCTION_SET_POSITION_CALLBACK_PERIOD = 5
BrickletJoystick.FUNCTION_SET_ANALOG_VALUE_CALLBACK_PERIOD = 7
BrickletJoystick.FUNCTION_SET_POSITION_CALLBACK_THRESHOLD = 9
BrickletJoystick.FUNCTION_SET_ANALOG_VALUE_CALLBACK_THRESHOLD = 11
BrickletJoystick.FUNCTION_SET_DEBOUNCE_PERIOD = 13
| Parameters: |
|
|---|
Changes the response expected flag for all setter and callback configuration functions of this device at once.
This constant is used to identify a Joystick Bricklet.
The getIdentity() function and the
IPConnection.EnumerateCallback
callback of the IP Connection have a deviceIdentifier parameter to specify
the Brick's or Bricklet's type.
This constant represents the human readable name of a Joystick Bricklet.