git.haldean.org wallbot / b865493
start working on real HAL Haldean Brown 4 years ago
2 changed file(s) with 48 addition(s) and 23 deletion(s). Raw diff Collapse all Expand all
33
44 proc initHal*(stepdeg: float): Hal =
55 return Hal(
6 kind: Simulated,
67 steprads: stepdeg / 360 * math.PI,
78 current: MachineState(
89 lpos: 0, rpos: 0, ldir: CCW, rdir: CCW, lenable: false, renable: false,
910 media: Inactive))
1011
1112 proc enable*(hal: var Hal, left: bool, right: bool) =
12 hal.current.lenable = left
13 hal.current.renable = right
14 displayPush(DisplayRequest(kind: UPDATE, newstate: hal.current))
13 case hal.kind
14 of Simulated:
15 hal.current.lenable = left
16 hal.current.renable = right
17 displayPush(DisplayRequest(kind: UPDATE, newstate: hal.current))
18 of Actual:
19 discard
1520
1621 proc direction*(hal: var Hal, left: Dir, right: Dir) =
17 hal.current.ldir = left
18 hal.current.rdir = right
19 displayPush(DisplayRequest(kind: UPDATE, newstate: hal.current))
22 case hal.kind
23 of Simulated:
24 hal.current.ldir = left
25 hal.current.rdir = right
26 displayPush(DisplayRequest(kind: UPDATE, newstate: hal.current))
27 of Actual:
28 discard
2029
2130 proc media*(hal: var Hal, media: MediaState) =
22 if media != hal.current.media:
23 hal.current.media = media
24 displayPush(DisplayRequest(kind: UPDATE, newstate: hal.current))
31 case hal.kind
32 of Simulated:
33 if media != hal.current.media:
34 hal.current.media = media
35 displayPush(DisplayRequest(kind: UPDATE, newstate: hal.current))
36 of Actual:
37 discard
2538
2639 proc sendMotion*(hal: var Hal, m: Motion): MachineState =
2740 if m.ldir != hal.current.ldir or m.rdir != hal.current.rdir:
3245 let frames = int(ceil(time * frameRate))
3346 let s = hal.current
3447
35 for frame in 0..frames:
36 let t = frame / frameRate
37 let lticks = if s.lenable: min(m.ln, uint32(t * float(m.lhz))) else: 0
38 let rticks = if s.renable: min(m.rn, uint32(t * float(m.rhz))) else: 0
39 let ldist = (if s.ldir == CCW: 1.0 else: -1.0) * hal.steprads * float(lticks)
40 let rdist = (if s.rdir == CCW: 1.0 else: -1.0) * hal.steprads * float(rticks)
48 case hal.kind
49 of Simulated:
50 for frame in 0..frames:
51 let t = frame / frameRate
52 let lticks = if s.lenable: min(m.ln, uint32(t * float(m.lhz))) else: 0
53 let rticks = if s.renable: min(m.rn, uint32(t * float(m.rhz))) else: 0
54 let ldist = (if s.ldir == CCW: 1.0 else: -1.0) * hal.steprads * float(lticks)
55 let rdist = (if s.rdir == CCW: 1.0 else: -1.0) * hal.steprads * float(rticks)
4156
42 let step = MachineState(
43 lpos: s.lpos + ldist, rpos: s.rpos + rdist,
44 ldir: s.ldir, rdir: s.rdir,
45 lenable: s.lenable, renable: s.renable, media: s.media)
46 displayPush(DisplayRequest(kind: UPDATE, newstate: step))
47 hal.current = step
57 let step = MachineState(
58 lpos: s.lpos + ldist, rpos: s.rpos + rdist,
59 ldir: s.ldir, rdir: s.rdir,
60 lenable: s.lenable, renable: s.renable, media: s.media)
61 displayPush(DisplayRequest(kind: UPDATE, newstate: step))
62 hal.current = step
63 of Actual:
64 discard
4865 return hal.current
2020 renable*: bool
2121 media*: MediaState
2222
23 HalType* = enum
24 Simulated
25 Actual
26
2327 Hal* = object of RootObj
24 steprads*: float
25 current*: MachineState
28 case kind*: HalType
29 of Simulated:
30 steprads*: float
31 current*: MachineState
32 of Actual:
33 discard
2634
2735 Motion* = object of RootObj
2836 ln*: uint32