Repository navigation
Prototype use of DataChannelRereader in EV3GyroSensor #795
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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. | ||
|
|
@@ -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), | ||
| }); | ||
|
|
@@ -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() { | ||
| switchMode(MODE_ANGLE, SWITCH_DELAY); | ||
| return getMode(1); | ||
| return (EV3GyroAngleMode)getMode(1); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is not necessary to change that line |
||
| } | ||
|
|
||
| /** | ||
|
|
@@ -118,4 +121,53 @@ public void reset() { | |
| switchMode(MODE_RATE_ANGLE, SWITCH_DELAY); | ||
| } | ||
|
|
||
| public static class EV3GyroAngleMode implements SensorMode { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Every mode returns a
SampleProviderUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.