-
Notifications
You must be signed in to change notification settings - Fork 1
/
spacex.lisp
469 lines (406 loc) · 15.6 KB
/
spacex.lisp
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
(ql:quickload "dexador")
(ql:quickload "cl-json")
; *** CONFIG ***
(defvar PORT "9515")
; *** Global variables ***
; Easier for testing
(defvar driver nil)
; Rotation controls
(defvar yaw-left-button nil)
(defvar yaw-right-button nil)
(defvar pitch-up-button nil)
(defvar pitch-down-button nil)
(defvar roll-left-button nil)
(defvar roll-right-button nil)
; transation controls
(defvar translate-left-button nil)
(defvar translate-right-button nil)
(defvar translate-up-button nil)
(defvar translate-down-button nil)
(defvar translate-forward-button nil)
(defvar translate-backward-button nil)
; readings
(defvar reading-roll nil)
(defvar reading-roll-rate nil)
(defvar reading-pitch nil)
(defvar reading-pitch-rate nil)
(defvar reading-yaw nil)
(defvar reading-yaw-rate nil)
(defvar reading-translate-x nil)
(defvar reading-translate-y nil)
(defvar reading-translate-z nil)
(defvar reading-translate-rate nil)
(defvar reading-translate-range nil)
(defvar prev-roll 0)
(defvar last-time (/ (get-internal-real-time) internal-time-units-per-second))
(defvar lastx 0)
(defvar lasty 0)
(defvar lastz 0)
; *** Utils ***
(defvar f "spacex.lisp")
(defvar spacex-sim-url "https://iss-sim.spacex.com/")
(defun l (name) (load name))
(defun string-sub-any (string list)
"subsitute any char in list from string"
(if (= (length list) 0)
string
(string-sub-any (remove (char list 0) string) (subseq list 1))))
(defun text-to-value (text)
(with-input-from-string (s (string-sub-any text "°/sm ")) (read s)))
(defun string-replace (string part replacement &key (test #'char=))
"Returns a new string in which all the occurences of the part
is replaced with replacement."
(let ((copy-string string))
(with-output-to-string (out)
(loop with part-length = (length part)
for old-pos = 0 then (+ pos part-length)
for pos = (search part copy-string
:start2 old-pos
:test test)
do (write-string copy-string out
:start old-pos
:end (or pos (length copy-string)))
when pos do (write-string replacement out)
while pos))))
(defun string-to-cl (s)
"Convert a JSON string to Lisp object"
(with-input-from-string (sc s)
(json:decode-json sc)))
(defun format-url (&rest list) (format nil "~{~A~^/~}" list))
(defun current-date-string ()
"Returns current date as a string."
(multiple-value-bind (sec min hr day mon yr dow dst-p tz)
(get-decoded-time)
(declare (ignore day mon yr dow dst-p tz)) (format nil "~d:~d:~d" hr min sec))
)
(defun call-n-times (func n)
(if (< n 1)
'done
(progn
(funcall func)
(call-n-times func (- n 1)))))
(defun print-status()
(let
(
(roll (text-to-value (text reading-roll)))
(roll-rate (text-to-value (text reading-roll-rate)))
(pitch (text-to-value (text reading-pitch)))
(pitch-rate (text-to-value (text reading-pitch-rate)))
(yaw (text-to-value (text reading-yaw)))
(yaw-rate (text-to-value (text reading-yaw-rate)))
(x (text-to-value (text reading-translate-x)))
(y (text-to-value (text reading-translate-y)))
(z (text-to-value (text reading-translate-z)))
(rate (text-to-value (text reading-translate-rate)))
(dt (- (/ (get-internal-real-time) internal-time-units-per-second) last-time))
)
(progn
(princ "------------------------")
(terpri)
(format t "Time: ~s" (current-date-string))
(terpri)
(format t "Roll: ~f, rate: ~f" roll roll-rate)
(terpri)
(format t "Pitch ~f, rate: ~f" pitch pitch-rate)
(terpri)
(format t "Yaw ~f, rate: ~f" yaw yaw-rate)
(terpri)
(format t "x: ~f, rate: ~f" x (/ (- x lastx) dt))
(terpri)
(format t "y: ~f, rate: ~f" y (/ (- y lasty) dt))
(terpri)
(format t "z: ~f, rate: ~f" z (/ (- z lastz) dt))
(terpri)
(format t "rate: ~f" rate)
(terpri)
)))
; *** Payload Template ***
(defvar payload-init-session "{\"capabilities\":{\"firstMatch\":[{}],\"alwaysMatch\":{\"browserName\":\"chrome\",\"platformName\":\"any\",\"goog:chromeOptions\":{\"extensions\":[],\"args\":[]}}}}")
(defvar payload-goto "{\"url\":\"URL\"}")
(defvar payload-find-elem-by-selector "{\"using\":\"css selector\",\"value\":\"SELECTOR\"}")
(defvar payload-click "{\"id\":\"ID\"}")
; *** Classes ***
; Driver to control Chrome WebDriver
(defclass Driver ()
((url
:initarg :url
:reader url
:documentation "WebDriver url"
)
(session-id
:initform nil
:accessor session-id))
(:documentation "Driver to control WebDriver"))
(defmethod init-session ((obj Driver))
(let ((resp (dex:post
(format-url (url obj) "session")
:content payload-init-session)))
(setf (session-id obj) (cdr (car (cdr (cdr (car (string-to-cl resp)))))))))
(defmethod goto ((obj Driver) &key dest)
"Open an URL on webdriver"
(dex:post
(format-url (url obj) "session" (session-id obj) "url")
:content (string-replace payload-goto "URL" dest)))
(defmethod find-elem-by-selector ((obj Driver) &key selector)
"Select and Create an elem by css selector"
(let ((resp (dex:post
(format-url (url obj) "session" (session-id obj) "element")
:content (string-replace payload-find-elem-by-selector "SELECTOR" selector))))
(make-instance 'Elem
:selector selector
:session-url (format-url (url driver) "session" (session-id driver))
:dom-id (cdr (car (cdr (car (string-to-cl resp)))))
)))
(defmethod print-object ((obj Driver) stream)
(print-unreadable-object (obj stream :type t)
(with-accessors ((driver-url url)
(driver-session-id session-id))
obj
(format stream "~a, SessionId: ~a" driver-url driver-session-id))))
(defun make-driver (&key (host "http://127.0.0.1") (port PORT))
(let ((url (concatenate 'string host ":" port)))
(make-instance 'Driver :url url)))
; Element in DOM
(defclass Elem ()
((session-url
:initarg :session-url
:reader session-url)
(selector
:initarg :selector
:reader selector)
(dom-id
:initarg :dom-id
:reader dom-id))
(:documentation "ELement in DOM"))
(defmethod click ((obj Elem))
(dex:post
(format-url (session-url obj) "element" (dom-id obj) "click")
:content (string-replace payload-click "ID" (dom-id obj))))
;
(defmethod text ((obj Elem))
(let ((resp
(dex:get
(format-url (session-url obj) "element" (dom-id obj) "text"))))
(cdr (car (string-to-cl resp)))))
(defmethod print-object ((obj Elem) stream)
(print-unreadable-object (obj stream :type t)
(format stream "~a Id:~a" (selector obj) (dom-id obj))))
(defun make-driver (&key (host "http://127.0.0.1") (port "9515"))
(let ((url (concatenate 'string host ":" port)))
(make-instance 'Driver :url url)))
; *** Controllers ***
(defun bound-target (target max-rate)
"Bound target based on max-rate. Max-rate is a positive number"
(cond ((and (< target 0) (< target (- max-rate)))
(- max-rate))
((and (> target 0) (> target max-rate))
max-rate)
(t target)))
(defun auto-rotate (&key value rate max-rate dec-btn inc-btn jump)
(let ((target-rate (bound-target (/ value 3) max-rate)))
(if (> rate target-rate)
(call-n-times (lambda() (click dec-btn)) (round (* (- rate target-rate) (/ 1 jump))))
(call-n-times (lambda() (click inc-btn)) (round (* (- target-rate rate) (/ 1 jump))))
)))
(defun auto-translate (&key value rate max-rate dec-btn inc-btn)
(let ((target-rate (bound-target value max-rate)))
(if (> value 0)
(cond ((> rate (- target-rate)) (click dec-btn))
((< rate (- target-rate)) (click inc-btn)))
(cond ((<= rate target-rate) (click inc-btn))
((< rate target-rate) (click dec-btn)))
)))
; *** Main ***
(defun init-sim()
(let ((d (make-driver)))
(progn
(setq driver d)
(init-session d)
(goto d :dest spacex-sim-url)
(handler-bind ((dex:http-request-failed #'dex:retry-request))
(click (find-elem-by-selector driver :selector "#begin-button")))
d)))
(defun init-controllers (driver)
(let (
; rotation controls
(yaw-left-btn (find-elem-by-selector driver :selector "#yaw-left-button"))
(yaw-right-btn (find-elem-by-selector driver :selector "#yaw-right-button"))
(pitch-up-btn (find-elem-by-selector driver :selector "#pitch-up-button"))
(pitch-down-btn (find-elem-by-selector driver :selector "#pitch-down-button"))
(roll-left-btn (find-elem-by-selector driver :selector "#roll-left-button"))
(roll-right-btn (find-elem-by-selector driver :selector "#roll-right-button"))
; transation controls
(translate-left-btn (find-elem-by-selector driver :selector "#translate-left-button"))
(translate-right-btn (find-elem-by-selector driver :selector "#translate-right-button"))
(translate-up-btn (find-elem-by-selector driver :selector "#translate-up-button"))
(translate-down-btn (find-elem-by-selector driver :selector "#translate-down-button"))
(translate-forward-btn (find-elem-by-selector driver :selector "#translate-forward-button"))
(translate-backward-btn (find-elem-by-selector driver :selector "#translate-backward-button"))
; readings
(reading-roll-div (find-elem-by-selector driver :selector "#roll > div.error"))
(reading-roll-rate-div (find-elem-by-selector driver :selector "#roll > div.rate"))
(reading-pitch-div (find-elem-by-selector driver :selector "#pitch > div.error"))
(reading-pitch-rate-div (find-elem-by-selector driver :selector "#pitch> div.rate"))
(reading-yaw-div (find-elem-by-selector driver :selector "#yaw > div.error"))
(reading-yaw-rate-div (find-elem-by-selector driver :selector "#yaw > div.rate"))
(reading-translate-x-div (find-elem-by-selector driver :selector "#x-range > div.distance"))
(reading-translate-y-div (find-elem-by-selector driver :selector "#y-range > div.distance"))
(reading-translate-z-div (find-elem-by-selector driver :selector "#z-range > div.distance"))
(reading-translate-rate-div (find-elem-by-selector driver :selector "#rate > div.rate"))
(reading-translate-range-div (find-elem-by-selector driver :selector "#range > div.rate"))
)
(progn
(setq yaw-left-button yaw-left-btn)
(setq yaw-right-button yaw-right-btn)
(setq pitch-up-button pitch-up-btn)
(setq pitch-down-button pitch-down-btn)
(setq roll-left-button roll-left-btn)
(setq roll-right-button roll-right-btn)
(setq translate-left-button translate-left-btn)
(setq translate-right-button translate-right-btn)
(setq translate-up-button translate-up-btn)
(setq translate-down-button translate-down-btn)
(setq translate-forward-button translate-forward-btn)
(setq translate-backward-button translate-backward-btn)
(setq reading-roll reading-roll-div)
(setq reading-roll-rate reading-roll-rate-div)
(setq reading-pitch reading-pitch-div)
(setq reading-pitch-rate reading-pitch-rate-div)
(setq reading-yaw reading-yaw-div)
(setq reading-yaw-rate reading-yaw-rate-div)
(setq reading-translate-x reading-translate-x-div)
(setq reading-translate-y reading-translate-y-div)
(setq reading-translate-z reading-translate-z-div)
(setq reading-translate-rate reading-translate-rate-div)
(setq reading-translate-range reading-translate-range-div)
(if (null reading-translate-range)
(init-controllers driver)
'done
)
)))
(defun autopilot
(
yaw-left-button
yaw-right-button
pitch-up-button
pitch-down-button
roll-left-button
roll-right-button
translate-left-button
translate-right-button
translate-up-button
translate-down-button
translate-forward-button
translate-backward-button
reading-roll-div
reading-roll-rate-div
reading-pitch-div
reading-pitch-rate-div
reading-yaw-div
reading-yaw-rate-div
reading-translate-x-div
reading-translate-y-div
reading-translate-z-div
)
(let (
(roll (text-to-value (text reading-roll-div)))
(roll-rate (text-to-value (text reading-roll-rate-div)))
(pitch (text-to-value (text reading-pitch-div)))
(pitch-rate (text-to-value (text reading-pitch-rate-div)))
(yaw (text-to-value (text reading-yaw-div)))
(yaw-rate (text-to-value (text reading-yaw-rate-div)))
(x (text-to-value (text reading-translate-x-div)))
(y (text-to-value (text reading-translate-y-div)))
(z (text-to-value (text reading-translate-z-div)))
(dt (- (/ (get-internal-real-time) internal-time-units-per-second) last-time))
)
(progn
(print-status)
(auto-rotate :value roll :rate roll-rate :max-rate .4 :dec-btn roll-left-button :inc-btn roll-right-button :jump .1)
(auto-rotate :value pitch :rate pitch-rate :max-rate .4 :dec-btn pitch-up-button :inc-btn pitch-down-button :jump .1)
(auto-rotate :value yaw :rate yaw-rate :max-rate .4 :dec-btn yaw-left-button :inc-btn yaw-right-button :jump .1)
(auto-translate :value x :rate (/ (- x lastx) dt) :max-rate (/ x 20) :dec-btn translate-forward-button :inc-btn translate-backward-button)
(auto-translate :value y :rate (/ (- y lasty) dt) :max-rate (/ y 10) :dec-btn translate-left-button :inc-btn translate-right-button)
(auto-translate :value z :rate (/ (- z lastz) dt) :max-rate (/ z 10) :dec-btn translate-down-button :inc-btn translate-up-button)
(setq lastx x)
(setq lasty y)
(setq lastz z)
(setq last-time (/ (get-internal-real-time) internal-time-units-per-second))
; Infinte loop
(sleep .4)
(autopilot
yaw-left-button
yaw-right-button
pitch-up-button
pitch-down-button
roll-left-button
roll-right-button
translate-left-button
translate-right-button
translate-up-button
translate-down-button
translate-forward-button
translate-backward-button
reading-roll
reading-roll-rate
reading-pitch
reading-pitch-rate
reading-yaw
reading-yaw-rate
reading-translate-x
reading-translate-y
reading-translate-z
))))
(defun ap ()
"short call to run autopilot"
(autopilot
yaw-left-button
yaw-right-button
pitch-up-button
pitch-down-button
roll-left-button
roll-right-button
translate-left-button
translate-right-button
translate-up-button
translate-down-button
translate-forward-button
translate-backward-button
reading-roll
reading-roll-rate
reading-pitch
reading-pitch-rate
reading-yaw
reading-yaw-rate
reading-translate-x
reading-translate-y
reading-translate-z
))
(defun main ()
(let ((sim (init-sim)))
(sleep 10) ; Wait for begin annimation
(init-controllers sim)
(autopilot
yaw-left-button
yaw-right-button
pitch-up-button
pitch-down-button
roll-left-button
roll-right-button
translate-left-button
translate-right-button
translate-up-button
translate-down-button
translate-forward-button
translate-backward-button
reading-roll
reading-roll-rate
reading-pitch
reading-pitch-rate
reading-yaw
reading-yaw-rate
reading-translate-x
reading-translate-y
reading-translate-z
)))
(main)