Arnaud VALLEY / Mbed 2 deprecated Pinscape_Controller_V2_arnoz

Dependencies:   mbed FastIO FastPWM USBDevice

Committer:
mjr
Date:
Fri Apr 22 17:58:35 2016 +0000
Revision:
53:9b2611964afc
Parent:
52:8298b2a73eb2
Save some debugging instrumentation to be removed for release

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mjr 17:ab3cec0c8bf4 1 // Potentiometer plunger sensor
mjr 17:ab3cec0c8bf4 2 //
mjr 17:ab3cec0c8bf4 3 // This file implements our generic plunger sensor interface for a
mjr 43:7a6364d82a41 4 // potentiometer. The potentiometer resistance must be linear in
mjr 43:7a6364d82a41 5 // position. To connect physically, wire the fixed ends of the
mjr 43:7a6364d82a41 6 // potentiometer to +3.3V and GND (respectively), and connect the
mjr 43:7a6364d82a41 7 // wiper to an ADC-capable GPIO pin on the KL25Z. The wiper voltage
mjr 43:7a6364d82a41 8 // that we read on the ADC will vary linearly with the wiper position.
mjr 43:7a6364d82a41 9 // Mechanically attach the wiper to the plunger so that the wiper moves
mjr 43:7a6364d82a41 10 // in lock step with the plunger.
mjr 43:7a6364d82a41 11 //
mjr 43:7a6364d82a41 12 // Although this class is nominally for potentiometers, it will also
mjr 43:7a6364d82a41 13 // work with any other type of sensor that provides a single analog
mjr 43:7a6364d82a41 14 // voltage level that maps linearly to the position, such as an LVDT.
mjr 17:ab3cec0c8bf4 15
mjr 17:ab3cec0c8bf4 16
mjr 35:e959ffba78fd 17 class PlungerSensorPot: public PlungerSensor
mjr 17:ab3cec0c8bf4 18 {
mjr 17:ab3cec0c8bf4 19 public:
mjr 35:e959ffba78fd 20 PlungerSensorPot(PinName ao) : pot(ao)
mjr 17:ab3cec0c8bf4 21 {
mjr 48:058ace2aed1d 22 // start our sample timer with an arbitrary zero point of now
mjr 48:058ace2aed1d 23 timer.start();
mjr 53:9b2611964afc 24 totScanTime = 0;
mjr 53:9b2611964afc 25 nScans = 0;
mjr 17:ab3cec0c8bf4 26 }
mjr 17:ab3cec0c8bf4 27
mjr 35:e959ffba78fd 28 virtual void init()
mjr 17:ab3cec0c8bf4 29 {
mjr 17:ab3cec0c8bf4 30 }
mjr 17:ab3cec0c8bf4 31
mjr 48:058ace2aed1d 32 // read the sensor
mjr 48:058ace2aed1d 33 virtual bool read(PlungerReading &r)
mjr 17:ab3cec0c8bf4 34 {
mjr 48:058ace2aed1d 35 // get the starting time of the sampling
mjr 48:058ace2aed1d 36 uint32_t t0 = timer.read_us();
mjr 48:058ace2aed1d 37
mjr 23:14f8c5004cd0 38 // Take a few readings and use the average, to reduce the effect
mjr 23:14f8c5004cd0 39 // of analog voltage fluctuations. The voltage range on the ADC
mjr 23:14f8c5004cd0 40 // is 0-3.3V, and empirically it looks like we can expect random
mjr 23:14f8c5004cd0 41 // voltage fluctuations of up to 50 mV, which is about 1.5% of
mjr 23:14f8c5004cd0 42 // the overall range. We try to quantize at about the mm level
mjr 23:14f8c5004cd0 43 // (in terms of the plunger motion range), which is about 1%.
mjr 23:14f8c5004cd0 44 // So 1.5% noise is big enough to be visible in the joystick
mjr 23:14f8c5004cd0 45 // reports. Averaging several readings should help smooth out
mjr 23:14f8c5004cd0 46 // random noise in the readings.
mjr 44:b5ac89b9cd5d 47 //
mjr 44:b5ac89b9cd5d 48 // Readings through the standard AnalogIn class take about 30us
mjr 47:df7a88cd249c 49 // each, so taking 5 readings takes about 150us. This is fast
mjr 47:df7a88cd249c 50 // enough to resolve even the fastest plunger motiono with no
mjr 47:df7a88cd249c 51 // aliasing.
mjr 48:058ace2aed1d 52 r.pos = uint16_t((
mjr 47:df7a88cd249c 53 uint32_t(pot.read_u16())
mjr 47:df7a88cd249c 54 + uint32_t(pot.read_u16())
mjr 47:df7a88cd249c 55 + uint32_t(pot.read_u16())
mjr 47:df7a88cd249c 56 + uint32_t(pot.read_u16())
mjr 47:df7a88cd249c 57 + uint32_t(pot.read_u16())
mjr 47:df7a88cd249c 58 ) / 5U);
mjr 48:058ace2aed1d 59
mjr 52:8298b2a73eb2 60 // Get the elapsed time of the sample, and figure the indicated
mjr 48:058ace2aed1d 61 // sample time as the midpoint between the start and end times.
mjr 48:058ace2aed1d 62 // (Note that the timer might overflow the uint32_t between t0
mjr 48:058ace2aed1d 63 // and now, in which case it will appear that now < t0. The
mjr 48:058ace2aed1d 64 // calculation will always work out right anyway, because it's
mjr 48:058ace2aed1d 65 // effectively performed mod 2^32-1.)
mjr 52:8298b2a73eb2 66 uint32_t dt = timer.read_us() - t0;
mjr 52:8298b2a73eb2 67 r.t = t0 + dt/2;
mjr 52:8298b2a73eb2 68
mjr 52:8298b2a73eb2 69 // add the current sample to our timing statistics
mjr 52:8298b2a73eb2 70 totScanTime += dt;
mjr 52:8298b2a73eb2 71 nScans += 1;
mjr 48:058ace2aed1d 72
mjr 48:058ace2aed1d 73 // success
mjr 17:ab3cec0c8bf4 74 return true;
mjr 17:ab3cec0c8bf4 75 }
mjr 52:8298b2a73eb2 76
mjr 52:8298b2a73eb2 77 // figure the average scan time in microseconds
mjr 53:9b2611964afc 78 virtual uint32_t getAvgScanTime()
mjr 53:9b2611964afc 79 {
mjr 53:9b2611964afc 80 return nScans != 0 ? uint32_t(totScanTime/nScans) : 0;
mjr 53:9b2611964afc 81 }
mjr 23:14f8c5004cd0 82
mjr 17:ab3cec0c8bf4 83 private:
mjr 48:058ace2aed1d 84 // analog input for the pot wiper
mjr 23:14f8c5004cd0 85 AnalogIn pot;
mjr 48:058ace2aed1d 86
mjr 48:058ace2aed1d 87 // timer for input timestamps
mjr 48:058ace2aed1d 88 Timer timer;
mjr 52:8298b2a73eb2 89
mjr 52:8298b2a73eb2 90 // total sensor scan time in microseconds, and number of scans completed
mjr 53:9b2611964afc 91 uint64_t totScanTime;
mjr 52:8298b2a73eb2 92 int nScans;
mjr 17:ab3cec0c8bf4 93 };