AI2 Component  (Version nb184)
Ev3UltrasonicSensor.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2016 MIT, All rights reserved
3 // Released under the Apache License, Version 2.0
4 // http://www.apache.org/licenses/LICENSE-2.0
5 
6 package com.google.appinventor.components.runtime;
7 
19 import android.os.Handler;
20 
30 @DesignerComponent(version = YaVersion.EV3_ULTRASONICSENSOR_COMPONENT_VERSION,
31  description = "A component that provides a high-level interface to an ultrasonic sensor on a " +
32  "LEGO MINDSTORMS EV3 robot.",
33  category = ComponentCategory.LEGOMINDSTORMS,
34  nonVisible = true,
35  iconName = "images/legoMindstormsEv3.png")
36 @SimpleObject
37 public class Ev3UltrasonicSensor extends LegoMindstormsEv3Sensor implements Deleteable {
38  private static final int SENSOR_TYPE = 30;
39  private static final int SENSOR_MODE_CM = 0;
40  private static final int SENSOR_MODE_INCH = 1;
41  private static final String SENSOR_MODE_CM_STRING = "cm";
42  private static final String SENSOR_MODE_INCH_STRING = "inch";
43 
44  private static final int DEFAULT_BOTTOM_OF_RANGE = 30;
45  private static final int DEFAULT_TOP_OF_RANGE = 90;
46  private static final String DEFAULT_SENSOR_MODE_STRING = SENSOR_MODE_CM_STRING;
47  private static final int DELAY_MILLISECONDS = 50;
48 
49  private String modeString = SENSOR_MODE_CM_STRING;
50  private int mode = SENSOR_MODE_CM;
51  private Handler eventHandler;
52  private final Runnable sensorValueChecker;
53  private double previousDistance = -1.0;
54  private int bottomOfRange;
55  private int topOfRange;
56  private boolean belowRangeEventEnabled;
57  private boolean withinRangeEventEnabled;
58  private boolean aboveRangeEventEnabled;
59 
64  super(container, "Ev3UltrasonicSensor");
65 
66  eventHandler = new Handler();
67  sensorValueChecker = new Runnable() {
68  public void run() {
69  String functionName = "";
70 
71  if (bluetooth != null && bluetooth.IsConnected()) {
72  double currentDistance = getDistance(functionName);
73 
74  if (previousDistance < 0.0) {
75  previousDistance = currentDistance;
76  eventHandler.postDelayed(this, DELAY_MILLISECONDS);
77  return;
78  }
79 
80  if (currentDistance < bottomOfRange) {
81  if (belowRangeEventEnabled && previousDistance >= bottomOfRange)
82  BelowRange();
83  } else if (currentDistance > topOfRange) {
84  if (aboveRangeEventEnabled && previousDistance <= topOfRange)
85  AboveRange();
86  } else {
87  if (withinRangeEventEnabled && (previousDistance < bottomOfRange || previousDistance > topOfRange))
88  WithinRange();
89  }
90 
91  previousDistance = currentDistance;
92  }
93 
94  eventHandler.postDelayed(this, DELAY_MILLISECONDS);
95  }
96  };
97 
98  eventHandler.post(sensorValueChecker);
99 
100  TopOfRange(DEFAULT_TOP_OF_RANGE);
101  BottomOfRange(DEFAULT_BOTTOM_OF_RANGE);
102  BelowRangeEventEnabled(false);
103  AboveRangeEventEnabled(false);
105  Unit(DEFAULT_SENSOR_MODE_STRING);
106  }
107 
108  @SimpleFunction(description = "Returns the current distance in centimeters as a value between " +
109  "0 and 254, or -1 if the distance can not be read.")
110  public double GetDistance() {
111  String functionName = "GetDistance";
112  return getDistance(functionName);
113  }
114 
115  private double getDistance(String functionName) {
116  double distance = readInputSI(functionName, 0, sensorPortNumber, SENSOR_TYPE, mode);
117  return distance == 255 ? -1.0 : distance;
118  }
119 
124  @SimpleProperty(description = "The bottom of the range used for the BelowRange, WithinRange, " +
125  "and AboveRange events.",
126  category = PropertyCategory.BEHAVIOR)
127  public int BottomOfRange() {
128  return bottomOfRange;
129  }
130 
136  defaultValue = "" + DEFAULT_BOTTOM_OF_RANGE)
138  public void BottomOfRange(int bottomOfRange) {
139  this.bottomOfRange = bottomOfRange;
140  }
141 
146  @SimpleProperty(description = "The top of the range used for the BelowRange, WithinRange, and " +
147  "AboveRange events.",
148  category = PropertyCategory.BEHAVIOR)
149  public int TopOfRange() {
150  return topOfRange;
151  }
152 
158  defaultValue = "" + DEFAULT_TOP_OF_RANGE)
160  public void TopOfRange(int topOfRange) {
161  this.topOfRange = topOfRange;
162  }
163 
168  @SimpleProperty(description = "Whether the BelowRange event should fire when the distance " +
169  "goes below the BottomOfRange.",
170  category = PropertyCategory.BEHAVIOR)
171  public boolean BelowRangeEventEnabled() {
172  return belowRangeEventEnabled;
173  }
174 
180  defaultValue = "False")
182  public void BelowRangeEventEnabled(boolean enabled) {
183  belowRangeEventEnabled = enabled;
184  }
185 
189  @SimpleEvent(description = "Called when the detected distance has gone below the range.")
190  public void BelowRange() {
191  EventDispatcher.dispatchEvent(this, "BelowRange");
192  }
193 
198  @SimpleProperty(description = "Whether the WithinRange event should fire when the distance" +
199  " goes between the BottomOfRange and the TopOfRange.",
200  category = PropertyCategory.BEHAVIOR)
201  public boolean WithinRangeEventEnabled() {
202  return withinRangeEventEnabled;
203  }
204 
210  defaultValue = "False")
212  public void WithinRangeEventEnabled(boolean enabled) {
213  withinRangeEventEnabled = enabled;
214  }
215 
219  @SimpleEvent(description = "Called when the detected distance has gone within the range.")
220  public void WithinRange() {
221  EventDispatcher.dispatchEvent(this, "WithinRange");
222  }
223 
228  @SimpleProperty(description = "Whether the AboveRange event should fire when the distance " +
229  "goes above the TopOfRange.",
230  category = PropertyCategory.BEHAVIOR)
231  public boolean AboveRangeEventEnabled() {
232  return aboveRangeEventEnabled;
233  }
234 
240  defaultValue = "False")
242  public void AboveRangeEventEnabled(boolean enabled) {
243  aboveRangeEventEnabled = enabled;
244  }
245 
249  @SimpleEvent(description = "Called when the detected distance has gone above the range.")
250  public void AboveRange() {
251  EventDispatcher.dispatchEvent(this, "AboveRange");
252  }
253 
258  defaultValue = DEFAULT_SENSOR_MODE_STRING)
260  public void Unit(String unitName) {
261  String functionName = "Unit";
262  try {
263  setMode(unitName);
264  } catch(IllegalArgumentException e) {
265  form.dispatchErrorOccurredEvent(this, functionName, ErrorMessages.ERROR_EV3_ILLEGAL_ARGUMENT, functionName);
266  }
267  }
268 
272  @SimpleProperty(description = "The distance unit, which can be either \"cm\" or \"inch\".",
273  category = PropertyCategory.BEHAVIOR)
274  public String Unit() {
275  return modeString;
276  }
277 
281  @SimpleFunction(description = "Measure the distance in centimeters.")
282  public void SetCmUnit() {
283  String functionName = "SetCmUnit";
284  try {
285  setMode(SENSOR_MODE_CM_STRING);
286  } catch(IllegalArgumentException e) {
287  form.dispatchErrorOccurredEvent(this, functionName, ErrorMessages.ERROR_EV3_ILLEGAL_ARGUMENT, functionName);
288  }
289  }
290 
294  @SimpleFunction(description = "Measure the distance in inches.")
295  public void SetInchUnit() {
296  String functionName = "SetInchUnit";
297  try {
298  setMode(SENSOR_MODE_INCH_STRING);
299  } catch(IllegalArgumentException e) {
300  form.dispatchErrorOccurredEvent(this, functionName, ErrorMessages.ERROR_EV3_ILLEGAL_ARGUMENT, functionName);
301  }
302  }
303 
304  private void setMode(String newModeString) {
305  previousDistance = -1.0;
306 
307  if (SENSOR_MODE_CM_STRING.equals(newModeString)) {
308  mode = SENSOR_MODE_CM;
309  }
310  else if (SENSOR_MODE_INCH_STRING.equals(newModeString)) {
311  mode = SENSOR_MODE_INCH;
312  }
313  else
314  throw new IllegalArgumentException();
315 
316  this.modeString = newModeString;
317  }
318 
319  // Deleteable implementation
320  @Override
321  public void onDelete() {
322  eventHandler.removeCallbacks(sensorValueChecker);
323  super.onDelete();
324  }
325 }
com.google.appinventor.components.runtime.EventDispatcher
Definition: EventDispatcher.java:22
com.google.appinventor.components.runtime.LegoMindstormsEv3Sensor.sensorPortNumber
int sensorPortNumber
Definition: LegoMindstormsEv3Sensor.java:26
com.google.appinventor.components.annotations.SimpleFunction
Definition: SimpleFunction.java:23
com.google.appinventor.components.runtime.Ev3UltrasonicSensor.BelowRangeEventEnabled
boolean BelowRangeEventEnabled()
Definition: Ev3UltrasonicSensor.java:171
com.google.appinventor.components.runtime.util.ErrorMessages
Definition: ErrorMessages.java:17
com.google.appinventor.components.runtime.util
-*- mode: java; c-basic-offset: 2; -*-
Definition: AccountChooser.java:7
com.google.appinventor.components.runtime.LegoMindstormsEv3Base.bluetooth
BluetoothClient bluetooth
Definition: LegoMindstormsEv3Base.java:30
com.google.appinventor.components.common.YaVersion
Definition: YaVersion.java:14
com.google.appinventor.components.annotations.DesignerProperty
Definition: DesignerProperty.java:25
com.google.appinventor.components.runtime.LegoMindstormsEv3Sensor.readInputSI
final double readInputSI(String functionName, int layer, int no, int type, int mode)
Definition: LegoMindstormsEv3Sensor.java:90
com.google.appinventor.components.runtime.Ev3UltrasonicSensor.Unit
String Unit()
Definition: Ev3UltrasonicSensor.java:274
com.google.appinventor.components
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_NON_NEGATIVE_INTEGER
static final String PROPERTY_TYPE_NON_NEGATIVE_INTEGER
Definition: PropertyTypeConstants.java:206
com.google.appinventor.components.runtime.Ev3UltrasonicSensor.WithinRange
void WithinRange()
Definition: Ev3UltrasonicSensor.java:220
com.google.appinventor.components.runtime.Ev3UltrasonicSensor.onDelete
void onDelete()
Definition: Ev3UltrasonicSensor.java:321
com.google.appinventor.components.runtime.BluetoothConnectionBase.IsConnected
final boolean IsConnected()
Definition: BluetoothConnectionBase.java:204
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_BOOLEAN
static final String PROPERTY_TYPE_BOOLEAN
Definition: PropertyTypeConstants.java:35
com.google.appinventor.components.runtime.Ev3UltrasonicSensor.AboveRange
void AboveRange()
Definition: Ev3UltrasonicSensor.java:250
com.google.appinventor.components.runtime.Ev3UltrasonicSensor.BottomOfRange
int BottomOfRange()
Definition: Ev3UltrasonicSensor.java:127
com.google.appinventor.components.runtime.Ev3UltrasonicSensor.WithinRangeEventEnabled
void WithinRangeEventEnabled(boolean enabled)
Definition: Ev3UltrasonicSensor.java:212
com.google.appinventor.components.runtime.Ev3UltrasonicSensor.SetInchUnit
void SetInchUnit()
Definition: Ev3UltrasonicSensor.java:295
com.google.appinventor.components.annotations.DesignerComponent
Definition: DesignerComponent.java:22
com.google.appinventor.components.annotations.SimpleEvent
Definition: SimpleEvent.java:20
com.google.appinventor.components.annotations.PropertyCategory.BEHAVIOR
BEHAVIOR
Definition: PropertyCategory.java:15
com.google.appinventor.components.runtime.Ev3UltrasonicSensor.BottomOfRange
void BottomOfRange(int bottomOfRange)
Definition: Ev3UltrasonicSensor.java:138
com.google.appinventor.components.runtime.Ev3UltrasonicSensor.AboveRangeEventEnabled
void AboveRangeEventEnabled(boolean enabled)
Definition: Ev3UltrasonicSensor.java:242
com.google.appinventor.components.runtime.EventDispatcher.dispatchEvent
static boolean dispatchEvent(Component component, String eventName, Object...args)
Definition: EventDispatcher.java:188
com.google.appinventor.components.runtime.Ev3UltrasonicSensor.Unit
void Unit(String unitName)
Definition: Ev3UltrasonicSensor.java:260
com.google.appinventor.components.annotations.SimpleProperty
Definition: SimpleProperty.java:23
com.google.appinventor.components.runtime.Ev3UltrasonicSensor.TopOfRange
int TopOfRange()
Definition: Ev3UltrasonicSensor.java:149
com.google.appinventor.components.runtime.Ev3UltrasonicSensor.TopOfRange
void TopOfRange(int topOfRange)
Definition: Ev3UltrasonicSensor.java:160
com.google.appinventor.components.annotations.PropertyCategory
Definition: PropertyCategory.java:13
com.google.appinventor.components.runtime.ComponentContainer
Definition: ComponentContainer.java:16
com.google.appinventor.components.runtime
Copyright 2009-2011 Google, All Rights reserved.
Definition: AccelerometerSensor.java:8
com.google.appinventor.components.runtime.Ev3UltrasonicSensor.AboveRangeEventEnabled
boolean AboveRangeEventEnabled()
Definition: Ev3UltrasonicSensor.java:231
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_LEGO_EV3_ULTRASONIC_SENSOR_MODE
static final String PROPERTY_TYPE_LEGO_EV3_ULTRASONIC_SENSOR_MODE
Definition: PropertyTypeConstants.java:136
com.google.appinventor.components.runtime.LegoMindstormsEv3Sensor
Definition: LegoMindstormsEv3Sensor.java:24
com.google.appinventor.components.runtime.Deleteable
Definition: Deleteable.java:15
com.google.appinventor.components.common
Definition: ComponentCategory.java:7
com.google.appinventor.components.common.ComponentCategory
Definition: ComponentCategory.java:48
com.google.appinventor.components.runtime.Ev3UltrasonicSensor.BelowRange
void BelowRange()
Definition: Ev3UltrasonicSensor.java:190
com.google.appinventor.components.runtime.Form.dispatchErrorOccurredEvent
void dispatchErrorOccurredEvent(final Component component, final String functionName, final int errorNumber, final Object... messageArgs)
Definition: Form.java:1011
com.google.appinventor.components.annotations.SimpleObject
Definition: SimpleObject.java:23
com.google.appinventor.components.runtime.Ev3UltrasonicSensor
Definition: Ev3UltrasonicSensor.java:37
com.google.appinventor.components.runtime.Ev3UltrasonicSensor.WithinRangeEventEnabled
boolean WithinRangeEventEnabled()
Definition: Ev3UltrasonicSensor.java:201
com.google.appinventor.components.runtime.Ev3UltrasonicSensor.BelowRangeEventEnabled
void BelowRangeEventEnabled(boolean enabled)
Definition: Ev3UltrasonicSensor.java:182
com.google
com
com.google.appinventor.components.runtime.Ev3UltrasonicSensor.GetDistance
double GetDistance()
Definition: Ev3UltrasonicSensor.java:110
com.google.appinventor.components.runtime.util.ErrorMessages.ERROR_EV3_ILLEGAL_ARGUMENT
static final int ERROR_EV3_ILLEGAL_ARGUMENT
Definition: ErrorMessages.java:216
com.google.appinventor.components.runtime.AndroidNonvisibleComponent.form
final Form form
Definition: AndroidNonvisibleComponent.java:19
com.google.appinventor.components.runtime.Ev3UltrasonicSensor.Ev3UltrasonicSensor
Ev3UltrasonicSensor(ComponentContainer container)
Definition: Ev3UltrasonicSensor.java:63
com.google.appinventor.components.common.PropertyTypeConstants
Definition: PropertyTypeConstants.java:14
com.google.appinventor.components.annotations
com.google.appinventor
com.google.appinventor.components.runtime.Ev3UltrasonicSensor.SetCmUnit
void SetCmUnit()
Definition: Ev3UltrasonicSensor.java:282