7 package com.google.appinventor.components.runtime;
20 import java.util.ArrayList;
21 import java.util.List;
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,
38 iconName =
"images/legoMindstormsNxt.png")
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;
55 private String driveMotors;
56 private List<Integer> driveMotorPorts;
57 private double wheelDiameter;
58 private boolean stopBeforeDisconnect;
64 super(container,
"NxtDrive");
73 if (stopBeforeDisconnect) {
74 for (
int port : driveMotorPorts) {
76 MODE_BRAKE, REGULATION_MODE_IDLE, 0, MOTOR_RUN_STATE_IDLE, 0);
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.",
98 driveMotors = motorPortLetters;
99 driveMotorPorts =
new ArrayList<Integer>();
100 for (
int i = 0; i < motorPortLetters.length(); i++) {
101 char ch = motorPortLetters.charAt(i);
104 }
catch (IllegalArgumentException e) {
114 @
SimpleProperty(description =
"The diameter of the wheels used for driving.",
117 return (
float) wheelDiameter;
126 defaultValue =
"4.32")
129 this.wheelDiameter = wheelDiameter;
135 @
SimpleProperty(description =
"Whether to stop the drive motors before disconnecting.",
138 return stopBeforeDisconnect;
147 defaultValue =
"True")
150 this.stopBeforeDisconnect = stopBeforeDisconnect;
153 @
SimpleFunction(description =
"Move the robot forward indefinitely, with the " +
154 "specified percentage of maximum power, by powering both drive motors forward.")
156 move(
"MoveForwardIndefinitely", power, 0L);
159 @
SimpleFunction(description =
"Move the robot backward indefinitely, with the " +
160 "specified percentage of maximum power, by powering both drive motors backward.")
162 move(
"MoveBackwardIndefinitely", -power, 0L);
165 @
SimpleFunction(description =
"Move the robot forward the given distance, with the " +
166 "specified percentage of maximum power, by powering both drive motors forward.")
168 long tachoLimit = (long) (360.0 * distance / (wheelDiameter * Math.PI));
170 move(
"MoveForward", power, tachoLimit);
173 @
SimpleFunction(description =
"Move the robot backward the given distance, with the " +
174 "specified percentage of maximum power, by powering both drive motors backward.")
176 long tachoLimit = (long) (360.0 * distance / (wheelDiameter * Math.PI));
178 move(
"MoveBackward", -power, tachoLimit);
181 private void move(String functionName,
int power,
long tachoLimit) {
186 for (
int port : driveMotorPorts) {
188 MODE_MOTORON, REGULATION_MODE_MOTOR_SPEED, 0, MOTOR_RUN_STATE_RUNNING, tachoLimit);
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.")
196 int numDriveMotors = driveMotorPorts.size();
197 if (numDriveMotors >= 2) {
198 int forwardMotorIndex = 0;
199 int backwardMotorIndex = numDriveMotors - 1;
200 turnIndefinitely(
"TurnClockwiseIndefinitely", power, forwardMotorIndex, backwardMotorIndex);
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.")
208 int numDriveMotors = driveMotorPorts.size();
209 if (numDriveMotors >= 2) {
210 int forwardMotorIndex = numDriveMotors - 1;
211 int backwardMotorIndex = 0;
212 turnIndefinitely(
"TurnCounterClockwiseIndefinitely", power, forwardMotorIndex,
217 private void turnIndefinitely(String functionName,
int power,
int forwardMotorIndex,
218 int reverseMotorIndex) {
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);
234 @SimpleFunction(description =
"Stop the drive motors of the robot.")
236 String functionName =
"Stop";
241 for (
int port : driveMotorPorts) {
243 MODE_BRAKE, REGULATION_MODE_IDLE, 0, MOTOR_RUN_STATE_IDLE, 0);