Skip to content

Commit c328fbb

Browse files
committed
add pulseFilter
1 parent 86aeffb commit c328fbb

File tree

7 files changed

+131
-2
lines changed

7 files changed

+131
-2
lines changed

README.md

100755100644
File mode changed.

examples/filter/filter.ino

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/* This sketch shows how to filter signals */
2+
3+
// ---- Create servo object to generate PWM signals ---
4+
#include <Servo.h>
5+
Servo servo;
6+
7+
// ----------- Signal deadband and calibration --------
8+
#include <PulseInput.h>
9+
volatile unsigned int input; /* variable is required to capture signal */
10+
11+
#define BAND 12 /* Deadband. Changes in signal within this band are ignored.
12+
Large values result in a steadier signal, at cost of lower precision */
13+
14+
PulseFilter <&input, BAND> filter;
15+
16+
// Simpler form:
17+
//PulseFilter <&input> filter; /* The filter defaults to a deadband of 4 if none is specified */
18+
19+
// ----------------------------------------------------
20+
21+
void setup() {
22+
Serial.begin(9600);
23+
24+
// PWM output at pin 2
25+
servo.attach(2);
26+
servo.writeMicroseconds(1000); // initial signal value
27+
28+
// assign variables to receive signals
29+
attachPulseInput(8, input);
30+
31+
// subtract initial average from input
32+
/*
33+
delay(1000); // pause before reading values [required for proper calibration]
34+
filter.calibrate(); // comment to only filter signal
35+
*/
36+
}
37+
38+
void loop() {
39+
// generate variable signal:
40+
int target = analogRead(A0) + 1000; // read potentiometer from A0
41+
servo.writeMicroseconds(target);
42+
43+
// display comparison:
44+
Serial.print( "Raw: " );
45+
Serial.print( input );
46+
47+
Serial.print("\t Filter: ");
48+
Serial.print( filter.update() );
49+
Serial.println();
50+
}

examples/read/read.ino

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* This sketch shows how to read PWM signals for servos */
2+
3+
// Create servo objects to generate signals
4+
#include <Servo.h>
5+
Servo servo;
6+
7+
// 1. Each signal must be read with an volatile unsigned int
8+
#include <PulseInput.h>
9+
volatile unsigned int input; /* each signal requires a variable - MUST be volatile unsigned int */
10+
11+
void setup() {
12+
// 2. assign variables to receive signal
13+
attachPulseInput(8, input); // pin 8 as INPUT_PULLUP
14+
15+
/* Generate pwm signals [in us] */
16+
servo.attach(2); // Output at PIN 2
17+
servo.writeMicroseconds(1500); // ON time = 1500 us
18+
19+
Serial.begin(9600);
20+
21+
/* To stop reading an already specified input, use this function: */
22+
//detachPulseInput(8); // Stop reading input at pin 8
23+
}
24+
25+
void loop() {
26+
// read signal variable
27+
Serial.println(input);
28+
}

keywords.txt

100755100644
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
pulseInput KEYWORD1
1+
PulseInput KEYWORD1
2+
PulseFilter KEYWORD1
23
attachPulseInput KEYWORD2
34
detachPulseInput KEYWORD2
45

library.properties

100755100644
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=pulseInput
2-
version=1.0.0
2+
version=1.1.0
33
author=RCmags <memoryofatrufestival@gmail.com>
44
maintainer=RCmags <memoryofatrufestival@gmail.com>
55
sentence=Non-blocking alternative to pulseIn

src/PulseFilter.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#ifndef PulseFilter_h
2+
#define PulseFilter_h
3+
4+
//--------- Constants --------
5+
6+
#define N_SAMPLE 25 // Samples used to calculate average
7+
#define DEFAULT_BAND 4 // Error range of deadband filter. Changes in signal within this band are ignored.
8+
9+
//---------- Class -----------
10+
11+
template<volatile uint16_t* input, int BAND = DEFAULT_BAND>
12+
class PulseFilter {
13+
private:
14+
uint16_t mean=0;
15+
int filter=0;
16+
17+
public:
18+
/* input average */
19+
void calibrate() {
20+
uint32_t sum = 0;
21+
for( int count = 0; count < N_SAMPLE; count += 1 ) {
22+
sum += (*input);
23+
delay(20); // period of 50hz pwm signal
24+
}
25+
// store truncated mean
26+
float f_mean = float(sum)/float(N_SAMPLE);
27+
mean = uint16_t(f_mean);
28+
}
29+
30+
/* update and return output */
31+
int update() {
32+
int value = int(*input) - int(mean);
33+
// deadband
34+
int change = value - filter;
35+
if( change > BAND ) {
36+
filter += change - BAND;
37+
} else if ( change < -BAND ) {
38+
filter += change + BAND;
39+
}
40+
return filter;
41+
}
42+
};
43+
44+
//------- Clear labels -------
45+
46+
#undef N_SAMPLE
47+
#undef DEFAULT_BAND
48+
49+
#endif

src/PulseInput.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <PinChangeInterrupt.h>
2+
#include <PulseFilter.h>
23

34
#ifndef PulseInput_h
45
#define PulseInput_h

0 commit comments

Comments
 (0)