センサーをテストするためのプログラムです。
[On-Off]ボタンで電源の入切
[Run]ボタン:実行、読出センサーポートの切り替え: 1 > 2 > 3 >
初期設定で各ポートは次のように設定しています:
1:タッチセンサー(読出値:真偽0,1)
2:光センサー(読出値:0~100)
3:汎用(読出値:0~1023)
データの読出しは0.5秒おきに行い、1分後に時間切れ終了になります。
[View]ボタン:タッチセンサーの読出モード切り替え:真偽値 > パルスカウント(押して放すとカウント) > エッジカウント(押す・放すどちらもカウント)
[Prgm]ボタン:各ポートのセンサータイプ切り替え:タッチ > 光 > 汎用 > 空白(汎用・不活性) >
SensorTest.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 137 |
/*センサーテスト [Run]button -- start, change port: 1 > 2 > 3 > Default sensor: Port 1:Touch(t)0,1/count, 2:Light(l)0-100, 3:Raw(r)0-1023 Read number every TimeCycle:500 milliseconds Stop after TimeOut:60 seconds [View]button -- change touch sensor mode: Bool(b)0,1 > Pulse(p)count > Edge(e)count > [Prgm]button -- change sensor type on port: Touch > Light > Raw > Blanc(Passive Raw) > */ import java.lang.*; import josx.platform.rcx.*; class SensorTest implements SensorConstants, Segment { public final static int TimeCycle = 500; //millisecond public final static int TimeOut = 60; //seconds public final static int TimeOutCycle = 1000*TimeOut/TimeCycle; public final static int Touch = 0; //type public final static int Light = 1; public final static int Raw = 2; public final static int Blanc = 3; //Passive Raw public final static int Bool = 0; //mode public final static int[] aTYPE = {SENSOR_TYPE_TOUCH,SENSOR_TYPE_LIGHT, SENSOR_TYPE_RAW,SENSOR_TYPE_RAW}; public final static int[] aTypeMODE = {SENSOR_MODE_BOOL,SENSOR_MODE_PCT, SENSOR_MODE_RAW,SENSOR_MODE_RAW}; public final static int[] aTouchMODE = {SENSOR_MODE_BOOL, SENSOR_MODE_PULSE,SENSOR_MODE_EDGE}; public final static char[] charType = {'t','l','r',' '}; //Touch,Light,Raw,Blanc public final static char[] charMode = {'b','p','e'}; //Bool,Pulse,Edge static int[] portType = {Touch,Light,Raw}; static int[] portTouchMode = {Bool,Bool,Bool}; static int port = 0; //Port Label 1 static int type = 0; //Touch static int mode = 0; //Bool static char charNum(int num) { return (char)('0'+num%10); //'0'-'9' } public static void main (String[] aArg) throws Exception { for(int p=0; p<3; p++) Sensor.SENSORS[p].setTypeAndMode(aTYPE[portType[p]],aTypeMODE[portType[p]]); Sensor.SENSORS[port].activate(); Sound.playTone(1000,2); Button.RUN.addButtonListener(new ButtonListener() { public void buttonPressed(Button button) { Sensor.SENSORS[port].passivate(); port = (port+1)%3; type = portType[port]; if (type==Touch) mode = portTouchMode[port]; if (type!=Blanc) Sensor.SENSORS[port].activate(); Sound.playTone(1000,2); } public void buttonReleased(Button button) { } } ); Button.PRGM.addButtonListener(new ButtonListener() { public void buttonPressed(Button button) { type = (type+1)%4; portType[port] = type; Sensor.SENSORS[port].setTypeAndMode(aTYPE[type],aTypeMODE[type]); if (type==Touch) { mode = Bool; portTouchMode[port] = mode; } if (type==Blanc) Sensor.SENSORS[port].passivate(); else Sensor.SENSORS[port].activate(); Sound.playTone(700,2); } public void buttonReleased(Button button) { } } ); Button.VIEW.addButtonListener(new ButtonListener() { public void buttonPressed(Button button) { if (type==Touch) { mode = (mode+1)%3; portTouchMode[port] = mode; Sensor.SENSORS[port].setTypeAndMode(aTYPE[type],aTouchMODE[mode]); Sound.playTone(500,2); } } public void buttonReleased(Button button) { } } ); for(int i=0; i<TimeOutCycle; i++) { LCD.clear(); LCD.setSegment(SENSOR_1_VIEW+port*2); LCD.setSegment(SENSOR_1_ACTIVE+port*2); TextLCD.printChar(charType[type],0); //t,l,r,' ' int val = Sensor.SENSORS[port].readValue(); if (type==Touch) { //0-99 TextLCD.printChar(charMode[mode],1); //b,p,e TextLCD.printChar(charNum(val),3); if (10<=val) TextLCD.printChar(charNum(val/10),4); } else if (type==Light) { //00-100 TextLCD.printChar(charNum(val),2); TextLCD.printChar(charNum(val/10),3); if (100<=val) TextLCD.printChar(charNum(val/100),4); } else { //Raw,Blanc:0000-1023 LCD.showNumber(val); } LCD.setSegment(STANDING+i%2); //or WALKING LCD.refresh(); try { Thread.sleep(TimeCycle); } catch(InterruptedException e) { } } Sensor.SENSORS[port].passivate(); LCD.clear(); LCD.refresh(); Sound.beepSequence(); try { Thread.sleep(1000); } catch(InterruptedException e) { } } } |
