This example takes an existing model,
models a proximity sensor based on this motion model. In this scenario, a digital
sensor measures the distance between the car an obstacle 10 m (30 ft) away. The
model outputs the sensor measurement, and the position of the car, taking these
conditions into consideration:
The car comes to a hard stop when it reaches the obstacle.
In the physical world, a sensor measures the distance imprecisely, causing random numerical errors.
A digital sensor operates at fixed time intervals.
To start, open the
moving_car model. In the MATLAB® Command Window, enter
open_system(fullfile(matlabroot,... 'help', 'toolbox', 'simulink', 'examples', 'moving_car'))
You first need to model the hard stop when the car position reaches
10. The Integrator, Second Order block has a
parameter for that purpose.
Double-click the Integrator, Second Order block. The Block Parameters dialog box appears.
Select Limit x and enter
Upper limit x.
The background color for the parameter changes to indicate a modification that is not applied to the model.
Click OK to apply the changes and close the dialog box.
Add a sensor that measures the distance from the obstacle.
Modify the model:
Find the actual distance. To find the distance between the
obstacle position and the vehicle position, add the
Subtract block. Also add the Constant
block to set the constant value of
the position of the obstacle.
To model the imperfect measurement that would be typical
to a real sensor. Generate noise by using the
Band-Limited White Noise block from the
Sources library. Set the Noise
power parameter to
Add the noise to the measurement by using an
Add block from the Math Operations
The digital sensor fires every 0.1 seconds. In Simulink®, sampling of a signal at a given interval
requires a zero order hold. Add the Zero-Order
Hold block from the Discrete
library. After you add the block to the model, change the
Sample Time parameter to
Add another Outport to connect to the sensor output.
Connect the new blocks. Note that the output of Integrator, Second-Order is already connected to another port. To create a branch in that signal, left-click the signal to highlight potential ports for connection, and click the appropriate port.
Add signal names to the model to make it easier to understand.
Double-click the signal. An editable textbox appears.
Type the signal name.
To finish, click away from the textbox.
Repeat these steps to add the names as shown.
Compare the Actual distance signal with the Measured distance signal.
Create and connect a new Viewer (Scope) to the Actual distance following Add Signal Viewer. Note that the name of the signal appears in the viewer title.
Add the Measured distance signal to the same viewer. Right-click the signal, and select Connect to Viewer > Scope1. Make sure you are connecting to the viewer you created in the previous step.
Run the model. The Viewer shows the two signals, Actual distance in yellow and Measured distance in blue.
Zoom into the graph to observe the effect of noise and sampling. Click the Zoom button . Left-click and drag a window around the region you want to see.
You can repeatedly zoom in to observe details.
From the plot, you can see that the measurement can deviate from the actual value by as much as 0.3 meters. This information becomes useful when designing a safety feature, for example, a collision warning.