]> git.armaanb.net Git - atreides.git/blob - case/case.rkt
5f94b76aa05951e0ec484641465e06ba993ea06d
[atreides.git] / case / case.rkt
1 #lang racket
2 ;; Atreus 2 case design
3 ;; Copyright © 2019 Phil Hagelberg and contributors
4 ;; released under the GPLv3 or later
5
6 ;; TODO:
7 ;; * port notch on bottom plate and spacer
8 ;; * top layer
9
10 (require xml)
11
12 ;; glowforge uses 96 dpi, 25.4 mm in an inch
13 (define scale (/ 96 25.4))
14 (define width 260)
15 (define height 132)
16
17
18 (define cols 6) ; per hand
19 (define rows 4)
20 (define angle (degrees->radians 10))
21 (define corner-radius 6.0)
22
23 (define alps-switch-width 15.887)
24 (define alps-switch-height 13.087)
25 (define cherry-switch-width 13.62)
26 (define cherry-switch-height 13.72)
27 (define cherry? true)
28 (define switch-height (if cherry? cherry-switch-height alps-switch-height))
29 (define switch-width (if cherry? cherry-switch-width alps-switch-width))
30
31 (define switch-spacing 19.0)
32
33 (define screw-radius 1.4) ; for M3 screws + kerf
34
35 (define side-screw-distance (* switch-spacing rows))
36 (define bottom-screw-distance (* switch-spacing cols))
37 (define left corner-radius)
38 (define bottom 95) ; outer bottom
39 (define left-top (+ left (* side-screw-distance (sin angle))))
40 (define top (- bottom (* side-screw-distance (cos angle))))
41 (define right (- width corner-radius))
42 (define right-top (- right (* side-screw-distance (sin angle))))
43 (define mid-bottom (+ bottom (* bottom-screw-distance (sin angle)) -3))
44 (define mid-offset 25)
45 (define mid-x (/ width 2))
46 (define mid-left (- mid-x mid-offset))
47 (define mid-right (+ mid-x mid-offset))
48
49 (define hull-coords (list (list right-top top)
50                           (list right bottom)
51                           (list mid-right mid-bottom)
52                           (list mid-left mid-bottom)
53                           (list left bottom)
54                           (list left-top top)))
55
56 ;;; screws
57 (define screws
58   `(g () ,@(for/list ([s (append (take hull-coords 2)
59                                  ;; the bottom middle has only one screw but
60                                  ;; two hull positions
61                                  (list (list (/ width 2) mid-bottom))
62                                  (drop hull-coords 4))])
63              `(circle ((r ,(number->string screw-radius))
64                        (cx ,(number->string (first s)))
65                        (cy ,(number->string (second s))))))))
66
67 ;;; outline
68 (define outline-coords (append hull-coords (take hull-coords 2)))
69
70 (define (to-next-screw? theta current-screw)
71   (let* ([current (list-ref outline-coords current-screw)]
72          [cx (first current)] [cy (second current)]
73          [next (list-ref outline-coords (add1 current-screw))]
74          [nx (first next)] [ny (second next)]
75          [dx (- nx cx)] [dy (- ny cy)]
76          [next-theta (- (radians->degrees (atan dy dx)))])
77     (= (floor (modulo (floor next-theta) 180))
78        (floor (modulo (- theta 90) 180)))))
79
80 ;; trace the outline by going from screw to screw until you've gone full-circle
81 (define (outline-points coords theta current-screw)
82   (if (< -360 (- theta 90) 360)
83       (let* ([current (list-ref outline-coords current-screw)]
84              [sx (first current)] [sy (second current)]
85              [x (+ sx (* (cos (degrees->radians theta)) corner-radius))]
86              [y (- sy (* (sin (degrees->radians theta)) corner-radius))]
87              [coords (cons (format "~s,~s" x y) coords)])
88         (if (to-next-screw? theta current-screw)
89             (outline-points coords theta (add1 current-screw))
90             (outline-points coords (sub1 theta) current-screw)))
91       coords))
92
93 (define port-depth 8)
94
95 (define port-curve (list (format "~s,~s" mid-right (- top corner-radius))
96                          (format "~s,~s" (+ mid-x corner-radius)
97                                  (+ top port-depth
98                                     (- corner-radius)))
99                          (format "~s,~s" (- mid-x corner-radius)
100                                  (+ top port-depth
101                                     (- corner-radius)))
102                          (format "~s,~s" mid-left (- top corner-radius))))
103
104 (define (outline with-port?)
105   `(polygon ((points ,(string-join (let ((noport (outline-points '() 90 0)))
106                                      (if with-port?
107                                          (append noport port-curve)
108                                          noport)))))))
109
110 ;;; switches
111
112 (define column-offsets `(8 5 0 6 11 ,(+ 8 switch-spacing switch-spacing)))
113
114 (define (switch row col)
115   (let* ([x (* (+ 1 col) switch-spacing)]
116          [y (+ (list-ref column-offsets col) (* switch-spacing row))])
117     `(rect ((height ,(number->string switch-height))
118             (width ,(number->string switch-width))
119             (x ,(number->string x))
120             (y ,(number->string y))))))
121
122 (define hand-height (+ (* switch-spacing rows) (- switch-spacing switch-height)
123                        (list-ref column-offsets 0)))
124 (define switch-x-offset -6.5)
125 (define switch-y-offset (- bottom hand-height -3.5))
126
127 (define switches
128   `(g ((transform ,(format "translate(~s, ~s) rotate(~s, ~s, ~s)"
129                            switch-x-offset switch-y-offset
130                            (radians->degrees angle)
131                            0 hand-height)))
132       ,@(for/list ([col (in-range cols)]
133                    #:when true
134                    [row (if (= 5 col) '(0 1) (in-range rows))])
135           (switch row col))))
136
137 (define switches-right
138   `(g ((transform ,(format "translate(~s,~s) scale(-1, 1)" width 0)))
139       ,switches))
140
141 (define logo-doc (call-with-input-file "logo-fragment.svg" read-xml))
142
143 (define pcb-doc (call-with-input-file "pcb-fragment.svg" read-xml))
144
145 (define (layer plate)
146   (document (prolog '() false '())
147             (xexpr->xml
148              `(svg ((xmlns:svg "http://www.w3.org/2000/svg")
149                     (height ,(number->string (* height scale)))
150                     (width ,(number->string (* width scale))))
151                    ,@(if (eq? plate 'switch)
152                          `((g ((transform "translate(436, 115)")
153                                (stroke "red"))
154                               ,(xml->xexpr (document-element logo-doc))))
155                          '())
156                    ,@(if (eq? plate 'spacer)
157                          (list (xml->xexpr (document-element pcb-doc)))
158                          (list))
159                    (g ((transform ,(format "scale(~s, ~s)" scale scale))
160                        (stroke-width "1")
161                        (stroke "black")
162                        (fill-opacity "0"))
163                       ,screws
164                       ,(outline (not (eq? plate 'switch)))
165                       ,@(if (eq? plate 'switch)
166                             (list switches switches-right)
167                             (list)))))
168             '()))
169
170 ;; to laser cut these, you have to open them in inkscape, then save,
171 ;; and then upload; for some reason the glowforge crashes if you try to cut
172 ;; them directly.
173
174 (define (write-out-layer layer-name)
175   (call-with-output-file (format "case2-~a.svg" (symbol->string layer-name))
176     (lambda (out)
177       (display "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" out)
178       (display-xml (layer layer-name) out))
179     #:exists 'replace))
180
181 (write-out-layer 'switch)
182 (write-out-layer 'bottom)
183 (write-out-layer 'spacer)