Pinscape Controller version 1 fork. This is a fork to allow for ongoing bug fixes to the original controller version, from before the major changes for the expansion board project.

Dependencies:   FastIO FastPWM SimpleDMA mbed

Fork of Pinscape_Controller by Mike R

Files at this revision

API Documentation at this revision

Comitter:
mjr
Date:
Wed Jun 03 18:50:34 2015 +0000
Parent:
22:71422c359f2a
Child:
24:e902bc7cdc1e
Commit message:
Use regular AnalogIn (not FastAnalogIn) for potentiometer readings, and take the average of several readings, to reduce noise; add a minimum distance threshold before a firing event when crossing the rest position.

Changed in this revision

config.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
potSensor.h Show annotated file Show diff for this revision Revisions of this file
--- a/config.h	Wed Apr 01 22:57:31 2015 +0000
+++ b/config.h	Wed Jun 03 18:50:34 2015 +0000
@@ -87,7 +87,7 @@
 // If you're NOT using the CCD sensor, comment out the next line (by adding
 // two slashes at the start of the line).
 
-#define ENABLE_CCD_SENSOR
+//#define ENABLE_CCD_SENSOR
 
 // The KL25Z pins that the CCD sensor is physically attached to:
 //
@@ -112,21 +112,30 @@
 //
 // Plunger potentiometer sensor.
 //
-// If you ARE using a potentiometer as the plunger sensor, un-comment the
-// next line (by removing the two slashes at the start of the line), and be
-// sure to comment out the ENABLE_CCD_SENSOR line above.
+// If you're using a potentiometer as the plunger sensor, un-comment the
+// next line (by removing the two slashes at the start of the line), and 
+// also comment out the ENABLE_CCD_SENSOR line above.
 
-//#define ENABLE_POT_SENSOR
+#define ENABLE_POT_SENSOR
 
-// The KL25Z pin that your potentiometer is attached to.  Wire the end of
-// the potentiometer at the retracted end of the plunger to the 3.3V output
-// from the KL25Z.  Wire the variable output from the potentiometer to the
-// gpio pin below.  This must be an AnalogIn capable pin - only a few of the
-// KL25Z gpio pins qualify, so check the pinout diagram to find a suitable
-// candidate if you need to change this for any reason.  Note that we use
-// the same analog input that the CCD sensor would use if it were enabled,
-// which is why you have to be sure to disable the CCD code if you're using
-// this.
+// The KL25Z pin that your potentiometer is attached to.  The potentiometer
+// requires wiring three connectins:
+//
+// - Wire the fixed resistance end of the potentiometer nearest the KNOB 
+//   end of the plunger to the 3.3V output from the KL25Z
+//
+// - Wire the other fixed resistance end to KL25Z Ground
+//
+// -  Wire the potentiometer wiper (the variable output terminal) to the 
+//    KL25Z pin identified below.  
+//
+// Note that you can change the pin selection below, but if you do, the new
+// pin must be AnalogIn capable.  Only a few of the KL25Z pins qualify.  Refer
+// to the KL25Z pinout diagram to find another AnalogIn pin if you need to
+// change this for any reason.  Note that the default is to use the same analog 
+// input that the CCD sensor would use if it were enabled, which is why you 
+// have to be sure to disable the CCD support in the software if you're using 
+// a potentiometer as the sensor.
 
 const PinName POT_PIN = PTB0;
 
--- a/main.cpp	Wed Apr 01 22:57:31 2015 +0000
+++ b/main.cpp	Wed Jun 03 18:50:34 2015 +0000
@@ -1489,7 +1489,7 @@
                     //
                     // Note that negative values are allowed.  Zero represents the
                     // "park" position, where the plunger sits when at rest.  A mechanical 
-                    // plunger has a smmall amount of travel in the "push" direction,
+                    // plunger has a small amount of travel in the "push" direction,
                     // since the barrel spring can be compressed slightly.  Negative
                     // values represent travel in the push direction.
                     if (pos > cfg.d.plungerMax)
