Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 56 additions & 4 deletions src/main/java/ev3dev/sensors/ev3/EV3GyroSensor.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

import ev3dev.sensors.BaseSensor;
import ev3dev.sensors.GenericMode;
import ev3dev.utils.DataChannelRereader;
import lejos.hardware.port.Port;
import lejos.hardware.sensor.SensorMode;
import lejos.robotics.SampleProvider;

import java.io.File;
import java.nio.file.Path;

/**
* <b>EV3 Gyro sensors</b><br>
* The digital EV3 Gyro Sensor measures the sensors rotational motion and changes in its orientation.
Expand Down Expand Up @@ -43,8 +47,7 @@ public EV3GyroSensor(final Port portName) {
setModes(new SensorMode[]{
new GenericMode(this.PATH_DEVICE, 1, "Rate",
-Float.MAX_VALUE, +Float.MAX_VALUE, 1.0f),
new GenericMode(this.PATH_DEVICE, 1, "Angle",
-Float.MAX_VALUE, +Float.MAX_VALUE, 1.0f),
new EV3GyroAngleMode(this.PATH_DEVICE),
new GenericMode(this.PATH_DEVICE, 2, "Angle and Rate",
-Float.MAX_VALUE, +Float.MAX_VALUE, 1.0f),
});
Expand Down Expand Up @@ -82,9 +85,9 @@ public SampleProvider getRateMode() {
* @return A sampleProvider
* See {@link lejos.robotics.SampleProvider leJOS conventions for SampleProviders}
*/
public SampleProvider getAngleMode() {
public EV3GyroAngleMode getAngleMode() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every mode returns a SampleProvider

@dwalend dwalend Jan 18, 2021 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EV3GyroAngleMode is a SampleProvider - by implementing SensorMode.

switchMode(MODE_ANGLE, SWITCH_DELAY);
return getMode(1);
return (EV3GyroAngleMode)getMode(1);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not necessary to change that line

}

/**
Expand Down Expand Up @@ -118,4 +121,53 @@ public void reset() {
switchMode(MODE_RATE_ANGLE, SWITCH_DELAY);
}

public static class EV3GyroAngleMode implements SensorMode {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That mode is not necessary. I understand that you added the method getAngle() but it breaks the contract.
It is necessary to upgrade with DataChannelRereader:
https://github.com/ev3dev-lang-java/ev3dev-lang-java/blob/master/src/main/java/ev3dev/sensors/GenericMode.java

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In what way does it break the contract?

Touching GenericMode is a much broader change. I'll put in a pull request to show that.


protected final File pathDevice;
private final int sampleSize = 1;
private final String modeName = "Angle";
private final DataChannelRereader rereader;

/**
* Create new generic sensor handler.
*
* @param pathDevice Reference to the object responsible for mode setting and value reading.
*/
public EV3GyroAngleMode(final File pathDevice) {
this.pathDevice = pathDevice;

this.rereader = new DataChannelRereader(Path.of(pathDevice.toString(),"value0"),32);
}

@Override
public String getName() {
return modeName;
}

@Override
public int sampleSize() {
return sampleSize;
}


/**
* Fetches a sample from the sensor.
*
* <p>Note: this function works properly only when
* the sensor is already in the appropriate mode. Otherwise,
* returned data will be invalid.</p>
*
* @param sample The array to store the sample in.
* @param offset The elements of the sample are stored in the array starting at the offset position.
*/
@Override
public void fetchSample(float[] sample, int offset) {
float reading = (float)readAngle();
sample[offset] = reading;
}

public int readAngle() {
return rereader.readIntFromAscii();
}
}
}
7 changes: 7 additions & 0 deletions src/main/java/ev3dev/utils/DataChannelRereader.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ public synchronized String readString() {
}
}

/**
* @return an int from parsing a UTF_8 string in the file
*/
public int readIntFromAscii() {
return Integer.parseInt(readString());
}

public Path getPath() {
return path;
}
Expand Down