Skip to main content

machine_crank_cam/
knife.rs

1//! A rotary knife (cross cutter): a knife roll of radius R with `knife.knives` blades cuts a web that runs past it at
2//! `web.speed_m_s`. The joint is the roll's angle phi (rad), 0 with a blade at the cut. The cam's master is the web, in
3//! metres; one format length L of web per cut, one blade pitch 2 pi / n of the roll.
4//!
5//! The cam: in the synchronous zone, +- alpha (`knife.sync_angle_deg`) about each cut, the blade's tip runs with the web
6//! (slope 1 / R); between cuts a motion law (`knife.transition_law`, VDI 2143) takes the roll the rest of the pitch,
7//! 2 pi / n - 2 alpha, over the rest of the format, L - 2 R alpha. A format shorter than the roll's circumference over
8//! n makes it speed up between cuts, a longer one slow down: its mean speed ratio there is
9//! (2 pi / n - 2 alpha) / (L - 2 R alpha) radians per metre of web against 1 / R in the zone.
10//!
11//! The cut (effects.process_force) brakes the roll with a force at the blade's tip, a table over the angle from the
12//! cut (`cut.angle_deg`, `cut.force_n`), while the roll turns forwards.
13
14use crate::{Table, axis, forwards, machine, master, payload};
15use sim_components::cam::{Cam, Law, Segment};
16use sim_components::chain::Mechanism;
17use sim_components::joint::RotaryJoint;
18use sim_core::{CamMaster, Config, ConfigError, Label, Machine, Part, Scene, Shape, SignalInfo};
19use std::f64::consts::TAU;
20
21pub struct RotaryKnife {
22    pub radius: f64,
23    pub knives: f64,
24    pub inertia: f64,
25    /// Knife bars at the radius (the payload), kg.
26    pub bars: f64,
27    pub attached: bool,
28    pub(crate) cut: Option<Table>,
29    pub cam: Cam,
30    pub master: Option<CamMaster>,
31}
32
33/// The rotary knife's cam (the module's): over a format length `format` of web, `knives` blades on a roll of `radius`,
34/// synchronous over +- `sync` rad about each cut, `law` between. `None` where the format is no longer than the zone.
35pub fn knife_cam(radius: f64, knives: f64, sync: f64, format: f64, law: Law) -> Option<Cam> {
36    let zone = Segment { master: radius * sync, slave: sync, law: Law::Linear };
37    let rest = Segment { master: format - 2.0 * radius * sync, slave: TAU / knives - 2.0 * sync, law };
38    Cam::from_segments(&[zone, rest, zone]).ok()
39}
40
41pub fn build(cfg: &Config) -> Result<Box<dyn Machine>, ConfigError> {
42    let joint = RotaryJoint::from_config(cfg)?;
43    let (radius, knives) = (cfg.positive("knife.radius_m")?, cfg.positive("knife.knives")?);
44    let sync = cfg.positive("knife.sync_angle_deg")?.to_radians();
45    if knives.fract() != 0.0 || 2.0 * sync >= TAU / knives {
46        return Err(ConfigError::Invalid("knife.sync_angle_deg".into()));
47    }
48    let law = cfg
49        .get("knife.transition_law")?
50        .as_str()
51        .and_then(Law::from_name)
52        .filter(|l| !matches!(l, Law::Dwell | Law::Linear))
53        .ok_or_else(|| ConfigError::Invalid("knife.transition_law".into()))?;
54    let cam = knife_cam(radius, knives, sync, cfg.positive("knife.format_length_m")?, law)
55        .ok_or_else(|| ConfigError::Invalid("knife.format_length_m".into()))?;
56    let master = master(cfg, cfg.positive("web.speed_m_s")?, cam.period)?;
57    let knife = RotaryKnife {
58        radius,
59        knives,
60        inertia: cfg.positive("knife.inertia_kgm2")?,
61        bars: payload(cfg)?,
62        attached: true,
63        cut: if cfg.bool("effects.process_force")? {
64            Some(Table::from_config(cfg, "cut.angle_deg", "cut.force_n")?)
65        } else {
66            None
67        },
68        cam,
69        master,
70    };
71    let phi = axis(cfg, "knife", "axis-knife", "rad", joint.range)?;
72    machine(cfg, knife, (joint, None, "rad"), phi, true)
73}
74
75impl RotaryKnife {
76    /// The cut's force at the blade's tip at the roll's angle and speed, N.
77    pub fn cut_force(&self, phi: f64, speed: f64) -> f64 {
78        let Some(table) = &self.cut else { return 0.0 };
79        let pitch = TAU / self.knives;
80        let from_cut = phi - (phi / pitch).round() * pitch;
81        table.at(from_cut.to_degrees()) * forwards(speed, 0.05)
82    }
83    fn j(&self) -> f64 {
84        self.inertia + if self.attached { self.bars * self.radius * self.radius } else { 0.0 }
85    }
86}
87
88impl Mechanism for RotaryKnife {
89    /// In the scene's frame: the roll with its knife bars spinning about -y (their centre stays put), the rotor with it;
90    /// the cut braking the blade at the cut point under the roll, along -x there.
91    fn frame_momentum(&self, q: &[f64], qd: &[f64], rotor_spin: &[f64]) -> Option<sim_core::FrameMomentum> {
92        let mut f =
93            sim_core::FrameMomentum { external: [-self.cut_force(q[0], qd[0]), 0.0, 0.0], ..Default::default() };
94        sim_components::base::add_spin(&mut f, [0.0, -1.0, 0.0], self.j(), qd[0]);
95        f.angular[1] -= rotor_spin[0];
96        Some(f)
97    }
98    fn dynamics(&self, _q: &[f64], _qd: &[f64], mass: &mut [f64], bias: &mut [f64]) {
99        mass[0] = self.j();
100        bias[0] = 0.0;
101    }
102    fn potential(&self, _q: &[f64]) -> f64 {
103        0.0
104    }
105    fn set_payload_attached(&mut self, attached: bool) {
106        self.attached = attached;
107    }
108    fn has_process(&self) -> bool {
109        self.cut.is_some()
110    }
111    fn process_torque(&self, q: &[f64], qd: &[f64], out: &mut [f64]) {
112        out[0] -= self.cut_force(q[0], qd[0]) * self.radius;
113    }
114    fn cam(&self) -> Option<CamMaster> {
115        self.master
116    }
117    fn cam_at(&self, master: f64, out: &mut [[f64; 4]]) {
118        out[0] = self.cam.at(master);
119    }
120    fn cam_curve(&self) -> Option<sim_core::CamCurve> {
121        let cam = self.cam.clone();
122        Some(std::sync::Arc::new(move |master, out: &mut [[f64; 4]]| out[0] = cam.at(master)))
123    }
124
125    fn signal_infos(&self) -> Vec<SignalInfo> {
126        vec![
127            SignalInfo::new("knife.surface_speed", "group-knife", "quantity-surface-speed", "m/s"),
128            SignalInfo::new("knife.cut_force", "group-process", "quantity-process-force", "N"),
129        ]
130    }
131    fn signals(&self, q: &[f64], qd: &[f64], _qdd: &[f64], _force: &[f64], out: &mut Vec<f64>) {
132        out.extend([self.radius * qd[0], self.cut_force(q[0], qd[0])]);
133    }
134
135    /// The blades: at the roll's outer radius they meet an obstacle first, and they are boxes of the scene (the roll is a
136    /// cylinder, which no collision box stands for).
137    fn collision_parts(&self) -> &'static [&'static str] {
138        &["blade"]
139    }
140
141    /// Seen along the roll: the roll with its blades turning above the web, the anvil roll beneath, the motor behind.
142    fn scene(&self, q: &[f64], rotors: &[f64], rotor_speeds: &[f64], out: &mut Scene) {
143        let r = self.radius;
144        let centre = [0.0, 0.0, r];
145        out.parts.push(Part::new(
146            "web",
147            Shape::Box { size: [6.0 * r, 0.3, 0.004] },
148            [0.0, 0.0, 0.0],
149            [0.85, 0.82, 0.7],
150        ));
151        out.parts.push(Part::new(
152            "anvil",
153            Shape::Cylinder { radius: 0.8 * r, length: 0.3 },
154            [0.0, 0.0, -0.8 * r],
155            [0.45, 0.47, 0.5],
156        ));
157        out.parts.push(Part::new(
158            "knife-roll",
159            Shape::Cylinder { radius: 0.9 * r, length: 0.3 },
160            centre,
161            [0.55, 0.55, 0.6],
162        ));
163        for k in 0..self.knives as usize {
164            // Blade k points down (at the cut) where phi = k pitch: the scene's angle -pi/2 turned by phi.
165            let a = -std::f64::consts::FRAC_PI_2 + q[0] - k as f64 * TAU / self.knives;
166            out.parts.push(
167                Part::new(
168                    "blade",
169                    Shape::Box { size: [0.2 * r, 0.32, 0.01] },
170                    [0.9 * r * a.cos(), 0.0, r + 0.9 * r * a.sin()],
171                    [0.85, 0.85, 0.9],
172                )
173                .turned(a),
174            );
175        }
176        out.parts.push(Part::new(
177            sim_core::scene::MOTOR_PART,
178            Shape::Cylinder { radius: 0.05, length: 0.12 },
179            [0.0, 0.25, r],
180            [0.9, 0.45, 0.1],
181        ));
182        out.parts.push(Part::new("gearbox", Shape::Box { size: [0.1, 0.06, 0.1] }, [0.0, 0.18, r], [0.25, 0.25, 0.28]));
183        out.parts.push(
184            Part::new("rotor-bar", Shape::Box { size: [0.07, 0.004, 0.012] }, [0.0, 0.32, r], [0.1, 0.1, 0.1])
185                .turned(rotors[0]),
186        );
187        out.labels.push(Label {
188            pos: [0.0, 0.25, 2.2 * r],
189            owner_key: "motor-joint-1",
190            value: rotor_speeds[0] * 60.0 / TAU,
191            unit_key: "unit-rpm",
192            decimals: 0,
193        });
194        out.labels.push(Label {
195            pos: [1.5 * r, 0.0, 2.0 * r],
196            owner_key: "axis-knife",
197            value: q[0].to_degrees(),
198            unit_key: "unit-deg",
199            decimals: 1,
200        });
201    }
202}