Skip to content

Commit

Permalink
Merge pull request #3 from jwithelm/feature/add_simulink_library_block
Browse files Browse the repository at this point in the history
Feature/add simulink library block
  • Loading branch information
ennerf authored Jan 20, 2023
2 parents 87d793d + f5115c2 commit 4bff9f5
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### 1.2.1 (Jan 12, 2023)
* Added Simulink library for HebiJoystick

### 1.2 (Jan 26, 2017)
* Added support for keyboards
* Changed project name from HebiJoystick to MatlabInput
Expand Down
10 changes: 8 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<modelVersion>4.0.0</modelVersion>

<groupId>us.hebi.matlab</groupId>
<version>1.3-SNAPSHOT</version>
<artifactId>input</artifactId>
<version>1.2.1</version>

<name>matlab-input</name>
<description>MATLAB library for joystick and keyboard input</description>
Expand All @@ -27,6 +27,12 @@
</developer>
</developers>

<contributors>
<contributor>
<name>Jonas Withelm</name>
</contributor>
</contributors>

<scm>
<url>https://github.com/HebiRobotics/MatlabInput</url>
<connection>scm:git:git://github.com/HebiRobotics/MatlabInput.git</connection>
Expand Down Expand Up @@ -158,4 +164,4 @@
</plugins>
</build>

</project>
</project>
28 changes: 19 additions & 9 deletions src/main/assembly/release.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
Expand All @@ -7,18 +8,31 @@
<format>tar.gz</format>
<format>dir</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
<!-- Copy Readme, License -->
<fileSet>
<directory>${project.basedir}/</directory>
<includes>
<include>README*</include>
<include>LICENSE*</include>
</includes>
</fileSet>
<!-- Copy backing MATLAB scripts -->
<fileSet>
<directory>${project.build.directory}/classes/matlab/</directory>
<outputDirectory>hebi/</outputDirectory>
<outputDirectory>matlab/</outputDirectory>
<lineEnding>dos</lineEnding>
</fileSet>
<!-- Copy backing Simulink scripts -->
<fileSet>
<directory>${project.build.directory}/classes/simulink/</directory>
<outputDirectory>simulink/</outputDirectory>
</fileSet>
<!-- Copy Java library (includes all Java dependencies) -->
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>hebi/</outputDirectory>
<directory>${project.build.directory}/</directory>
<outputDirectory>matlab/</outputDirectory>
<includes>
<include>${releaseName}.jar</include>
</includes>
Expand All @@ -27,7 +41,7 @@
<dependencySets>
<!-- Copy native binaries into lib directory -->
<dependencySet>
<outputDirectory>hebi/lib/</outputDirectory>
<outputDirectory>matlab/lib/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>provided</scope>
Expand All @@ -37,7 +51,3 @@
</dependencySet>
</dependencySets>
</assembly>




121 changes: 121 additions & 0 deletions src/main/resources/simulink/hebijoyinput.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
function hebijoyinput(block)
% hebijoyinput is a HebiJoystick implementation for Simulink
%
% hebijoyinput is an alternative to MathWorks's 'Joystick Input'
% block from 'Simulink 3D Animation' toolbox. 'hebijoyinput' is based
% on 'HebiJoystick'. 'Simulink 3D Animation' toolbox is not required
% for usage!
%
% See also:
% HebiJoystick (https://github.com/HebiRobotics/MatlabInput)
% Level-2 MATLAB S-Function (msfuntmpl)

% Copyright (c) 2023 Jonas Withelm
% SPDX-License-Identifier: Apache-2.0

setup(block);

%endfunction


function setup(block)

%% Register number of dialog parameters
block.NumDialogPrms = 3; % joyid
block.DialogPrmsTunable = {'Nontunable','Nontunable','Nontunable'}; % not tunable during simulation

%% Setup joystick
joyid = block.DialogPrm(1).Data;
forcefeed = block.DialogPrm(3).Data;

% initialize joystick
joy = HebiJoystick(joyid);

% read capabilities of joystick
caps = joy.caps();

% store persistent data
set_param(block.BlockHandle,'UserData',joy);

%% Register number of output ports
block.NumOutputPorts = 2;

%% Register number of input ports
if forcefeed && (caps.Forces > 0)
block.NumInputPorts = 1;
else
block.NumInputPorts = 0;
end

%% Set up the port properties to be inherited or dynamic
block.SetPreCompInpPortInfoToDynamic;
block.SetPreCompOutPortInfoToDynamic;

%% Set the output port properties
block.OutputPort(1).DimensionsMode = 'Fixed';
block.OutputPort(1).SamplingMode = 'sample';
block.OutputPort(1).Dimensions = caps.Axes;
block.OutputPort(1).DatatypeID = 0; % double
block.OutputPort(1).Complexity = 'Real';

block.OutputPort(2).DimensionsMode = 'Fixed';
block.OutputPort(2).SamplingMode = 'sample';
block.OutputPort(2).Dimensions = caps.Buttons;
block.OutputPort(2).DatatypeID = 0; % double
block.OutputPort(2).Complexity = 'Real';

if forcefeed && (caps.Forces > 0)
block.InputPort(1).DimensionsMode = 'Fixed';
block.InputPort(1).SamplingMode = 'sample';
block.InputPort(1).Dimensions = caps.Forces;
block.InputPort(1).DatatypeID = 0; % double
block.InputPort(1).Complexity = 'Real';
block.InputPort(1).DirectFeedthrough = false;
end

%% Set up the continuous states
block.NumContStates = 0;

%% Set block sample time
block.SampleTimes = [1/50 0]; % Discrete sample time (50 Hz)

%% Set the block simStateCompliance to default (i.e., same as a built-in block)
block.SimStateCompliance = 'DefaultSimState';

%% Register methods
block.RegBlockMethod('Outputs', @Outputs);
block.RegBlockMethod('Terminate', @Terminate);

%endfunction


function Outputs(block)

joy = get_param(block.BlockHandle,'UserData');

caps = joy.caps();

% read force feedback from block input and send to joystick
forcefeed = block.DialogPrm(3).Data;
if forcefeed && (caps.Forces > 0)
for idx=1:caps.Forces
joy.force(idx, block.InputPort(idx).Data);
end
end

% read axes and buttons from joystick and write to block outputs
[axes, buttons, ~] = joy.read();

block.OutputPort(1).Data = axes;
block.OutputPort(2).Data = buttons;

%endfunction


function Terminate(block)

joy = get_param(block.BlockHandle,'UserData');

joy.close();

%endfunction
Binary file added src/main/resources/simulink/matlabinput_lib.slx
Binary file not shown.
13 changes: 13 additions & 0 deletions src/main/resources/simulink/slblocks.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function blkStruct = slblocks
% This function specifies that the library 'matlabinput_lib'
% should appear in the Library Browser with the
% name 'MatlabInput'

Browser.Library = 'matlabinput_lib';
% 'matlabinput_lib' is the name of the library

Browser.Name = 'MatlabInput';
% 'MatlabInput' is the library name that appears
% in the Library Browser

blkStruct.Browser = Browser;

0 comments on commit 4bff9f5

Please sign in to comment.