4 * Copyright (c) 1993-2002 Sun Microsystems, Inc. All Rights Reserved.
\r
6 * This software is the confidential and proprietary information of Sun
\r
7 * Microsystems, Inc. ("Confidential Information"). You shall not
\r
8 * disclose such Confidential Information and shall use it only in
\r
9 * accordance with the terms of the license agreement you entered into
\r
12 * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
\r
13 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
\r
14 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
\r
15 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
\r
16 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
\r
17 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
\r
21 * This class is a wrapper for a PWMControl introduced by porting to SSJava. It
\r
22 * maintains two key fields motorLeftUpTime and motorRightUpTime and expects
\r
23 * that the control thread who is running from the other side gets the current
\r
27 @LATTICE("UP<V,V<T,V*")
\r
28 @METHODDEFAULT("OUT<THIS,THIS<V,V<IN,V*,THISLOC=THIS,RETURNLOC=OUT")
\r
29 public class MotorControl {
\r
31 boolean DEBUG = false;
\r
33 int motorLeftUpTime = 150;
\r
35 int motorRightUpTime = 150;
\r
41 public MotorControl(int speedFactor, int agilityFactor) {
\r
42 this.speedFactor = speedFactor;
\r
43 this.agilityFactor = agilityFactor;
\r
46 // A poor's man ajustimg for the 0 speed which found
\r
47 // to be 450000 nano seconds.
\r
48 @RETURNLOC("THIS,MotorControl.T")
\r
49 private int normalizeTime(@LOC("IN") int timePosition) {
\r
50 if ((timePosition <= 50) && (timePosition >= 44)) {
\r
53 return timePosition;
\r
57 * setSpeedSpin - Set speed for the spin case motor 1.
\r
60 * pulse width (time position)
\r
62 public void setSpeedSpinLeft(@LOC("IN") int timePosition) {
\r
63 /* Speed comes in as a number between 0 - 100 */
\r
64 /* It represents values between 1 to 2 ms */
\r
65 /* 100-input to get reverse polarity for this motor */
\r
66 /* since it's mounted in reverse direction to the other motor */
\r
68 System.out.println("setSpeedSpinLeft: input-> " + Integer.toString(timePosition));
\r
70 @LOC("THIS,MotorControl.V") int timePos = normalizeTime(timePosition);
\r
72 @LOC("THIS,MotorControl.V") int motorUpTime = (int) (timePos * agilityFactor * speedFactor);
\r
73 // System.out.println("Left Motor UpTime1: " +
\r
74 // Integer.toString(motorUpTime));
\r
75 // Since the right motor is hanging in reverse position
\r
76 // the direction should be opposit
\r
77 // Bug in wait. Can't take 0 nanoseconds
\r
78 if (motorUpTime == 0) {
\r
80 // System.out.println("returning....");
\r
82 } else if (motorUpTime == 1000000) {
\r
87 System.out.println("setSpeedSpinLeft: output-> = " + Integer.toString(motorUpTime));
\r
89 // synchronized (this) {
\r
90 /* Factor in the speed and agility factors */
\r
91 motorLeftUpTime = motorUpTime;
\r
96 * setSpeedSpinRight - Set speed for the spin case right motor.
\r
99 * pulse width (time position)
\r
101 public void setSpeedSpinRight(@LOC("IN") int timePosition) {
\r
102 /* Speed comes in as a number between 0 - 100 */
\r
103 /* It represents values between 1 to 2 ms */
\r
104 /* An input of 50 should result in 0 speed. */
\r
105 /* 100 should result in full speed forward */
\r
106 /* while 0 should result in full speed backwords */
\r
108 System.out.println("setSpeedSpinRight: input-> " + Integer.toString(timePosition));
\r
110 @LOC("THIS,MotorControl.V") int timePos = normalizeTime(timePosition);
\r
111 @LOC("THIS,MotorControl.V") int motorUpTime = (int) ((timePos) * agilityFactor * speedFactor);
\r
112 // Bug in wait. Cant take 0 nanoseconds
\r
113 if (motorUpTime == 0) {
\r
115 // System.out.println("returning....");
\r
117 } else if (motorUpTime == 1000000) {
\r
122 System.out.println("setSpeedSpinRight: output-> = " + Integer.toString(motorUpTime));
\r
124 // synchronized (this) {
\r
125 /* Factor in the speed and agility factors */
\r
126 motorRightUpTime = motorUpTime;
\r
131 * setSpeedTurnM1 - set speed considering agility factor for motor 1
\r
134 * pulse width (time position)
\r
136 public void setSpeedTurnLeft(@LOC("IN") int timePosition) {
\r
137 /* Speed comes in as a number between 0 - 100 */
\r
138 /* It represents values between 1 to 2 ms */
\r
139 /* 100-input to get reverse polarity for this motor */
\r
140 /* since it's mounted in reverse direction to the other motor */
\r
142 System.out.println("setSpeedTurnLeft: input-> " + Integer.toString(timePosition));
\r
144 @LOC("THIS,MotorControl.V") int timePosLocal = normalizeTime(timePosition);
\r
145 @LOC("THIS,MotorControl.V") int motorUpTime =
\r
146 (timePosLocal * 100 + ((100 - timePosLocal) * (100 - agilityFactor))) * speedFactor;
\r
147 if (motorUpTime == 0) {
\r
149 // System.out.println("returning....");
\r
151 } else if (motorUpTime == 1000000) {
\r
155 System.out.println("setSpeedTurnLeft: output-> = " + Integer.toString(motorUpTime));
\r
157 // synchronized (this) {
\r
158 /* Factor in the speed and agility factors */
\r
159 motorLeftUpTime = motorUpTime;
\r
164 * setSpeedTurnM1 - set speed considering agility factor for motor 2
\r
167 * pulse width (time position)
\r
169 public void setSpeedTurnRight(@LOC("IN") int timePosition) {
\r
170 /* Speed comes in as a number between 0 - 100 */
\r
171 /* It represents values between 1 to 2 ms */
\r
173 System.out.println("setSpeedTurnRight: input-> " + Integer.toString(timePosition));
\r
175 @LOC("THIS,MotorControl.V") int timePos = normalizeTime(timePosition);
\r
176 @LOC("THIS,MotorControl.V") int motorUpTime =
\r
177 ((timePos * 100) + ((100 - timePos) * (100 - agilityFactor))) * speedFactor;
\r
178 if (motorUpTime == 0) {
\r
180 // System.out.println("returning....");
\r
182 } else if (motorUpTime == 1000000) {
\r
187 System.out.println("setSpeedTurnRight: output-> " + Integer.toString(motorUpTime));
\r
189 // synchronized (this) {
\r
190 /* Factor in the speed and agility factors */
\r
191 motorRightUpTime = motorUpTime;
\r
196 * setSpeedLeft - speed control for motor 1.
\r
199 * pulse width (time position)
\r
201 public void setSpeedLeft(@LOC("IN") int timePosition) {
\r
202 /* Speed comes in as a number between 0 - 100 */
\r
203 /* It represents values between 1 to 2 ms */
\r
204 /* 100-input to get reverse polarity for this motor */
\r
205 /* since it's mounted in reverse direction to the other motor */
\r
207 System.out.println("setSpeedLeft: input-> " + Integer.toString(timePosition));
\r
209 @LOC("THIS,MotorControl.V") int timePos = normalizeTime(timePosition);
\r
210 @LOC("THIS,MotorControl.V") int motorUpTime = (int) (timePos * 100) * speedFactor;
\r
212 * System.out.println("motorUpTime = " + Integer.toStri@LOC("IN")
\r
213 * ng(motorUpTime) + " timePos = " + Integer.toString((int)timePos) +
\r
214 * " timePosition = " + Integer.toString((int)timePosition) +
\r
215 * " speedFactor = " + Integer.toString(speedFactor));
\r
217 if (motorUpTime == 0) {
\r
219 // System.out.println("returning....");
\r
221 } else if (motorUpTime == 1000000) {
\r
225 System.out.println("setSpeedLeft: output-> " + Integer.toString(motorUpTime));
\r
227 // synchronized (this) {
\r
228 /* Factor in speedFactor */
\r
229 motorLeftUpTime = motorUpTime;
\r
234 * setSpeedRight - speed control for motor 1.
\r
237 * pulse width (time position)
\r
239 public void setSpeedRight(@LOC("IN") int timePosition) {
\r
241 System.out.println("setSpeedRight: input-> " + Integer.toString(timePosition));
\r
243 /* Speed comes in as a number between 0 - 100 */
\r
244 /* It represents values between 1 to 2 ms */
\r
245 @LOC("THIS,MotorControl.V") int timePos = normalizeTime(timePosition);
\r
246 @LOC("THIS,MotorControl.V") int motorUpTime = (int) (timePos * 100) * speedFactor;
\r
247 if (motorUpTime == 0) {
\r
249 // System.out.println("returning....");
\r
251 } else if (motorUpTime == 1000000) {
\r
255 System.out.println("setSpeedRight: output-> " + Integer.toString(motorUpTime));
\r
257 // synchronized (this) {
\r
258 /* Factor in speedFactor */
\r
259 motorRightUpTime = motorUpTime;
\r
263 public void setUrgentReverse() {
\r
264 // synchronized (this) {
\r
265 motorLeftUpTime = 1;
\r
266 motorRightUpTime = 1;
\r
270 public void setUrgentStraight() {
\r
271 // synchronized (this) {
\r
272 motorLeftUpTime = 99;
\r
273 motorRightUpTime = 99;
\r
277 public void justSync() {
\r
278 // synchronized (this) {
\r
279 motorLeftUpTime = motorLeftUpTime;
\r
280 motorRightUpTime = motorRightUpTime;
\r
285 * Control debug messageing. true - Activate debug messages false - deactivate
\r
288 public void setDebug(boolean debug) {
\r