Skip to main content
Jump to: navigation, search

BaSyx / Introductory Examples / Java / Example Temperature

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.

  1. /**
  2.  * The sensor interface enables reading of one sensor value
  3.  */
  4. public interface ISensor {
  5.  
  6. 	public double readValue();
  7.  
  8. }


The TemperatureSensor stub is used during the example. It implements a very basic simulation model for the oven temperature.

  1. /**
  2.  * A sensor for reading a temperature value that is dependent on a heater
  3.  */
  4. public class TemperatureSensor implements ISensor {
  5. 	private final double maxTemperature = 50;
  6. 	private final double minTemperature = 20;
  7. 	private final double changeRate = 0.1d;
  8.  
  9. 	private double currentTemperature = 20.0;
  10.  
  11. 	public TemperatureSensor(final Heater heater) {
  12.  
  13. 		// Start a new Thread that updates the temperature in every tick
  14. 		new Thread(() -> {
  15. 			while (true) {
  16. 				try {
  17. 					Thread.sleep(100);
  18. 				} catch (InterruptedException e) {
  19. 					e.printStackTrace();
  20. 				}
  21. 				double targetTemperature = minTemperature;
  22. 				if (heater.isActive()) {
  23. 					targetTemperature = maxTemperature;
  24. 				}
  25. 				currentTemperature = (1 - changeRate) * currentTemperature + changeRate * targetTemperature;
  26. 			}
  27. 		}).start();
  28. 	}
  29.  
  30. 	public double readValue() {
  31. 		return currentTemperature;
  32. 	}
  33. }

Back to the top