Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "BaSyx / Introductory Examples / Java / Example Temperature"
< BaSyx | Introductory Examples
(Created page with "= Temperature sensor stub = This stub code realizes an interface to a temperature sensor. It uses the generic sensor interface that provides access to a single value. The...") |
m (Adds line numbers) |
||
Line 6: | Line 6: | ||
The ISensor interface defines common interface functions to access a sensor. | The ISensor interface defines common interface functions to access a sensor. | ||
− | <syntaxhighlight lang="java" style="margin-left: 4em"> | + | <syntaxhighlight lang="java" style="margin-left: 4em" line> |
/** | /** | ||
* The sensor interface enables reading of one sensor value | * The sensor interface enables reading of one sensor value | ||
Line 21: | Line 21: | ||
The TemperatureSensor stub is used during the example. It implements a very basic simulation model for the oven temperature. | The TemperatureSensor stub is used during the example. It implements a very basic simulation model for the oven temperature. | ||
− | <syntaxhighlight lang="java" style="margin-left: 4em"> | + | <syntaxhighlight lang="java" style="margin-left: 4em" line> |
/** | /** | ||
* A sensor for reading a temperature value that is dependent on a heater | * A sensor for reading a temperature value that is dependent on a heater |
Latest revision as of 08:56, 23 August 2021
Temperature sensor stub
This stub code realizes an interface to a temperature sensor. It uses the generic sensor interface that provides access to a single value.
The ISensor interface defines common interface functions to access a sensor.
/**
* The sensor interface enables reading of one sensor value
*/
public interface ISensor {
public double readValue();
}
The TemperatureSensor stub is used during the example. It implements a very basic simulation model for the oven temperature.
/**
* A sensor for reading a temperature value that is dependent on a heater
*/
public class TemperatureSensor implements ISensor {
private final double maxTemperature = 50;
private final double minTemperature = 20;
private final double changeRate = 0.1d;
private double currentTemperature = 20.0;
public TemperatureSensor(final Heater heater) {
// Start a new Thread that updates the temperature in every tick
new Thread(() -> {
while (true) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
double targetTemperature = minTemperature;
if (heater.isActive()) {
targetTemperature = maxTemperature;
}
currentTemperature = (1 - changeRate) * currentTemperature + changeRate * targetTemperature;
}
}).start();
}
public double readValue() {
return currentTemperature;
}
}