Skip to content

Commit a7e323f

Browse files
committed
Adding Subsumption examples
1 parent 9fe12da commit a7e323f

File tree

10 files changed

+274
-37
lines changed

10 files changed

+274
-37
lines changed

ev3dev-lang-java/build.gradle

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ repositories {
1313
}
1414

1515
dependencies {
16-
compile("com.github.ev3dev-lang-java:ev3dev-lang-java:v0.6.4-SNAPSHOT")
16+
compile("com.github.ev3dev-lang-java:ev3dev-lang-java:v0.6.0-RELEASE")
17+
compile("ch.qos.logback:logback-classic:1.2.3")
18+
compile("org.projectlombok:lombok:1.16.16")
1719
}
1820

1921
//Compile
@@ -25,16 +27,13 @@ compileJava {
2527
//Jar
2628
jar {
2729
baseName = "${rootProject.name}"
28-
manifest {
29-
from file("${projectDir}/META-INF/MANIFEST.MF")
30-
}
3130
}
3231

3332
//Fat Jar
3433
task fatJar(type: Jar) {
3534
baseName = "${rootProject.name}" + "-all"
3635
manifest {
37-
from file("${projectDir}/META-INF/MANIFEST.MF")
36+
from file("${projectDir}/src/main/resources/META-INF/MANIFEST.MF")
3837
}
3938
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
4039
with jar
@@ -51,36 +50,6 @@ buildscript {
5150
classpath "org.hidetake:gradle-ssh-plugin:2.0.0"
5251
}
5352
}
54-
5553
apply plugin: 'org.hidetake.ssh'
5654

57-
remotes {
58-
ev3dev {
59-
host = '192.168.1.2'
60-
user = 'robot'
61-
password = 'maker'
62-
}
63-
}
64-
65-
task deploy << {
66-
ssh.run {
67-
session(remotes.ev3dev) {
68-
put from: "./build/libs/" + "${rootProject.name}" + "-all-" + version + ".jar", into: "/home/robot/"
69-
}
70-
}
71-
}
72-
deploy.dependsOn clean, fatJar
73-
74-
task remoteRun << {
75-
ssh.run {
76-
session(remotes.ev3dev) {
77-
println "java -jar /home/robot/" + "${rootProject.name}" + "-all-" + version + ".jar"
78-
execute "java -jar /home/robot/" + "${rootProject.name}" + "-all-" + version + ".jar"
79-
}
80-
}
81-
}
82-
83-
task deployAndRun << {
84-
85-
}
86-
deployAndRun.dependsOn deploy, remoteRun
55+
apply from: 'deploy.gradle'

ev3dev-lang-java/deploy.gradle

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
remotes {
3+
ev3dev {
4+
host = '192.168.1.206'
5+
user = 'robot'
6+
password = 'maker'
7+
}
8+
}
9+
10+
task deploy << {
11+
ssh.run {
12+
session(remotes.ev3dev) {
13+
put from: "./build/libs/" + "${rootProject.name}" + "-all-" + version + ".jar", into: "/home/robot/"
14+
}
15+
}
16+
}
17+
deploy.dependsOn clean, fatJar
18+
19+
task remoteRun << {
20+
ssh.run {
21+
session(remotes.ev3dev) {
22+
println "java -server -jar /home/robot/" + "${rootProject.name}" + "-all-" + version + ".jar"
23+
execute "java -server -jar /home/robot/" + "${rootProject.name}" + "-all-" + version + ".jar"
24+
}
25+
}
26+
}
27+
28+
task deployAndRun << {
29+
30+
}
31+
deployAndRun.dependsOn deploy, remoteRun
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package lejos.commons.subsumption;
2+
3+
import ev3dev.actuators.lego.motors.EV3LargeRegulatedMotor;
4+
import ev3dev.sensors.ev3.EV3UltrasonicSensor;
5+
import lejos.hardware.port.MotorPort;
6+
import lejos.hardware.port.SensorPort;
7+
import lejos.robotics.RegulatedMotor;
8+
import lejos.robotics.subsumption.Arbitrator;
9+
import lejos.robotics.subsumption.Behavior;
10+
11+
public class BumperCar {
12+
13+
public static void main(String [] args) {
14+
15+
//https://en.wikipedia.org/wiki/Subsumption_architecture
16+
System.out.println("Example using Subsumption architecture");
17+
18+
System.out.println("Starting motor on A");
19+
final RegulatedMotor motorLeft = new EV3LargeRegulatedMotor(MotorPort.A);
20+
System.out.println("Starting motor on B");
21+
final RegulatedMotor motorRight = new EV3LargeRegulatedMotor(MotorPort.B);
22+
23+
EV3UltrasonicSensor us1 = new EV3UltrasonicSensor(SensorPort.S1);
24+
25+
Behavior b1 = new DriveForward(motorLeft, motorRight);
26+
Behavior b2 = new HitWall(motorLeft, motorRight, us1);
27+
Behavior [] bArray = {b1, b2};
28+
Arbitrator arby = new Arbitrator(bArray);
29+
arby.go();
30+
}
31+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package lejos.commons.subsumption;
2+
3+
import lejos.robotics.RegulatedMotor;
4+
import lejos.robotics.subsumption.Behavior;
5+
6+
public class DriveForward implements Behavior {
7+
8+
private boolean suppressed = false;
9+
10+
private final RegulatedMotor motorLeft;
11+
private final RegulatedMotor motorRight;
12+
13+
public DriveForward(
14+
final RegulatedMotor motorLeft,
15+
final RegulatedMotor motorRight) {
16+
this.motorLeft = motorLeft;
17+
this.motorRight = motorRight;
18+
}
19+
20+
public boolean takeControl() {
21+
return true;
22+
}
23+
24+
public void suppress() {
25+
suppressed = true;
26+
}
27+
28+
public void action() {
29+
suppressed = false;
30+
motorLeft.forward();
31+
motorRight.forward();
32+
while( !suppressed )
33+
Thread.yield();
34+
motorLeft.stop(); // clean up
35+
motorRight.stop();
36+
}
37+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package lejos.commons.subsumption;
2+
3+
import ev3dev.sensors.ev3.EV3UltrasonicSensor;
4+
import lejos.robotics.RegulatedMotor;
5+
import lejos.robotics.SampleProvider;
6+
import lejos.robotics.subsumption.Behavior;
7+
8+
public class HitWall implements Behavior {
9+
10+
11+
private boolean suppressed = false;
12+
13+
private final RegulatedMotor motorLeft;
14+
private final RegulatedMotor motorRight;
15+
private final EV3UltrasonicSensor us1;
16+
17+
public HitWall(
18+
final RegulatedMotor motorLeft,
19+
final RegulatedMotor motorRight,
20+
final EV3UltrasonicSensor us1) {
21+
this.motorLeft = motorLeft;
22+
this.motorRight = motorRight;
23+
this.us1 = us1;
24+
}
25+
26+
public boolean takeControl() {
27+
SampleProvider sp = us1.getDistanceMode();
28+
float [] sample = new float[sp.sampleSize()];
29+
sp.fetchSample(sample, 0);
30+
int distanceValue = (int)sample[0];
31+
32+
return distanceValue < 25;
33+
}
34+
35+
public void suppress() {
36+
suppressed = true;
37+
}
38+
39+
public void action() {
40+
suppressed = false;
41+
motorLeft.rotate(-180, true);
42+
motorRight.rotate(-360, true);
43+
44+
while( motorRight.isMoving() && !suppressed )
45+
Thread.yield();
46+
47+
motorLeft.stop();
48+
motorRight.stop();
49+
}
50+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package lejos.commons.subsumption.ko;
2+
3+
import lejos.robotics.subsumption.Arbitrator;
4+
import lejos.robotics.subsumption.Behavior;
5+
6+
public class BumperCarKO {
7+
8+
public static void main(String [] args) {
9+
10+
//https://en.wikipedia.org/wiki/Subsumption_architecture
11+
System.out.println("Example using Subsumption architecture");
12+
13+
Behavior b1 = new DriveForward();
14+
Behavior b2 = new HitWall();
15+
Behavior [] bArray = {b1, b2};
16+
Arbitrator arby = new Arbitrator(bArray);
17+
arby.go();
18+
19+
}
20+
21+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package lejos.commons.subsumption.ko;
2+
3+
import ev3dev.actuators.lego.motors.Motor;
4+
import lejos.robotics.subsumption.*;
5+
6+
public class DriveForward implements Behavior {
7+
8+
private boolean suppressed = false;
9+
10+
public boolean takeControl() {
11+
return true;
12+
}
13+
14+
public void suppress() {
15+
suppressed = true;
16+
}
17+
18+
public void action() {
19+
suppressed = false;
20+
Motor.A.forward();
21+
Motor.B.forward();
22+
while( !suppressed )
23+
Thread.yield();
24+
Motor.A.stop(); // clean up
25+
Motor.B.stop();
26+
}
27+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package lejos.commons.subsumption.ko;
2+
3+
import ev3dev.actuators.lego.motors.Motor;
4+
import ev3dev.sensors.ev3.EV3UltrasonicSensor;
5+
import lejos.hardware.port.SensorPort;
6+
import lejos.robotics.SampleProvider;
7+
import lejos.robotics.subsumption.*;
8+
9+
public class HitWall implements Behavior {
10+
11+
private EV3UltrasonicSensor sonar;
12+
private boolean suppressed = false;
13+
14+
public HitWall() {
15+
sonar = new EV3UltrasonicSensor(SensorPort.S1);
16+
}
17+
18+
public boolean takeControl() {
19+
SampleProvider sp = sonar.getDistanceMode();
20+
float [] sample = new float[sp.sampleSize()];
21+
sp.fetchSample(sample, 0);
22+
int distanceValue = (int)sample[0];
23+
24+
return distanceValue < 25;
25+
}
26+
27+
public void suppress() {
28+
suppressed = true;
29+
}
30+
31+
public void action() {
32+
suppressed = false;
33+
Motor.A.rotate(-180, true);
34+
Motor.B.rotate(-360, true);
35+
36+
while( Motor.B.isMoving() && !suppressed )
37+
Thread.yield();
38+
39+
Motor.A.stop();
40+
Motor.B.stop();
41+
}
42+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
3+
Technical note: 2017/06/04
4+
With this example, it is possible to observe a problem with current library.
5+
It is necessary to stop the execution until the complete object creation is ready.
6+
7+
ev3dev#1|Example using Subsumption architecture
8+
ev3dev#1|2017-06-04 18:14:14 [main] DEBUG ev3dev.hardware.EV3DevDevice - Detecting device on port: in1
9+
ev3dev#1|2017-06-04 18:14:16 [main] DEBUG ev3dev.hardware.EV3DevDevice - Detected port on path: /sys/class/lego-sensor/sensor0/address
10+
ev3dev#1|Arbitrator created
11+
ev3dev#1|2017-06-04 18:14:18 [main] DEBUG e.a.lego.motors.BaseRegulatedMotor - Detecting motor on port: outA
12+
ev3dev#1|2017-06-04 18:14:18 [main] DEBUG ev3dev.hardware.EV3DevDevice - Detecting device on port: outA
13+
ev3dev#1|2017-06-04 18:14:18 [main] DEBUG ev3dev.hardware.EV3DevDevice - Detected port on path: /sys/class/lego-port/port4/address
14+
ev3dev#1|2017-06-04 18:14:18 [main] DEBUG e.a.lego.motors.BaseRegulatedMotor - Setting port in mode: tacho-motor
15+
ev3dev#1|2017-06-04 18:14:19 [main] DEBUG ev3dev.hardware.EV3DevDevice - Detecting device on port: outA
16+
ev3dev#1|2017-06-04 18:14:19 [main] DEBUG ev3dev.hardware.EV3DevDevice - Detected port on path: /sys/class/tacho-motor/motor8/address
17+
ev3dev#1|2017-06-04 18:14:19 [main] ERROR ev3dev.utils.Sysfs - File: '/sys/class/tacho-motor/motor8/command' without write permissions.
18+
ev3dev#1|Exception in thread "main" java.lang.ExceptionInInitializerError
19+
ev3dev#1| at lejos_commons.subsumption.DriveForward.action(DriveForward.java:20)
20+
ev3dev#1| at lejos.robotics.subsumption.Arbitrator.go(Arbitrator.java:103)
21+
ev3dev#1| at lejos_commons.subsumption.BumperCar.main(BumperCar.java:17)
22+
ev3dev#1|Caused by: java.lang.RuntimeException: Operation not executed: /sys/class/tacho-motor/motor8/command with value reset
23+
ev3dev#1| at ev3dev.hardware.EV3DevDevice.setStringAttribute(EV3DevDevice.java:91)
24+
ev3dev#1| at ev3dev.actuators.lego.motors.BaseRegulatedMotor.<init>(BaseRegulatedMotor.java:82)
25+
ev3dev#1| at ev3dev.actuators.lego.motors.NXTRegulatedMotor.<init>(NXTRegulatedMotor.java:24)
26+
ev3dev#1| at ev3dev.actuators.lego.motors.Motor.<clinit>(Motor.java:31)
27+
ev3dev#1| ... 3 more
28+
Failed command ev3dev#1 with status 1: java -server -jar /home/robot/ev3dev-lang-java-all-0.1.0.jar
29+
*/

ev3dev-lang-java/src/main/resources/META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ Manifest-Version: 1.0
22
Implementation-Title: EV3Dev-lang-java / examples
33
Implementation-Version: 0.3.0
44
Implementation-Vendor: Juan Antonio Breña Moral
5-
Main-Class: hardware.sensors.ev3.TouchSensorDemo
5+
Main-Class: ev3dev.actuators.lego.motors.MultipleMotorsStopDemo
66

77

0 commit comments

Comments
 (0)