import lejos.hardware.lcd.LCD;
import lejos.hardware.port.SensorPort;
import lejos.hardware.sensor.EV3UltrasonicSensor;
import lejos.robotics.SampleProvider;
import lejos.utility.Delay;
public class NewUl {
private EV3UltrasonicSensor ev3UltrasonicSensor;
public NewUl() {
// TODO Auto-generated constructor stub
ev3UltrasonicSensor=new EV3UltrasonicSensor(SensorPort.S4);
}
public void getData() {
SampleProvider sampleProvider=ev3UltrasonicSensor.getDistanceMode();
float[] sample=new float[sampleProvider.sampleSize()];
sampleProvider.fetchSample(sample, 0);
LCD.clear();
LCD.drawString(String.valueOf(sample[0]),0,3);
LCD.refresh();
Delay.msDelay(3000);
LCD.clear();
LCD.refresh();
}
public void close() {
ev3UltrasonicSensor.close();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
NewUl newUl=new NewUl();
newUl.getData();
newUl.close();
}
}
这是一个简单的 lejos 程序EV3UltrasonicSensor
。第 13 行代码是:
ev3UltrasonicSensor=new EV3UltrasonicSensor(SensorPort.S4);
第31行代码是:
NewUl newUl=new NewUl();
抛出的异常是:
Exception in thread "main" java.lang.IllegalArgumentException: Invalid sensor mode at lejos.hardware.sensor.UARTSensor.<init>(UARTSensor.java:62)
at lejos.hardware.sensor.EV3UltrasonicSensor.<init>(EV3UltrasonicSensor.java:75)
at control.NewUl.<init>(NewUl.java:13) at control.NewUl.main(NewUl.java:31)
这么简单的程序也会出现错误吗?这让我很困惑。
你能给我一些帮助或一些建议吗?任何帮助表示赞赏,如果您需要了解更多信息,请随时提出。