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
20 //public class PWMNative extends PWMControl implements Runnable, InterruptEventListener {
\r
21 public class PWMNative extends PWMControl {
\r
23 private int PULSE_INTERVAL = 2000;
\r
24 private int NATIVE_OFFSET = 100;
\r
26 private Object tc0Obj;
\r
27 private Object tc1Obj;
\r
28 private int pulseUpTime;
\r
30 // private TimerCounter tc0;
\r
31 // private TimerCounter tc1;
\r
32 // private TimerCounter[] tcSet = new TimerCounter[2];
\r
34 PWMNative(PWMManager pwmMan, int motor1bit, int motor2bit) {
\r
35 super(pwmMan, motor1bit, motor2bit);
\r
37 System.out.println("PWMNative constructor.....Start");
\r
39 tc0Obj = new Object();
\r
40 tc1Obj = new Object();
\r
42 // TimerCounter.setPrescalerClockSource(TimerCounter.INTERNAL_PERIPHERAL_CLOCK);
\r
43 // TimerCounter.setPrescalerReloadRegisterValue(375);
\r
44 // TimerCounter.setPrescalerEnabled(true);
\r
46 // tc0 = tcSet[0] = new TimerCounter(0);
\r
47 // tc0.setMode_IO_Line_A(TimerCounter.TIMER_0_OUTPUT);
\r
48 // // bring TIMER_0_OUTPUT_DIVIDE_BY_2 out via IO_Line_B
\r
49 // tc0.setMode_IO_Line_B(TimerCounter.TIMER_0_OUTPUT_DIVIDE_BY_2);
\r
50 // // In JemBuilder, go to Pin Setup 2 and allocate Port E bit 6 to
\r
51 // // be Timer IO_Line_B.
\r
53 // // Connect signal lead of servo (usually the white one) to IOE6 on
\r
55 // tc0.set_IO_Line_A_Polarity(TimerCounter.POLARITY_ACTIVE_STATE_HIGH);
\r
56 // tc0.set_IO_Line_B_Polarity(TimerCounter.POLARITY_ACTIVE_STATE_HIGH);
\r
57 // tc0.setExternalTimerEnableMode(TimerCounter.TIMER_ENABLED_ONLY_VIA_MTEN_AND_TRIGGER);
\r
58 // tc0.setReloadRegisterValue(100);
\r
59 // tc0.setExternalStartTriggerMode(TimerCounter.NO_EXTERNAL_START_TRIGGER);
\r
60 // tc0.setMasterTimerEnabled(true);
\r
61 // tc0.setGlobalInterruptEnabled(true);
\r
62 // tc0.setTimeOutInterruptEnabled(true);
\r
63 // tc0.setTimerTriggerRegister(false);
\r
64 // System.out.println("PWMNative: Constructor completed 1....");
\r
66 * tc1 = tcSet[1] = new TimerCounter ( 1 ); tc1.setMode_IO_Line_A(
\r
67 * TimerCounter.TIMER_1_OUTPUT ); //bring TIMER_0_OUTPUT_DIVIDE_BY_2 out via
\r
68 * IO_Line_B tc1.setMode_IO_Line_B( TimerCounter.TIMER_1_OUTPUT_DIVIDE_BY_2
\r
69 * ); //In JemBuilder, go to Pin Setup 2 and allocate Port E bit 6 to //be
\r
71 * tc1.set_IO_Line_A_Polarity(TimerCounter.POLARITY_ACTIVE_STATE_HIGH);
\r
72 * tc1.set_IO_Line_B_Polarity(TimerCounter.POLARITY_ACTIVE_STATE_HIGH);
\r
73 * tc1.setExternalTimerEnableMode
\r
74 * (TimerCounter.TIMER_ENABLED_ONLY_VIA_MTEN_AND_TRIGGER );
\r
75 * tc1.setReloadRegisterValue( 100 );
\r
76 * tc1.setExternalStartTriggerMode(TimerCounter.NO_EXTERNAL_START_TRIGGER );
\r
77 * tc1.setMasterTimerEnabled( true ); tc1.setGlobalInterruptEnabled( true);
\r
78 * tc1.setTimeOutInterruptEnabled( true); tc1.setTimerTriggerRegister( false
\r
82 * // Add interrupt event listener for GPTC
\r
83 * InterruptController.addInterruptListener( this,
\r
84 * InterruptController.GPTC_INTERRUPT );
\r
86 * // Turn on interrupts InterruptController.enableInterrupt(
\r
87 * InterruptController.GPTC_INTERRUPT );
\r
89 * // start all prescaler based timers TimerCounter.setPrescalerEnabled(
\r
92 // t2 = new Thread(this);
\r
94 // t2.setPriority(20);
\r
95 System.out.println("PWMNative: Constructor return.....");
\r
98 public void setUpTime(int upTime) {
\r
99 // synchronized (obj) {
\r
100 pulseUpTime = upTime;
\r
105 * public void interruptEvent() { TimerCounter tc; int tcIntrState;
\r
107 * System.out.println("PWMNative: InterruptEvent"); do { for ( int tcNum=0;
\r
108 * tcNum<2; tcNum++ ) { tc = tcSet[ tcNum ];
\r
109 * if(tc.readAndClear_TimeOutInterruptStatus()){ switch(tcNum){ case 0:
\r
110 * System.out.println("PWMNative: Interrupt case 0"); synchronized(tc0Obj){
\r
111 * System.out.println("PWMNative: Interrupt notify 0"); tc0Obj.notify(); }
\r
112 * break; case 1: System.out.println("PWMNative: Interrupt case 1");
\r
113 * synchronized(tc1Obj){ System.out.println("PWMNative: Interrupt notify 1");
\r
114 * tc1Obj.notify(); } break; default:; } } } } while (
\r
115 * TimerCounter.isGPTCInterruptPending() ); }
\r
117 public void run() {
\r
119 // System.out.println("PWMNative: run method........");
\r
120 // int upTime0 = 150; // 1.5 milli seconds = 0 speed
\r
121 // int upTime1 = 150; // 1.5 milli seconds = 0 speed
\r
124 // synchronized (obj) {
\r
126 // * System.out.println("PWMNative: Updating Up Times......Left = " +
\r
127 // * Integer.toString(motorLeftUpTime) + " Right = " +
\r
128 // * Integer.toString(motorRightUpTime));
\r
130 // upTime0 = motorLeftUpTime;
\r
131 // upTime1 = motorRightUpTime;
\r
134 // // Timer number 1
\r
135 // tc0.setReloadRegisterValue(upTime0);
\r
136 // outToPortMLeft(PULSE_HIGH);
\r
137 // tc0.setTimerTriggerRegister(true);
\r
139 // } while (tc0.didTimeOutInterruptOccur());
\r
140 // outToPortMLeft(PULSE_LOW);
\r
141 // tc0.setTimerTriggerRegister(false);
\r
142 // tc0.resetTimeOutInterruptStatus();
\r
144 // // System.out.println("PWMNative: Big loop long suspend1");
\r
146 // tc0Obj.wait(18, 500000);
\r
147 // } catch (Exception e) {
\r
149 // // System.out.println("PWMNative: Big loop long suspend2");
\r
151 // tc0.setReloadRegisterValue(upTime1);
\r
152 // outToPortMRight(PULSE_HIGH);
\r
153 // tc0.setTimerTriggerRegister(true);
\r
155 // } while (tc0.didTimeOutInterruptOccur());
\r
156 // outToPortMRight(PULSE_LOW);
\r
157 // tc0.setTimerTriggerRegister(false);
\r
158 // tc0.resetTimeOutInterruptStatus();
\r
161 // tc0Obj.wait(18, 500000);
\r
162 // } catch (Exception e) {
\r
166 * // Timer number 2 tc1.setReloadRegisterValue( upTime1 );
\r
167 * tc1.setTimerTriggerRegister( true ); synchronized(tc1Obj){ try{
\r
168 * System.out.println("PWMNative: Sleep 3"); tc1Obj.wait();
\r
169 * System.out.println("PWMNative: Wake Up 3"); }catch(Exception e){ } }
\r
170 * tc1.setTimerTriggerRegister( false ); tc1.resetTimeOutInterruptStatus();
\r
171 * tc1.setReloadRegisterValue( PULSE_INTERVAL - upTime1 ); //this sets pulse
\r
172 * interval tc1.setTimerTriggerRegister( true ); synchronized(tc1Obj){ try{
\r
173 * System.out.println("PWMNative: Sleep 3"); tc1Obj.wait();
\r
174 * System.out.println("PWMNative: Wake Up 3"); }catch(Exception e){ } }
\r
175 * tc1.setTimerTriggerRegister( false ); tc1.resetTimeOutInterruptStatus();
\r
180 public void setManualMode() {
\r
182 System.out.println("PWMControl: setManualMode... ");
\r
183 // synchronized (obj) {
\r
184 if (manualMode == false) {
\r
190 public void setAutomatedMode() {
\r
192 System.out.println("PWMControl: setAutomatedMode... ");
\r
194 * synchronized(obj){ if(manualMode == true){
\r
195 * System.out.println("PWMControl: wake me up... "); obj.notifyAll();
\r
196 * manualMode = false; } }
\r
201 * setSpeedSpin - Set speed for the spin case motor 1.
\r
204 * pulse width (time position)
\r
206 public void setSpeedSpinLeft(int timePosition) {
\r
207 /* Speed comes in as a number between 0 - 100 */
\r
208 /* It represents values between 1 to 2 ms */
\r
209 /* 100-input to get reverse polarity for this motor */
\r
210 /* since it's mounted in reverse direction to the other motor */
\r
213 System.out.println("setSpeedSpinLeft: input-> " + Integer.toString(timePosition));
\r
215 int timePos = timePosition + NATIVE_OFFSET;
\r
216 int motorUpTime = (int) (timePos * agilityFactor * speedFactor) / 10000;
\r
219 System.out.println("setSpeedSpinLeft: output-> = " + Integer.toString(motorUpTime));
\r
221 // synchronized (obj) {
\r
222 /* Factor in the speed and agility factors */
\r
223 motorLeftUpTime = motorUpTime;
\r
228 * setSpeedSpinRight - Set speed for the spin case right motor.
\r
231 * pulse width (time position)
\r
233 public void setSpeedSpinRight(int timePosition) {
\r
234 /* Speed comes in as a number between 0 - 100 */
\r
235 /* It represents values between 1 to 2 ms */
\r
236 /* An input of 50 should result in 0 speed. */
\r
237 /* 100 should result in full speed forward */
\r
238 /* while 0 should result in full speed backwords */
\r
240 System.out.println("setSpeedSpinRight: input-> " + Integer.toString(timePosition));
\r
242 int timePos = timePosition + NATIVE_OFFSET;
\r
243 int motorUpTime = (int) ((timePos) * agilityFactor * speedFactor) / 10000;
\r
246 System.out.println("setSpeedSpinRight: output-> = " + Integer.toString(motorUpTime));
\r
248 // synchronized (obj) {
\r
249 /* Factor in the speed and agility factors */
\r
250 motorRightUpTime = motorUpTime;
\r
255 * setSpeedTurnM1 - set speed considering agility factor for motor 1
\r
258 * pulse width (time position)
\r
260 public void setSpeedTurnLeft(int timePosition) {
\r
261 /* Speed comes in as a number between 0 - 100 */
\r
262 /* It represents values between 1 to 2 ms */
\r
263 /* 100-input to get reverse polarity for this motor */
\r
264 /* since it's mounted in reverse direction to the other motor */
\r
266 System.out.println("setSpeedTurnLeft: input-> " + Integer.toString(timePosition));
\r
268 int timePosLocal = timePosition + NATIVE_OFFSET;
\r
270 ((timePosLocal * 100 + ((100 - timePosLocal) * (100 - agilityFactor))) * speedFactor) / 10000;
\r
272 System.out.println("setSpeedTurnLeft: output-> = " + Integer.toString(motorUpTime));
\r
274 // synchronized (obj) {
\r
275 /* Factor in the speed and agility factors */
\r
276 motorLeftUpTime = motorUpTime;
\r
281 * setSpeedTurnM1 - set speed considering agility factor for motor 2
\r
284 * pulse width (time position)
\r
286 public void setSpeedTurnRight(int timePosition) {
\r
287 /* Speed comes in as a number between 0 - 100 */
\r
288 /* It represents values between 1 to 2 ms */
\r
290 System.out.println("setSpeedTurnRight: input-> " + Integer.toString(timePosition));
\r
292 int timePos = timePosition + NATIVE_OFFSET;
\r
294 (((timePos * 100) + ((100 - timePos) * (100 - agilityFactor))) * speedFactor) / 10000;
\r
297 System.out.println("setSpeedTurnRight: output-> " + Integer.toString(motorUpTime));
\r
299 // synchronized (obj) {
\r
300 /* Factor in the speed and agility factors */
\r
301 motorRightUpTime = motorUpTime;
\r
306 * setSpeedLeft - speed control for motor 1.
\r
309 * pulse width (time position)
\r
311 public void setSpeedLeft(int timePosition) {
\r
312 /* Speed comes in as a number between 0 - 100 */
\r
313 /* It represents values between 1 to 2 ms */
\r
314 /* 100-input to get reverse polarity for this motor */
\r
315 /* since it's mounted in reverse direction to the other motor */
\r
317 System.out.println("setSpeedLeft: input-> " + Integer.toString(timePosition));
\r
319 int timePos = timePosition + NATIVE_OFFSET;
\r
320 int motorUpTime = (int) ((timePos * 100) * speedFactor) / 10000;
\r
323 System.out.println("setSpeedLeft: output-> " + Integer.toString(motorUpTime));
\r
325 // synchronized (obj) {
\r
326 /* Factor in speedFactor */
\r
327 motorLeftUpTime = motorUpTime;
\r
332 * setSpeedRight - speed control for motor 1.
\r
335 * pulse width (time position)
\r
337 public void setSpeedRight(int timePosition) {
\r
339 System.out.println("setSpeedRight: input-> " + Integer.toString(timePosition));
\r
341 /* Speed comes in as a number between 0 - 100 */
\r
342 /* It represents values between 1 to 2 ms */
\r
343 int timePos = timePosition + NATIVE_OFFSET;
\r
344 int motorUpTime = (int) (((timePos * 100) * speedFactor) / 10000);
\r
347 System.out.println("setSpeedRight: output-> " + Integer.toString(motorUpTime));
\r
349 // synchronized (obj) {
\r
350 /* Factor in speedFactor */
\r
351 motorRightUpTime = motorUpTime;
\r
355 public void setSpeedAgilityFactors(int speed, int agility) {
\r
356 // synchronized (obj) {
\r
357 speedFactor = speed;
\r
358 agilityFactor = agility;
\r
362 public void setUrgentReverse() {
\r
363 // synchronized (obj) {
\r
364 motorLeftUpTime = 1;
\r
365 motorRightUpTime = 1;
\r
370 * Control debug messageing. true - Activate debug messages false - deactivate
\r
373 public void setDebug(boolean debug) {
\r