This repository has been archived by the owner on Feb 17, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added ev3dev-lang. motor class implemented only to get address, only...
- Loading branch information
Showing
8 changed files
with
247 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[submodule "ev3dev-lang"] | ||
path = ev3dev-lang | ||
url = https://github.com/ev3dev/ev3dev-lang.git |
Submodule ev3dev-lang
added at
e5e1b7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package ev3dev; | ||
|
||
import ev3dev.exception.InvalidPortException; | ||
import ev3dev.hardware.LegoPort; | ||
|
||
public class TestStart { | ||
|
||
public static void main(String[] args) throws InvalidPortException { | ||
LegoPort port; | ||
for (int i = 0; i <= 7; i++){ | ||
port = new LegoPort(i); | ||
System.out.println( | ||
"\nAddress: " + port.getAddress() + | ||
"\nDriver Nm: " + port.getDriverName() + | ||
"\nMode: " + port.getMode() + | ||
"\nStatus: " + port.getStatus() | ||
); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package ev3dev.exception; | ||
|
||
/*** | ||
* This exception is thrown if:<br> | ||
* <b>LegoPort</b> port is lower than 0 or higher than 7<br> | ||
* <b>TachoMotor</b> port number does not found. | ||
* @author Anthony | ||
* | ||
*/ | ||
public class InvalidPortException extends Exception { | ||
|
||
/** | ||
* | ||
*/ | ||
private static final long serialVersionUID = 1L; | ||
|
||
public InvalidPortException(){ | ||
super(); | ||
} | ||
|
||
public InvalidPortException(String message){ | ||
super(message); | ||
} | ||
|
||
public InvalidPortException(String message, Throwable cause){ | ||
super(message, cause); | ||
} | ||
|
||
public InvalidPortException(Throwable cause){ | ||
super(cause); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,19 @@ | ||
package ev3dev.hardware; | ||
|
||
public abstract class Device { | ||
|
||
private LegoPort port; | ||
|
||
public Device(){ | ||
//TODO the available devices should be enumerated until a suitable device is found | ||
} | ||
|
||
public Device(LegoPort port){ | ||
|
||
this.port = port; | ||
} | ||
|
||
public boolean isConnected(){ | ||
//TODO I don't understand around the Device class... :( | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,100 @@ | ||
package ev3dev.hardware; | ||
|
||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.security.AccessControlException; | ||
|
||
import ev3dev.EV3; | ||
import ev3dev.exception.InvalidPortException; | ||
|
||
public class LegoPort { | ||
|
||
private static String SYSTEM_CLASS_PATH = "/sys/class/lego-port"; | ||
|
||
private int port = 0; | ||
|
||
private String address; | ||
public static final int PORT_1 = 0; | ||
|
||
public static final int PORT_2 = 1; | ||
|
||
public static final int PORT_3 = 2; | ||
|
||
private String driver_name; | ||
public static final int PORT_4 = 3; | ||
|
||
private String[] modes; | ||
public static final int PORT_A = 4; | ||
|
||
private String mode; | ||
public static final int PORT_B = 5; | ||
|
||
private String set_device; | ||
public static final int PORT_C = 6; | ||
|
||
private String status; | ||
public static final int PORT_D = 7; | ||
|
||
public LegoPort(int port){ | ||
public LegoPort(int port) throws InvalidPortException{ | ||
if (port < 0){ | ||
throw new InvalidPortException("Port is lower than 0, Port: " + port); | ||
} else if (port > 7){ | ||
throw new InvalidPortException("Port is higher than 7, Port: " + port); | ||
} | ||
this.port = port; | ||
} | ||
|
||
public final String getAddress(){ | ||
public String getAddress(){ | ||
String address = null; | ||
try { //TODO I shouldn't do like this. Do Error handling. | ||
address = EV3.read("lego-port", "address"); | ||
address = EV3.read("lego-port", "port" + port, "address"); | ||
} catch (Exception e){ | ||
e.printStackTrace(); | ||
} | ||
return address; | ||
} | ||
|
||
public String getDriverName(){ | ||
String drivername = null; | ||
try { | ||
drivername = EV3.read("lego-port", "port" + port, "driver_name"); | ||
} catch (Exception e){ | ||
e.printStackTrace(); | ||
} | ||
return drivername; | ||
} | ||
|
||
public String[] getModes(){ | ||
String[] modes = null; | ||
//TODO This | ||
return modes; | ||
} | ||
|
||
public String getMode(){ | ||
String mode = null; | ||
try { | ||
mode = EV3.read("lego-port", "port" + port, "mode"); | ||
} catch (Exception e){ | ||
e.printStackTrace(); | ||
} | ||
return mode; | ||
} | ||
|
||
public void setMode(String mode){ | ||
try { | ||
EV3.write("lego-port", "port" + port, "mode", mode); | ||
} catch (Exception e){ | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public void setDevice(String driver){ | ||
try { | ||
EV3.write("lego-port", "port" + port, "set_device", driver); | ||
} catch (Exception e){ | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public String getStatus(){ | ||
String status = null; | ||
try { | ||
status = EV3.read("lego-port", "port" + port, "status"); | ||
} catch (Exception e){ | ||
e.printStackTrace(); | ||
} | ||
return status; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package ev3dev.hardware; | ||
|
||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.security.AccessControlException; | ||
|
||
import ev3dev.EV3; | ||
import ev3dev.exception.InvalidPortException; | ||
|
||
public class Motor { | ||
|
||
private static final String SYSTEM_CLASS_NAME = "tacho-motor"; | ||
private LegoPort port; | ||
private String address; | ||
|
||
/*** | ||
* Creates a new motor object. | ||
* @param port Specify a LegoPort | ||
* @throws InvalidPortException If the LegoPort isn't a OUTPUT, invalid or a tacho-motor. | ||
*/ | ||
public Motor(LegoPort port) throws InvalidPortException{ | ||
this.port = port; | ||
address = port.getAddress(); | ||
|
||
//Verify is the LegoPort connecting a motor / is a output | ||
if (!address.contains("out")){ | ||
throw new InvalidPortException("The specified port (" + port.getAddress() + ") isn't a output."); | ||
} else if (port.getStatus() != "tacho-motor"){ | ||
throw new InvalidPortException("The specified port (" + port.getAddress() + ") isn't a motor (" + port.getStatus() + ")"); | ||
} | ||
|
||
} | ||
|
||
/*** | ||
* Get the address of this motor. | ||
* @return LegoPort address described in String | ||
* @throws IOException If the motor doesn't exist or IO ERROR | ||
*/ | ||
public String getAddress() throws IOException{ | ||
return EV3.read(SYSTEM_CLASS_NAME, "tacho-motor", ""); | ||
} | ||
|
||
private static int getMotorNumber(String address){ | ||
int motors = EV3.getNumbersOfSubClass("motor"); | ||
if (motors == -1){ | ||
return -1; | ||
} | ||
for (int i = 0; i < motors; i++){ | ||
try { | ||
if (EV3.read("tacho-motor", "motor" + i, "address").equals(address)){ | ||
return i; | ||
} | ||
} catch (IOException ignore){} | ||
} | ||
return -1; | ||
} | ||
} |