# 再探速度测量¶

## 受控对象模型¶

within ModelicaByExample.Components.SpeedMeasurement.Examples;
model Plant "The basic plant model"
extends ModelicaByExample.Components.Rotational.Examples.SMD;
Components.IdealSensor idealSensor
annotation (Placement(transformation(extent={{-10,-10},{10,10}},
rotation=90, origin={-20,30})));
equation
connect(idealSensor.flange, inertia1.flange_a) annotation (Line(
points={{-20,20},{-20,0},{-10,0}}, color={0,0,0},
smooth=Smooth.None));
annotation(experiment(StopTime=10, Tolerance=1e-006));
end Plant;


## 取样保持传感器¶

within ModelicaByExample.Components.SpeedMeasurement.Components;
model SampleHold "A sample-hold ideal speed sensor"
extends Interfaces.SpeedSensor;
parameter Modelica.SIunits.Time sample_rate;
initial equation
w = der(flange.phi);
equation
when sample(0, sample_rate) then
w = der(flange.phi);
end when;
end SampleHold;


within ModelicaByExample.Components.SpeedMeasurement.Interfaces;
partial model SpeedSensor "The base class for all of our sensor models"
extends Modelica.Mechanics.Rotational.Interfaces.PartialAbsoluteSensor;
Modelica.Blocks.Interfaces.RealOutput w "Sensed speed"
annotation (Placement(transformation(extent={{100,-10},{120,10}})));
end SpeedSensor;


partial model PartialAbsoluteSensor
"Partial model to measure a single absolute flange variable"
extends Modelica.Icons.RotationalSensor;

Flange_a flange "Flange from which speed will be measured"
annotation(Placement(transformation(extent={{-110,-10},{-90,10}}, rotation=0)));
equation
0 = flange.tau;
end PartialAbsoluteSensor;


within ModelicaByExample.Components.SpeedMeasurement.Examples;
model PlantWithSampleHold "Comparison between ideal and sample-hold sensor"
extends Plant;
Components.SampleHold sampleHold(sample_rate=0.125)
annotation (Placement(transformation(extent={{-10,-10},{10,10}},
rotation=90, origin={20,30})));
equation
connect(sampleHold.flange, inertia1.flange_b) annotation (Line(
points={{20,20},{20,0},{10,0}}, color={0,0,0},
smooth=Smooth.None));
annotation (experiment(StopTime=10, Tolerance=1e-006));
end PlantWithSampleHold;


## 间隔测量¶

within ModelicaByExample.Components.SpeedMeasurement.Components;
model IntervalMeasure
"Estimate speed by measuring interval between encoder teeth"
extends Interfaces.SpeedSensor;
parameter Integer teeth;
Real next_phi, prev_phi;
Real last_time;
protected
parameter Modelica.SIunits.Angle tooth_angle=2*Modelica.Constants.pi/teeth;
initial equation
next_phi = flange.phi+tooth_angle;
prev_phi = flange.phi-tooth_angle;
last_time = time;
equation
when {flange.phi>=pre(next_phi),flange.phi<=pre(prev_phi)} then
w = tooth_angle/(time-pre(last_time));
next_phi = flange.phi+tooth_angle;
prev_phi = flange.phi-tooth_angle;
last_time = time;
end when;
end IntervalMeasure;


within ModelicaByExample.Components.SpeedMeasurement.Examples;
model PlantWithIntervalMeasure
"Comparison between ideal and an interval measuring sensor"
extends Plant;
Components.IntervalMeasure intervalMeasure(teeth=200)
annotation (Placement(transformation(
extent={{-10,-10},{10,10}}, rotation=90,
origin={20,30})));
equation
connect(intervalMeasure.flange, inertia1.flange_b) annotation (Line(
points={{20,20},{20,0},{10,0}}, color={0,0,0},
smooth=Smooth.None));
annotation (experiment(StopTime=10, Tolerance=1e-006));
end PlantWithIntervalMeasure;


## 脉冲计数器¶

within ModelicaByExample.Components.SpeedMeasurement.Components;
model PulseCounter "Compute speed using pulse counting"
extends Interfaces.SpeedSensor;
parameter Modelica.SIunits.Time sample_time;
parameter Integer teeth;
Modelica.SIunits.Angle next_phi, prev_phi;
Integer count;
protected
parameter Modelica.SIunits.Angle tooth_angle=2*Modelica.Constants.pi/teeth;
initial equation
next_phi = flange.phi+tooth_angle;
prev_phi = flange.phi-tooth_angle;
count = 0;
algorithm
when {flange.phi>=pre(next_phi), flange.phi<=pre(prev_phi)} then
next_phi := flange.phi + tooth_angle;
prev_phi := flange.phi - tooth_angle;
count := pre(count) + 1;
end when;
when sample(0,sample_time) then
w := pre(count)*tooth_angle/sample_time;
count := 0 "Another equation for count?";
end when;
end PulseCounter;


within ModelicaByExample.Components.SpeedMeasurement.Examples;
model PlantWithPulseCounter
"Comparison between ideal and pulse counting sensor"
extends Plant;
Components.PulseCounter pulseCounter(sample_time=0.125, teeth=200)
annotation (Placement(transformation(
extent={{-10,-10},{10,10}}, rotation=90,
origin={20,30})));
equation
connect(pulseCounter.flange, inertia1.flange_b) annotation (Line(
points={{20,20},{20,0},{10,0}}, color={0,0,0},
smooth=Smooth.None));
annotation (experiment(StopTime=10, Tolerance=1e-006));
end PlantWithPulseCounter;