Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: USBDevice mbed FastAnalogIn FastIO FastPWM SimpleDMA
potSensor.h@43:7a6364d82a41, 2016-02-06 (annotated)
- Committer:
- mjr
- Date:
- Sat Feb 06 20:21:48 2016 +0000
- Revision:
- 43:7a6364d82a41
- Parent:
- 35:e959ffba78fd
- Child:
- 44:b5ac89b9cd5d
Before floating point plunger ranging
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 | 35:e959ffba78fd | 20 | PlungerSensorPot(PinName ao) : pot(ao) |
mjr | 17:ab3cec0c8bf4 | 21 | { |
mjr | 17:ab3cec0c8bf4 | 22 | } |
mjr | 17:ab3cec0c8bf4 | 23 | |
mjr | 35:e959ffba78fd | 24 | virtual void init() |
mjr | 17:ab3cec0c8bf4 | 25 | { |
mjr | 35:e959ffba78fd | 26 | // The potentiometer doesn't have pixels, but we still need an |
mjr | 35:e959ffba78fd | 27 | // integer range for normalizing our digitized voltage level values. |
mjr | 35:e959ffba78fd | 28 | // The number here is fairly arbitrary; the higher it is, the finer |
mjr | 35:e959ffba78fd | 29 | // the digitized steps. A 40" 1080p HDTV has about 55 pixels per inch |
mjr | 35:e959ffba78fd | 30 | // on its physical display, so if the on-screen plunger is displayed |
mjr | 35:e959ffba78fd | 31 | // at roughly the true physical size, it's about 3" on screen or about |
mjr | 35:e959ffba78fd | 32 | // 165 pixels. So the minimum quantization size here should be about |
mjr | 35:e959ffba78fd | 33 | // the same. For the pot sensor, this is just a scaling factor, |
mjr | 35:e959ffba78fd | 34 | // so higher values don't cost us anything (unlike the CCD, where the |
mjr | 35:e959ffba78fd | 35 | // read time is proportional to the number of pixels we sample). |
mjr | 35:e959ffba78fd | 36 | npix = 4096; |
mjr | 17:ab3cec0c8bf4 | 37 | } |
mjr | 17:ab3cec0c8bf4 | 38 | |
mjr | 35:e959ffba78fd | 39 | virtual bool highResScan(int &pos) |
mjr | 17:ab3cec0c8bf4 | 40 | { |
mjr | 23:14f8c5004cd0 | 41 | // Take a few readings and use the average, to reduce the effect |
mjr | 23:14f8c5004cd0 | 42 | // of analog voltage fluctuations. The voltage range on the ADC |
mjr | 23:14f8c5004cd0 | 43 | // is 0-3.3V, and empirically it looks like we can expect random |
mjr | 23:14f8c5004cd0 | 44 | // voltage fluctuations of up to 50 mV, which is about 1.5% of |
mjr | 23:14f8c5004cd0 | 45 | // the overall range. We try to quantize at about the mm level |
mjr | 23:14f8c5004cd0 | 46 | // (in terms of the plunger motion range), which is about 1%. |
mjr | 23:14f8c5004cd0 | 47 | // So 1.5% noise is big enough to be visible in the joystick |
mjr | 23:14f8c5004cd0 | 48 | // reports. Averaging several readings should help smooth out |
mjr | 23:14f8c5004cd0 | 49 | // random noise in the readings. |
mjr | 23:14f8c5004cd0 | 50 | pos = int((pot.read() + pot.read() + pot.read())/3.0 * npix); |
mjr | 17:ab3cec0c8bf4 | 51 | return true; |
mjr | 17:ab3cec0c8bf4 | 52 | } |
mjr | 17:ab3cec0c8bf4 | 53 | |
mjr | 35:e959ffba78fd | 54 | virtual bool lowResScan(int &pos) |
mjr | 23:14f8c5004cd0 | 55 | { |
mjr | 23:14f8c5004cd0 | 56 | // Use an average of several readings. Note that even though this |
mjr | 23:14f8c5004cd0 | 57 | // is nominally a "low res" scan, we can still afford to take an |
mjr | 43:7a6364d82a41 | 58 | // average. The point of the low res interface is to speed things |
mjr | 43:7a6364d82a41 | 59 | // up for the image sensor types, which have a large number of |
mjr | 43:7a6364d82a41 | 60 | // analog samples to read. In our case, we only have the one |
mjr | 43:7a6364d82a41 | 61 | // input to sample, so our normal scan is already so fast that |
mjr | 43:7a6364d82a41 | 62 | // there's no need to do anything different here. |
mjr | 35:e959ffba78fd | 63 | pos = int((pot.read() + pot.read() + pot.read())/3.0 * npix); |
mjr | 35:e959ffba78fd | 64 | return true; |
mjr | 23:14f8c5004cd0 | 65 | } |
mjr | 23:14f8c5004cd0 | 66 | |
mjr | 17:ab3cec0c8bf4 | 67 | private: |
mjr | 23:14f8c5004cd0 | 68 | AnalogIn pot; |
mjr | 17:ab3cec0c8bf4 | 69 | }; |