Skip to main content

sim_netlist/
worm.rs

1//! A worm stage (`machine.worm`): what it passes on, driven forward by the motor and back by the load.
2//!
3//! A gearbox passes the torque t of its spring on as t eta to its output and t / i to its rotor (`mech::Gearbox`), and
4//! eta fades from 1 at rest to its value within a power t w of 0.5 W at the output. A worm that turns back (back
5//! efficiency above 0) does the same while the motor drives (with its forward efficiency), and while the load drives it
6//! back passes t eta_back^-1 (as the gearbox, with its back efficiency): `factors`.
7//!
8//! A worm whose back efficiency is 0 or less locks itself: the wheel cannot turn the worm. It follows Coulomb's law at
9//! its mesh, without a fade, in both models. A screw thread of lead angle g and friction angle p, sliding at the sign s
10//! of its speed, puts into the mesh (at the wheel, i times the worm's torque) what the wheel takes, o, and a drag
11//! D = into - o of s times
12//!
13//! - (1 / eta - 1) s o on the driving flank (s o > 0, eta = tan g / tan(g + p)), and
14//! - (1 - eta_back) (-s o) on the overhauling one (s o < 0, eta_back = tan(g - p) / tan g, below 0 where g < p),
15//!
16//! so the mesh only ever takes power (D w >= 0); while the load drives it back the motor has to push, by -eta_back of
17//! the load's torque over i, and the load's power and the motor's both go into the friction. At rest the friction holds
18//! anything between the two flanks' levels (`levels`), which take in 0 for a locking worm: it holds its load with no
19//! motor torque.
20//!
21//! The elastic model (`ConfigMachine::worm_drag`) has o as its gear spring's torque, a state: the wheel gets the
22//! gearbox's output torque as it is, and the rotor the flank's drag besides, sliding at the rotor's speed. Below
23//! `STICK_SPEED` it sticks (Karnopp), the drag taking out the rotor's speed over `STICK_TIME_S` within the levels and never
24//! along the motion: a locking worm at rest stands.
25//!
26//! The rigid model (`ConfigMachine::rigid_locking`) has o as an unknown beside the accelerations. The overhauling flank
27//! there has no rigid solution with a heavy wheel (Painleve's paradox): with the rotor's inertia at the wheel
28//! R = i^2 J and the wheel's J_L, the wheel's equation J_L a = o + L and the rotor's into = i t - R a give
29//! o (1 + R / J_L) + D(o) = i t - R L / J_L, whose left side falls with o on that flank where R / J_L < -eta_back: two
30//! solutions or none (a stiff mesh there is unstable and jams into stick-slip). So the overhauling flank takes its load
31//! from the torque the wheel holds at rest against the axes' forces, o_rest, not from o (`overhauling_drag`): the
32//! same at a steady speed, a known drag otherwise. The drag is the larger of the two flanks' (each where above 0),
33//! which joins them continuously and rises with o, so the solve has exactly one answer. A worm moving with no motor
34//! torque under a load that drives it back brakes by (1 - eta_back) o_rest > o_rest and stops; at rest, and below
35//! `STICK_SPEED`, it sticks (`rigid_locking`).
36
37use crate::netlist::WormNode;
38
39/// The power at the output over which the efficiency fades in, W: the gearbox's (`mech::Gearbox::loss_factor`).
40const FADE_W: f64 = 0.5;
41
42/// (factor on the torque at the output, factor on the torque at the rotor) of a worm that turns back, at the power
43/// `power` = t w at the output (positive while the motor drives).
44pub fn factors(worm: &WormNode, power: f64) -> (f64, f64) {
45    let x = (power / FADE_W).tanh();
46    ((if x >= 0.0 { worm.efficiency } else { worm.back_efficiency }).powf(x), 1.0)
47}
48
49/// The levels of a self-locking worm's friction at its rotor while the gear's torque at the rotor is `rotor` (t / i):
50/// (sliding forward, sliding backward), both at least 0. Forward the flank that `rotor` loads is the driving one where
51/// it is above 0, (1 / eta - 1) rotor, the overhauling one where below, (1 - eta_back) (-rotor); backward the other way.
52pub fn levels(worm: &WormNode, rotor: f64) -> (f64, f64) {
53    let (driving, overhauling) = (1.0 / worm.efficiency - 1.0, 1.0 - worm.back_efficiency);
54    (
55        driving * rotor.max(0.0) + overhauling * (-rotor).max(0.0),
56        driving * (-rotor).max(0.0) + overhauling * rotor.max(0.0),
57    )
58}
59
60/// Below this speed of its wheel, rad/s, a self-locking worm sticks where its friction can hold
61/// (Karnopp's band): Coulomb's drag flips with the speed's sign, so a fixed step never lands on 0 but settles a step's
62/// worth of acceleration short of it (1.6e-5 rad/s at 1 rad/s2 on the 15.6 us sub-step), booking a drag its averaged
63/// motion does not have. Far below a commanded speed, far above that.
64pub const STICK_SPEED: f64 = 1e-3;
65
66/// The time over which a stuck worm's wheel takes out a speed inside the band, s: it decelerates at -w / this, so that
67/// it comes to rest in place of creeping on; far above the sub-step (RK4 stays stable up to 2.8 of it).
68pub const STICK_TIME_S: f64 = 1e-3;
69
70/// What a self-locking worm moving at `speed` (the wheel's, not 0) drags on its overhauling flank, at the wheel and
71/// against the motion: (1 - eta_back) times the torque `held` that the wheel holds at rest, where the load drives it
72/// (s held < 0, s the sign of the speed), 0 where the load is lifted (see the module comment).
73pub fn overhauling_drag(worm: &WormNode, held: f64, speed: f64) -> f64 {
74    (1.0 - worm.back_efficiency) * (-speed.signum() * held).max(0.0)
75}
76
77/// The worm's efficiencies `loss_factor` times as lossy as at the reference temperature: 1 - eta becomes
78/// (1 - eta) f both ways, the gearbox's law (`mech::ThermalFeedback::gearbox_efficiency`) applied to the worm's own
79/// efficiencies. Forward not below 0.01, as the gearbox's; a self-locking worm stays locked (back efficiency at most 0)
80/// and one that turns back keeps turning back (above 0, at least 0.01).
81pub fn warmed(worm: &WormNode, loss_factor: f64) -> WormNode {
82    let scaled = |eta: f64| 1.0 - (1.0 - eta) * loss_factor;
83    let back = scaled(worm.back_efficiency);
84    WormNode {
85        efficiency: scaled(worm.efficiency).clamp(0.01, 1.0),
86        back_efficiency: if worm.back_efficiency > 0.0 { back.clamp(0.01, 1.0) } else { back.min(0.0) },
87        ..*worm
88    }
89}
90
91#[cfg(test)]
92mod tests {
93    use super::*;
94
95    /// Far from rest the factors are the efficiencies: forward 0.8 at the output; back 1 / 0.5 = 2 at the output of a
96    /// worm that turns back; at rest it passes t to both.
97    #[test]
98    fn a_worm_passes_its_efficiencies() {
99        let turning = WormNode { ratio: 40.0, efficiency: 0.8, back_efficiency: 0.5 };
100        assert_eq!(factors(&turning, 100.0), (0.8, 1.0));
101        assert_eq!(factors(&turning, -100.0), (2.0, 1.0));
102        assert_eq!(factors(&turning, 0.0), (1.0, 1.0));
103    }
104
105    /// A locking worm (eta 0.4, eta_back -0.2) with 10 N m on its wheel behind a ratio of 40, 0.25 N m at the rotor:
106    /// sliding forward lifts it, (1 / 0.4 - 1) 0.25 = 0.375 N m; backward lowers it, (1 + 0.2) 0.25 = 0.3 N m, so the
107    /// motor pushes 0.3 - 0.25 = 0.05 N m; at rest it holds anything from -0.3 to 0.375, the load's 0.25 among it. With
108    /// the load the other way the levels swap.
109    #[test]
110    fn a_locking_worm_slides_at_its_flanks_levels() {
111        let locking = WormNode { ratio: 40.0, efficiency: 0.4, back_efficiency: -0.2 };
112        let close = |(a, b): (f64, f64), (c, d): (f64, f64)| assert!((a - c).abs() < 1e-12 && (b - d).abs() < 1e-12);
113        close(levels(&locking, 0.25), (0.375, 0.3));
114        close(levels(&locking, -0.25), (0.3, 0.375));
115        close(levels(&locking, 0.0), (0.0, 0.0));
116    }
117    /// Holding 10 N m at rest, the locking worm (eta_back = -0.2) lowering it (speed -1 against the held +10) drags
118    /// (1 + 0.2) 10 = 12 N m; lifting it (speed +1) the overhauling flank carries nothing.
119    #[test]
120    fn a_locking_worm_drags_on_its_overhauling_flank_by_what_it_holds() {
121        let locking = WormNode { ratio: 40.0, efficiency: 0.4, back_efficiency: -0.2 };
122        assert!((overhauling_drag(&locking, 10.0, -1.0) - 12.0).abs() < 1e-12);
123        assert!((overhauling_drag(&locking, -10.0, 1.0) - 12.0).abs() < 1e-12);
124        assert_eq!(overhauling_drag(&locking, 10.0, 1.0), 0.0);
125    }
126
127    /// Warmed so that it loses half as much (f = 0.5): 1 - (1 - 0.8) 0.5 = 0.9 forward, 1 - 0.5 0.5 = 0.75 back; a
128    /// locking worm's (eta 0.4, back -0.2) back efficiency becomes 1 - 1.2 0.5 = 0.4, which would turn back, so it stays
129    /// at 0; cold (f = 2) it locks harder, 1 - 1.2 2 = -1.4, and forward 1 - 0.6 2 = -0.2, held at the floor 0.01.
130    #[test]
131    fn a_warm_worm_loses_less_and_a_locking_one_stays_locked() {
132        let turning = WormNode { ratio: 40.0, efficiency: 0.8, back_efficiency: 0.5 };
133        let locking = WormNode { efficiency: 0.4, back_efficiency: -0.2, ..turning };
134        let close = |a: f64, b: f64| assert!((a - b).abs() < 1e-12, "{a} against {b}");
135        let w = warmed(&turning, 0.5);
136        close(w.efficiency, 0.9);
137        close(w.back_efficiency, 0.75);
138        close(warmed(&locking, 0.5).back_efficiency, 0.0);
139        let cold = warmed(&locking, 2.0);
140        close(cold.efficiency, 0.01);
141        close(cold.back_efficiency, -1.4);
142        assert_eq!(warmed(&turning, 1.0), turning);
143    }
144}