-
Notifications
You must be signed in to change notification settings - Fork 0
/
Motion.groovy
126 lines (103 loc) · 3.3 KB
/
Motion.groovy
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
123
124
125
126
import org.arl.fjage.*
import org.arl.unet.*
import org.arl.unet.nodeinfo.*
class Motion extends UnetAgent
{
private AgentID node
private int myAddr
private double[] coordinates
private double distance
private enum State {
REST, MOTION
}
private FSMBehavior fsm = FSMBuilder.build {
state(State.REST) {
onEnter {
node.mobility = false
after(rnd(60, 300).seconds) {
setNextState(State.MOTION)
}
}
}
state(State.MOTION) {
onEnter {
def bo = AgentLocalRandom.current().nextDouble(0.1, 0.4).mps
def x = AgentLocalRandom.current().nextInt(dimension).m // x-coordinate of the point
def y = AgentLocalRandom.current().nextInt(dimension).m // y-coordinate of the point
def z = -15.m // z-coordinate is constant in our case.
double[] newlocation = [x, y, z]
distance = Math.sqrt(
(newlocation[0] - coordinates[0]) * (newlocation[0] - coordinates[0]) +
(newlocation[1] - coordinates[1]) * (newlocation[1] - coordinates[1]) +
(newlocation[2] - coordinates[2]) * (newlocation[2] - coordinates[2])
)
node.mobility = true
node.speed = bo
if (coordinates[0] < newlocation[0])
{
double a = newlocation[0] - coordinates[0]
if (coordinates[1] < newlocation[1])
{
double b = newlocation[1] - coordinates[1]
node.heading = Math.toDegrees(Math.atan2(a, b))
}
else if (coordinates[1] == newlocation[1])
{
node.heading = 90.degrees
}
else if (coordinates[1] > newlocation[1])
{
double b = coordinates[1] - newlocation[1]
node.heading = 90.degrees + Math.toDegrees(Math.atan2(a, b))
}
}
else if (coordinates[0] == newlocation[0])
{
if (coordinates[1] < newlocation[1])
{
node.heading = 0.degrees
}
else if (coordinates[1] == newlocation[1])
{
node.heading = 0.degrees
}
else if (coordinates[1] > newlocation[1])
{
node.heading = 180.degrees
}
}
else if (coordinates[0] > newlocation[0])
{
double a = coordinates[0] - newlocation[0]
if (coordinates[1] < newlocation[1])
{
double b = newlocation[1] - coordinates[1]
node.heading = 270.degrees + Math.toDegrees(Math.atan2(a, b))
}
else if (coordinates[1] == newlocation[1])
{
node.heading = 270.degrees
}
else if (coordinates[1] > newlocation[1])
{
double b = coordinates[1] - newlocation[1]
node.heading = 270.degrees - Math.toDegrees(Math.atan2(a, b))
}
}
after(distance/bo) {
setNextState(State.REST)
}
}
}
}
@Override
void startup()
{
node = agentForService(Services.NODE_INFO)
node.mobility = true
myAddr = node.Address
coordinates = node.location
add(fsm)
}
int dimension // Parameter to be assigned by the Simulation file.
}