An I/O controller for virtual pinball machines: accelerometer nudge sensing, analog plunger input, button input encoding, LedWiz compatible output controls, and more.

Dependencies:   mbed FastIO FastPWM USBDevice

Fork of Pinscape_Controller by Mike R

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers distanceSensor.h Source File

distanceSensor.h

00001 // Plunger sensor type for distance sensors.
00002 //
00003 // This type of sensor measures the distance to a target by sending
00004 // optical or sound signals and watching for the reflection.  There are
00005 // many types of these sensors, including sensors that measure the 
00006 // intensity of reflected sound or light signals, sensors that measure
00007 // the round-trip time of "pings", and sensors that measure optical
00008 // parallax.
00009 //
00010 // The basic installation for this type of sensor involves placing the
00011 // sensor itself in a fixed location at one end of the plunger, pointing
00012 // down the length of the plunger, and placing a reflective target at
00013 // the end of the plunger.  The target can simply be an ordinary plunger
00014 // tip, if the sensor is at the far end of the plunger facing forward
00015 // (facing the front of the cabinet).  Alternatively, the target can
00016 // be a disk or similar object attached to the end of the plunger, and
00017 // the sensor can be placed at the front of the machine facing the target.
00018 // In either case, the sensor measures the distance to the target at any
00019 // given time, and we interpret that into the plunger position.
00020 //
00021 // Here are the specific sensor types we currently support:
00022 //
00023 // VL6180X: This is an optical (IR) "time of flight" sensor that measures
00024 // the distance to the target by sending optical pings and timing the 
00025 // return signal, converting the result to distance via the known speed 
00026 // of light.  This sensor has nominal 1mm precision, although its true
00027 // precision in testing is closer to 5mm.  Sample times are around 16ms.
00028 // This makes the sensor acceptable but not great by Pinscape standards;
00029 // we generally consider 2.5ms read times and .25mm precision to be the
00030 // minimum standards.  However, this sensor is inexpensive and easier to
00031 // set up than most of the better options, so it might be attractive to
00032 // some cab builders despite the quality tradeoffs.
00033 
00034 #ifndef _DISTANCESENSOR_H_
00035 #define _DISTANCESENSOR_H_
00036 
00037 #include "plunger.h"
00038 #include "VL6180X.h"
00039 
00040 // Base class for distance sensors
00041 class PlungerSensorDistance: public PlungerSensor
00042 {
00043 public:
00044     PlungerSensorDistance(int nativeScale) : PlungerSensor(nativeScale)
00045     {
00046         totalTime = 0;
00047         nRuns = 0;
00048     }
00049 
00050     // get the average scan time
00051     virtual uint32_t getAvgScanTime() { return uint32_t(totalTime / nRuns); }
00052 
00053 protected:
00054     // collect scan time statistics
00055     void collectScanTimeStats(uint32_t dt)
00056     {
00057         totalTime += dt;
00058         nRuns += 1;
00059     }
00060 
00061     // scan time statistics
00062     uint64_t totalTime;       // total time consumed by all reads so far
00063     uint32_t nRuns;           // number of runs so far
00064 };
00065 
00066 // PlungerSensor interface implementation for VL6180X sensors.  
00067 //
00068 // The VL6180X reports distances in millimeter quanta, so the native
00069 // sensor units are millimeters.  A physical plunger has about 3" of
00070 // total travel, but leave a little extra padding for measurement
00071 // inaccuracies and other unusual situations, so'll use an actual
00072 // native scale of 150mm.
00073 class PlungerSensorVL6180X: public PlungerSensorDistance
00074 {
00075 public:
00076     PlungerSensorVL6180X(PinName sda, PinName scl, PinName gpio0)
00077         : PlungerSensorDistance(150),
00078           sensor(sda, scl, I2C_ADDRESS, gpio0, true)
00079     {
00080     }
00081     
00082     // fixed I2C bus address for the VL6180X
00083     static const int I2C_ADDRESS = 0x29;
00084     
00085     virtual void init()
00086     {
00087         // initialize the sensor and set the default configuration
00088         sensor.init();
00089         sensor.setDefaults();
00090         
00091         // start a reading
00092         sensor.startRangeReading();
00093     }
00094     
00095     virtual bool ready()
00096     {
00097         // make sure a reading has been initiated
00098         sensor.startRangeReading();
00099         
00100         // check if a reading is ready
00101         return sensor.rangeReady();
00102     }
00103     
00104     virtual bool readRaw(PlungerReading &r)
00105     {
00106         // if we have a new reading ready, collect it
00107         if (sensor.rangeReady())
00108         {
00109             // Get the range reading.  Note that we already know that the
00110             // sensor has a reading ready, so it shouldn't be possible to 
00111             // time out on the read.  (The sensor could have timed out on 
00112             // convergence, but if it did, that's in the past already so 
00113             // it's not something we have to wait for now.)
00114             uint8_t d;
00115             uint32_t t, dt;
00116             lastErr = sensor.getRange(d, t, dt, 100);
00117             
00118             // if we got a reading, update the last reading
00119             if (lastErr == 0)
00120             {
00121                 // save the new reading
00122                 last.pos = d;
00123                 last.t = t;
00124             
00125                 // collect scan time statistics
00126                 collectScanTimeStats(dt);
00127             }
00128     
00129             // start a new reading
00130             sensor.startRangeReading();
00131         }
00132         
00133         // return the most recent reading
00134         r = last;
00135         return lastErr == 0;
00136     }
00137     
00138 protected:
00139     // underlying sensor interface
00140     VL6180X sensor;
00141     
00142     // last reading and error status
00143     PlungerReading last;
00144     int lastErr;
00145 };
00146 
00147 
00148 #endif