changes + add two more benchmarks without annotations
[IRC.git] / Robust / src / Benchmarks / SSJava / JavaNatorInfer / PWMNative.java
1 /*\r
2  * PWMNative.java\r
3  *\r
4  * Copyright (c) 1993-2002 Sun Microsystems, Inc. All Rights Reserved.\r
5  *\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
10  * with Sun.\r
11  *\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
18  */\r
19 \r
20 //public class PWMNative extends PWMControl implements Runnable, InterruptEventListener  {\r
21 public class PWMNative extends PWMControl {\r
22 \r
23   private int PULSE_INTERVAL = 2000;\r
24   private int NATIVE_OFFSET = 100;\r
25   private Object obj;\r
26   private Object tc0Obj;\r
27   private Object tc1Obj;\r
28   private int pulseUpTime;\r
29 \r
30   // private TimerCounter tc0;\r
31   // private TimerCounter tc1;\r
32   // private TimerCounter[] tcSet = new TimerCounter[2];\r
33 \r
34   PWMNative(PWMManager pwmMan, int motor1bit, int motor2bit) {\r
35     super(pwmMan, motor1bit, motor2bit);\r
36 \r
37     System.out.println("PWMNative constructor.....Start");\r
38     obj = new Object();\r
39     tc0Obj = new Object();\r
40     tc1Obj = new Object();\r
41 \r
42     // TimerCounter.setPrescalerClockSource(TimerCounter.INTERNAL_PERIPHERAL_CLOCK);\r
43     // TimerCounter.setPrescalerReloadRegisterValue(375);\r
44     // TimerCounter.setPrescalerEnabled(true);\r
45 \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
52     //\r
53     // // Connect signal lead of servo (usually the white one) to IOE6 on\r
54     // // JStamp\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
65     /*\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
70      * Timer IO_Line_B.\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
79      * );\r
80      */\r
81     /*\r
82      * // Add interrupt event listener for GPTC\r
83      * InterruptController.addInterruptListener( this,\r
84      * InterruptController.GPTC_INTERRUPT );\r
85      * \r
86      * // Turn on interrupts InterruptController.enableInterrupt(\r
87      * InterruptController.GPTC_INTERRUPT );\r
88      * \r
89      * // start all prescaler based timers TimerCounter.setPrescalerEnabled(\r
90      * true );\r
91      */\r
92     // t2 = new Thread(this);\r
93     // t2.start();\r
94     // t2.setPriority(20);\r
95     System.out.println("PWMNative: Constructor return.....");\r
96   }\r
97 \r
98   public void setUpTime(int upTime) {\r
99     // synchronized (obj) {\r
100     pulseUpTime = upTime;\r
101     // }\r
102   }\r
103 \r
104   /*\r
105    * public void interruptEvent() { TimerCounter tc; int tcIntrState;\r
106    * \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
116    */\r
117   public void run() {\r
118     //\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
122     //\r
123     // while (true) {\r
124     // synchronized (obj) {\r
125     // /*\r
126     // * System.out.println("PWMNative: Updating Up Times......Left = " +\r
127     // * Integer.toString(motorLeftUpTime) + " Right = " +\r
128     // * Integer.toString(motorRightUpTime));\r
129     // */\r
130     // upTime0 = motorLeftUpTime;\r
131     // upTime1 = motorRightUpTime;\r
132     // }\r
133     //\r
134     // // Timer number 1\r
135     // tc0.setReloadRegisterValue(upTime0);\r
136     // outToPortMLeft(PULSE_HIGH);\r
137     // tc0.setTimerTriggerRegister(true);\r
138     // do {\r
139     // } while (tc0.didTimeOutInterruptOccur());\r
140     // outToPortMLeft(PULSE_LOW);\r
141     // tc0.setTimerTriggerRegister(false);\r
142     // tc0.resetTimeOutInterruptStatus();\r
143     //\r
144     // // System.out.println("PWMNative: Big loop long suspend1");\r
145     // try {\r
146     // tc0Obj.wait(18, 500000);\r
147     // } catch (Exception e) {\r
148     // }\r
149     // // System.out.println("PWMNative: Big loop long suspend2");\r
150     //\r
151     // tc0.setReloadRegisterValue(upTime1);\r
152     // outToPortMRight(PULSE_HIGH);\r
153     // tc0.setTimerTriggerRegister(true);\r
154     // do {\r
155     // } while (tc0.didTimeOutInterruptOccur());\r
156     // outToPortMRight(PULSE_LOW);\r
157     // tc0.setTimerTriggerRegister(false);\r
158     // tc0.resetTimeOutInterruptStatus();\r
159     //\r
160     // try {\r
161     // tc0Obj.wait(18, 500000);\r
162     // } catch (Exception e) {\r
163     // }\r
164 \r
165     /*\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
176      */\r
177     // }\r
178   }\r
179 \r
180   public void setManualMode() {\r
181     if (DEBUG)\r
182       System.out.println("PWMControl: setManualMode... ");\r
183     // synchronized (obj) {\r
184     if (manualMode == false) {\r
185       manualMode = true;\r
186     }\r
187     // }\r
188   }\r
189 \r
190   public void setAutomatedMode() {\r
191     if (DEBUG)\r
192       System.out.println("PWMControl: setAutomatedMode... ");\r
193     /*\r
194      * synchronized(obj){ if(manualMode == true){\r
195      * System.out.println("PWMControl: wake me up... "); obj.notifyAll();\r
196      * manualMode = false; } }\r
197      */\r
198   }\r
199 \r
200   /**\r
201    * setSpeedSpin - Set speed for the spin case motor 1.\r
202    * \r
203    * @param uptime\r
204    *          pulse width (time position)\r
205    */\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
211 \r
212     if (DEBUG) {\r
213       System.out.println("setSpeedSpinLeft: input-> " + Integer.toString(timePosition));\r
214     }\r
215     int timePos = timePosition + NATIVE_OFFSET;\r
216     int motorUpTime = (int) (timePos * agilityFactor * speedFactor) / 10000;\r
217 \r
218     if (DEBUG) {\r
219       System.out.println("setSpeedSpinLeft: output-> = " + Integer.toString(motorUpTime));\r
220     }\r
221     // synchronized (obj) {\r
222     /* Factor in the speed and agility factors */\r
223     motorLeftUpTime = motorUpTime;\r
224     // }\r
225   }\r
226 \r
227   /**\r
228    * setSpeedSpinRight - Set speed for the spin case right motor.\r
229    * \r
230    * @param uptime\r
231    *          pulse width (time position)\r
232    */\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
239     if (DEBUG) {\r
240       System.out.println("setSpeedSpinRight: input-> " + Integer.toString(timePosition));\r
241     }\r
242     int timePos = timePosition + NATIVE_OFFSET;\r
243     int motorUpTime = (int) ((timePos) * agilityFactor * speedFactor) / 10000;\r
244 \r
245     if (DEBUG) {\r
246       System.out.println("setSpeedSpinRight: output-> = " + Integer.toString(motorUpTime));\r
247     }\r
248     // synchronized (obj) {\r
249     /* Factor in the speed and agility factors */\r
250     motorRightUpTime = motorUpTime;\r
251     // }\r
252   }\r
253 \r
254   /**\r
255    * setSpeedTurnM1 - set speed considering agility factor for motor 1\r
256    * \r
257    * @param uptime\r
258    *          pulse width (time position)\r
259    */\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
265     if (DEBUG) {\r
266       System.out.println("setSpeedTurnLeft: input-> " + Integer.toString(timePosition));\r
267     }\r
268     int timePosLocal = timePosition + NATIVE_OFFSET;\r
269     int motorUpTime =\r
270         ((timePosLocal * 100 + ((100 - timePosLocal) * (100 - agilityFactor))) * speedFactor) / 10000;\r
271     if (DEBUG) {\r
272       System.out.println("setSpeedTurnLeft: output-> = " + Integer.toString(motorUpTime));\r
273     }\r
274     // synchronized (obj) {\r
275     /* Factor in the speed and agility factors */\r
276     motorLeftUpTime = motorUpTime;\r
277     // }\r
278   }\r
279 \r
280   /**\r
281    * setSpeedTurnM1 - set speed considering agility factor for motor 2\r
282    * \r
283    * @param uptime\r
284    *          pulse width (time position)\r
285    */\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
289     if (DEBUG) {\r
290       System.out.println("setSpeedTurnRight: input-> " + Integer.toString(timePosition));\r
291     }\r
292     int timePos = timePosition + NATIVE_OFFSET;\r
293     int motorUpTime =\r
294         (((timePos * 100) + ((100 - timePos) * (100 - agilityFactor))) * speedFactor) / 10000;\r
295 \r
296     if (DEBUG) {\r
297       System.out.println("setSpeedTurnRight: output-> " + Integer.toString(motorUpTime));\r
298     }\r
299     // synchronized (obj) {\r
300     /* Factor in the speed and agility factors */\r
301     motorRightUpTime = motorUpTime;\r
302     // }\r
303   }\r
304 \r
305   /**\r
306    * setSpeedLeft - speed control for motor 1.\r
307    * \r
308    * @param uptime\r
309    *          pulse width (time position)\r
310    */\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
316     if (DEBUG) {\r
317       System.out.println("setSpeedLeft: input-> " + Integer.toString(timePosition));\r
318     }\r
319     int timePos = timePosition + NATIVE_OFFSET;\r
320     int motorUpTime = (int) ((timePos * 100) * speedFactor) / 10000;\r
321 \r
322     if (DEBUG) {\r
323       System.out.println("setSpeedLeft: output-> " + Integer.toString(motorUpTime));\r
324     }\r
325     // synchronized (obj) {\r
326     /* Factor in speedFactor */\r
327     motorLeftUpTime = motorUpTime;\r
328     // }\r
329   }\r
330 \r
331   /**\r
332    * setSpeedRight - speed control for motor 1.\r
333    * \r
334    * @param uptime\r
335    *          pulse width (time position)\r
336    */\r
337   public void setSpeedRight(int timePosition) {\r
338     if (DEBUG) {\r
339       System.out.println("setSpeedRight: input-> " + Integer.toString(timePosition));\r
340     }\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
345 \r
346     if (DEBUG) {\r
347       System.out.println("setSpeedRight: output-> " + Integer.toString(motorUpTime));\r
348     }\r
349     // synchronized (obj) {\r
350     /* Factor in speedFactor */\r
351     motorRightUpTime = motorUpTime;\r
352     // }\r
353   }\r
354 \r
355   public void setSpeedAgilityFactors(int speed, int agility) {\r
356     // synchronized (obj) {\r
357     speedFactor = speed;\r
358     agilityFactor = agility;\r
359     // }\r
360   }\r
361 \r
362   public void setUrgentReverse() {\r
363     // synchronized (obj) {\r
364     motorLeftUpTime = 1;\r
365     motorRightUpTime = 1;\r
366     // }\r
367   }\r
368 \r
369   /**\r
370    * Control debug messageing. true - Activate debug messages false - deactivate\r
371    * debug messages\r
372    */\r
373   public void setDebug(boolean debug) {\r
374     DEBUG = debug;\r
375   }\r
376 \r
377 }\r