AI2 Component  (Version nb184)
NxtDrive.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2009-2011 Google, All Rights reserved
3 // Copyright 2011-2012 MIT, All rights reserved
4 // Released under the Apache License, Version 2.0
5 // http://www.apache.org/licenses/LICENSE-2.0
6 
7 package com.google.appinventor.components.runtime;
8 
19 
20 import java.util.ArrayList;
21 import java.util.List;
22 
23 // TODO(lizlooney) - We need to document what configuration of robot this component will work
24 // with.
33 @DesignerComponent(version = YaVersion.NXT_DRIVE_COMPONENT_VERSION,
34  description = "A component that provides a high-level interface to a LEGO MINDSTORMS NXT " +
35  "robot, with functions that can move and turn the robot.",
36  category = ComponentCategory.LEGOMINDSTORMS,
37  nonVisible = true,
38  iconName = "images/legoMindstormsNxt.png")
39 @SimpleObject
40 public class NxtDrive extends LegoMindstormsNxtBase {
41 
42  // Constants for setOutputState parameters.
43  private static final int MODE_MOTORON = 0x01;
44  private static final int MODE_BRAKE = 0x02;
45  private static final int MODE_REGULATED = 0x04;
46  private static final int REGULATION_MODE_IDLE = 0x00;
47  private static final int REGULATION_MODE_MOTOR_SPEED = 0x01;
48  private static final int REGULATION_MODE_MOTOR_SYNC = 0x02;
49  private static final int MOTOR_RUN_STATE_IDLE = 0x00;
50  private static final int MOTOR_RUN_STATE_RAMPUP = 0x10;
51  private static final int MOTOR_RUN_STATE_RUNNING = 0x20;
52  private static final int MOTOR_RUN_STATE_RAMPDOWN = 0x40;
53 
54 
55  private String driveMotors;
56  private List<Integer> driveMotorPorts;
57  private double wheelDiameter;
58  private boolean stopBeforeDisconnect;
59 
63  public NxtDrive(ComponentContainer container) {
64  super(container, "NxtDrive");
65 
66  DriveMotors("CB"); // C & B are the left & right drive motors of the ShooterBot robot.
67  WheelDiameter(4.32f);
69  }
70 
71  @Override
72  public void beforeDisconnect(BluetoothConnectionBase bluetoothConnection) {
73  if (stopBeforeDisconnect) {
74  for (int port : driveMotorPorts) {
75  setOutputState("Disconnect", port, 0,
76  MODE_BRAKE, REGULATION_MODE_IDLE, 0, MOTOR_RUN_STATE_IDLE, 0);
77  }
78  }
79  }
80 
84  @SimpleProperty(description = "The motor ports that are used for driving: the left wheel's " +
85  "motor port followed by the right wheel's motor port.",
86  category = PropertyCategory.BEHAVIOR, userVisible = false)
87  public String DriveMotors() {
88  return driveMotors;
89  }
90 
95  defaultValue = "CB")
97  public void DriveMotors(String motorPortLetters) {
98  driveMotors = motorPortLetters;
99  driveMotorPorts = new ArrayList<Integer>();
100  for (int i = 0; i < motorPortLetters.length(); i++) {
101  char ch = motorPortLetters.charAt(i);
102  try {
103  driveMotorPorts.add(convertMotorPortLetterToNumber(ch));
104  } catch (IllegalArgumentException e) {
105  form.dispatchErrorOccurredEvent(this, "DriveMotors",
107  }
108  }
109  }
110 
114  @SimpleProperty(description = "The diameter of the wheels used for driving.",
115  category = PropertyCategory.BEHAVIOR, userVisible = false)
116  public float WheelDiameter() {
117  return (float) wheelDiameter;
118  }
119 
126  defaultValue = "4.32")
128  public void WheelDiameter(float wheelDiameter) {
129  this.wheelDiameter = wheelDiameter;
130  }
131 
135  @SimpleProperty(description = "Whether to stop the drive motors before disconnecting.",
136  category = PropertyCategory.BEHAVIOR)
137  public boolean StopBeforeDisconnect() {
138  return stopBeforeDisconnect;
139  }
140 
147  defaultValue = "True")
149  public void StopBeforeDisconnect(boolean stopBeforeDisconnect) {
150  this.stopBeforeDisconnect = stopBeforeDisconnect;
151  }
152 
153  @SimpleFunction(description = "Move the robot forward indefinitely, with the " +
154  "specified percentage of maximum power, by powering both drive motors forward.")
155  public void MoveForwardIndefinitely(int power) {
156  move("MoveForwardIndefinitely", power, 0L);
157  }
158 
159  @SimpleFunction(description = "Move the robot backward indefinitely, with the " +
160  "specified percentage of maximum power, by powering both drive motors backward.")
161  public void MoveBackwardIndefinitely(int power) {
162  move("MoveBackwardIndefinitely", -power, 0L);
163  }
164 
165  @SimpleFunction(description = "Move the robot forward the given distance, with the " +
166  "specified percentage of maximum power, by powering both drive motors forward.")
167  public void MoveForward(int power, double distance) {
168  long tachoLimit = (long) (360.0 * distance / (wheelDiameter * Math.PI));
169  // This doesn't work accurately, but it is the best we can do with bluetooth direct commands.
170  move("MoveForward", power, tachoLimit);
171  }
172 
173  @SimpleFunction(description = "Move the robot backward the given distance, with the " +
174  "specified percentage of maximum power, by powering both drive motors backward.")
175  public void MoveBackward(int power, double distance) {
176  long tachoLimit = (long) (360.0 * distance / (wheelDiameter * Math.PI));
177  // This doesn't work accurately, but it is the best we can do with bluetooth direct commands.
178  move("MoveBackward", -power, tachoLimit);
179  }
180 
181  private void move(String functionName, int power, long tachoLimit) {
182  if (!checkBluetooth(functionName)) {
183  return;
184  }
185 
186  for (int port : driveMotorPorts) {
187  setOutputState(functionName, port, power,
188  MODE_MOTORON, REGULATION_MODE_MOTOR_SPEED, 0, MOTOR_RUN_STATE_RUNNING, tachoLimit);
189  }
190  }
191 
192  @SimpleFunction(description = "Turn the robot clockwise indefinitely, with the specified " +
193  "percentage of maximum power, by powering the left drive motor forward and the right " +
194  "drive motor backward.")
195  public void TurnClockwiseIndefinitely(int power) {
196  int numDriveMotors = driveMotorPorts.size();
197  if (numDriveMotors >= 2) {
198  int forwardMotorIndex = 0; // left
199  int backwardMotorIndex = numDriveMotors - 1; // right
200  turnIndefinitely("TurnClockwiseIndefinitely", power, forwardMotorIndex, backwardMotorIndex);
201  }
202  }
203 
204  @SimpleFunction(description = "Turn the robot counterclockwise indefinitely, with the " +
205  "specified percentage of maximum power, by powering the right drive motor forward and " +
206  "the left drive motor backward.")
207  public void TurnCounterClockwiseIndefinitely(int power) {
208  int numDriveMotors = driveMotorPorts.size();
209  if (numDriveMotors >= 2) {
210  int forwardMotorIndex = numDriveMotors - 1; // right
211  int backwardMotorIndex = 0; // left
212  turnIndefinitely("TurnCounterClockwiseIndefinitely", power, forwardMotorIndex,
213  backwardMotorIndex);
214  }
215  }
216 
217  private void turnIndefinitely(String functionName, int power, int forwardMotorIndex,
218  int reverseMotorIndex) {
219  if (!checkBluetooth(functionName)) {
220  return;
221  }
222 
223  setOutputState(functionName, driveMotorPorts.get(forwardMotorIndex), power,
224  MODE_MOTORON, REGULATION_MODE_MOTOR_SPEED, 0, MOTOR_RUN_STATE_RUNNING, 0);
225  setOutputState(functionName, driveMotorPorts.get(reverseMotorIndex), -power,
226  MODE_MOTORON, REGULATION_MODE_MOTOR_SPEED, 0, MOTOR_RUN_STATE_RUNNING, 0);
227  }
228 
229  // TODO(lizlooney) - it would be nice to have TurnClockwise and TurnCounterClockwise (or
230  // TurnRight and TurnLeft?) methods that take an angle (in degrees). I think we'd need to know
231  // the distance between the drive wheels, so that could be a property (similar to
232  // the WheelDiameter property).
233 
234  @SimpleFunction(description = "Stop the drive motors of the robot.")
235  public void Stop() {
236  String functionName = "Stop";
237  if (!checkBluetooth(functionName)) {
238  return;
239  }
240 
241  for (int port : driveMotorPorts) {
242  setOutputState(functionName, port, 0,
243  MODE_BRAKE, REGULATION_MODE_IDLE, 0, MOTOR_RUN_STATE_IDLE, 0);
244  }
245  }
246 }
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_FLOAT
static final String PROPERTY_TYPE_FLOAT
Definition: PropertyTypeConstants.java:76
com.google.appinventor.components.annotations.SimpleFunction
Definition: SimpleFunction.java:23
com.google.appinventor.components.runtime.NxtDrive.DriveMotors
String DriveMotors()
Definition: NxtDrive.java:87
com.google.appinventor.components.runtime.NxtDrive
Definition: NxtDrive.java:40
com.google.appinventor.components.runtime.NxtDrive.DriveMotors
void DriveMotors(String motorPortLetters)
Definition: NxtDrive.java:97
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.NxtDrive.StopBeforeDisconnect
boolean StopBeforeDisconnect()
Definition: NxtDrive.java:137
com.google.appinventor.components.runtime.NxtDrive.Stop
void Stop()
Definition: NxtDrive.java:235
com.google.appinventor.components.common.YaVersion
Definition: YaVersion.java:14
com.google.appinventor.components.annotations.DesignerProperty
Definition: DesignerProperty.java:25
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_STRING
static final String PROPERTY_TYPE_STRING
Definition: PropertyTypeConstants.java:237
com.google.appinventor.components.runtime.NxtDrive.NxtDrive
NxtDrive(ComponentContainer container)
Definition: NxtDrive.java:63
com.google.appinventor.components
com.google.appinventor.components.runtime.util.ErrorMessages.ERROR_NXT_INVALID_MOTOR_PORT
static final int ERROR_NXT_INVALID_MOTOR_PORT
Definition: ErrorMessages.java:56
com.google.appinventor.components.runtime.NxtDrive.MoveForward
void MoveForward(int power, double distance)
Definition: NxtDrive.java:167
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_BOOLEAN
static final String PROPERTY_TYPE_BOOLEAN
Definition: PropertyTypeConstants.java:35
com.google.appinventor.components.annotations.DesignerComponent
Definition: DesignerComponent.java:22
com.google.appinventor.components.annotations.PropertyCategory.BEHAVIOR
BEHAVIOR
Definition: PropertyCategory.java:15
com.google.appinventor.components.runtime.NxtDrive.TurnCounterClockwiseIndefinitely
void TurnCounterClockwiseIndefinitely(int power)
Definition: NxtDrive.java:207
com.google.appinventor.components.runtime.NxtDrive.MoveBackward
void MoveBackward(int power, double distance)
Definition: NxtDrive.java:175
com.google.appinventor.components.runtime.LegoMindstormsNxtBase.setOutputState
final void setOutputState(String functionName, int port, int power, int mode, int regulationMode, int turnRatio, int runState, long tachoLimit)
Definition: LegoMindstormsNxtBase.java:137
com.google.appinventor.components.runtime.LegoMindstormsNxtBase.convertMotorPortLetterToNumber
final int convertMotorPortLetterToNumber(String motorPortLetter)
Definition: LegoMindstormsNxtBase.java:456
com.google.appinventor.components.annotations.SimpleProperty
Definition: SimpleProperty.java:23
com.google.appinventor.components.runtime.NxtDrive.MoveBackwardIndefinitely
void MoveBackwardIndefinitely(int power)
Definition: NxtDrive.java:161
com.google.appinventor.components.runtime.BluetoothConnectionBase
Definition: BluetoothConnectionBase.java:41
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.LegoMindstormsNxtBase
Definition: LegoMindstormsNxtBase.java:29
com.google.appinventor.components.common
Definition: ComponentCategory.java:7
com.google.appinventor.components.runtime.NxtDrive.StopBeforeDisconnect
void StopBeforeDisconnect(boolean stopBeforeDisconnect)
Definition: NxtDrive.java:149
com.google.appinventor.components.common.ComponentCategory
Definition: ComponentCategory.java:48
com.google.appinventor.components.runtime.LegoMindstormsNxtBase.checkBluetooth
final boolean checkBluetooth(String functionName)
Definition: LegoMindstormsNxtBase.java:250
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
com
com.google.appinventor.components.runtime.NxtDrive.TurnClockwiseIndefinitely
void TurnClockwiseIndefinitely(int power)
Definition: NxtDrive.java:195
com.google.appinventor.components.runtime.NxtDrive.WheelDiameter
void WheelDiameter(float wheelDiameter)
Definition: NxtDrive.java:128
com.google.appinventor.components.runtime.AndroidNonvisibleComponent.form
final Form form
Definition: AndroidNonvisibleComponent.java:19
com.google.appinventor.components.runtime.NxtDrive.beforeDisconnect
void beforeDisconnect(BluetoothConnectionBase bluetoothConnection)
Definition: NxtDrive.java:72
com.google.appinventor.components.common.PropertyTypeConstants
Definition: PropertyTypeConstants.java:14
com.google.appinventor.components.annotations
com.google.appinventor
com.google.appinventor.components.runtime.NxtDrive.MoveForwardIndefinitely
void MoveForwardIndefinitely(int power)
Definition: NxtDrive.java:155
com.google.appinventor.components.runtime.NxtDrive.WheelDiameter
float WheelDiameter()
Definition: NxtDrive.java:116