AI2 Component  (Version nb184)
Ev3GyroSensor.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_GYROSENSOR_COMPONENT_VERSION,
31  description = "A component that provides a high-level interface to a gyro 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 Ev3GyroSensor extends LegoMindstormsEv3Sensor implements Deleteable {
38  private static final int DELAY_MILLISECONDS = 50;
39  private static final int SENSOR_TYPE = 32;
40  private static final int SENSOR_MODE_ANGLE = 0;
41  private static final int SENSOR_MODE_RATE = 1;
42  private static final String SENSOR_MODE_ANGLE_STRING = "angle";
43  private static final String SENSOR_MODE_RATE_STRING = "rate";
44  private static final String DEFAULT_SENSOR_MODE_STRING = SENSOR_MODE_ANGLE_STRING;
45 
46  private Handler eventHandler;
47  private final Runnable sensorValueChecker;
48  private int mode = SENSOR_MODE_ANGLE;
49  private String modeString = SENSOR_MODE_ANGLE_STRING;
50  private double previousValue = -1.0;
51  private boolean sensorValueChangedEventEnabled = false;
52 
56  public Ev3GyroSensor(ComponentContainer container) {
57  super(container, "Ev3GyroSensor");
58 
59  eventHandler = new Handler();
60  sensorValueChecker = new Runnable() {
61  public void run() {
62  String functionName = "";
63 
64  if (bluetooth != null && bluetooth.IsConnected()) {
65  double currentValue = getSensorValue(functionName);
66 
67  if (previousValue < 0.0) {
68  previousValue = currentValue;
69  eventHandler.postDelayed(this, DELAY_MILLISECONDS);
70  return;
71  }
72 
73  // trigger events according to the conditions
74  if (mode == SENSOR_MODE_RATE && Math.abs(currentValue) >= 1.0)
75  SensorValueChanged(currentValue);
76  else if (mode == SENSOR_MODE_ANGLE && Math.abs(currentValue - previousValue) >= 1.0)
77  SensorValueChanged(currentValue);
78 
79  previousValue = currentValue;
80  }
81 
82  eventHandler.postDelayed(this, DELAY_MILLISECONDS);
83  }
84  };
85  eventHandler.post(sensorValueChecker);
86 
87  Mode(DEFAULT_SENSOR_MODE_STRING);
89  }
90 
95  @SimpleFunction(description = "Returns the current angle or rotation speed based on current mode, " +
96  "or -1 if the value cannot be read from sensor.")
97  public double GetSensorValue() {
98  String functionName = "";
99  return getSensorValue(functionName);
100  }
101 
106  defaultValue = DEFAULT_SENSOR_MODE_STRING)
108  public void Mode(String modeName) {
109  String functionName = "Mode";
110  try {
111  setMode(modeName);
112  } catch(IllegalArgumentException e) {
113  form.dispatchErrorOccurredEvent(this, functionName, ErrorMessages.ERROR_EV3_ILLEGAL_ARGUMENT, functionName);
114  }
115  }
116 
120  @SimpleProperty(description = "The sensor mode can be a text constant of either \"rate\" or \"angle\", " +
121  "which correspond to SetAngleMode or SetRateMode respectively.",
122  category = PropertyCategory.BEHAVIOR)
123  public String Mode() {
124  return modeString;
125  }
126 
130  @SimpleFunction(description = "Measures the orientation of the sensor.")
131  public void SetAngleMode() {
132  setMode(SENSOR_MODE_ANGLE_STRING);
133  }
134 
138  @SimpleFunction(description = "Measures the angular velocity of the sensor.")
139  public void SetRateMode() {
140  setMode(SENSOR_MODE_RATE_STRING);
141  }
142 
146  @SimpleProperty(description = "Whether the SensorValueChanged event should fire when the sensor value changed.",
147  category = PropertyCategory.BEHAVIOR)
149  return sensorValueChangedEventEnabled;
150  }
151 
156  defaultValue = "False")
158  public void SensorValueChangedEventEnabled(boolean enabled) {
159  sensorValueChangedEventEnabled = enabled;
160  }
161 
165  @SimpleEvent(description = "Called then the sensor value changed.")
166  public void SensorValueChanged(double sensorValue) {
167  EventDispatcher.dispatchEvent(this, "SensorValueChanged", sensorValue);
168  }
169 
170  private double getSensorValue(String functionName) {
171  return readInputSI(functionName,
172  0,
174  SENSOR_TYPE,
175  mode);
176  }
177 
178  private void setMode(String newModeString) {
179  if (SENSOR_MODE_ANGLE_STRING.equals(newModeString))
180  mode = SENSOR_MODE_ANGLE;
181  else if (SENSOR_MODE_RATE_STRING.equals(newModeString))
182  mode = SENSOR_MODE_RATE;
183  else
184  throw new IllegalArgumentException();
185 
186  this.modeString = newModeString;
187  }
188 
189  // Deleteable implementation
190  @Override
191  public void onDelete() {
192  super.onDelete();
193  }
194 }
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.Ev3GyroSensor.Ev3GyroSensor
Ev3GyroSensor(ComponentContainer container)
Definition: Ev3GyroSensor.java:56
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.runtime.Ev3GyroSensor.SensorValueChangedEventEnabled
boolean SensorValueChangedEventEnabled()
Definition: Ev3GyroSensor.java:148
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.Ev3GyroSensor.Mode
String Mode()
Definition: Ev3GyroSensor.java:123
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
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.Ev3GyroSensor.onDelete
void onDelete()
Definition: Ev3GyroSensor.java:191
com.google.appinventor.components.annotations.DesignerComponent
Definition: DesignerComponent.java:22
com.google.appinventor.components.runtime.Ev3GyroSensor.SensorValueChangedEventEnabled
void SensorValueChangedEventEnabled(boolean enabled)
Definition: Ev3GyroSensor.java:158
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.Ev3GyroSensor.Mode
void Mode(String modeName)
Definition: Ev3GyroSensor.java:108
com.google.appinventor.components.runtime.Ev3GyroSensor.SetRateMode
void SetRateMode()
Definition: Ev3GyroSensor.java:139
com.google.appinventor.components.runtime.EventDispatcher.dispatchEvent
static boolean dispatchEvent(Component component, String eventName, Object...args)
Definition: EventDispatcher.java:188
com.google.appinventor.components.annotations.SimpleProperty
Definition: SimpleProperty.java:23
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_LEGO_EV3_GYRO_SENSOR_MODE
static final String PROPERTY_TYPE_LEGO_EV3_GYRO_SENSOR_MODE
Definition: PropertyTypeConstants.java:143
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.Ev3GyroSensor
Definition: Ev3GyroSensor.java:37
com.google.appinventor.components.runtime
Copyright 2009-2011 Google, All Rights reserved.
Definition: AccelerometerSensor.java:8
com.google.appinventor.components.runtime.Ev3GyroSensor.SensorValueChanged
void SensorValueChanged(double sensorValue)
Definition: Ev3GyroSensor.java:166
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.Form.dispatchErrorOccurredEvent
void dispatchErrorOccurredEvent(final Component component, final String functionName, final int errorNumber, final Object... messageArgs)
Definition: Form.java:1011
com.google.appinventor.components.runtime.Ev3GyroSensor.SetAngleMode
void SetAngleMode()
Definition: Ev3GyroSensor.java:131
com.google.appinventor.components.annotations.SimpleObject
Definition: SimpleObject.java:23
com.google.appinventor.components.runtime.Ev3GyroSensor.GetSensorValue
double GetSensorValue()
Definition: Ev3GyroSensor.java:97
com.google
com
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.common.PropertyTypeConstants
Definition: PropertyTypeConstants.java:14
com.google.appinventor.components.annotations
com.google.appinventor