Use a piezoelectric sensor as a force detecting endstop switch. Analog input is smoothed using a moving average filter. Connect the piezo sensor parallel with a 1-10 MOhm resistor and 10-47nF capacitor to the analog input.
main.cpp@2:c2ed639b8034, 2016-03-08 (annotated)
- Committer:
- droelf25
- Date:
- Tue Mar 08 20:02:22 2016 +0000
- Revision:
- 2:c2ed639b8034
- Parent:
- 1:81b6cfb6b112
.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
droelf25 | 0:09e2c587fd7b | 1 | #include "mbed.h" |
droelf25 | 0:09e2c587fd7b | 2 | |
droelf25 | 1:81b6cfb6b112 | 3 | #define TRIGGER_VOLTAGE_MV 1450 |
droelf25 | 2:c2ed639b8034 | 4 | #define USE_HYSTERESIS //comment this out to use a debounce delay instead of hysteresis |
droelf25 | 1:81b6cfb6b112 | 5 | #define HYSTERESIS_TRESHOLD_MV 500 //Sensor voltage has to drop below this value to enable next signal. Should prevent multiple triggers caused by voltage fluctuations |
droelf25 | 0:09e2c587fd7b | 6 | #define FILTER_SAMPLES 5 |
droelf25 | 0:09e2c587fd7b | 7 | #define SIGNAL_LENGTH_MS 10 |
droelf25 | 1:81b6cfb6b112 | 8 | #define DEBOUNCE_WAIT_MS 300 //only used when hysteresis is not enabled. Wait a fixed amount of milliseconds to prevent multiple triggers |
droelf25 | 0:09e2c587fd7b | 9 | |
droelf25 | 0:09e2c587fd7b | 10 | AnalogIn sensor(A0); |
droelf25 | 0:09e2c587fd7b | 11 | DigitalOut led(LED1); |
droelf25 | 1:81b6cfb6b112 | 12 | DigitalOut signal(D2); |
droelf25 | 1:81b6cfb6b112 | 13 | //InterruptIn user(USER_BUTTON); //no user button on Nucleo F303K8 |
droelf25 | 0:09e2c587fd7b | 14 | |
droelf25 | 0:09e2c587fd7b | 15 | short buffer[FILTER_SAMPLES]; |
droelf25 | 0:09e2c587fd7b | 16 | short bufferPos= 0; |
droelf25 | 0:09e2c587fd7b | 17 | |
droelf25 | 0:09e2c587fd7b | 18 | short flashCounter = 0; |
droelf25 | 0:09e2c587fd7b | 19 | |
droelf25 | 0:09e2c587fd7b | 20 | bool isInitialized = false; |
droelf25 | 0:09e2c587fd7b | 21 | bool isReady = true; |
droelf25 | 1:81b6cfb6b112 | 22 | bool hysteresis = false; |
droelf25 | 0:09e2c587fd7b | 23 | |
droelf25 | 0:09e2c587fd7b | 24 | Ticker ledFlasher; |
droelf25 | 0:09e2c587fd7b | 25 | Timeout signalTimeout; |
droelf25 | 0:09e2c587fd7b | 26 | Timeout debounceTimeout; |
droelf25 | 0:09e2c587fd7b | 27 | |
droelf25 | 0:09e2c587fd7b | 28 | void flashLED() |
droelf25 | 0:09e2c587fd7b | 29 | { |
droelf25 | 0:09e2c587fd7b | 30 | if(flashCounter++ > 30) |
droelf25 | 0:09e2c587fd7b | 31 | ledFlasher.detach(); |
droelf25 | 0:09e2c587fd7b | 32 | |
droelf25 | 0:09e2c587fd7b | 33 | led = !led; |
droelf25 | 0:09e2c587fd7b | 34 | } |
droelf25 | 0:09e2c587fd7b | 35 | |
droelf25 | 0:09e2c587fd7b | 36 | void reset() |
droelf25 | 0:09e2c587fd7b | 37 | { |
droelf25 | 0:09e2c587fd7b | 38 | isReady = true; |
droelf25 | 0:09e2c587fd7b | 39 | } |
droelf25 | 0:09e2c587fd7b | 40 | |
droelf25 | 0:09e2c587fd7b | 41 | void endSignal() |
droelf25 | 0:09e2c587fd7b | 42 | { |
droelf25 | 0:09e2c587fd7b | 43 | led = 0; |
droelf25 | 0:09e2c587fd7b | 44 | signal = 0; |
droelf25 | 0:09e2c587fd7b | 45 | } |
droelf25 | 0:09e2c587fd7b | 46 | |
droelf25 | 0:09e2c587fd7b | 47 | void startSignal() |
droelf25 | 0:09e2c587fd7b | 48 | { |
droelf25 | 0:09e2c587fd7b | 49 | led = 1; |
droelf25 | 0:09e2c587fd7b | 50 | signal = 1; |
droelf25 | 0:09e2c587fd7b | 51 | signalTimeout.attach_us(&endSignal, 1000 * SIGNAL_LENGTH_MS); |
droelf25 | 1:81b6cfb6b112 | 52 | #ifndef USE_HYSTERESIS |
droelf25 | 1:81b6cfb6b112 | 53 | debounceTimeout.attach_us(&reset, 1000 * DEBOUNCE_WAIT_MS); |
droelf25 | 1:81b6cfb6b112 | 54 | #endif |
droelf25 | 0:09e2c587fd7b | 55 | } |
droelf25 | 0:09e2c587fd7b | 56 | |
droelf25 | 0:09e2c587fd7b | 57 | short getAverageVoltage() |
droelf25 | 0:09e2c587fd7b | 58 | { |
droelf25 | 0:09e2c587fd7b | 59 | short sum; |
droelf25 | 0:09e2c587fd7b | 60 | for (int i = 0; i < FILTER_SAMPLES; i++) |
droelf25 | 0:09e2c587fd7b | 61 | sum += buffer[i]; |
droelf25 | 0:09e2c587fd7b | 62 | |
droelf25 | 0:09e2c587fd7b | 63 | return sum / FILTER_SAMPLES; |
droelf25 | 0:09e2c587fd7b | 64 | } |
droelf25 | 0:09e2c587fd7b | 65 | |
droelf25 | 0:09e2c587fd7b | 66 | int main() |
droelf25 | 0:09e2c587fd7b | 67 | { |
droelf25 | 0:09e2c587fd7b | 68 | if(!isInitialized) { |
droelf25 | 0:09e2c587fd7b | 69 | isInitialized = true; |
droelf25 | 0:09e2c587fd7b | 70 | led = 0; |
droelf25 | 0:09e2c587fd7b | 71 | signal = 0; |
droelf25 | 0:09e2c587fd7b | 72 | ledFlasher.attach(&flashLED, 0.03f); |
droelf25 | 1:81b6cfb6b112 | 73 | //user.mode(PullDown); |
droelf25 | 1:81b6cfb6b112 | 74 | //user.fall(&startSignal); |
droelf25 | 0:09e2c587fd7b | 75 | } |
droelf25 | 0:09e2c587fd7b | 76 | |
droelf25 | 0:09e2c587fd7b | 77 | while(1) { |
droelf25 | 0:09e2c587fd7b | 78 | buffer[bufferPos++] = sensor.read() * 3300; |
droelf25 | 0:09e2c587fd7b | 79 | |
droelf25 | 0:09e2c587fd7b | 80 | if(bufferPos == FILTER_SAMPLES) |
droelf25 | 0:09e2c587fd7b | 81 | bufferPos = 0; |
droelf25 | 0:09e2c587fd7b | 82 | |
droelf25 | 1:81b6cfb6b112 | 83 | #ifdef USE_HYSTERESIS |
droelf25 | 1:81b6cfb6b112 | 84 | short voltage = getAverageVoltage(); |
droelf25 | 1:81b6cfb6b112 | 85 | |
droelf25 | 1:81b6cfb6b112 | 86 | if(voltage <= HYSTERESIS_TRESHOLD_MV) |
droelf25 | 1:81b6cfb6b112 | 87 | hysteresis = false; |
droelf25 | 1:81b6cfb6b112 | 88 | |
droelf25 | 1:81b6cfb6b112 | 89 | if(voltage >= TRIGGER_VOLTAGE_MV && !hysteresis) { |
droelf25 | 1:81b6cfb6b112 | 90 | startSignal(); |
droelf25 | 1:81b6cfb6b112 | 91 | hysteresis = true; |
droelf25 | 1:81b6cfb6b112 | 92 | } |
droelf25 | 1:81b6cfb6b112 | 93 | #else |
droelf25 | 0:09e2c587fd7b | 94 | if(isReady) { |
droelf25 | 1:81b6cfb6b112 | 95 | if(getAverageVoltage() >= TRIGGER_VOLTAGE_MV) { |
droelf25 | 0:09e2c587fd7b | 96 | startSignal(); |
droelf25 | 0:09e2c587fd7b | 97 | isReady = false; |
droelf25 | 1:81b6cfb6b112 | 98 | |
droelf25 | 0:09e2c587fd7b | 99 | } |
droelf25 | 0:09e2c587fd7b | 100 | } |
droelf25 | 1:81b6cfb6b112 | 101 | #endif |
droelf25 | 0:09e2c587fd7b | 102 | } |
droelf25 | 0:09e2c587fd7b | 103 | } |