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: mbed FastIO FastPWM USBDevice
Fork of Pinscape_Controller by
plunger.h@48:058ace2aed1d, 2016-02-26 (annotated)
- Committer:
- mjr
- Date:
- Fri Feb 26 18:42:03 2016 +0000
- Revision:
- 48:058ace2aed1d
- Parent:
- 47:df7a88cd249c
- Child:
- 52:8298b2a73eb2
New plunger processing 1
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| mjr | 35:e959ffba78fd | 1 | // Plunger Sensor Interface |
| mjr | 35:e959ffba78fd | 2 | // |
| mjr | 35:e959ffba78fd | 3 | // This module defines the abstract interface to the plunger sensors. |
| mjr | 35:e959ffba78fd | 4 | // We support several different physical sensor types, so we need a |
| mjr | 35:e959ffba78fd | 5 | // common interface for use in the main code. |
| mjr | 35:e959ffba78fd | 6 | |
| mjr | 35:e959ffba78fd | 7 | #ifndef PLUNGER_H |
| mjr | 35:e959ffba78fd | 8 | #define PLUNGER_H |
| mjr | 35:e959ffba78fd | 9 | |
| mjr | 48:058ace2aed1d | 10 | // Plunger reading with timestamp |
| mjr | 48:058ace2aed1d | 11 | struct PlungerReading |
| mjr | 48:058ace2aed1d | 12 | { |
| mjr | 48:058ace2aed1d | 13 | // Raw sensor reading, normalied to 0x0000..0xFFFF range |
| mjr | 48:058ace2aed1d | 14 | int pos; |
| mjr | 48:058ace2aed1d | 15 | |
| mjr | 48:058ace2aed1d | 16 | // Rimestamp of reading, in microseconds, relative to an arbitrary |
| mjr | 48:058ace2aed1d | 17 | // zero point. Note that a 32-bit int can only represent about 71.5 |
| mjr | 48:058ace2aed1d | 18 | // minutes worth of microseconds, so this value is only meaningful |
| mjr | 48:058ace2aed1d | 19 | // to compute a delta from other recent readings. As long as two |
| mjr | 48:058ace2aed1d | 20 | // readings are within 71.5 minutes of each other, the time difference |
| mjr | 48:058ace2aed1d | 21 | // calculated from the timestamps using 32-bit math will be correct |
| mjr | 48:058ace2aed1d | 22 | // *even if a rollover occurs* between the two readings, since the |
| mjr | 48:058ace2aed1d | 23 | // calculation is done mod 2^32-1. |
| mjr | 48:058ace2aed1d | 24 | uint32_t t; |
| mjr | 48:058ace2aed1d | 25 | }; |
| mjr | 48:058ace2aed1d | 26 | |
| mjr | 35:e959ffba78fd | 27 | class PlungerSensor |
| mjr | 35:e959ffba78fd | 28 | { |
| mjr | 35:e959ffba78fd | 29 | public: |
| mjr | 48:058ace2aed1d | 30 | PlungerSensor() { } |
| mjr | 35:e959ffba78fd | 31 | |
| mjr | 48:058ace2aed1d | 32 | // ---------- Abstract sensor interface ---------- |
| mjr | 35:e959ffba78fd | 33 | |
| mjr | 35:e959ffba78fd | 34 | // Initialize the physical sensor device. This is called at startup |
| mjr | 35:e959ffba78fd | 35 | // to set up the device for first use. |
| mjr | 35:e959ffba78fd | 36 | virtual void init() = 0; |
| mjr | 35:e959ffba78fd | 37 | |
| mjr | 48:058ace2aed1d | 38 | // Read the sensor position, if possible. Returns true on success, |
| mjr | 48:058ace2aed1d | 39 | // false if it wasn't possible to take a reading. On success, fills |
| mjr | 48:058ace2aed1d | 40 | // in 'r' with the current reading. |
| mjr | 48:058ace2aed1d | 41 | // |
| mjr | 48:058ace2aed1d | 42 | // r.pos is set to the current raw sensor reading, normalized to the |
| mjr | 48:058ace2aed1d | 43 | // range 0x0000..0xFFFF. r.t is set to the timestamp of the reading, |
| mjr | 48:058ace2aed1d | 44 | // in |
| mjr | 48:058ace2aed1d | 45 | // |
| mjr | 48:058ace2aed1d | 46 | // Also sets 't' to the microsecond timestamp of the reading, if a |
| mjr | 48:058ace2aed1d | 47 | // reading was successfully taken. This timestamp is relative to an |
| mjr | 48:058ace2aed1d | 48 | // arbitrary zero point and rolls over when it overflows its 32-bit |
| mjr | 48:058ace2aed1d | 49 | // container (every 71.58 minutes). Callers can use this to calculate |
| mjr | 48:058ace2aed1d | 50 | // the interval between two readings (e.g., to figure the average |
| mjr | 48:058ace2aed1d | 51 | // velocity of the plunger between readings). |
| mjr | 35:e959ffba78fd | 52 | // |
| mjr | 47:df7a88cd249c | 53 | // Timing requirements: for best results, readings should be taken |
| mjr | 47:df7a88cd249c | 54 | // in well under 5ms. There are two factors that go into this limit. |
| mjr | 47:df7a88cd249c | 55 | // The first is the joystick report rate in the main loop. We want |
| mjr | 47:df7a88cd249c | 56 | // to send those as fast as possible to avoid any perceptible control |
| mjr | 47:df7a88cd249c | 57 | // input lag in the pinball emulation. "As fast as possible" is about |
| mjr | 47:df7a88cd249c | 58 | // every 10ms - that's VP's approximate sampling rate, so any faster |
| mjr | 47:df7a88cd249c | 59 | // wouldn't do any good, and could even slow things down by adding CPU |
| mjr | 47:df7a88cd249c | 60 | // load in the Windows drivers handling the extra messages. The second |
| mjr | 47:df7a88cd249c | 61 | // is the speed of the plunger motion. During release events, the |
| mjr | 47:df7a88cd249c | 62 | // plunger moves in a sinusoidal pattern (back and forth) as it reaches |
| mjr | 47:df7a88cd249c | 63 | // the extreme of its travel and bounces back off the spring. To |
| mjr | 47:df7a88cd249c | 64 | // resolve this kind of cyclical motion accurately, we have to take |
| mjr | 47:df7a88cd249c | 65 | // samples much faster than the cycle period - otherwise we encounter |
| mjr | 47:df7a88cd249c | 66 | // an effect known as aliasing, where we mistake a bounce for a small |
| mjr | 47:df7a88cd249c | 67 | // forward motion. Tests with real plungers indicate that the bounce |
| mjr | 47:df7a88cd249c | 68 | // period is on the order of 10ms, so ideally we'd like to take |
| mjr | 47:df7a88cd249c | 69 | // samples much faster than that. |
| mjr | 35:e959ffba78fd | 70 | // |
| mjr | 47:df7a88cd249c | 71 | // Returns true on success, false on failure. Returning false means |
| mjr | 47:df7a88cd249c | 72 | // that it wasn't possible to take a valid reading. |
| mjr | 48:058ace2aed1d | 73 | virtual bool read(PlungerReading &r) = 0; |
| mjr | 47:df7a88cd249c | 74 | |
| mjr | 47:df7a88cd249c | 75 | // Send an exposure report to the host, via the joystick interface. This |
| mjr | 47:df7a88cd249c | 76 | // is for image sensors, and can be omitted by other sensor types. For |
| mjr | 35:e959ffba78fd | 77 | // image sensors, this takes one exposure and sends all pixels to the host |
| mjr | 47:df7a88cd249c | 78 | // through special joystick reports. This is used by tools on the host PC |
| mjr | 47:df7a88cd249c | 79 | // to let the user view the low-level sensor pixel data, which can be |
| mjr | 47:df7a88cd249c | 80 | // helpful during installation to adjust the sensor positioning and light |
| mjr | 47:df7a88cd249c | 81 | // source. |
| mjr | 45:c42166b2878c | 82 | // |
| mjr | 48:058ace2aed1d | 83 | // Flag bits: |
| mjr | 48:058ace2aed1d | 84 | // 0x01 -> low res scan (default is high res scan) |
| mjr | 48:058ace2aed1d | 85 | // |
| mjr | 48:058ace2aed1d | 86 | // Visualization modes: |
| mjr | 48:058ace2aed1d | 87 | // 0 -> raw pixels |
| mjr | 48:058ace2aed1d | 88 | // 1 -> processed pixels (noise reduction, etc) |
| mjr | 48:058ace2aed1d | 89 | // 2 -> exaggerated contrast mode |
| mjr | 48:058ace2aed1d | 90 | // 3 -> edge visualization |
| mjr | 45:c42166b2878c | 91 | // |
| mjr | 45:c42166b2878c | 92 | // If processed mode is selected, the sensor should apply any pixel |
| mjr | 45:c42166b2878c | 93 | // processing it normally does when taking a plunger position reading, |
| mjr | 45:c42166b2878c | 94 | // such as exposure correction, noise reduction, etc. In raw mode, we |
| mjr | 45:c42166b2878c | 95 | // simply send the pixels as read from the sensor. Both modes are useful |
| mjr | 45:c42166b2878c | 96 | // in setting up the physical sensor. |
| mjr | 48:058ace2aed1d | 97 | virtual void sendExposureReport(class USBJoystick &js, uint8_t flags, uint8_t visMode) { } |
| mjr | 48:058ace2aed1d | 98 | |
| mjr | 48:058ace2aed1d | 99 | protected: |
| mjr | 35:e959ffba78fd | 100 | }; |
| mjr | 35:e959ffba78fd | 101 | |
| mjr | 35:e959ffba78fd | 102 | #endif /* PLUNGER_H */ |
