Main Content

年代pecify Initialization, Output, and Termination Behavior

ThesetupImplandstepImplmethods hook the C functions to the System object™. You need to initialize the color sensor only once at model initialization. Hence, thecolorSensor_Initfunction is called in thesetupImplmethod. This function is declared in thecolorSensor.hfile. To read the RGB values of the color sensor, thecolorSensor_Stepfunction is called in thesetupImplmethod. 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.

  1. In the MATLAB®editor, open thecolorSensor.mfile.

  2. Update thesetupImplmethod 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.hfunction. The second and third arguments of coder.ceval are the Arduino®hardware pin number and value, respectively.

  3. Update thestepImplmethod 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

  4. Update thereleaseImplmethod 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
  5. 年代ave the changes tocolorSensor.m.

In the next section, you willUpdate Paths for Source and Header Files.

年代ee Also

|||