public static void main(String[] aArg) throws Exception { String left = "Turn left "; String right = "Turn right"; LightSensor light = new LightSensor(SensorPort.S3); final int blackWhiteThreshold = 45; DataLogger dl = new DataLogger("Memory10MS_C.txt"); int lightValue; Runtime rt = Runtime.getRuntime(); final int forward = 1; final int stop = 3; final int flt = 4; final int power = 80; // Use the light sensor as a reflection sensor light.setFloodlight(true); LCD.drawString("Light %: ", 0, 0); // Show light percent until LEFT is pressed LCD.drawString("Press LEFT", 0, 2); LCD.drawString("to start", 0, 3); while (!Button.LEFT.isDown()) { LCD.drawInt(light.readValue(), 3, 9, 0); } // Follow line until ESCAPE is pressed LCD.drawString("Press ESCAPE", 0, 2); LCD.drawString("to stop ", 0, 3); dl.start(); while (!Button.ESCAPE.isDown()) { lightValue = light.readValue(); if (lightValue > blackWhiteThreshold) { // On white, turn right LCD.drawString(right, 0, 1); MotorPort.B.controlMotor(0, stop); MotorPort.C.controlMotor(power, forward); } else { // On black, turn left LCD.drawString(left, 0, 1); MotorPort.B.controlMotor(power, forward); MotorPort.C.controlMotor(0, stop); } LCD.drawInt(lightValue, 3, 9, 0); dl.writeSample(rt.freeMemory()); Thread.sleep(10); } // Stop car gently with free wheel drive MotorPort.B.controlMotor(0, flt); MotorPort.C.controlMotor(0, flt); LCD.clear(); dl.close(); LCD.drawString("Program stopped", 0, 0); Thread.sleep(1000); }
/* モータ制御初期化 */ public void reset() { /* 左モータエンコーダリセット */ MotorPort.C.setPWMMode(BasicMotorPort.PWM_BRAKE); MotorPort.C.resetTachoCount(); MotorPort.C.controlMotor(0, 0); /* 右モータエンコーダリセット */ MotorPort.B.setPWMMode(BasicMotorPort.PWM_BRAKE); MotorPort.B.resetTachoCount(); MotorPort.B.controlMotor(0, 0); }
/* モータ制御停止 */ public void stop() { MotorPort.C.controlMotor(0, 0); /* 左モータPWM出力セット */ MotorPort.B.controlMotor(0, 0); /* 右モータPWM出力セット */ }
/* 左モータPWM出力セット */ public void setPwmL(int pwm) { MotorPort.C.controlMotor(pwm, 1); }
/* 左モータ回転角度[deg]取得 */ public int getCountL() { return MotorPort.C.getTachoCount(); }