Skip to content
Closed

#651 #655

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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ev3dev.actuators.lego.motors;

import ev3dev.hardware.EV3DevMotorDevice;
import ev3dev.hardware.EV3DevPlatform;
import ev3dev.hardware.EV3DevPlatforms;
import ev3dev.sensors.Battery;
import lejos.hardware.port.Port;
Expand Down Expand Up @@ -85,9 +86,12 @@ public BaseRegulatedMotor(final Port motorPort, float moveP, float moveI, float
if(log.isDebugEnabled())
log.debug("Detecting motor on port: {}", port);
this.detect(LEGO_PORT, port);
// ev3 is slow and it can detect the motor, so let the kernel do its work
// other platforms cannot detect devices automatically, so it is necessary to manually force a tacho motor
String mode = ev3DevPlatforms.getPlatform() == EV3DevPlatform.EV3BRICK ? AUTO_MODE : TACHO_MOTOR;
if(log.isDebugEnabled())
log.debug("Setting port in mode: {}", TACHO_MOTOR);
this.setStringAttribute(MODE, TACHO_MOTOR);
log.debug("Setting port in mode: {}", mode);
this.setStringAttributeIfDifferent(MODE, mode);
Delay.msDelay(1000);
this.detect(TACHO_MOTOR, port);
//TODO Review to implement asynchronous solution
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ev3dev/actuators/lego/motors/BasicMotor.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public BasicMotor(final Port motorPort) {
log.debug("Detecting motor on port: {}", port);
this.detect(LEGO_PORT, port);
log.debug("Setting port in mode: {}", DC_MOTOR);
this.setStringAttribute(MODE, DC_MOTOR);
this.setStringAttributeIfDifferent(MODE, DC_MOTOR);
Delay.msDelay(500);
this.detect(DC_MOTOR, port);
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/ev3dev/hardware/EV3DevDevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ protected void setStringAttribute(final String attribute, final String value){
}
}

/**
* Set a value on an attribute, but only if different.
*
* @param attribute attribute name
* @param value requested value
*/
protected void setStringAttributeIfDifferent(final String attribute, final String value){
Sysfs.writeStringIfDifferent(this.PATH_DEVICE + "/" + attribute, value);
}

/**
* Set a value on an attribute
*
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/ev3dev/utils/Sysfs.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public static String readString(final String filePath) {
try {
final Path path = Paths.get(filePath);
if(existFile(path) && Files.isReadable(path)){
final String result = Files.readAllLines(path, Charset.forName("UTF-8")).get(0);
final List<String> lines = Files.readAllLines(path, Charset.forName("UTF-8"));
final String result = lines.size() == 0 ? "" : lines.get(0);
if(log.isTraceEnabled())
log.trace("value: {}", result);
return result;
Expand All @@ -94,6 +95,18 @@ public static float readFloat(final String filePath) {
return Float.parseFloat(readString(filePath));
}

/**
* Write a sysfs attribute, but only do so if the value is different.
* This can be useful in avoiding triggering kernel-side changes.
* @param filePath Path to the attribute.
* @param value Requested attribute value.
*/
public static void writeStringIfDifferent(final String filePath, final String value) {
if (!value.equals(readString(filePath))) {
writeString(filePath, value);
}
}

/**
*
* @param filePath path
Expand Down