Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.93 KB | None | 0 0
  1. package org.eclipse.leshan.client.demo;
  2.  
  3. import java.math.BigDecimal;
  4. import java.math.RoundingMode;
  5. import java.util.concurrent.Executors;
  6. import java.util.concurrent.ScheduledExecutorService;
  7. import java.util.concurrent.TimeUnit;
  8.  
  9.  
  10. import org.eclipse.leshan.client.resource.BaseInstanceEnabler;
  11. import org.eclipse.leshan.core.response.ExecuteResponse;
  12. import org.eclipse.leshan.core.response.ReadResponse;
  13. import org.eclipse.leshan.util.NamedThreadFactory;
  14.  
  15. import com.pi4j.io.gpio.GpioController;
  16. import com.pi4j.io.gpio.GpioFactory;
  17. import com.pi4j.io.gpio.GpioPinDigitalInput;
  18. import com.pi4j.io.gpio.GpioPinDigitalOutput;
  19. import com.pi4j.io.gpio.PinPullResistance;
  20. import com.pi4j.io.gpio.RaspiPin;
  21.  
  22.  
  23. public class DistanceSensor extends BaseInstanceEnabler {
  24.  
  25. private static GpioPinDigitalOutput sensorTriggerPin ;
  26. private static GpioPinDigitalInput sensorEchoPin ;
  27.  
  28. final static GpioController gpio = GpioFactory.getInstance();
  29.  
  30.  
  31. private static final String UNIT_CENTIMETER = "cm";
  32. private static final int SENSOR_VALUE = 5700;
  33. private static final int UNITS = 5701;
  34. private static final int MAX_MEASURED_VALUE = 5602;
  35. private static final int MIN_MEASURED_VALUE = 5601;
  36. private static final int RESET_MIN_MAX_MEASURED_VALUES = 5605;
  37. private final ScheduledExecutorService scheduler;
  38.  
  39. private double currentDistance = 0d;
  40. private double minMeasuredValue = currentDistance;
  41. private double maxMeasuredValue = currentDistance;
  42.  
  43.  
  44. public DistanceSensor( ) {
  45.  
  46. this.scheduler = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("Temperature Sensor"));
  47. scheduler.scheduleAtFixedRate(new Runnable() {
  48.  
  49. @Override
  50. public void run() {
  51. adjustDistance();
  52. }
  53. }, 2, 2, TimeUnit.SECONDS);
  54.  
  55.  
  56.  
  57. }
  58.  
  59. public synchronized ReadResponse read(int resourceId) {
  60. switch (resourceId) {
  61. case MIN_MEASURED_VALUE:
  62. return ReadResponse.success(resourceId, getTwoDigitValue(minMeasuredValue));
  63. case MAX_MEASURED_VALUE:
  64. return ReadResponse.success(resourceId, getTwoDigitValue(maxMeasuredValue));
  65. case SENSOR_VALUE:
  66. return ReadResponse.success(resourceId, getTwoDigitValue(currentDistance));
  67. case UNITS:
  68. return ReadResponse.success(resourceId, UNIT_CENTIMETER);
  69. default:
  70. return super.read(resourceId);
  71. }
  72. }
  73.  
  74. @Override
  75. public synchronized ExecuteResponse execute(int resourceId, String params) {
  76. switch (resourceId) {
  77. case RESET_MIN_MAX_MEASURED_VALUES:
  78. resetMinMaxMeasuredValues();
  79. return ExecuteResponse.success();
  80. default:
  81. return super.execute(resourceId, params);
  82. }
  83. }
  84.  
  85. private double getTwoDigitValue(double value) {
  86. BigDecimal toBeTruncated = BigDecimal.valueOf(value);
  87. return toBeTruncated.setScale(2, RoundingMode.HALF_UP).doubleValue();
  88. }
  89.  
  90.  
  91. private synchronized void adjustDistance() {
  92.  
  93. sensorTriggerPin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_04); // Trigger pin as OUTPUT
  94. sensorEchoPin = gpio.provisionDigitalInputPin(RaspiPin.GPIO_05,PinPullResistance.PULL_DOWN); // Echo pin as INPUT
  95.  
  96.  
  97. try {
  98. Thread.sleep(2000);
  99. sensorTriggerPin.high(); // Make trigger pin HIGH
  100. Thread.sleep((long) 0.00001);// Delay for 10 microseconds
  101. sensorTriggerPin.low(); //Make trigger pin LOW
  102.  
  103. while(sensorEchoPin.isLow()){ //Wait until the ECHO pin gets HIGH
  104.  
  105. }
  106. long startTime= System.nanoTime(); // Store the surrent time to calculate ECHO pin HIGH time.
  107. while(sensorEchoPin.isHigh()){ //Wait until the ECHO pin gets LOW
  108.  
  109. }
  110. long endTime= System.nanoTime(); // Store the echo pin HIGH end time to calculate ECHO pin HIGH time.
  111.  
  112. currentDistance = ((((endTime - startTime)/1e3)/2) / 29.1);
  113. Integer changedResource = adjustMinMaxMeasuredValue(currentDistance);
  114. if (changedResource != null) {
  115. fireResourcesChange(SENSOR_VALUE, changedResource);
  116. } else {
  117. fireResourcesChange(SENSOR_VALUE);
  118. }
  119. Thread.sleep(1000);
  120.  
  121. }
  122. catch (InterruptedException e) {
  123. e.printStackTrace();
  124. }
  125.  
  126.  
  127.  
  128.  
  129. }
  130.  
  131. private Integer adjustMinMaxMeasuredValue(double newDistance) {
  132.  
  133. if (newDistance > maxMeasuredValue) {
  134. maxMeasuredValue = newDistance;
  135. return MAX_MEASURED_VALUE;
  136. } else if (newDistance < minMeasuredValue) {
  137. minMeasuredValue = newDistance;
  138. return MIN_MEASURED_VALUE;
  139. } else {
  140. return null;
  141. }
  142. }
  143.  
  144. private void resetMinMaxMeasuredValues() {
  145. minMeasuredValue = currentDistance;
  146. maxMeasuredValue = currentDistance;
  147. }
  148.  
  149.  
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement