-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSensor.java
94 lines (77 loc) · 3.32 KB
/
Sensor.java
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
/**
* This class simulates the behavior of Sensor. It sends angle and previous angle values to the controller to compute action.
*/
import java.io.*;
class Sensor implements Runnable {
Physics physics;
private ObjectOutputStream out;
private double samplingPeriod_phy; // delay in physical time (in second)
private long samplingPeriod_phy_ms; // delay in physical time (in ms)
private double samplingPeriod_sim; // delay in simulation time (in second)
private TriggerType triggerType;
private double threshold; // only applicable in event based sensor (in degrees)
Sensor(Physics phy, ObjectOutputStream out, TriggerType type, double threshold, double sensorSamplingPeriod_sim, double sensorSamplingPeriod_phy) {
this.physics = phy;
this.out = out;
this.triggerType = type;
this.samplingPeriod_phy = sensorSamplingPeriod_phy;
this.samplingPeriod_sim = sensorSamplingPeriod_sim;
this.samplingPeriod_phy_ms = Math.round(samplingPeriod_phy*1000);
this.threshold = threshold;
}
public synchronized void run() {
while (true) {
// Sensor will get four data from each pendulum
// {angle, angleDot, pos, posDot}
double sensorData[] = new double[4 * physics.NUM_POLES];
Pendulum[] pendulums = physics.get_pendulums();
for (int i = 0; i < pendulums.length; i++) {
synchronized(pendulums[i]) {
double angle, angleDot, pos, posDot;
angle = pendulums[i].get_angle();
angleDot = pendulums[i].get_angleDot();
pos = pendulums[i].get_pos();
posDot = pendulums[i].get_posDot();
if (triggerType == TriggerType.EVENT_TRIGGER) {
// Do not send if it is in event triggered mode and the angle is small
if (angle * 180 / 3.14 < threshold && angle * 180 / 3.14 > -threshold) {
try {
Thread.sleep(samplingPeriod_phy_ms);
} catch (Exception e) {
e.printStackTrace();
}
continue;
}
}
sensorData[i*4+0] = angle;
sensorData[i*4+1] = angleDot;
sensorData[i*4+2] = pos;
sensorData[i*4+3] = posDot;
}
}
sendMessage_doubleArray(sensorData);
System.out.println("---------------");
try {
Thread.sleep(samplingPeriod_phy_ms);
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* This method sends the Double message on the object output stream.
*/
void sendMessage_doubleArray(double[] data) {
try {
out.writeObject(data);
out.flush();
System.out.print("client> ");
for(int i=0; i< data.length; i++){
System.out.print(data[i] + " ");
}
System.out.println();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}