Skip to content

Commit b4cee6e

Browse files
add deadband cause it's not packaged (https://github.com/cellularmitosis/Deadband)
1 parent a27cecf commit b4cee6e

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed

Deadband/Deadband.cpp

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
Deadband.cpp - An Arduino library for implementing deadband on digital values.
3+
https://github.com/cellularmitosis/Deadband
4+
v0.1 (Sept 7, 2013)
5+
6+
Deadband: see http://en.wikipedia.org/wiki/Deadband%23Backlash
7+
8+
If you've ever used a mill or a lathe which had some slop in the carriage,
9+
or an old car which had slop in the steering, that's deadband.
10+
11+
The Deadband library allows you to create "digital slop".
12+
13+
Copyright (c) 2013 Jason Pepas
14+
15+
This software is released under the terms of the MIT License:
16+
17+
Permission is hereby granted, free of charge, to any person obtaining a copy
18+
of this software and associated documentation files (the "Software"), to deal
19+
in the Software without restriction, including without limitation the rights
20+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
21+
copies of the Software, and to permit persons to whom the Software is
22+
furnished to do so, subject to the following conditions:
23+
24+
The above copyright notice and this permission notice shall be included in
25+
all copies or substantial portions of the Software.
26+
27+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
30+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
32+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
33+
THE SOFTWARE.
34+
*/
35+
36+
#include "Deadband.h"
37+
38+
39+
DeadbandFilter* createDeadbandFilter(uint8_t numberOfLSBsToConsume)
40+
{
41+
// thanks to http://stackoverflow.com/a/252810 --jpepas
42+
DeadbandFilter *filter = NULL;
43+
filter = (DeadbandFilter*)malloc(sizeof(DeadbandFilter));
44+
if (filter == NULL)
45+
{
46+
return NULL;
47+
}
48+
49+
filter->numberOfLSBsToConsume = numberOfLSBsToConsume;
50+
filter->deadbandWindowLowerEdge = 0;
51+
filter->previousOutput = 0;
52+
53+
return filter;
54+
}
55+
56+
57+
uint16_t deadband(DeadbandFilter *filter, uint16_t newInput)
58+
{
59+
if (filter == NULL || filter->numberOfLSBsToConsume == 0)
60+
{
61+
return newInput;
62+
}
63+
64+
uint16_t windowWidth = (1 << filter->numberOfLSBsToConsume) - 1;
65+
uint16_t deadbandWindowUpperEdge = filter->deadbandWindowLowerEdge + windowWidth;
66+
67+
if (newInput > deadbandWindowUpperEdge)
68+
{
69+
deadbandWindowUpperEdge = newInput;
70+
filter->deadbandWindowLowerEdge = deadbandWindowUpperEdge - windowWidth;
71+
filter->previousOutput = deadbandWindowUpperEdge;
72+
return deadbandWindowUpperEdge;
73+
}
74+
else if (newInput < filter->deadbandWindowLowerEdge)
75+
{
76+
filter->deadbandWindowLowerEdge = newInput;
77+
filter->previousOutput = newInput;
78+
return filter->deadbandWindowLowerEdge;
79+
}
80+
else
81+
{
82+
return filter->previousOutput;
83+
}
84+
}

Deadband/Deadband.h

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
Deadband.h - An Arduino library for implementing deadband on digital values.
3+
https://github.com/cellularmitosis/Deadband
4+
v0.1 (Sept 7, 2013)
5+
6+
Deadband: see http://en.wikipedia.org/wiki/Deadband%23Backlash
7+
8+
If you've ever used a mill or a lathe which had some slop in the carriage,
9+
or an old car which had slop in the steering, that's deadband.
10+
11+
The Deadband library allows you to create "digital slop".
12+
13+
Copyright (c) 2013 Jason Pepas
14+
15+
This software is released under the terms of the MIT License:
16+
17+
Permission is hereby granted, free of charge, to any person obtaining a copy
18+
of this software and associated documentation files (the "Software"), to deal
19+
in the Software without restriction, including without limitation the rights
20+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
21+
copies of the Software, and to permit persons to whom the Software is
22+
furnished to do so, subject to the following conditions:
23+
24+
The above copyright notice and this permission notice shall be included in
25+
all copies or substantial portions of the Software.
26+
27+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
30+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
32+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
33+
THE SOFTWARE.
34+
*/
35+
36+
#ifndef Deadband_h
37+
#define Deadband_h
38+
39+
#include <stdlib.h>
40+
#include <stdint.h>
41+
42+
struct _DeadbandFilter
43+
{
44+
uint8_t numberOfLSBsToConsume;
45+
uint16_t deadbandWindowLowerEdge;
46+
uint16_t previousOutput;
47+
};
48+
49+
typedef struct _DeadbandFilter DeadbandFilter;
50+
51+
DeadbandFilter* createDeadbandFilter(uint8_t numberOfLSBsToConsume);
52+
uint16_t deadband(DeadbandFilter *filter, uint16_t newInput);
53+
54+
#endif // Deadband_h

0 commit comments

Comments
 (0)