From 644cc523cf2873660fa656602e94a922370a5116 Mon Sep 17 00:00:00 2001
From: Andy <andy@bodgesoc.org>
Date: Sun, 23 Jan 2011 22:23:06 +0000
Subject: [PATCH] If something claims to be backwards compatible, it should at least use the
 same arithmetic.

Signed-off-by: Andy <andy@bodgesoc.org>
---
 src/hal/components/gearchange.comp |   41 +++++++++++++++++++++---------------
 1 files changed, 24 insertions(+), 17 deletions(-)

diff --git a/src/hal/components/gearchange.comp b/src/hal/components/gearchange.comp
index 2ca7250..09968c5 100644
--- a/src/hal/components/gearchange.comp
+++ b/src/hal/components/gearchange.comp
@@ -1,5 +1,5 @@
-component gearchange """Calculate the correct motor speed setpoint for a 
-machine with multiple gear ratios.""";
+component gearchange """Adjust motor speed command for geared spindles.
+Scales the input value to the output depending on selected gear.""";
 
 pin in float speed-in "Speed command input.";
 pin out float speed-out "Speed command to DAC/PWM";
@@ -14,11 +14,11 @@ pin in unsigned gear-number "Gear selection as a numeric";
 pin out float speed-filtered "Speed command to display, limited and absolute";
 pin out float accel-limit "Acceleration limit for the selected gear";
 param rw float min#[32: personality+1] = 0 
-    "Minimum allowed speed in gear range N";
+"Minimum allowed speed in gear range N";
 param rw float max#[32 : personality+1] = 100000 
-    "Maximum allowed speed in gear range N";
+"Maximum allowed speed in gear range N";
 param rw float acc#[32 : personality+1] = 0 
-    "Maximum Acceleration in rpm/s. 0 = no limit";
+"Maximum Acceleration in rpm/s. 0 = no limit";
 param rw float scale#[32 : personality+1] = 1.0 "Ratio in gear range 1";
 param rw bit reverse "negates the value of scale2, for compatibility";
 
@@ -34,19 +34,26 @@ loadrt gearchange count=2 personality=5,6
 For a single instance you can omit the count, eg with 11 gears you can use:
 loadrt gearchange personality=11
 
-It accepts negative speeds, negative outputs and negative scales to allow for 
-gear ranges that reverse the motion.
+By default the number of gears is set to 3, so that gear1 and gear2 exist for
+compatibility with earlier (2 gear) versions of the component. (There will also 
+be a surplus gear0 in that case)
 
-The scale is the ratio between input speed and output speed, so the same VFD 
-scale parameter will work for any gear. The output value is that motor speed 
-which would be required to give the requested output speed with a gear ratio of
-1. If gear 1 is the slowest gear then other ratios will probably need to be 
-fractional. If the highest numbered gear is the slowest then the scales will 
-probably need to be greater than one. 
+The Component accepts negative speeds, negative outputs and negative scales to 
+allow for gear ranges that reverse the motion.
+
+The scale is the ratio between output speed and input speed, so the same VFD 
+scale parameter (typically set by the PWM scale parameter) will work for any 
+gear. 
+The output value is that motor speed which would be required to give the 
+requested output speed with a gear ratio of 1 at the set PWM scale. For example 
+if gear 1 has a max speed of 1000rpm and requires 10V to the VFD to achieve 
+that, and gear 2 has a max speed of 4000rpm then gearchange.N.ratio2 should be 
+set to 4. Then a spindle speed of 1000rpm would give 10V output in gear 1 and 
+2.5V in gear 2. 
 
 The gear can be input either as a numeric value (possibly assembled from 
 \\fBweighted_sum\\fR components to convert the positions of several levers into 
-a gear) or as an individual select bit for each gear. """;
+a gear) or as an individual select bit for each gear.""";
 
 function _;
 license "GPL";
@@ -70,12 +77,12 @@ FUNCTION(_) {
         speed_filtered = min(gear);}
     else { min_lim = 0; max_lim = 0; }
     
-    speed_target =  speed_filtered * ((scale(gear) != 0)? scale(gear) : 1)
+    speed_target =  speed_filtered / ((scale(gear) != 0)? scale(gear) : 1)
     *((speed_in < 0)? -1 : 1) ;
     delta = acc(gear) * fperiod * ((speed_target > old_speed)? 1 : -1);
     speed_out = 
-        ((acc(gear) != 0 && fabs(speed_target - old_speed) > fabs(delta))?
-        (old_speed += delta) : (old_speed = speed_target));
+    ((acc(gear) != 0 && fabs(speed_target - old_speed) > fabs(delta))?
+     (old_speed += delta) : (old_speed = speed_target));
     
     dir_out = ((scale(gear) < 0)? !dir_in : dir_in);
     dir_out = ((reverse && gear == 2)? !dir_out : dir_out); // Backwards compat
-- 
1.7.0.4

