你好 StackOverflow 用户。这与 Java 而不是机器人本身有关。我试图做的是将传感器与运动方法分开,以使代码易于阅读,但我遇到了一个问题。
传感器.java
package sensors;
import lejos.hardware.sensor.EV3ColorSensor;
import lejos.hardware.sensor.EV3GyroSensor;
import lejos.hardware.sensor.EV3TouchSensor;
import lejos.hardware.sensor.EV3UltrasonicSensor;
public class Sensors {
EV3TouchSensor touchSensor;
EV3ColorSensor colorSensor;
EV3GyroSensor gyroSensor;
EV3UltrasonicSensor ultrasonicSensor;
public Sensors(EV3TouchSensor t, EV3ColorSensor c, EV3GyroSensor g, EV3UltrasonicSensor u) {
touchSensor = t;
colorSensor = c;
gyroSensor = g;
ultrasonicSensor = u;
}
public int getColorSample(){
int sample = colorSensor.getColorID();
return sample;
}
}
运动.java
public class Movement {
RegulatedMotor left;
RegulatedMotor right;
EV3TouchSensor touchSensor;
EV3ColorSensor colorSensor;
EV3GyroSensor gyroSensor;
EV3UltrasonicSensor ultrasonicSensor;
public Movement(RegulatedMotor l, RegulatedMotor r, EV3TouchSensor t, EV3ColorSensor c, EV3GyroSensor g,
EV3UltrasonicSensor u) {
left = l;
right = r;
touchSensor = t;
colorSensor = c;
gyroSensor = g;
ultrasonicSensor = u;
}
//initialize sensors
Sensors sensors = new Sensors(touchSensor, colorSensor, gyroSensor, ultrasonicSensor);
public void moveForward() {
// get the color sample of the ground
//int sample = colorSensor.getColorID();
int sample = sensors.getColorSample();
// while machine is on the ground color
while (sample != 7) {
// get new sample
//sample = colorSensor.getColorID();
sample = sensors.getColorSample();
// move forward
syncForward();
}
// if on black, stop motors.
syncStop();
}
不要看其他方法,因为这些方法运行流畅,但错误出现在第 30 行,它试图从传感器类中获取样本。我不知道,我也给出了注释掉的行,它们可以流畅地工作。该错误来自访问传感器类,我想不出一个解决方案。
我会欠你的!