ThesetupImpl
andstepImpl
methods hook the C functions to the System object™. You need to initialize the color sensor only once at model initialization. Hence, thecolorSensor_Init
function is called in thesetupImpl
method. This function is declared in thecolorSensor.h
file. To read the RGB values of the color sensor, thecolorSensor_Step
function is called in thesetupImpl
method. Nothing needs to be done at termination. The stepImpl
method for the colorSensor
system object defines the red, green, and blue output. Follow these steps to update the initial, output, and terminal code sections of thecolorSensor
年代ystem object that you created in 年代elect System Object Template.
In the MATLAB®editor, open thecolorSensor.m
file.
Update thesetupImpl
method using the following code.
methods(Access = protected)functionsetupImpl(obj)%#okifcoder.target('Rtw')coder.cinclude('colorSensor.h'); coder.ceval('colorSensor_Init');else% Place simulation setup code hereendend...end
The coder.ceval
function executes calls to the C wrapper functions in digitalio_arduino.h
function. The second and third arguments of coder.ceval
are the Arduino®hardware pin number and value, respectively.
Update thestepImpl
method with the following code.
methods(Access = protected) ...function[red,green,blue] = stepImpl(obj)%#okred=double(0); green =double(0); blue=double(0);ifcoder.target('Rtw')% Call C-function implementing device output coder.cinclude('colorSensor.h'); coder.ceval('colorSensor_Step',coder.wref(red),coder.wref(green),coder.wref(blue));else% Place simulation output code herered=0; green=0; blue=0;endend...end
Update thereleaseImpl
method with the following code.
methods(Access = protected) ...functionreleaseImpl(obj)%#okifcoder.target('Rtw')% Call C-function implementing device termination% No termination code for Arduinoelse% Place simulation termination code hereendend...end
年代ave the changes tocolorSensor.m
.
In the next section, you willUpdate Paths for Source and Header Files.