System call from Simulink with external hardware (Raspberry PI)

5 views (last 30 days)
I want to make a system call out of a Matlab Simulink model running on external hardware. In my case I want to switch the original Raspberry Pi Touch Display (7") off and an.
I tried using a Matlab function with a Matlab "system" command but it just doesn't have any affect on the display (the system calls itself works with the terminal).
function display_backlight(old_status)
coder.extrinsic('system')
if old_status == 1
system('echo 1 | sudo tee /sys/class/backlight/rpi_backlight/bl_power')
else
system('echo 0 | sudo tee /sys/class/backlight/rpi_backlight/bl_power')
end
end
Any ideas how to make this work or do I need to use another block like mentioned here https://stackoverflow.com/questions/4875981/system-call-from-simulink-possible (Link in the answer doesn't work)
Or do I even have to write this in C and integrate this to Simulink?
BTW: I asked the same question on stackoverflow but didn't get a response there (https://stackoverflow.com/questions/50024134/system-call-from-simulink-with-external-hardware-raspberry-pi)

Accepted Answer

Uwe Lau
Uwe Lau on 4 May 2018
Edited: Uwe Lau on 4 May 2018
This is my solution as it won't work directly from a matlab simulink block:
  • 1. Add a "Matlab System" Block
  • 2. Add an .m source file which runs .c/.h code.
display_backlight_on.m
classdef display_backlight_on < matlab.System ...
& coder.ExternalDependency ...
& matlab.system.mixin.Propagates ...
& matlab.system.mixin.CustomIcon
properties
% Public, tunable properties.
end
properties (Nontunable)
% Public, non-tunable properties.
end
properties (Access = private)
% Pre-computed constants.
end
methods
% Constructor
function obj = display_backlight_on_trigger(varargin)
% Support name-value pair arguments when constructing the object.
setProperties(obj,nargin,varargin{:});
end
end
methods (Access=protected)
function setupImpl(obj) %#ok<MANU>
end
function stepImpl(obj,turn_on) %#ok<INUSD>
if isempty(coder.target)
% Place simulation output code here
else
% Call C-function implementing device output
coder.cinclude('display_backlight_on.h');
coder.ceval('display_backlight_on_command',turn_on);
end
end
function releaseImpl(obj) %#ok<MANU>
if isempty(coder.target)
% Place simulation termination code here
else
% Call C-function implementing device termination
%coder.ceval('sink_terminate');
end
end
end
methods (Access=protected)
%%Define input properties
function num = getNumInputsImpl(~)
num = 1;
end
function num = getNumOutputsImpl(~)
num = 0;
end
function flag = isInputSizeLockedImpl(~,~)
flag = true;
end
function varargout = isInputFixedSizeImpl(~,~)
varargout{1} = true;
end
function flag = isInputComplexityLockedImpl(~,~)
flag = true;
end
function icon = getIconImpl(~)
% Define a string as the icon for the System block in Simulink.
icon = 'display_backlight_on';
end
end
methods (Static, Access=protected)
function simMode = getSimulateUsingImpl(~)
simMode = 'Interpreted execution';
end
function isVisible = showSimulateUsingImpl
isVisible = false;
end
end
methods (Static)
function name = getDescriptiveName()
name = 'display_backlight_on';
end
function b = isSupportedContext(context)
b = context.isCodeGenTarget('rtw');
end
function updateBuildInfo(buildInfo, context)
if context.isCodeGenTarget('rtw')
% Update buildInfo
srcDir = fullfile(fileparts(mfilename('fullpath')),'../src'); %#ok<NASGU>
includeDir = fullfile(fileparts(mfilename('fullpath')),'../include');
addIncludePaths(buildInfo,includeDir);
addSourceFiles(buildInfo,'display_backlight_on.c',srcDir);
end
end
end
end
  • 3. Add a .c/.h file which runs the system command from c.
display_backlight_on.c
#include <display_backlight_on.h>
char command[50];
void display_backlight_on_command(boolean_T turn_on)
{
if (turn_on == 1)
{
strcpy( command, "echo 0 | sudo tee /sys/class/backlight/rpi_backlight/bl_power" );
system(command);
}
}
display_backlight_on.h
#ifndef _DISPLAY_BACKLIGHT_ON_H_
#define _DISPLAY_BACKLIGHT_ON_H_
#include "rtwtypes.h"
void display_backlight_on_command(boolean_T turn_on);
#endif //_DISPLAY_BACKLIGHT_ON_H_

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!