
モーターをテストするプログラムです。
[On-Off]ボタンで電源の入切
[Run]ボタンで実行、動作を切り替えます: 前進 > スイッチ > 後退 > スイッチ >
A,B,Cどのポートに接続しても動作は同じです
[Prgm]ボタンでスイッチ 動作を切り替えます: ストップ > 滑走 > 逆転 >
[View]ボタンでパワーを設定します(0-7): 0 > 1 > … > 7 >
1分後に時間切れ終了になります。
MotorTest.java
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
/*モーターテスト [Run]button -- start, do action: Forward(f) > Switch(s) > Backward(b) > Switch(s) > [Prgm]button -- set Switch: Stop(s) > Float(f) > Reverse(r) > [View]button -- set Power: 0-7 All motors(A,B,C) do same action Stop after TimeOut:60 seconds */ import java.lang.*; import josx.platform.rcx.*; class MotorTest implements SensorConstants, Segment { public final static int TimeOut = 60; //seconds public final static int TimeCycle = 500; //millisecond public final static int TimeOutCycle = 1000*TimeOut/TimeCycle; public final static char[] cAction = {'f','s','b','s'}; //Forward,Switch,Backward,Switch public final static char[] cSwitch = {'s','f','r'}; //Stop,Float,Reverse static int action = 0; //0:Forward static int aswitch = 0; //0:Stop static int power = 3; //0-7 static void doAction() { char a = cAction[action]; if (a=='f') { Motor.A.forward(); Motor.B.forward(); Motor.C.forward(); } else if (a=='b') { Motor.A.backward(); Motor.B.backward(); Motor.C.backward(); } else if (a=='s') { char s = cSwitch[aswitch]; if (s=='s') { Motor.A.stop(); Motor.B.stop(); Motor.C.stop(); } else if (s=='f') { Motor.A.flt(); Motor.B.flt(); Motor.C.flt(); } else if (s=='r') { Motor.A.reverseDirection(); Motor.B.reverseDirection(); Motor.C.reverseDirection(); } } } static void setPower() { Motor.A.setPower(power); Motor.B.setPower(power); Motor.C.setPower(power); } static char charNum(int num) { return (char)('0'+num%10); } public static void main (String[] aArg) throws Exception { setPower(); doAction(); LCD.clear(); TextLCD.printChar(cAction[action],0); //f,s,b,s TextLCD.printChar(cSwitch[aswitch],1); //s,f,r TextLCD.printChar(charNum(power),4); LCD.refresh(); Sound.playTone(1000,2); Button.RUN.addButtonListener(new ButtonListener() { public void buttonPressed(Button button) { action = (action+1)%4; doAction(); TextLCD.printChar(cAction[action],0); //f,s,b,s LCD.refresh(); Sound.playTone(1000,2); } public void buttonReleased(Button button) { } } ); Button.PRGM.addButtonListener(new ButtonListener() { public void buttonPressed(Button button) { aswitch = (aswitch+1)%3; TextLCD.printChar(cSwitch[aswitch],1); //s,f,r LCD.refresh(); Sound.playTone(700,2); } public void buttonReleased(Button button) { } } ); Button.VIEW.addButtonListener(new ButtonListener() { public void buttonPressed(Button button) { power = (power+1)%8; setPower(); TextLCD.printChar(charNum(power),4); LCD.refresh(); Sound.playTone(500,2); } public void buttonReleased(Button button) { } } ); for(int i=0; i<TimeOutCycle; i++) { LCD.setSegment(STANDING+i%2); //or WALKING LCD.refresh(); try { Thread.sleep(TimeCycle); } catch(InterruptedException e) { } } LCD.clear(); LCD.refresh(); Sound.beepSequence(); try { Thread.sleep(1000); } catch(InterruptedException e) { } } } |