@@ -1511,25 +1511,44 @@
                 // The plunger has moved forward since the previous report.
                 // Watch it for a few more ms to see if we can get a stable
                 // new position.
-                int pos1 = plungerSensor.lowResScan();
+                int pos0 = plungerSensor.lowResScan();
+                int pos1 = pos0;
                 Timer tw;
                 tw.start();
                 while (tw.read_ms() < 6)
                 {
-                    // if we've crossed the rest position, it's a firing event
-                    if (pos1 < cfg.d.plungerZero)
+                    // read the new position
+                    int pos2 = plungerSensor.lowResScan();
+                    
+                    // If it's stable over consecutive readings, stop looping.
+                    // (Count it as stable if the position is within about 1/8".
+                    // pos1 and pos2 are reported in pixels, so they range from
+                    // 0 to npix.  The overall travel of a standard plunger is
+                    // about 3.2", so we have (npix/3.2) pixels per inch, hence
+                    // 1/8" is (npix/3.2)*(1/8) pixels.)
+                    if (abs(pos2 - pos1) < int(npix/(3.2*8)))
+                        break;
+
+                    // If we've crossed the rest position, and we've moved by
+                    // a minimum distance from where we starting this loop, begin
+                    // a firing event.  (We require a minimum distance to prevent
+                    // spurious firing from random analog noise in the readings
+                    // when the plunger is actually just sitting still at the 
+                    // rest position.  If it's at rest, it's normal to see small
+                    // random fluctuations in the analog reading +/- 1% or so
+                    // from the 0 point, especially with a sensor like a
+                    // potentionemeter that reports the position as a single 
+                    // analog voltage.)  Note that we compare the latest reading
+                    // to the first reading of the loop - we don't require the
+                    // threshold motion over consecutive readings, but any time
+                    // over the stability wait loop.
+                    if (pos1 < cfg.d.plungerZero
+                        && abs(pos2 - pos0) > int(npix/(3.2*8)))
                     {
                         firing = 1;
                         break;
                     }
-                    
-                    // read the new position
-                    int pos2 = plungerSensor.lowResScan();
-                    
-                    // if it's stable, stop looping
-                    if (abs(pos2 - pos1) < int(npix/(3.2*8)))
-                        break;
-                        
+                                            
                     // the new reading is now the prior reading
                     pos1 = pos2;
                 }
--- a/potSensor.h	Wed Apr 01 22:57:31 2015 +0000
+++ b/potSensor.h	Wed Jun 03 18:50:34 2015 +0000
@@ -22,28 +22,41 @@
 public:
     PlungerSensor() : pot(POT_PIN)
     {
-        pot.enable();
     }
     
     void init() 
     {
     }
     
-    int lowResScan()
-    {
-        return int(pot.read() * npix);
-    }
-        
     bool highResScan(int &pos)
     {
-        pos = int(pot.read() * npix);
+        // Take a few readings and use the average, to reduce the effect
+        // of analog voltage fluctuations.  The voltage range on the ADC
+        // is 0-3.3V, and empirically it looks like we can expect random
+        // voltage fluctuations of up to 50 mV, which is about 1.5% of
+        // the overall range.  We try to quantize at about the mm level
+        // (in terms of the plunger motion range), which is about 1%.
+        // So 1.5% noise is big enough to be visible in the joystick
+        // reports.  Averaging several readings should help smooth out
+        // random noise in the readings.
+        pos = int((pot.read() + pot.read() + pot.read())/3.0 * npix);
         return true;
     }
     
+    int lowResScan()
+    {
+        // Use an average of several readings.  Note that even though this
+        // is nominally a "low res" scan, we can still afford to take an
+        // average.  The point of the low res interface is speed, and since
+        // we only have one analog value to read, we can afford to take
+        // several samples here even in the low res case.
+        return int((pot.read() + pot.read() + pot.read())/3.0 * npix);
+    }
+        
     void sendExposureReport(USBJoystick &) 
     { 
     }
     
 private:
-    FastAnalogIn pot;
+    AnalogIn pot;
 };