--- /dev/null
+/*\r
+ * PWMNative.java\r
+ *\r
+ * Copyright (c) 1993-2002 Sun Microsystems, Inc. All Rights Reserved.\r
+ *\r
+ * This software is the confidential and proprietary information of Sun\r
+ * Microsystems, Inc. ("Confidential Information"). You shall not\r
+ * disclose such Confidential Information and shall use it only in\r
+ * accordance with the terms of the license agreement you entered into\r
+ * with Sun.\r
+ *\r
+ * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF\r
+ * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED\r
+ * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A\r
+ * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR\r
+ * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR\r
+ * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.\r
+ */\r
+\r
+//public class PWMNative extends PWMControl implements Runnable, InterruptEventListener {\r
+public class PWMNative extends PWMControl {\r
+\r
+ private int PULSE_INTERVAL = 2000;\r
+ private int NATIVE_OFFSET = 100;\r
+ private Object obj;\r
+ private Object tc0Obj;\r
+ private Object tc1Obj;\r
+ private int pulseUpTime;\r
+\r
+ // private TimerCounter tc0;\r
+ // private TimerCounter tc1;\r
+ // private TimerCounter[] tcSet = new TimerCounter[2];\r
+\r
+ PWMNative(PWMManager pwmMan, int motor1bit, int motor2bit) {\r
+ super(pwmMan, motor1bit, motor2bit);\r
+\r
+ System.out.println("PWMNative constructor.....Start");\r
+ obj = new Object();\r
+ tc0Obj = new Object();\r
+ tc1Obj = new Object();\r
+\r
+ // TimerCounter.setPrescalerClockSource(TimerCounter.INTERNAL_PERIPHERAL_CLOCK);\r
+ // TimerCounter.setPrescalerReloadRegisterValue(375);\r
+ // TimerCounter.setPrescalerEnabled(true);\r
+\r
+ // tc0 = tcSet[0] = new TimerCounter(0);\r
+ // tc0.setMode_IO_Line_A(TimerCounter.TIMER_0_OUTPUT);\r
+ // // bring TIMER_0_OUTPUT_DIVIDE_BY_2 out via IO_Line_B\r
+ // tc0.setMode_IO_Line_B(TimerCounter.TIMER_0_OUTPUT_DIVIDE_BY_2);\r
+ // // In JemBuilder, go to Pin Setup 2 and allocate Port E bit 6 to\r
+ // // be Timer IO_Line_B.\r
+ //\r
+ // // Connect signal lead of servo (usually the white one) to IOE6 on\r
+ // // JStamp\r
+ // tc0.set_IO_Line_A_Polarity(TimerCounter.POLARITY_ACTIVE_STATE_HIGH);\r
+ // tc0.set_IO_Line_B_Polarity(TimerCounter.POLARITY_ACTIVE_STATE_HIGH);\r
+ // tc0.setExternalTimerEnableMode(TimerCounter.TIMER_ENABLED_ONLY_VIA_MTEN_AND_TRIGGER);\r
+ // tc0.setReloadRegisterValue(100);\r
+ // tc0.setExternalStartTriggerMode(TimerCounter.NO_EXTERNAL_START_TRIGGER);\r
+ // tc0.setMasterTimerEnabled(true);\r
+ // tc0.setGlobalInterruptEnabled(true);\r
+ // tc0.setTimeOutInterruptEnabled(true);\r
+ // tc0.setTimerTriggerRegister(false);\r
+ // System.out.println("PWMNative: Constructor completed 1....");\r
+ /*\r
+ * tc1 = tcSet[1] = new TimerCounter ( 1 ); tc1.setMode_IO_Line_A(\r
+ * TimerCounter.TIMER_1_OUTPUT ); //bring TIMER_0_OUTPUT_DIVIDE_BY_2 out via\r
+ * IO_Line_B tc1.setMode_IO_Line_B( TimerCounter.TIMER_1_OUTPUT_DIVIDE_BY_2\r
+ * ); //In JemBuilder, go to Pin Setup 2 and allocate Port E bit 6 to //be\r
+ * Timer IO_Line_B.\r
+ * tc1.set_IO_Line_A_Polarity(TimerCounter.POLARITY_ACTIVE_STATE_HIGH);\r
+ * tc1.set_IO_Line_B_Polarity(TimerCounter.POLARITY_ACTIVE_STATE_HIGH);\r
+ * tc1.setExternalTimerEnableMode\r
+ * (TimerCounter.TIMER_ENABLED_ONLY_VIA_MTEN_AND_TRIGGER );\r
+ * tc1.setReloadRegisterValue( 100 );\r
+ * tc1.setExternalStartTriggerMode(TimerCounter.NO_EXTERNAL_START_TRIGGER );\r
+ * tc1.setMasterTimerEnabled( true ); tc1.setGlobalInterruptEnabled( true);\r
+ * tc1.setTimeOutInterruptEnabled( true); tc1.setTimerTriggerRegister( false\r
+ * );\r
+ */\r
+ /*\r
+ * // Add interrupt event listener for GPTC\r
+ * InterruptController.addInterruptListener( this,\r
+ * InterruptController.GPTC_INTERRUPT );\r
+ * \r
+ * // Turn on interrupts InterruptController.enableInterrupt(\r
+ * InterruptController.GPTC_INTERRUPT );\r
+ * \r
+ * // start all prescaler based timers TimerCounter.setPrescalerEnabled(\r
+ * true );\r
+ */\r
+ // t2 = new Thread(this);\r
+ // t2.start();\r
+ // t2.setPriority(20);\r
+ System.out.println("PWMNative: Constructor return.....");\r
+ }\r
+\r
+ public void setUpTime(int upTime) {\r
+ // synchronized (obj) {\r
+ pulseUpTime = upTime;\r
+ // }\r
+ }\r
+\r
+ /*\r
+ * public void interruptEvent() { TimerCounter tc; int tcIntrState;\r
+ * \r
+ * System.out.println("PWMNative: InterruptEvent"); do { for ( int tcNum=0;\r
+ * tcNum<2; tcNum++ ) { tc = tcSet[ tcNum ];\r
+ * if(tc.readAndClear_TimeOutInterruptStatus()){ switch(tcNum){ case 0:\r
+ * System.out.println("PWMNative: Interrupt case 0"); synchronized(tc0Obj){\r
+ * System.out.println("PWMNative: Interrupt notify 0"); tc0Obj.notify(); }\r
+ * break; case 1: System.out.println("PWMNative: Interrupt case 1");\r
+ * synchronized(tc1Obj){ System.out.println("PWMNative: Interrupt notify 1");\r
+ * tc1Obj.notify(); } break; default:; } } } } while (\r
+ * TimerCounter.isGPTCInterruptPending() ); }\r
+ */\r
+ public void run() {\r
+ //\r
+ // System.out.println("PWMNative: run method........");\r
+ // int upTime0 = 150; // 1.5 milli seconds = 0 speed\r
+ // int upTime1 = 150; // 1.5 milli seconds = 0 speed\r
+ //\r
+ // while (true) {\r
+ // synchronized (obj) {\r
+ // /*\r
+ // * System.out.println("PWMNative: Updating Up Times......Left = " +\r
+ // * Integer.toString(motorLeftUpTime) + " Right = " +\r
+ // * Integer.toString(motorRightUpTime));\r
+ // */\r
+ // upTime0 = motorLeftUpTime;\r
+ // upTime1 = motorRightUpTime;\r
+ // }\r
+ //\r
+ // // Timer number 1\r
+ // tc0.setReloadRegisterValue(upTime0);\r
+ // outToPortMLeft(PULSE_HIGH);\r
+ // tc0.setTimerTriggerRegister(true);\r
+ // do {\r
+ // } while (tc0.didTimeOutInterruptOccur());\r
+ // outToPortMLeft(PULSE_LOW);\r
+ // tc0.setTimerTriggerRegister(false);\r
+ // tc0.resetTimeOutInterruptStatus();\r
+ //\r
+ // // System.out.println("PWMNative: Big loop long suspend1");\r
+ // try {\r
+ // tc0Obj.wait(18, 500000);\r
+ // } catch (Exception e) {\r
+ // }\r
+ // // System.out.println("PWMNative: Big loop long suspend2");\r
+ //\r
+ // tc0.setReloadRegisterValue(upTime1);\r
+ // outToPortMRight(PULSE_HIGH);\r
+ // tc0.setTimerTriggerRegister(true);\r
+ // do {\r
+ // } while (tc0.didTimeOutInterruptOccur());\r
+ // outToPortMRight(PULSE_LOW);\r
+ // tc0.setTimerTriggerRegister(false);\r
+ // tc0.resetTimeOutInterruptStatus();\r
+ //\r
+ // try {\r
+ // tc0Obj.wait(18, 500000);\r
+ // } catch (Exception e) {\r
+ // }\r
+\r
+ /*\r
+ * // Timer number 2 tc1.setReloadRegisterValue( upTime1 );\r
+ * tc1.setTimerTriggerRegister( true ); synchronized(tc1Obj){ try{\r
+ * System.out.println("PWMNative: Sleep 3"); tc1Obj.wait();\r
+ * System.out.println("PWMNative: Wake Up 3"); }catch(Exception e){ } }\r
+ * tc1.setTimerTriggerRegister( false ); tc1.resetTimeOutInterruptStatus();\r
+ * tc1.setReloadRegisterValue( PULSE_INTERVAL - upTime1 ); //this sets pulse\r
+ * interval tc1.setTimerTriggerRegister( true ); synchronized(tc1Obj){ try{\r
+ * System.out.println("PWMNative: Sleep 3"); tc1Obj.wait();\r
+ * System.out.println("PWMNative: Wake Up 3"); }catch(Exception e){ } }\r
+ * tc1.setTimerTriggerRegister( false ); tc1.resetTimeOutInterruptStatus();\r
+ */\r
+ // }\r
+ }\r
+\r
+ public void setManualMode() {\r
+ if (DEBUG)\r
+ System.out.println("PWMControl: setManualMode... ");\r
+ // synchronized (obj) {\r
+ if (manualMode == false) {\r
+ manualMode = true;\r
+ }\r
+ // }\r
+ }\r
+\r
+ public void setAutomatedMode() {\r
+ if (DEBUG)\r
+ System.out.println("PWMControl: setAutomatedMode... ");\r
+ /*\r
+ * synchronized(obj){ if(manualMode == true){\r
+ * System.out.println("PWMControl: wake me up... "); obj.notifyAll();\r
+ * manualMode = false; } }\r
+ */\r
+ }\r
+\r
+ /**\r
+ * setSpeedSpin - Set speed for the spin case motor 1.\r
+ * \r
+ * @param uptime\r
+ * pulse width (time position)\r
+ */\r
+ public void setSpeedSpinLeft(int timePosition) {\r
+ /* Speed comes in as a number between 0 - 100 */\r
+ /* It represents values between 1 to 2 ms */\r
+ /* 100-input to get reverse polarity for this motor */\r
+ /* since it's mounted in reverse direction to the other motor */\r
+\r
+ if (DEBUG) {\r
+ System.out.println("setSpeedSpinLeft: input-> " + Integer.toString(timePosition));\r
+ }\r
+ int timePos = timePosition + NATIVE_OFFSET;\r
+ int motorUpTime = (int) (timePos * agilityFactor * speedFactor) / 10000;\r
+\r
+ if (DEBUG) {\r
+ System.out.println("setSpeedSpinLeft: output-> = " + Integer.toString(motorUpTime));\r
+ }\r
+ // synchronized (obj) {\r
+ /* Factor in the speed and agility factors */\r
+ motorLeftUpTime = motorUpTime;\r
+ // }\r
+ }\r
+\r
+ /**\r
+ * setSpeedSpinRight - Set speed for the spin case right motor.\r
+ * \r
+ * @param uptime\r
+ * pulse width (time position)\r
+ */\r
+ public void setSpeedSpinRight(int timePosition) {\r
+ /* Speed comes in as a number between 0 - 100 */\r
+ /* It represents values between 1 to 2 ms */\r
+ /* An input of 50 should result in 0 speed. */\r
+ /* 100 should result in full speed forward */\r
+ /* while 0 should result in full speed backwords */\r
+ if (DEBUG) {\r
+ System.out.println("setSpeedSpinRight: input-> " + Integer.toString(timePosition));\r
+ }\r
+ int timePos = timePosition + NATIVE_OFFSET;\r
+ int motorUpTime = (int) ((timePos) * agilityFactor * speedFactor) / 10000;\r
+\r
+ if (DEBUG) {\r
+ System.out.println("setSpeedSpinRight: output-> = " + Integer.toString(motorUpTime));\r
+ }\r
+ // synchronized (obj) {\r
+ /* Factor in the speed and agility factors */\r
+ motorRightUpTime = motorUpTime;\r
+ // }\r
+ }\r
+\r
+ /**\r
+ * setSpeedTurnM1 - set speed considering agility factor for motor 1\r
+ * \r
+ * @param uptime\r
+ * pulse width (time position)\r
+ */\r
+ public void setSpeedTurnLeft(int timePosition) {\r
+ /* Speed comes in as a number between 0 - 100 */\r
+ /* It represents values between 1 to 2 ms */\r
+ /* 100-input to get reverse polarity for this motor */\r
+ /* since it's mounted in reverse direction to the other motor */\r
+ if (DEBUG) {\r
+ System.out.println("setSpeedTurnLeft: input-> " + Integer.toString(timePosition));\r
+ }\r
+ int timePosLocal = timePosition + NATIVE_OFFSET;\r
+ int motorUpTime =\r
+ ((timePosLocal * 100 + ((100 - timePosLocal) * (100 - agilityFactor))) * speedFactor) / 10000;\r
+ if (DEBUG) {\r
+ System.out.println("setSpeedTurnLeft: output-> = " + Integer.toString(motorUpTime));\r
+ }\r
+ // synchronized (obj) {\r
+ /* Factor in the speed and agility factors */\r
+ motorLeftUpTime = motorUpTime;\r
+ // }\r
+ }\r
+\r
+ /**\r
+ * setSpeedTurnM1 - set speed considering agility factor for motor 2\r
+ * \r
+ * @param uptime\r
+ * pulse width (time position)\r
+ */\r
+ public void setSpeedTurnRight(int timePosition) {\r
+ /* Speed comes in as a number between 0 - 100 */\r
+ /* It represents values between 1 to 2 ms */\r
+ if (DEBUG) {\r
+ System.out.println("setSpeedTurnRight: input-> " + Integer.toString(timePosition));\r
+ }\r
+ int timePos = timePosition + NATIVE_OFFSET;\r
+ int motorUpTime =\r
+ (((timePos * 100) + ((100 - timePos) * (100 - agilityFactor))) * speedFactor) / 10000;\r
+\r
+ if (DEBUG) {\r
+ System.out.println("setSpeedTurnRight: output-> " + Integer.toString(motorUpTime));\r
+ }\r
+ // synchronized (obj) {\r
+ /* Factor in the speed and agility factors */\r
+ motorRightUpTime = motorUpTime;\r
+ // }\r
+ }\r
+\r
+ /**\r
+ * setSpeedLeft - speed control for motor 1.\r
+ * \r
+ * @param uptime\r
+ * pulse width (time position)\r
+ */\r
+ public void setSpeedLeft(int timePosition) {\r
+ /* Speed comes in as a number between 0 - 100 */\r
+ /* It represents values between 1 to 2 ms */\r
+ /* 100-input to get reverse polarity for this motor */\r
+ /* since it's mounted in reverse direction to the other motor */\r
+ if (DEBUG) {\r
+ System.out.println("setSpeedLeft: input-> " + Integer.toString(timePosition));\r
+ }\r
+ int timePos = timePosition + NATIVE_OFFSET;\r
+ int motorUpTime = (int) ((timePos * 100) * speedFactor) / 10000;\r
+\r
+ if (DEBUG) {\r
+ System.out.println("setSpeedLeft: output-> " + Integer.toString(motorUpTime));\r
+ }\r
+ // synchronized (obj) {\r
+ /* Factor in speedFactor */\r
+ motorLeftUpTime = motorUpTime;\r
+ // }\r
+ }\r
+\r
+ /**\r
+ * setSpeedRight - speed control for motor 1.\r
+ * \r
+ * @param uptime\r
+ * pulse width (time position)\r
+ */\r
+ public void setSpeedRight(int timePosition) {\r
+ if (DEBUG) {\r
+ System.out.println("setSpeedRight: input-> " + Integer.toString(timePosition));\r
+ }\r
+ /* Speed comes in as a number between 0 - 100 */\r
+ /* It represents values between 1 to 2 ms */\r
+ int timePos = timePosition + NATIVE_OFFSET;\r
+ int motorUpTime = (int) (((timePos * 100) * speedFactor) / 10000);\r
+\r
+ if (DEBUG) {\r
+ System.out.println("setSpeedRight: output-> " + Integer.toString(motorUpTime));\r
+ }\r
+ // synchronized (obj) {\r
+ /* Factor in speedFactor */\r
+ motorRightUpTime = motorUpTime;\r
+ // }\r
+ }\r
+\r
+ public void setSpeedAgilityFactors(int speed, int agility) {\r
+ // synchronized (obj) {\r
+ speedFactor = speed;\r
+ agilityFactor = agility;\r
+ // }\r
+ }\r
+\r
+ public void setUrgentReverse() {\r
+ // synchronized (obj) {\r
+ motorLeftUpTime = 1;\r
+ motorRightUpTime = 1;\r
+ // }\r
+ }\r
+\r
+ /**\r
+ * Control debug messageing. true - Activate debug messages false - deactivate\r
+ * debug messages\r
+ */\r
+ public void setDebug(boolean debug) {\r
+ DEBUG = debug;\r
+ }\r
+\r
+}\r