Skip to main content

machine_crank_cam/
saw.rs

1//! A flying saw (or flying shear): a carriage that carries the saw along the material, driven by a pinion on a rack
2//! behind a gearbox. The joint is the carriage's travel x (m) along the material, from its end stop. The cam's master is
3//! the material, in metres, at `material.speed_m_s`; one cut length L of material per cut.
4//!
5//! The cam over one cut length, from the carriage waiting at `saw.home_m`:
6//! 1. synchronise: the carriage's speed rises from 0 to the material's while the material runs v^2 / a
7//!    (`saw.sync_acceleration_m_s2` = a, the mean acceleration). The speed blends as 3 u^2 - 2 u^3 (no jump in
8//!    acceleration, peak 1.5 a), so the carriage covers half of that, v^2 / (2 a): the synchronisation distance.
9//! 2. cut: the carriage runs with the material over the cut window W (`saw.cut_window_m`), for W / v seconds.
10//! 3. leave the material: the mirror of 1, again v^2 / (2 a) of carriage travel.
11//! 4. return: back by the whole stroke W + v^2 / a to home, by `saw.return_law` (VDI 2143), over the rest of the cut
12//!    length, L - 2 v^2 / a - W of material.
13//!
14//! The cut (effects.process_force) pushes on the carriage along the material with a table over the way into the window
15//! (`cut.position_m`, `cut.force_n`, positive along the material) while the carriage moves forwards.
16
17use crate::{Table, axis, forwards, gravity, machine, master, payload};
18use sim_components::cam::{Cam, Law, Segment};
19use sim_components::chain::Mechanism;
20use sim_components::joint::RotaryJoint;
21use sim_components::transmission::RackPinion;
22use sim_core::{CamMaster, Config, ConfigError, Label, Machine, Part, RatingCheck, Scene, Shape, SignalInfo};
23use std::f64::consts::TAU;
24
25pub struct FlyingSaw {
26    pub mass: f64,
27    pub payload: f64,
28    pub attached: bool,
29    pub home: f64,
30    /// The carriage's travel from home to the start of the window, v^2 / (2 a).
31    pub sync_distance: f64,
32    pub window: f64,
33    pub stroke: f64,
34    pub(crate) cut: Option<Table>,
35    pub cam: Cam,
36    pub master: Option<CamMaster>,
37    rack: RackPinion,
38    efficiency: f64,
39    /// Gravity, for the carriage's weight on a compliant base (`frame_momentum`).
40    g: f64,
41}
42
43/// The carriage's height in the scene (`scene`), m.
44const CARRIAGE_Z: f64 = 0.35;
45
46/// The flying saw's cam (the module's): material at `speed`, mean synchronising acceleration `acc`, cut window `window`,
47/// cut length `length`, `law` for the return. `None` where the cut length leaves no room for the return.
48pub fn saw_cam(speed: f64, acc: f64, window: f64, length: f64, law: Law) -> Option<Cam> {
49    let sync = speed * speed / acc;
50    let rest = length - 2.0 * sync - window;
51    if !(rest > 0.0) {
52        return None;
53    }
54    Cam::from_segments(&[
55        Segment { master: sync, slave: sync / 2.0, law: Law::Poly345 },
56        Segment { master: window, slave: window, law: Law::Linear },
57        Segment { master: sync, slave: sync / 2.0, law: Law::Poly345 },
58        Segment { master: rest, slave: -(window + sync), law },
59    ])
60    .ok()
61}
62
63pub fn build(cfg: &Config) -> Result<Box<dyn Machine>, ConfigError> {
64    let mut joint = RotaryJoint::linear_from_config(cfg)?;
65    let rack = RackPinion::from_config(cfg)?;
66    // The mesh friction's bristle capped by the lighter of the rotor and the carriage (`Converter::apply_to`).
67    let carriage = cfg.positive("mass.carriage_kg")?;
68    rack.converter().apply_to(cfg, &mut joint, carriage)?;
69    let lever = rack.radius();
70    let (speed, acc) = (cfg.positive("material.speed_m_s")?, cfg.positive("saw.sync_acceleration_m_s2")?);
71    let window = cfg.positive("saw.cut_window_m")?;
72    let law = cfg
73        .get("saw.return_law")?
74        .as_str()
75        .and_then(Law::from_name)
76        .filter(|l| !matches!(l, Law::Dwell | Law::Linear))
77        .ok_or_else(|| ConfigError::Invalid("saw.return_law".into()))?;
78    let cam = saw_cam(speed, acc, window, cfg.positive("saw.cut_length_m")?, law)
79        .ok_or_else(|| ConfigError::Invalid("saw.cut_length_m".into()))?;
80    let home = cfg.non_negative("saw.home_m")?;
81    let stroke = window + speed * speed / acc;
82    if home + stroke > joint.range.1 {
83        return Err(ConfigError::Invalid("saw.home_m".into()));
84    }
85    let saw = FlyingSaw {
86        mass: carriage,
87        payload: payload(cfg)?,
88        attached: true,
89        home,
90        sync_distance: speed * speed / (2.0 * acc),
91        window,
92        stroke,
93        cut: if cfg.bool("effects.process_force")? {
94            Some(Table::from_config(cfg, "cut.position_m", "cut.force_n")?)
95        } else {
96            None
97        },
98        master: master(cfg, speed, cam.period)?,
99        cam,
100        rack,
101        efficiency: joint.extra_efficiency,
102        g: gravity(cfg)?,
103    };
104    let x = axis(cfg, "x", "axis-x", "m", joint.range)?;
105    machine(cfg, saw, (joint, Some(lever), "m"), x, false)
106}
107
108impl FlyingSaw {
109    fn moving(&self) -> f64 {
110        self.mass + if self.attached { self.payload } else { 0.0 }
111    }
112    /// The cut's force on the carriage at its travel and speed, N, along the material.
113    pub fn cut_force(&self, x: f64, speed: f64) -> f64 {
114        let Some(table) = &self.cut else { return 0.0 };
115        table.at(x - self.home - self.sync_distance) * forwards(speed, 1e-3)
116    }
117}
118
119impl Mechanism for FlyingSaw {
120    /// In the scene's frame: the carriage with the saw (and the blade, the payload) along x at its height, the pinion's
121    /// rotor about y; the cut pushing the carriage along the material.
122    fn frame_momentum(&self, q: &[f64], qd: &[f64], rotor_spin: &[f64]) -> Option<sim_core::FrameMomentum> {
123        let at = [q[0], 0.0, CARRIAGE_Z];
124        let mut f = sim_core::FrameMomentum {
125            gravity: [0.0, 0.0, -self.g],
126            external: [self.cut_force(q[0], qd[0]), 0.0, 0.0],
127            external_at: at,
128            ..Default::default()
129        };
130        sim_components::base::add_point(&mut f, self.moving(), at, [qd[0], 0.0, 0.0]);
131        f.angular[1] += rotor_spin[0];
132        Some(f)
133    }
134    fn dynamics(&self, _q: &[f64], _qd: &[f64], mass: &mut [f64], bias: &mut [f64]) {
135        mass[0] = self.moving();
136        bias[0] = 0.0;
137    }
138    fn potential(&self, _q: &[f64]) -> f64 {
139        0.0
140    }
141    fn set_payload_attached(&mut self, attached: bool) {
142        self.attached = attached;
143    }
144    fn has_process(&self) -> bool {
145        self.cut.is_some()
146    }
147    fn process_torque(&self, q: &[f64], qd: &[f64], out: &mut [f64]) {
148        out[0] += self.cut_force(q[0], qd[0]);
149    }
150    fn cam(&self) -> Option<CamMaster> {
151        self.master
152    }
153    /// The cam from home.
154    fn cam_at(&self, master: f64, out: &mut [[f64; 4]]) {
155        out[0] = self.cam.at(master);
156        out[0][0] += self.home;
157    }
158    fn cam_curve(&self) -> Option<sim_core::CamCurve> {
159        let (cam, home) = (self.cam.clone(), self.home);
160        Some(std::sync::Arc::new(move |master, out: &mut [[f64; 4]]| {
161            out[0] = cam.at(master);
162            out[0][0] += home;
163        }))
164    }
165
166    fn signal_infos(&self) -> Vec<SignalInfo> {
167        vec![
168            self.rack.signal_info("1", "joint-1"),
169            SignalInfo::new("saw.cut_force", "group-process", "quantity-process-force", "N"),
170        ]
171    }
172    fn signals(&self, q: &[f64], qd: &[f64], _qdd: &[f64], force: &[f64], out: &mut Vec<f64>) {
173        out.push(RackPinion::feed_force(force[0], qd[0], self.efficiency));
174        out.push(self.cut_force(q[0], qd[0]));
175    }
176    fn rating_checks(&self) -> Vec<RatingCheck> {
177        self.rack.rating_checks("1", 0)
178    }
179
180    fn collision_parts(&self) -> &'static [&'static str] {
181        &["carriage", "payload"]
182    }
183
184    /// From the front: the material running past on its rollers, the carriage with the saw above it on the rack, the
185    /// window it cuts in marked on the frame.
186    fn scene(&self, q: &[f64], rotors: &[f64], rotor_speeds: &[f64], out: &mut Scene) {
187        let grey = [0.62, 0.64, 0.68];
188        let end = self.home + self.stroke + 0.3;
189        out.parts.push(Part::new(
190            "frame",
191            Shape::Segment { to: [end, 0.0, 0.25], width: 0.08 },
192            [-0.3, 0.0, 0.25],
193            grey,
194        ));
195        out.parts.push(Part::new(
196            "rack",
197            Shape::Segment { to: [end, -0.05, 0.3], width: 0.02 },
198            [-0.3, -0.05, 0.3],
199            [0.4, 0.4, 0.42],
200        ));
201        out.parts.push(Part::new(
202            "material",
203            Shape::Box { size: [end + 1.0, 0.2, 0.04] },
204            [end / 2.0, 0.0, 0.0],
205            [0.7, 0.7, 0.75],
206        ));
207        let start = self.home + self.sync_distance;
208        out.parts.push(Part::new(
209            "window",
210            Shape::Box { size: [self.window, 0.01, 0.01] },
211            [start + self.window / 2.0, -0.12, 0.25],
212            [0.85, 0.3, 0.2],
213        ));
214        out.parts.push(Part::new(
215            "carriage",
216            Shape::Box { size: [0.25, 0.25, 0.08] },
217            [q[0], 0.0, 0.35],
218            [0.2, 0.22, 0.26],
219        ));
220        if self.attached && self.payload > 0.0 {
221            out.parts.push(Part::new(
222                "payload",
223                Shape::Cylinder { radius: 0.12, length: 0.01 },
224                [q[0], 0.0, 0.15],
225                [0.85, 0.85, 0.9],
226            ));
227        }
228        out.parts.push(Part::new(
229            sim_core::scene::MOTOR_PART,
230            Shape::Cylinder { radius: 0.05, length: 0.12 },
231            [q[0], -0.2, 0.45],
232            [0.9, 0.45, 0.1],
233        ));
234        // The gearbox between motor and pinion: the part that warms (`JointMachine::thermal_parts`).
235        out.parts.push(Part::new(
236            "gearbox",
237            Shape::Box { size: [0.1, 0.06, 0.1] },
238            [q[0], -0.13, 0.45],
239            [0.25, 0.25, 0.28],
240        ));
241        out.parts.push(
242            Part::new("rotor-bar", Shape::Box { size: [0.07, 0.004, 0.012] }, [q[0], -0.27, 0.45], [0.1, 0.1, 0.1])
243                .turned(rotors[0]),
244        );
245        out.labels.push(Label {
246            pos: [q[0], -0.2, 0.6],
247            owner_key: "motor-joint-1",
248            value: rotor_speeds[0] * 60.0 / TAU,
249            unit_key: "unit-rpm",
250            decimals: 0,
251        });
252        out.labels.push(Label {
253            pos: [q[0], 0.0, 0.55],
254            owner_key: "axis-x",
255            value: q[0],
256            unit_key: "unit-m",
257            decimals: 3,
258        });
259    }
260    fn scene_point(&self, tool: &[f64]) -> Option<[f64; 3]> {
261        Some([tool[0], 0.0, 0.35])
262    }
263}