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
29 public class MotorControl {
\r
31 boolean DEBUG = true;
\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
49 private int normalizeTime( 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( 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 int timePos = normalizeTime(timePosition);
\r
72 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-> = " +
\r
88 // Integer.toString(motorUpTime));
\r
90 // synchronized (this) {
\r
91 /* Factor in the speed and agility factors */
\r
92 motorLeftUpTime = motorUpTime;
\r
95 System.out.println("MotorControl: setSpeedSpinLeft: output-> " + motorLeftUpTime);
\r
100 * setSpeedSpinRight - Set speed for the spin case right motor.
\r
103 * pulse width (time position)
\r
105 public void setSpeedSpinRight( int timePosition) {
\r
106 /* Speed comes in as a number between 0 - 100 */
\r
107 /* It represents values between 1 to 2 ms */
\r
108 /* An input of 50 should result in 0 speed. */
\r
109 /* 100 should result in full speed forward */
\r
110 /* while 0 should result in full speed backwords */
\r
112 System.out.println("setSpeedSpinRight: input-> " + Integer.toString(timePosition));
\r
114 int timePos = normalizeTime(timePosition);
\r
115 int motorUpTime = (int) ((timePos) * agilityFactor * speedFactor);
\r
116 // Bug in wait. Cant take 0 nanoseconds
\r
117 if (motorUpTime == 0) {
\r
119 // System.out.println("returning....");
\r
121 } else if (motorUpTime == 1000000) {
\r
126 // System.out.println("setSpeedSpinRight: output-> = " +
\r
127 // Integer.toString(motorUpTime));
\r
129 // synchronized (this) {
\r
130 /* Factor in the speed and agility factors */
\r
131 motorRightUpTime = motorUpTime;
\r
134 System.out.println("MotorControl: setSpeedSpinRight: output-> " + motorRightUpTime);
\r
139 * setSpeedTurnM1 - set speed considering agility factor for motor 1
\r
142 * pulse width (time position)
\r
144 public void setSpeedTurnLeft( int timePosition) {
\r
145 /* Speed comes in as a number between 0 - 100 */
\r
146 /* It represents values between 1 to 2 ms */
\r
147 /* 100-input to get reverse polarity for this motor */
\r
148 /* since it's mounted in reverse direction to the other motor */
\r
150 System.out.println("setSpeedTurnLeft: input-> " + Integer.toString(timePosition));
\r
152 int timePosLocal = normalizeTime(timePosition);
\r
154 (timePosLocal * 100 + ((100 - timePosLocal) * (100 - agilityFactor))) * speedFactor;
\r
155 if (motorUpTime == 0) {
\r
157 // System.out.println("returning....");
\r
159 } else if (motorUpTime == 1000000) {
\r
163 // System.out.println("setSpeedTurnLeft: output-> = " +
\r
164 // Integer.toString(motorUpTime));
\r
166 // synchronized (this) {
\r
167 /* Factor in the speed and agility factors */
\r
168 motorLeftUpTime = motorUpTime;
\r
171 System.out.println("MotorControl: setSpeedTurnLeft: output-> " + motorLeftUpTime);
\r
176 * setSpeedTurnM1 - set speed considering agility factor for motor 2
\r
179 * pulse width (time position)
\r
181 public void setSpeedTurnRight( int timePosition) {
\r
182 /* Speed comes in as a number between 0 - 100 */
\r
183 /* It represents values between 1 to 2 ms */
\r
185 System.out.println("setSpeedTurnRight: input-> " + Integer.toString(timePosition));
\r
187 int timePos = normalizeTime(timePosition);
\r
189 ((timePos * 100) + ((100 - timePos) * (100 - agilityFactor))) * speedFactor;
\r
190 if (motorUpTime == 0) {
\r
192 // System.out.println("returning....");
\r
194 } else if (motorUpTime == 1000000) {
\r
198 // synchronized (this) {
\r
199 /* Factor in the speed and agility factors */
\r
200 motorRightUpTime = motorUpTime;
\r
203 System.out.println("MotorControl: setSpeedTurnRight: output-> " + motorRightUpTime);
\r
208 * setSpeedLeft - speed control for motor 1.
\r
211 * pulse width (time position)
\r
213 public void setSpeedLeft( int timePosition) {
\r
214 /* Speed comes in as a number between 0 - 100 */
\r
215 /* It represents values between 1 to 2 ms */
\r
216 /* 100-input to get reverse polarity for this motor */
\r
217 /* since it's mounted in reverse direction to the other motor */
\r
219 System.out.println("setSpeedLeft: input-> " + Integer.toString(timePosition));
\r
221 int timePos = normalizeTime(timePosition);
\r
222 int motorUpTime = (int) (timePos * 100) * speedFactor;
\r
224 * System.out.println("motorUpTime = " + Integer.toStri
\r
225 * ng(motorUpTime) + " timePos = " + Integer.toString((int)timePos) +
\r
226 * " timePosition = " + Integer.toString((int)timePosition) +
\r
227 * " speedFactor = " + Integer.toString(speedFactor));
\r
229 if (motorUpTime == 0) {
\r
231 // System.out.println("returning....");
\r
233 } else if (motorUpTime == 1000000) {
\r
237 // synchronized (this) {
\r
238 /* Factor in speedFactor */
\r
239 motorLeftUpTime = motorUpTime;
\r
243 .println("MotorContol: setSpeedLeft: output-> " + Integer.toString(motorLeftUpTime));
\r
248 * setSpeedRight - speed control for motor 1.
\r
251 * pulse width (time position)
\r
253 public void setSpeedRight( int timePosition) {
\r
255 System.out.println("setSpeedRight: input-> " + Integer.toString(timePosition));
\r
257 /* Speed comes in as a number between 0 - 100 */
\r
258 /* It represents values between 1 to 2 ms */
\r
259 int timePos = normalizeTime(timePosition);
\r
260 int motorUpTime = (int) (timePos * 100) * speedFactor;
\r
261 if (motorUpTime == 0) {
\r
263 // System.out.println("returning....");
\r
265 } else if (motorUpTime == 1000000) {
\r
268 // synchronized (this) {
\r
269 /* Factor in speedFactor */
\r
270 motorRightUpTime = motorUpTime;
\r
273 System.out.println("MotorControl: setSpeedRight: output-> " + motorRightUpTime);
\r
277 public void setUrgentReverse() {
\r
278 // synchronized (this) {
\r
279 motorLeftUpTime = 1;
\r
280 motorRightUpTime = 1;
\r
283 System.out.println("MotorControl: setUrgentReverse: motorLeftUpTime-> " + motorLeftUpTime);
\r
284 System.out.println("MotorControl: setUrgentReverse: motorRightUpTime-> " + motorRightUpTime);
\r
288 public void setUrgentStraight() {
\r
289 // synchronized (this) {
\r
290 motorLeftUpTime = 99;
\r
291 motorRightUpTime = 99;
\r
293 System.out.println("MotorControl: setUrgentStraight: motorLeftUpTime-> " + motorLeftUpTime);
\r
294 System.out.println("MotorControl: setUrgentStraight: motorRightUpTime-> " + motorRightUpTime);
\r
299 public void justSync() {
\r
300 // synchronized (this) {
\r
301 motorLeftUpTime = motorLeftUpTime;
\r
302 motorRightUpTime = motorRightUpTime;
\r
304 System.out.println("MotorControl: justSync: motorLeftUpTime-> " + motorLeftUpTime);
\r
305 System.out.println("MotorControl: justSync: motorRightUpTime-> " + motorRightUpTime);
\r
311 * Control debug messageing. true - Activate debug messages false - deactivate
\r
314 public void setDebug(boolean debug) {
\r