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@44:b5ac89b9cd5d, 2016-02-07 (annotated)
- Committer:
- mjr
- Date:
- Sun Feb 07 03:07:11 2016 +0000
- Revision:
- 44:b5ac89b9cd5d
- Parent:
- 43:7a6364d82a41
Work in progress on CCD speed-ups
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 | 17:ab3cec0c8bf4 | 26 | } |
mjr | 17:ab3cec0c8bf4 | 27 | |
mjr | 44:b5ac89b9cd5d | 28 | virtual bool highResScan(float &pos) |
mjr | 17:ab3cec0c8bf4 | 29 | { |
mjr | 23:14f8c5004cd0 | 30 | // Take a few readings and use the average, to reduce the effect |
mjr | 23:14f8c5004cd0 | 31 | // of analog voltage fluctuations. The voltage range on the ADC |
mjr | 23:14f8c5004cd0 | 32 | // is 0-3.3V, and empirically it looks like we can expect random |
mjr | 23:14f8c5004cd0 | 33 | // voltage fluctuations of up to 50 mV, which is about 1.5% of |
mjr | 23:14f8c5004cd0 | 34 | // the overall range. We try to quantize at about the mm level |
mjr | 23:14f8c5004cd0 | 35 | // (in terms of the plunger motion range), which is about 1%. |
mjr | 23:14f8c5004cd0 | 36 | // So 1.5% noise is big enough to be visible in the joystick |
mjr | 23:14f8c5004cd0 | 37 | // reports. Averaging several readings should help smooth out |
mjr | 23:14f8c5004cd0 | 38 | // random noise in the readings. |
mjr | 44:b5ac89b9cd5d | 39 | // |
mjr | 44:b5ac89b9cd5d | 40 | // Readings through the standard AnalogIn class take about 30us |
mjr | 44:b5ac89b9cd5d | 41 | // each, so 5 readings is about 150us. This is plenty fast enough |
mjr | 44:b5ac89b9cd5d | 42 | // for even a low-res scan. |
mjr | 44:b5ac89b9cd5d | 43 | pos = (pot.read() + pot.read() + pot.read() + pot.read() + pot.read())/5.0; |
mjr | 17:ab3cec0c8bf4 | 44 | return true; |
mjr | 17:ab3cec0c8bf4 | 45 | } |
mjr | 17:ab3cec0c8bf4 | 46 | |
mjr | 44:b5ac89b9cd5d | 47 | virtual bool lowResScan(float &pos) |
mjr | 23:14f8c5004cd0 | 48 | { |
mjr | 44:b5ac89b9cd5d | 49 | // Since we have only one analog input to sample, our read time is |
mjr | 44:b5ac89b9cd5d | 50 | // very fast compared to the image sensor alternatives, so there's no |
mjr | 44:b5ac89b9cd5d | 51 | // need to do anything different for a faster low-res scan. Simply |
mjr | 44:b5ac89b9cd5d | 52 | // take a normal high-res reading. |
mjr | 44:b5ac89b9cd5d | 53 | return highResScan(pos); |
mjr | 23:14f8c5004cd0 | 54 | } |
mjr | 23:14f8c5004cd0 | 55 | |
mjr | 17:ab3cec0c8bf4 | 56 | private: |
mjr | 23:14f8c5004cd0 | 57 | AnalogIn pot; |
mjr | 17:ab3cec0c8bf4 | 58 | }; |