An input/output controller for virtual pinball machines, with plunger position tracking, accelerometer-based nudge sensing, button input encoding, and feedback device control.

Dependencies:   USBDevice mbed FastAnalogIn FastIO FastPWM SimpleDMA

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers potSensor.h Source File

potSensor.h

00001 // Potentiometer plunger sensor
00002 //
00003 // This file implements our generic plunger sensor interface for a
00004 // potentiometer.
00005 
00006 #include "FastAnalogIn.h"
00007 
00008 // The potentiometer doesn't have pixels, but we still need an
00009 // integer range for normalizing our digitized voltage level values.
00010 // The number here is fairly arbitrary; the higher it is, the finer
00011 // the digitized steps.  A 40" 1080p HDTV has about 55 pixels per inch
00012 // on its physical display, so if the on-screen plunger is displayed
00013 // at roughly the true physical size, it's about 3" on screen or about
00014 // 165 pixels.  So the minimum quantization size here should be about
00015 // the same.  For the pot sensor, this is just a scaling factor, 
00016 // so higher values don't cost us anything (unlike the CCD, where the
00017 // read time is proportional to the number of pixels we sample).
00018 const int npix = 4096;
00019 
00020 class PlungerSensor
00021 {
00022 public:
00023     PlungerSensor() : pot(POT_PIN)
00024     {
00025     }
00026     
00027     void init() 
00028     {
00029     }
00030     
00031     bool highResScan(int &pos)
00032     {
00033         // Take a few readings and use the average, to reduce the effect
00034         // of analog voltage fluctuations.  The voltage range on the ADC
00035         // is 0-3.3V, and empirically it looks like we can expect random
00036         // voltage fluctuations of up to 50 mV, which is about 1.5% of
00037         // the overall range.  We try to quantize at about the mm level
00038         // (in terms of the plunger motion range), which is about 1%.
00039         // So 1.5% noise is big enough to be visible in the joystick
00040         // reports.  Averaging several readings should help smooth out
00041         // random noise in the readings.
00042         pos = int((pot.read() + pot.read() + pot.read())/3.0 * npix);
00043         return true;
00044     }
00045     
00046     int lowResScan()
00047     {
00048         // Use an average of several readings.  Note that even though this
00049         // is nominally a "low res" scan, we can still afford to take an
00050         // average.  The point of the low res interface is speed, and since
00051         // we only have one analog value to read, we can afford to take
00052         // several samples here even in the low res case.
00053         return int((pot.read() + pot.read() + pot.read())/3.0 * npix);
00054     }
00055         
00056     void sendExposureReport(USBJoystick &) 
00057     { 
00058     }
00059     
00060 private:
00061     AnalogIn pot;
00062 };