Mirror with some correction
Dependencies: mbed FastIO FastPWM USBDevice
Plunger/potSensor.h@86:e30a1f60f783, 2017-04-21 (annotated)
- Committer:
- mjr
- Date:
- Fri Apr 21 18:50:37 2017 +0000
- Revision:
- 86:e30a1f60f783
- Parent:
- 82:4f6209cb5c33
- Child:
- 100:1ff35c07217c
Capture a bunch of alternative bar code decoder tests, mostly unsuccessful
Who changed what in which revision?
User | Revision | Line number | New 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 | 86:e30a1f60f783 | 20 | // Our native readings are taken as 16-bit ADC samples, so |
mjr | 86:e30a1f60f783 | 21 | // our native scale is an unsigned 16-bit int, 0..65535. |
mjr | 86:e30a1f60f783 | 22 | PlungerSensorPot(PinName ao) : PlungerSensor(65535), pot(ao) |
mjr | 17:ab3cec0c8bf4 | 23 | { |
mjr | 48:058ace2aed1d | 24 | // start our sample timer with an arbitrary zero point of now |
mjr | 48:058ace2aed1d | 25 | timer.start(); |
mjr | 53:9b2611964afc | 26 | totScanTime = 0; |
mjr | 53:9b2611964afc | 27 | nScans = 0; |
mjr | 17:ab3cec0c8bf4 | 28 | } |
mjr | 17:ab3cec0c8bf4 | 29 | |
mjr | 35:e959ffba78fd | 30 | virtual void init() |
mjr | 17:ab3cec0c8bf4 | 31 | { |
mjr | 17:ab3cec0c8bf4 | 32 | } |
mjr | 17:ab3cec0c8bf4 | 33 | |
mjr | 48:058ace2aed1d | 34 | // read the sensor |
mjr | 86:e30a1f60f783 | 35 | virtual bool readRaw(PlungerReading &r) |
mjr | 17:ab3cec0c8bf4 | 36 | { |
mjr | 48:058ace2aed1d | 37 | // get the starting time of the sampling |
mjr | 48:058ace2aed1d | 38 | uint32_t t0 = timer.read_us(); |
mjr | 48:058ace2aed1d | 39 | |
mjr | 23:14f8c5004cd0 | 40 | // Take a few readings and use the average, to reduce the effect |
mjr | 23:14f8c5004cd0 | 41 | // of analog voltage fluctuations. The voltage range on the ADC |
mjr | 23:14f8c5004cd0 | 42 | // is 0-3.3V, and empirically it looks like we can expect random |
mjr | 23:14f8c5004cd0 | 43 | // voltage fluctuations of up to 50 mV, which is about 1.5% of |
mjr | 23:14f8c5004cd0 | 44 | // the overall range. We try to quantize at about the mm level |
mjr | 23:14f8c5004cd0 | 45 | // (in terms of the plunger motion range), which is about 1%. |
mjr | 23:14f8c5004cd0 | 46 | // So 1.5% noise is big enough to be visible in the joystick |
mjr | 23:14f8c5004cd0 | 47 | // reports. Averaging several readings should help smooth out |
mjr | 23:14f8c5004cd0 | 48 | // random noise in the readings. |
mjr | 44:b5ac89b9cd5d | 49 | // |
mjr | 44:b5ac89b9cd5d | 50 | // Readings through the standard AnalogIn class take about 30us |
mjr | 47:df7a88cd249c | 51 | // each, so taking 5 readings takes about 150us. This is fast |
mjr | 47:df7a88cd249c | 52 | // enough to resolve even the fastest plunger motiono with no |
mjr | 47:df7a88cd249c | 53 | // aliasing. |
mjr | 48:058ace2aed1d | 54 | r.pos = uint16_t(( |
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 | + uint32_t(pot.read_u16()) |
mjr | 47:df7a88cd249c | 59 | + uint32_t(pot.read_u16()) |
mjr | 47:df7a88cd249c | 60 | ) / 5U); |
mjr | 48:058ace2aed1d | 61 | |
mjr | 52:8298b2a73eb2 | 62 | // Get the elapsed time of the sample, and figure the indicated |
mjr | 48:058ace2aed1d | 63 | // sample time as the midpoint between the start and end times. |
mjr | 48:058ace2aed1d | 64 | // (Note that the timer might overflow the uint32_t between t0 |
mjr | 48:058ace2aed1d | 65 | // and now, in which case it will appear that now < t0. The |
mjr | 48:058ace2aed1d | 66 | // calculation will always work out right anyway, because it's |
mjr | 48:058ace2aed1d | 67 | // effectively performed mod 2^32-1.) |
mjr | 52:8298b2a73eb2 | 68 | uint32_t dt = timer.read_us() - t0; |
mjr | 52:8298b2a73eb2 | 69 | r.t = t0 + dt/2; |
mjr | 52:8298b2a73eb2 | 70 | |
mjr | 52:8298b2a73eb2 | 71 | // add the current sample to our timing statistics |
mjr | 52:8298b2a73eb2 | 72 | totScanTime += dt; |
mjr | 52:8298b2a73eb2 | 73 | nScans += 1; |
mjr | 48:058ace2aed1d | 74 | |
mjr | 48:058ace2aed1d | 75 | // success |
mjr | 17:ab3cec0c8bf4 | 76 | return true; |
mjr | 17:ab3cec0c8bf4 | 77 | } |
mjr | 52:8298b2a73eb2 | 78 | |
mjr | 52:8298b2a73eb2 | 79 | // figure the average scan time in microseconds |
mjr | 53:9b2611964afc | 80 | virtual uint32_t getAvgScanTime() |
mjr | 53:9b2611964afc | 81 | { |
mjr | 53:9b2611964afc | 82 | return nScans != 0 ? uint32_t(totScanTime/nScans) : 0; |
mjr | 53:9b2611964afc | 83 | } |
mjr | 23:14f8c5004cd0 | 84 | |
mjr | 17:ab3cec0c8bf4 | 85 | private: |
mjr | 48:058ace2aed1d | 86 | // analog input for the pot wiper |
mjr | 23:14f8c5004cd0 | 87 | AnalogIn pot; |
mjr | 48:058ace2aed1d | 88 | |
mjr | 48:058ace2aed1d | 89 | // timer for input timestamps |
mjr | 48:058ace2aed1d | 90 | Timer timer; |
mjr | 52:8298b2a73eb2 | 91 | |
mjr | 52:8298b2a73eb2 | 92 | // total sensor scan time in microseconds, and number of scans completed |
mjr | 53:9b2611964afc | 93 | uint64_t totScanTime; |
mjr | 52:8298b2a73eb2 | 94 | int nScans; |
mjr | 17:ab3cec0c8bf4 | 95 | }; |