forked from bjj/2x_laser
-
Notifications
You must be signed in to change notification settings - Fork 8
/
laserfreq.comp
61 lines (54 loc) · 1.68 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
57
58
59
60
61
component laserfreq "Pulse driver for CO2 Laser";
author "Ben Jackson <[email protected]>";
pin in bit enable = 0 "Enable pulse generation";
pin in float velocity = 0 "Velocity of the laser head (e.g. motion.current-vel)";
pin in float pulse-per-unit = 0 "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 s32 duration "Duration of each pulse in ns";
function make_pulses nofp "Add to fast thread to make laser pulses";
function update fp "Add to update thread to compute pulse parameters";
variable int updated;
param r s32 interval;
param r s32 accum;
param r s32 pulse_remain;
license "GPL";
;;
FUNCTION(update) {
if (velocity == 0.0 || pulse_per_unit == 0) {
updated = 0;
} else {
if ( pulse_per_unit < 0.0 ) {
// duty cycle is just a function of % and duration
interval = duration * -100.0 / pulse_per_unit;
} else {
// velocity * pulse-per-unit gives pulses per second
// pulse interval is then 1/pps
interval = 1000000000.0 / (velocity * pulse_per_unit);
}
updated = 1;
}
}
FUNCTION(make_pulses) {
if (!enable || !updated) {
accum = 0;
pulse_remain = 0;
pulse = 0;
continuous = 0;
return;
}
accum += period;
if (accum > interval) {
accum -= interval;
if (accum > interval)
accum = 0;
continuous = (pulse_remain > 0);
pulse_remain = duration;
}
if (pulse_remain > 0) {
pulse = 1;
pulse_remain -= period;
} else {
pulse = 0;
}
}