-
Notifications
You must be signed in to change notification settings - Fork 0
/
satsim.js
295 lines (241 loc) · 7.26 KB
/
satsim.js
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
//rect = left/right coordinate from left, up/down from top, width, height
//--------------------------
//----Vars related to view--
//--------------------------
//used to adjust actual dimensions (1 km = 1px) to be human viewable
const VIEW_MULTIPLIER = 3;
//canvas is offset by 20 when drawn (help to get full screen with no scrolling)
const VIEW_ADJ = 20;
//------------------------------
//----Constants for simulation
//-----------------------------
//dimensions of simulation components (in km)
//minimum width = 50
const TARGET_WIDTH = 50 * VIEW_MULTIPLIER;
const TARGET_LEFT_X = 605 - (TARGET_WIDTH/2);
const TARGET_RIGHT_X = TARGET_LEFT_X + TARGET_WIDTH;
const TARGET_Y = 300;
const SWATH_WIDTH = .13 * VIEW_MULTIPLIER;
const SWATH_HEIGHT = 98 * VIEW_MULTIPLIER;
const SWATH_START_X = TARGET_LEFT_X - TARGET_WIDTH;
const SWATH_START_Y = (TARGET_Y + (TARGET_WIDTH/2)) - (SWATH_HEIGHT/2);
const SWATH_END_X = TARGET_RIGHT_X + TARGET_WIDTH;
const VELOCITY = 3;
//todo should be a function
const MAX_PITCH_ERROR = 3.4908;
const MAX_YAW_ERROR = MAX_PITCH_ERROR;
const MAX_ROLL_ERROR = .5;
//used for instability calculation
const INSTABILITY_STD_DEV = .03333;
//--------------------------------
//----Vars for simulation control
//--------------------------------
let instability = false;
//-----------------------------------
//----Vars to track simulation status
//-----------------------------------
let isSimulating = false;
let isInSimulationRange = true;
let inSetup = true;
let startScanX = TARGET_LEFT_X;
let endScanX = TARGET_RIGHT_X;
let swathX = SWATH_START_X;
let swathY = SWATH_START_Y;
let pitchError = 0;
let rollError = 0;
let yawError = 0;
let refPointX = swathX + SWATH_WIDTH;
let refPointY = SWATH_START_Y + (SWATH_HEIGHT/2);
//todo variables from here down need to be assessed
var missedLeft = false;
var missedArea = 0;
var tlPoint = 0; //area coordinates
var blPoint = 0;
var trPoint = 0;
var brPoint = 0;
function setup() {
createCanvas(windowWidth-VIEW_ADJ, windowHeight-VIEW_ADJ);
textSize(15);
setupUserInput();
fill('white');
stroke('black');
angleMode(DEGREES);
}
function draw() {
background(51);
if(inSetup) {
pitchError = pitchSlider.value();
rollError = rollSlider.value();
yawError = yawSlider.value();
}
displayDynamicText();
//add listener for inputs
pitchInput.changed(changePitchInput);
rollInput.changed(changeRollInput);
yawInput.changed(changeYawInput);
iCheckbox.changed(setInstability);
addInstability();
adjustForErrors();
let targetArea, swath, refPoint, scanX, scanY;
displaySimulationResources(targetArea, swath, refPoint);
let swathAngle;
moveSwath(swathAngle);
calculateArea();
listenForKeyTypes();
}
function calculateArea() {
//find x position of vertices of scanned area
if(refPointX == startScanX) {
if(rollError) {
tlPoint
} else {
tlPoint, blPoint = refPointX;
}
}
if(refPointX == endScanX) {
if(rollError) {
} else {
trPoint, brPoint = refPointX;
}
}
}
function moveSwath(swathAngle) {
if(isSimulating && isInSimulationRange) {
swathX = swathX + VELOCITY;
refPointX = swathX + SWATH_WIDTH
//todo am I calculating this correctly?
swathAngle = (592 * Math.cos(getDegrees(rollError)))/(592 * Math.sin(getDegrees(rollError)));
console.log(swathAngle)
}
//check to end movement of swath
if(refPointX > SWATH_END_X) {
isInSimulationRange = false;
instability = false;
}
}
function adjustForErrors() {
if(!isSimulating && inSetup) {
swathX = SWATH_START_X + pitchError;
swathY = SWATH_START_Y + yawError;
refPointX = swathX + SWATH_WIDTH;
refPointY = swathY + (SWATH_HEIGHT/2);
startScanX = TARGET_LEFT_X + pitchError;
endScanX = TARGET_RIGHT_X + pitchError;
}
}
function addInstability() {
if(instability && isSimulating) {
let potentialRollError = rollError + randomGaussian(0, INSTABILITY_STD_DEV);
rollError = (Math.abs(potentialRollError) > MAX_ROLL_ERROR) ? 0 : potentialRollError;
let potentialPitchError = pitchError + randomGaussian(0, INSTABILITY_STD_DEV);
pitchError = (Math.abs(potentialPitchError) > MAX_PITCH_ERROR) ? 0 : potentialPitchError;
let potentialYawError = yawError + randomGaussian(0, INSTABILITY_STD_DEV);
yawError = (Math.abs(potentialYawError) > MAX_YAW_ERROR) ? 0 : potentialYawError;
//console.log(`PitchError - ${pitchError} || RollError - ${rollError} || YawError - ${yawError}`)
}
}
function displaySimulationResources(targetArea, swath, refPoint) {
fill('green');
stroke('green');
targetArea = rect(TARGET_LEFT_X,TARGET_Y,TARGET_WIDTH, TARGET_WIDTH);
fill('red');
stroke('red');
push();
translate(swathX,swathY);
rotate(rollError);
swath = rect(0, 0, SWATH_WIDTH, SWATH_HEIGHT);
fill('white');
stroke('white');
refPoint = ellipse(SWATH_WIDTH, SWATH_HEIGHT/2, 2, 2);
pop();
displayScanPositions();
}
function displayScanPositions() {
//display initial swath scan position
if(refPointX >= startScanX) {
fill('blue');
stroke('blue');
push();
//point of reference is in the middle of the front of the swath
translate(startScanX,swathY);
rotate(rollError);
scan1 = rect(0,0,SWATH_WIDTH,SWATH_HEIGHT);
stroke('red');
pop();
}
//display final swath scan position
if((refPointX + SWATH_WIDTH) >= endScanX) {
fill('blue');
stroke('blue');
push();
//todo what do I mean by this?
//-1 to accomadate for endScan having to be divisible by 3
translate(endScanX,swathY);
rotate(rollError);
scan2 = rect(0,0,SWATH_WIDTH,SWATH_HEIGHT);
stroke('red');
pop();
}
}
function listenForKeyTypes() {
if (key === 's') {
isSimulating = true;
isInSimulationRange = true;
inSetup = false;
}
if (key === 'r') {
isSimulating = false;
inSetup = true;
isInSimulationRange = false;
setInstability();
adjustForErrors();
tlPoint = 0;
blPoint = 0;
trPoint = 0;
brPoint = 0;
}
}
function setupUserInput() {
pitchSlider = createSlider(-3.4908, 3.4908, 0, 0).position(20, 20);
pitchInput = createInput().position(280,20).size(50,17);
rollSlider = createSlider(-5.5, 5.5, 0, 0).position(20, 50);
rollInput = createInput().position(280,50).size(50,17);
yawSlider = createSlider(-10.4908, 10.4908, 0, 0).position(20, 80);
yawInput = createInput().position(280,80).size(50,17);
iCheckbox = createCheckbox('', false).position(20, 110);
}
function displayDynamicText() {
fill('white');
stroke('black');
text("Pitch Error", 190, 30);
text(pitchError, 350, 30);
text("Roll Error", 190, 60);
text(rollError, 350, 60);
text("Yaw Error", 190, 90);
text(yawError, 350, 90);
text("Instability", 50, 120);
text("Missed Area: ", 900, 30);
text(missedArea, 1050, 30);
//todo fix this location
text("Press 's' to start simulation.", 1000, 900);
text("Press 'r' to reset simulation.", 1000, 930);
}
function changePitchInput() {
pitchSlider.value(pitchInput.value());
}
function changeRollInput() {
rollSlider.value(rollInput.value());
}
function changeYawInput() {
yawSlider.value(yawInput.value());
}
function setInstability() {
if(iCheckbox.checked()) {
instability = true;
} else {
instability = false;
}
}
function getDegrees(rad) {
return rad * Math.PI/180;
}