-
Notifications
You must be signed in to change notification settings - Fork 23
/
laserfreq.comp
56 lines (52 loc) · 1.58 KB
/
laserfreq.comp
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
component laserfreq "Pulse driver for CO2 Laser";
author "Ben Jackson <[email protected]>";
pin in bit enable "Enable pulse generation";
pin in float velocity "Velocity of the laser head (e.g. motion.current-vel)";
pin in float pulse-per-unit "How many pulses per unit distance";
pin out bit pulse "Output firing pulse";
pin out bit continuous "True if current inputs are causing continuous (not pulsed) output";
param rw float duration "Duration of each pulse in seconds";
param rw float min-off-duration "If nonzero force continuous pulsing with at least this much off-time in seconds";
function make_pulses fp "Add to fast thread to make laser pulses";
function update fp "Add to update thread to compute pulse parameters";
variable int updated;
variable float interval;
variable float accum;
variable float pulse_remain;
license "GPL";
;;
FUNCTION(update) {
if (velocity == 0.0 || pulse_per_unit == 0) {
updated = 0;
} else {
// velocity * pulse-per-unit gives pulses per second
// pulse interval is then 1/pps
interval = 1.0 / (velocity * pulse_per_unit);
if (min_off_duration > 0 && interval < min_off_duration + duration)
interval = min_off_duration + duration;
updated = 1;
}
}
FUNCTION(make_pulses) {
if (!enable || !updated) {
accum = 0;
pulse_remain = 0;
pulse = 0;
continuous = 0;
return;
}
accum += fperiod;
if (accum > interval) {
accum -= interval;
if (accum > interval)
accum = 0.0;
continuous = (pulse_remain > 0);
pulse_remain = duration;
}
if (pulse_remain > 0) {
pulse = 1;
pulse_remain -= fperiod;
} else {
pulse = 0;
}
}