Hello StackOverflow users. This is related to the Java instead of the robot itself. What i was trying to do is to seperate the sensors from the movement methods to make the code easily readable, but I've ran into one problem.
Sensor.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;
}
}
Movement.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();
}
Don't look at the other methods, because these work fluently, but the error comes at the line 30, where it tries to get the sample from the sensors class. I have no idea, I've given the commented out lines as well, which work fluently. The error comes from accessing the sensors class and i can' think out a solution.
I'll be in your debt!