This README refers to the serialbridge module used for the Hardware In the Loop (HIL) simulation.
How to Build
The following steps will generate a mex
file that can be used in MATLAB.
With Just
Just takes care of setting the correct extension.
just build-env <MATLAB_PATH>
Where <MATLAB_PATH>
is the path to the MATLAB installation directory. This can be found by running matlabroot
in the MATLAB command window as follows:
fullfile(matlabroot(), 'bin', computer('arch'))
Manually
export MATLAB_LIB_DIR="<MATLAB_PATH>"
cargo build --release
After the build, your file will be available in the target/release/
folder with the name libserial_bridge.dylib
for mac systems and serial_bridge.dll
for windows systems.
mv libserial_bridge.dylib serialbridge.
<EXT>
is the extension of the library file (e.g. mexa64
for Linux, mexw64
for Windows, mexmaci64
for macOS).
Usage
All string arguments should be char arrays (e.g. 'main'
instead of "main"
).
serialbridge can be used in matlab in the following way:
-
serialbridge('Open', '<PORT_ID>', '<PORT_PATH>', 115200)
:-
'Open'
: specifies we want to open the serial port -
<PORT_ID>
: is the name of the serial port (e.g.'main'
,'payload'
, etc.) -
<PORT_PATH>
: is the serial port we want to open (e.g.'COM6'
) -
115200
: is the baudrate of the port
-
-
serialbridge('Write', '<PORT_ID>', single([0, 1, 2]))
:-
'Write'
: specifies that we want to write to the serial port -
<PORT_ID>
: is the name of the serial port (e.g.'main'
,'payload'
, etc.) -
[0, 1.2, 2.3]
: is the array of singles we want to write on serial.
-
-
data = serialbridge('Read', '<PORT_ID>', 10)
;-
'Read'
: specifies that we want to read from the serial port -
<PORT_ID>
: is the name of the serial port (e.g.'main'
,'payload'
, etc.) -
10
: How many floats to read from serial -
data
: array of floats read from the serial (e.g. actuatorData)
-
-
serialbridge('Close', '<PORT_ID>')
:-
'Close'
: specifies we want to close the serial port -
<PORT_ID>
: is the name of the serial port (e.g.'main'
,'payload'
, etc.)
-
Example in Matlab:
serialbridge('Open', 'main', '/dev/ttyACM0', 115200); % Opens the serial port
serialbridge('Write', 'main', single([1 2 3 4])); % Sends the array "[1 2 3 4]" to the serial device
data = serialbridge('Read', 'main', 2); % Receives 2 floats and stores them in the variable "data"
serialbridge('Close', 'main'); % Closes the serial port
Dev
Tho have rustanalyzer working properly, you need to add a config.toml
in the .cargo
directory at the root of this repository with the following content:
[env]
MATLAB_LIB_DIR = <MATLAB_PATH>
To get the <MATLAB_PATH>
you can follow the instructions in the "How to Build" section.