forked from c2huc2hu/universal-edition-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDelivery.java
More file actions
122 lines (115 loc) · 4.36 KB
/
Delivery.java
File metadata and controls
122 lines (115 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
public class Delivery implements Behavior {
// Left: -1 -2 -3
// Right: 1 2 3
public int targetDoor;
public float targetColor;
public float drivewayLength = 50f;
public float roadLength = 150f;
public float e = 0.2f; // tolerance
public float v; // velocity on road [cm/s] convert to rotation speed
public float b; // width of block [cm]
public float f; // estimated poll frequency [1/s]
private float prevSonic = -10000; // the last value of the sonic reading
private float nthHouse = 0; // the current house we're at
private float distToHouse; // the distance to the desired house. need to backtrack this (or we could just look for a coloured loop)
private float initialOrientation = 0;
private enum State {
START,
FINDING_LINE,
LINE_FOLLOWING,
DELIVERING,
TURNING_BACK,
RETURNING,
DONE
}
public State state = State.START;
// Constructor with default values
public Delivery(int targetDoor, float targetColor) {
this.targetDoor = targetDoor;
this.targetColor = targetColor;
this.drivewayLength = 30f;
this.roadLength = 140f;
}
// detailed constructor
public Delivery(int targetDoor, float targetColor, float drivewayLength, float roadLength) {
this.targetDoor = targetDoor;
this.targetColor = targetColor;
this.drivewayLength = drivewayLength;
this.roadLength = roadLength;
}
public boolean checkActive() {
return Robot.readyToDeliver==1 && this.state != State.DONE; // active when on line and not finished
}
// Assumed start:
// Robot is at a stop
// on the colored loop
// has a pizza to deliver
// Targeted end:
// Robot is at a stop
// on colored loop oriented antiparrallel to road
public void act(int dummy) {
// Make sure ultrasonic is looking the right direction, i.e. toward the side
System.out.println(this.state);
switch(this.state) {
case START: // look toward the correct side
Robot.look((int)(90*Math.signum(this.targetDoor)));
this.initialOrientation = Robot.gyro;
this.state = State.FINDING_LINE;
break;
case FINDING_LINE: // Find the line to follow
Robot.turn(20);
Robot.drive(20,-20);
if (Math.abs(Robot.color - this.targetColor) < e) {
this.state = State.LINE_FOLLOWING;
Robot.stop();
Robot.tachoReset();
}
break;
case LINE_FOLLOWING: // we need to test if we're on the line as well
Robot.lineFollow(this.v,350,30,500,this.targetColor); //v = 250 p = 350 i = 30 d= 500 tar = 0.312
if (Robot.dist > roadLength)
// report failure
System.out.println("reached end of road, didnt find anything :(");
else if (Robot.sonic - this.prevSonic < -e*10) // what are you trying to test here? you're testing for posedge(sonic), but why drivewayLength?
this.nthHouse++;
if (this.nthHouse == Math.abs(this.targetDoor)+1) {
this.state = State.DELIVERING; // found the right house!
Robot.stop();
this.distToHouse = Robot.dist;
}
this.prevSonic = Robot.sonic;
break;
case DELIVERING:
Robot.turn(90 * Math.signum(this.targetDoor));
if (Math.abs(Robot.gyro - this.initialOrientation) >= 90) {
Robot.stop();
Robot.drop(); // can do a blocking statement here because we don't care
this.state = State.TURNING_BACK;
}
break;
case TURNING_BACK:
Robot.turn(-90 * Math.signum(this.targetDoor)); // should do a 180 degree turn
Robot.turn(-10);
Robot.drive(20,-20);
if (Math.abs(Robot.color - this.targetColor) < e) {
this.state = State.LINE_FOLLOWING;
Robot.stop();
Robot.tachoReset();
this.state = State.RETURNING;
}
// if (Math.abs(Robot.gyro - this.initialOrientation -180) <= 5) {
// Robot.tachoReset();
// this.state = RETURNING;
// }
break;
case RETURNING:
Robot.lineFollow(this.v,350,30,500,this.targetColor); // drive back to starting position. yolo because we don't care about following the line
if (Robot.dist >= this.distToHouse) { // could also be implemented with the colour sensor
this.state = State.DONE;
}
break;
default:
System.out.println("this shouldn't happen: default state");
}
}
}