-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnxt-classes.lisp
72 lines (60 loc) · 2.49 KB
/
nxt-classes.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
;;;; Contains classes and infrastructure code applicable to both the Bluetooth
;;;; and the USB class.
(in-package :nxt)
(defclass nxt ()
((motor-states :accessor motor-states))
(:documentation "Common superclass of BLUETOOTH-NXT and USB-NXT."))
(defvar *nxt* nil
"Holds the default NXT instance (which functions use by default, if no
explicit NXT parameter is given. Replaces the old *connection* variable.")
(defmethod initialize-instance :after ((instance nxt) &key)
(setf (motor-states instance)
(coerce (loop repeat 3 collect (make-instance 'motor-state)) 'vector)))
(defclass motor-state ()
((command-timestamp :initform 0 :accessor command-timestamp)))
;;;; The following functions find or close an NXT, and take care of the *NXT*
;;;; default value.
;;;;
;;;; Maybe OPEN-CONNECTION isn't the ideal name anymore, because USB is
;;;; also an option. But I'm leaving the function names unchanged for
;;;; compatibility.
(defvar *default-bluetooth-device* "/dev/tty.NXT-DevB-1")
(defun open-connection
(&optional (look-for-types '(:usb :bluetooth))
(device *default-bluetooth-device*))
(when *nxt*
(error "Default connection is already open, close it first!"))
(setf *nxt* (find-an-nxt look-for-types device t)))
(defun close-connection ()
(unless *nxt*
(error "Connection is not open"))
(unwind-protect
(close-nxt *nxt*)
(setf *nxt* nil)))
;;;; Find an USB or a Bluetooth NXT.
;;;;
;;;; Note: Ideally we would accept a lego name here, so that we could
;;;; connect to two Legos at the same time, and (at least over USB) we would
;;;; enumerate all devices until we find the right one. For now though,
;;;; we just use the first device we can find.
;;;;
;;;; It's generally awkward that USB is so much nicer than bluetooth in
;;;; this regard, but all other NXT libraries I have found have the same
;;;; kind of asymmetry -- Bluetooth needs a /dev/something device, whereas
;;;; USB just takes a logical brick name.
(declaim (ftype (function (t &optional t) t) open-tcp-nxt))
(defun find-an-nxt
(&optional (look-for-types '(:usb :bluetooth))
(device *default-bluetooth-device*)
(errorp t))
(dolist (kind look-for-types
(when errorp (error "no NXT found (tried ~A, ~A)"
look-for-types
device)))
(let ((nxt
(ecase kind
(:usb (find-usb-nxt))
(:tcp (open-tcp-nxt device))
(:bluetooth (open-bluetooth-nxt device :if-does-not-exist nil)))))
(when nxt
(return nxt)))))