mirror of
https://github.com/letscontrolit/ESPEasy.git
synced 2026-07-28 04:07:47 +00:00
Added Plugin for Quadrature Encoder Interface (Rotary Encoder) (#335)
* First draft * Functional QEI plugin * Corrected spelling of limits.h
This commit is contained in:
@@ -0,0 +1,165 @@
|
||||
GNU LESSER GENERAL PUBLIC LICENSE
|
||||
Version 3, 29 June 2007
|
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
|
||||
This version of the GNU Lesser General Public License incorporates
|
||||
the terms and conditions of version 3 of the GNU General Public
|
||||
License, supplemented by the additional permissions listed below.
|
||||
|
||||
0. Additional Definitions.
|
||||
|
||||
As used herein, "this License" refers to version 3 of the GNU Lesser
|
||||
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
||||
General Public License.
|
||||
|
||||
"The Library" refers to a covered work governed by this License,
|
||||
other than an Application or a Combined Work as defined below.
|
||||
|
||||
An "Application" is any work that makes use of an interface provided
|
||||
by the Library, but which is not otherwise based on the Library.
|
||||
Defining a subclass of a class defined by the Library is deemed a mode
|
||||
of using an interface provided by the Library.
|
||||
|
||||
A "Combined Work" is a work produced by combining or linking an
|
||||
Application with the Library. The particular version of the Library
|
||||
with which the Combined Work was made is also called the "Linked
|
||||
Version".
|
||||
|
||||
The "Minimal Corresponding Source" for a Combined Work means the
|
||||
Corresponding Source for the Combined Work, excluding any source code
|
||||
for portions of the Combined Work that, considered in isolation, are
|
||||
based on the Application, and not on the Linked Version.
|
||||
|
||||
The "Corresponding Application Code" for a Combined Work means the
|
||||
object code and/or source code for the Application, including any data
|
||||
and utility programs needed for reproducing the Combined Work from the
|
||||
Application, but excluding the System Libraries of the Combined Work.
|
||||
|
||||
1. Exception to Section 3 of the GNU GPL.
|
||||
|
||||
You may convey a covered work under sections 3 and 4 of this License
|
||||
without being bound by section 3 of the GNU GPL.
|
||||
|
||||
2. Conveying Modified Versions.
|
||||
|
||||
If you modify a copy of the Library, and, in your modifications, a
|
||||
facility refers to a function or data to be supplied by an Application
|
||||
that uses the facility (other than as an argument passed when the
|
||||
facility is invoked), then you may convey a copy of the modified
|
||||
version:
|
||||
|
||||
a) under this License, provided that you make a good faith effort to
|
||||
ensure that, in the event an Application does not supply the
|
||||
function or data, the facility still operates, and performs
|
||||
whatever part of its purpose remains meaningful, or
|
||||
|
||||
b) under the GNU GPL, with none of the additional permissions of
|
||||
this License applicable to that copy.
|
||||
|
||||
3. Object Code Incorporating Material from Library Header Files.
|
||||
|
||||
The object code form of an Application may incorporate material from
|
||||
a header file that is part of the Library. You may convey such object
|
||||
code under terms of your choice, provided that, if the incorporated
|
||||
material is not limited to numerical parameters, data structure
|
||||
layouts and accessors, or small macros, inline functions and templates
|
||||
(ten or fewer lines in length), you do both of the following:
|
||||
|
||||
a) Give prominent notice with each copy of the object code that the
|
||||
Library is used in it and that the Library and its use are
|
||||
covered by this License.
|
||||
|
||||
b) Accompany the object code with a copy of the GNU GPL and this license
|
||||
document.
|
||||
|
||||
4. Combined Works.
|
||||
|
||||
You may convey a Combined Work under terms of your choice that,
|
||||
taken together, effectively do not restrict modification of the
|
||||
portions of the Library contained in the Combined Work and reverse
|
||||
engineering for debugging such modifications, if you also do each of
|
||||
the following:
|
||||
|
||||
a) Give prominent notice with each copy of the Combined Work that
|
||||
the Library is used in it and that the Library and its use are
|
||||
covered by this License.
|
||||
|
||||
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
||||
document.
|
||||
|
||||
c) For a Combined Work that displays copyright notices during
|
||||
execution, include the copyright notice for the Library among
|
||||
these notices, as well as a reference directing the user to the
|
||||
copies of the GNU GPL and this license document.
|
||||
|
||||
d) Do one of the following:
|
||||
|
||||
0) Convey the Minimal Corresponding Source under the terms of this
|
||||
License, and the Corresponding Application Code in a form
|
||||
suitable for, and under terms that permit, the user to
|
||||
recombine or relink the Application with a modified version of
|
||||
the Linked Version to produce a modified Combined Work, in the
|
||||
manner specified by section 6 of the GNU GPL for conveying
|
||||
Corresponding Source.
|
||||
|
||||
1) Use a suitable shared library mechanism for linking with the
|
||||
Library. A suitable mechanism is one that (a) uses at run time
|
||||
a copy of the Library already present on the user's computer
|
||||
system, and (b) will operate properly with a modified version
|
||||
of the Library that is interface-compatible with the Linked
|
||||
Version.
|
||||
|
||||
e) Provide Installation Information, but only if you would otherwise
|
||||
be required to provide such information under section 6 of the
|
||||
GNU GPL, and only to the extent that such information is
|
||||
necessary to install and execute a modified version of the
|
||||
Combined Work produced by recombining or relinking the
|
||||
Application with a modified version of the Linked Version. (If
|
||||
you use option 4d0, the Installation Information must accompany
|
||||
the Minimal Corresponding Source and Corresponding Application
|
||||
Code. If you use option 4d1, you must provide the Installation
|
||||
Information in the manner specified by section 6 of the GNU GPL
|
||||
for conveying Corresponding Source.)
|
||||
|
||||
5. Combined Libraries.
|
||||
|
||||
You may place library facilities that are a work based on the
|
||||
Library side by side in a single library together with other library
|
||||
facilities that are not Applications and are not covered by this
|
||||
License, and convey such a combined library under terms of your
|
||||
choice, if you do both of the following:
|
||||
|
||||
a) Accompany the combined library with a copy of the same work based
|
||||
on the Library, uncombined with any other library facilities,
|
||||
conveyed under the terms of this License.
|
||||
|
||||
b) Give prominent notice with the combined library that part of it
|
||||
is a work based on the Library, and explaining where to find the
|
||||
accompanying uncombined form of the same work.
|
||||
|
||||
6. Revised Versions of the GNU Lesser General Public License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions
|
||||
of the GNU Lesser General Public License from time to time. Such new
|
||||
versions will be similar in spirit to the present version, but may
|
||||
differ in detail to address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the
|
||||
Library as you received it specifies that a certain numbered version
|
||||
of the GNU Lesser General Public License "or any later version"
|
||||
applies to it, you have the option of following the terms and
|
||||
conditions either of that published version or of any later version
|
||||
published by the Free Software Foundation. If the Library as you
|
||||
received it does not specify a version number of the GNU Lesser
|
||||
General Public License, you may choose any version of the GNU Lesser
|
||||
General Public License ever published by the Free Software Foundation.
|
||||
|
||||
If the Library as you received it specifies that a proxy can decide
|
||||
whether future versions of the GNU Lesser General Public License shall
|
||||
apply, that proxy's public statement of acceptance of any version is
|
||||
permanent authorization for you to choose that version for the
|
||||
Library.
|
||||
@@ -0,0 +1,241 @@
|
||||
/*-------------------------------------------------------------------------
|
||||
Arduino library to ...
|
||||
|
||||
Written by Jochen Krapf,
|
||||
contributions by ... and other members of the open
|
||||
source community.
|
||||
|
||||
-------------------------------------------------------------------------
|
||||
This file is part of the MechInputs library.
|
||||
|
||||
MechInputs is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as
|
||||
published by the Free Software Foundation, either version 3 of
|
||||
the License, or (at your option) any later version.
|
||||
|
||||
MechInputs is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with MechInputs. If not, see
|
||||
<http://www.gnu.org/licenses/>.
|
||||
-------------------------------------------------------------------------*/
|
||||
|
||||
#include "QEIx4.h"
|
||||
#include <limits.h>
|
||||
|
||||
|
||||
// bit masks for state machine - don't change!!!
|
||||
#define QEIx4_STATE 0xC
|
||||
#define QEIx4_MASK 0x1C
|
||||
#define QEIx4_1x_INC 0x0100
|
||||
#define QEIx4_2x_INC 0x0200
|
||||
#define QEIx4_4x_INC 0x0400
|
||||
#define QEIx4_1x_DEC 0x1000
|
||||
#define QEIx4_2x_DEC 0x2000
|
||||
#define QEIx4_4x_DEC 0x4000
|
||||
#define QEIx4_1x_MASK 0x11FF
|
||||
#define QEIx4_2x_MASK 0x33FF
|
||||
#define QEIx4_4x_MASK 0x77FF
|
||||
#define QEIx4_DIR 0x20
|
||||
#define QEIx4_A 1
|
||||
#define QEIx4_B 2
|
||||
#define QEIx4_AB 3
|
||||
#define QEIx4_S0 0x0
|
||||
#define QEIx4_S1 0x4
|
||||
#define QEIx4_S2 0x8
|
||||
#define QEIx4_S3 0xC
|
||||
#define QEIx4_CCW 0
|
||||
#define QEIx4_CW 0x10
|
||||
#define QEIx4_IS_CHG 0x7700
|
||||
#define QEIx4_IS_INC 0x0700
|
||||
#define QEIx4_IS_DEC 0x7000
|
||||
#define QEIx4_IS_DIR 0x20
|
||||
|
||||
// state machine for decoting - don't change!!!
|
||||
uint16_t QEIx4::__stateLUT[32] = {
|
||||
// act state S0 in CCW direction
|
||||
QEIx4_CCW | QEIx4_S0,
|
||||
QEIx4_CW | QEIx4_S1 | QEIx4_A | QEIx4_4x_INC | QEIx4_DIR,
|
||||
QEIx4_CCW | QEIx4_S0 | QEIx4_B,
|
||||
QEIx4_CCW | QEIx4_S3 | QEIx4_AB | QEIx4_1x_DEC,
|
||||
// act state S1 in CCW direction
|
||||
QEIx4_CCW | QEIx4_S1,
|
||||
QEIx4_CCW | QEIx4_S1 | QEIx4_A,
|
||||
QEIx4_CCW | QEIx4_S0 | QEIx4_B | QEIx4_4x_DEC,
|
||||
QEIx4_CW | QEIx4_S2 | QEIx4_AB | QEIx4_1x_INC | QEIx4_DIR,
|
||||
// act state S2 in CCW direction
|
||||
QEIx4_CCW | QEIx4_S1 | QEIx4_2x_DEC,
|
||||
QEIx4_CCW | QEIx4_S2 | QEIx4_A,
|
||||
QEIx4_CW | QEIx4_S3 | QEIx4_B | QEIx4_4x_INC | QEIx4_DIR,
|
||||
QEIx4_CCW | QEIx4_S2 | QEIx4_AB,
|
||||
// act state S3 in CCW direction
|
||||
QEIx4_CW | QEIx4_S0 | QEIx4_2x_INC | QEIx4_DIR,
|
||||
QEIx4_CCW | QEIx4_S2 | QEIx4_A | QEIx4_4x_DEC,
|
||||
QEIx4_CCW | QEIx4_S3 | QEIx4_B,
|
||||
QEIx4_CCW | QEIx4_S3 | QEIx4_AB,
|
||||
|
||||
// act state S0 in CW direction
|
||||
QEIx4_CW | QEIx4_S0,
|
||||
QEIx4_CW | QEIx4_S1 | QEIx4_A | QEIx4_4x_INC,
|
||||
QEIx4_CW | QEIx4_S0 | QEIx4_B,
|
||||
QEIx4_CCW | QEIx4_S3 | QEIx4_AB | QEIx4_1x_DEC | QEIx4_DIR,
|
||||
// act state S1 in CW direction
|
||||
QEIx4_CW | QEIx4_S1,
|
||||
QEIx4_CW | QEIx4_S1 | QEIx4_A,
|
||||
QEIx4_CCW | QEIx4_S0 | QEIx4_B | QEIx4_4x_DEC | QEIx4_DIR,
|
||||
QEIx4_CW | QEIx4_S2 | QEIx4_AB | QEIx4_1x_INC,
|
||||
// act state S2 in CW direction
|
||||
QEIx4_CCW | QEIx4_S1 | QEIx4_2x_DEC | QEIx4_DIR,
|
||||
QEIx4_CW | QEIx4_S2 | QEIx4_A,
|
||||
QEIx4_CW | QEIx4_S3 | QEIx4_B | QEIx4_4x_INC,
|
||||
QEIx4_CW | QEIx4_S2 | QEIx4_AB,
|
||||
// act state S3 in CW direction
|
||||
QEIx4_CW | QEIx4_S0 | QEIx4_2x_INC,
|
||||
QEIx4_CCW | QEIx4_S2 | QEIx4_A | QEIx4_4x_DEC | QEIx4_DIR,
|
||||
QEIx4_CW | QEIx4_S3 | QEIx4_B,
|
||||
QEIx4_CW | QEIx4_S3 | QEIx4_AB
|
||||
};
|
||||
|
||||
// Helper for ISR call
|
||||
QEIx4* QEIx4::__instance[4] = { 0 };
|
||||
|
||||
|
||||
//#define DEB(x) printf (x)
|
||||
#define DEB(x)
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
QEIx4::QEIx4()
|
||||
{
|
||||
for (byte i=0; i<4; i++)
|
||||
if (__instance[i] == 0)
|
||||
{
|
||||
__instance[i] = this;
|
||||
DEB("::");
|
||||
break;
|
||||
}
|
||||
|
||||
_pinA = -1;
|
||||
_pinB = -1;
|
||||
_pinI = -1;
|
||||
_state = 0;
|
||||
_limitMin = LONG_MIN;
|
||||
_limitMax = LONG_MAX;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
QEIx4::~QEIx4()
|
||||
{
|
||||
for (byte i=0; i<4; i++)
|
||||
if (__instance[i] == this)
|
||||
{
|
||||
__instance[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void QEIx4::begin(int16_t pinA, int16_t pinB, int16_t pinI, uint8_t mode)
|
||||
{
|
||||
if (_pinA >= 0)
|
||||
detachInterrupt(digitalPinToInterrupt(_pinA));
|
||||
if (_pinB >= 0)
|
||||
detachInterrupt(digitalPinToInterrupt(_pinB));
|
||||
|
||||
_pinA = pinA;
|
||||
_pinB = pinB;
|
||||
_pinI = pinI;
|
||||
|
||||
_counter = 0;
|
||||
_bHasChanged = true;
|
||||
|
||||
if (mode == 1)
|
||||
_eventMask = QEIx4_1x_MASK;
|
||||
else if (mode == 2)
|
||||
_eventMask = QEIx4_2x_MASK;
|
||||
else
|
||||
_eventMask = QEIx4_4x_MASK;
|
||||
|
||||
pinMode(_pinA, INPUT_PULLUP);
|
||||
pinMode(_pinB, INPUT_PULLUP);
|
||||
pinMode(_pinI, INPUT_PULLUP);
|
||||
|
||||
if (_pinA >= 0)
|
||||
attachInterrupt(digitalPinToInterrupt(_pinA), ISR, CHANGE);
|
||||
if (_pinB >= 0)
|
||||
attachInterrupt(digitalPinToInterrupt(_pinB), ISR, CHANGE);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
long QEIx4::read()
|
||||
{
|
||||
noInterrupts();
|
||||
_bHasChanged = false;
|
||||
long ret = _counter;
|
||||
interrupts();
|
||||
return ret;
|
||||
}
|
||||
|
||||
void QEIx4::loop()
|
||||
{
|
||||
noInterrupts();
|
||||
processStateMachine();
|
||||
interrupts();
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void QEIx4::processStateMachine()
|
||||
{
|
||||
DEB(".");
|
||||
|
||||
_state &= QEIx4_MASK;
|
||||
if (digitalRead(_pinA)) _state |= QEIx4_A;
|
||||
if (digitalRead(_pinB)) _state |= QEIx4_B;
|
||||
|
||||
_state = __stateLUT[_state]; // magic is done by lookup-table
|
||||
_state &= _eventMask;
|
||||
|
||||
if (_state & QEIx4_IS_CHG) { // is any change?
|
||||
bool bCounterChange = false;
|
||||
|
||||
if ((_state & QEIx4_IS_INC) && (_counter < _limitMax)) { // has moved foreward?
|
||||
_counter++;
|
||||
bCounterChange = true;
|
||||
DEB("+");
|
||||
}
|
||||
if ((_state & QEIx4_IS_DEC) && (_counter > _limitMin)) { // has moved backward?
|
||||
_counter--;
|
||||
bCounterChange = true;
|
||||
DEB("-");
|
||||
}
|
||||
|
||||
if (_pinI >= 0 && _bIndexTrigger && bCounterChange && digitalRead(_pinI)) { // is index pin triggered?
|
||||
_bIndexTrigger = false;
|
||||
_counter = 0;
|
||||
DEB("I");
|
||||
}
|
||||
|
||||
if (bCounterChange) { // has counter changed?
|
||||
_bHasChanged = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void QEIx4::ISR()
|
||||
{
|
||||
for (byte i=0; i<4; i++)
|
||||
if (__instance[i])
|
||||
{
|
||||
__instance[i]->processStateMachine();
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
@@ -0,0 +1,133 @@
|
||||
/*--------------------------------------------------------------------
|
||||
Arduino library to ...
|
||||
|
||||
Written by Jochen Krapf,
|
||||
contributions by ... and other members of the open
|
||||
source community.
|
||||
|
||||
-------------------------------------------------------------------------
|
||||
This file is part of the MechInputs library.
|
||||
|
||||
MechInputs is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as
|
||||
published by the Free Software Foundation, either version 3 of
|
||||
the License, or (at your option) any later version.
|
||||
|
||||
MechInputs is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with MechInputs. If not, see
|
||||
<http://www.gnu.org/licenses/>.
|
||||
--------------------------------------------------------------------*/
|
||||
|
||||
#ifndef QEIX4_H
|
||||
#define QEIX4_H
|
||||
|
||||
#if (ARDUINO >= 100)
|
||||
#include <Arduino.h>
|
||||
#else
|
||||
#include <WProgram.h>
|
||||
#include <pins_arduino.h>
|
||||
#endif
|
||||
|
||||
class QEIx4
|
||||
{
|
||||
public:
|
||||
|
||||
/** constructor of QEIx4 object
|
||||
*/
|
||||
QEIx4();
|
||||
|
||||
/** destructor of QEIx4 object
|
||||
*/
|
||||
~QEIx4();
|
||||
|
||||
void begin(int16_t pinA, int16_t pinB, int16_t pinI=-1, uint8_t mode=4);
|
||||
|
||||
/** Gets the actual counter value.
|
||||
*
|
||||
* @return Actual counter value
|
||||
*/
|
||||
long read();
|
||||
|
||||
/** Gets the actual counter value as long operator.
|
||||
*
|
||||
* @return Actual counter value as long operator
|
||||
*/
|
||||
operator long() { // int-Operator
|
||||
return read();
|
||||
}
|
||||
|
||||
/** Sets the counter value at actual encoder position to given value.
|
||||
*
|
||||
* @param Counter value
|
||||
*/
|
||||
void write(long counter) {
|
||||
_counter = counter;
|
||||
}
|
||||
|
||||
/** Sets the counter value at actual encoder position to given value as assign operator.
|
||||
*
|
||||
* @param Counter value
|
||||
*/
|
||||
int operator= (long counter) { // Assign-Operator
|
||||
write(counter);
|
||||
return counter;
|
||||
}
|
||||
|
||||
void setLimit(long limitMin, long limitMax)
|
||||
{
|
||||
_limitMin = limitMin;
|
||||
_limitMax = limitMax;
|
||||
}
|
||||
|
||||
/** Sets the flag for zeroing on next high on index pin while AB lines triggers next counting. The trigger calls tha callback function in which the counter can be set to zero or the actual counter can be latched in for later offset calculation
|
||||
*
|
||||
* @param Flag for triggering. Set to 1 for call the attached callback. It is reseted after this call
|
||||
*/
|
||||
void setIndexTrigger(bool bIndexTrigger=true) {
|
||||
if (_pinI < 0)
|
||||
bIndexTrigger = false;
|
||||
_bIndexTrigger = bIndexTrigger;
|
||||
}
|
||||
|
||||
bool hasChanged(){
|
||||
return _bHasChanged;
|
||||
}
|
||||
|
||||
void loop();
|
||||
|
||||
protected:
|
||||
|
||||
/** Polls the state machine and updates the counter value.
|
||||
*/
|
||||
void processStateMachine();
|
||||
|
||||
/** Entry point for arduino interrupts - route to class instances
|
||||
*/
|
||||
static void ISR();
|
||||
|
||||
protected:
|
||||
|
||||
volatile long _counter;
|
||||
volatile bool _bHasChanged;
|
||||
volatile bool _bIndexTrigger;
|
||||
|
||||
int16_t _pinA;
|
||||
int16_t _pinB;
|
||||
int16_t _pinI;
|
||||
long _limitMin;
|
||||
long _limitMax;
|
||||
uint16_t _state;
|
||||
uint16_t _eMode;
|
||||
|
||||
private:
|
||||
static uint16_t __stateLUT[32];
|
||||
static QEIx4* __instance[4];
|
||||
uint16_t _eventMask;
|
||||
};
|
||||
|
||||
#endif // QEIX4_H
|
||||
@@ -0,0 +1,28 @@
|
||||
# MechInputs Library (https://github.com/jkDesignDE/MechInputs)
|
||||
|
||||
Arduino library for reading and debouncing mechanical inputs Like buttons, switches and encoders.
|
||||
|
||||
After downloading, rename folder to 'MechInputs' and install in Arduino Libraries folder. Restart Arduino IDE, then open File->Sketchbook->Library->MechInputs->* sketches.
|
||||
|
||||
# QEI (quadrature encoder interface) library, for decoding AB signals from a rotary encoder
|
||||
|
||||
Use cases:
|
||||
- Rotary encoder in closed loop motor regulation
|
||||
- Hand wheel
|
||||
- Input device for motion control (MoCo)
|
||||
|
||||
|
||||
A class to decode pulses on a rotary encoder with AB signals (quadrature encoder).
|
||||
It uses all 4 edges of the AB signals to increase the counter resolution 4 times of cycles per rotation/revolution (CPR) (e.g. an encoder with 500 CPR get 2000 counts per rotation)
|
||||
|
||||
In opposite to most common QEI implementation this is resistant to jitter and chatter on AB signals and motor vibrations.
|
||||
Whes reaching the next position the edge that triggerd this position (state) is ignored to aboid oscillating up/down counts.
|
||||
|
||||
It can also be used in polling mode i.g. in idle routines if interrupts are not desired.
|
||||
At this mode be sure that the sampling frequency is heigher than the maximum rotation speed (expeced counts per second)
|
||||
|
||||
The internal state machine is based on a look up table (LUT) to minimize interrupt retention time and get all necessary flags at once.
|
||||
|
||||
The library is designed to support closed loop speed- and motion-controller for also slow and smooth motions like movie camera motion control.
|
||||
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
name=MechInputs
|
||||
version=0.0.1
|
||||
author=Jochen Krapf
|
||||
maintainer=Jochen Krapf <jk@nerd2nerd.org>
|
||||
sentence=Library for Reading and Debouncing Mechanical Inputs Like Buttons, Switches and Encoders with Arduino-Based Controllers.
|
||||
paragraph=Library for Reading and Debouncing Mechanical Inputs Like Buttons, Switches and Encoders with Arduino-Based Controllers.
|
||||
category=Input
|
||||
url=https://github.com/jkDesignDE/MechInputs
|
||||
architectures=esp8266,avr
|
||||
@@ -0,0 +1,166 @@
|
||||
//#######################################################################################################
|
||||
//#################################### Plugin 059: Rotary Encoder #######################################
|
||||
//#######################################################################################################
|
||||
|
||||
// ESPEasy Plugin to process the quadrature encoder interface signals (e.g. rotary encoder)
|
||||
// written by Jochen Krapf (jk@nerd2nerd.org)
|
||||
|
||||
// Connection:
|
||||
// Use 1st and 2nd GPIO for encoders A and B signal.
|
||||
// Optional use 3rd GPIO for encoders I signel to reset counter to 0 at first trigger.
|
||||
// If counter runs in wrong direction, change A and B GPIOs in settings page
|
||||
|
||||
// Note: Up to 4 encoders can be used simultaniously
|
||||
|
||||
|
||||
#ifdef PLUGIN_BUILD_TESTING
|
||||
|
||||
#define PLUGIN_059
|
||||
#define PLUGIN_ID_059 59
|
||||
#define PLUGIN_NAME_059 "Switch Input - Rotary Encoder [TESTING]"
|
||||
#define PLUGIN_VALUENAME1_059 "Counter"
|
||||
|
||||
#include <QEIx4.h>
|
||||
|
||||
QEIx4* Plugin_059_QE = NULL;
|
||||
|
||||
#ifndef CONFIG
|
||||
#define CONFIG(n) (Settings.TaskDevicePluginConfig[event->TaskIndex][n])
|
||||
#endif
|
||||
#ifndef CONFIG_L
|
||||
#define CONFIG_L(n) (Settings.TaskDevicePluginConfigLong[event->TaskIndex][n])
|
||||
#endif
|
||||
#ifndef PIN
|
||||
#define PIN(n) (Settings.TaskDevicePin[n][event->TaskIndex])
|
||||
#endif
|
||||
|
||||
|
||||
boolean Plugin_059(byte function, struct EventStruct *event, String& string)
|
||||
{
|
||||
boolean success = false;
|
||||
|
||||
switch (function)
|
||||
{
|
||||
case PLUGIN_DEVICE_ADD:
|
||||
{
|
||||
Device[++deviceCount].Number = PLUGIN_ID_059;
|
||||
Device[deviceCount].Type = DEVICE_TYPE_TRIPLE;
|
||||
Device[deviceCount].Ports = 0;
|
||||
Device[deviceCount].VType = SENSOR_TYPE_SWITCH;
|
||||
Device[deviceCount].PullUpOption = false;
|
||||
Device[deviceCount].InverseLogicOption = false;
|
||||
Device[deviceCount].FormulaOption = false;
|
||||
Device[deviceCount].ValueCount = 1;
|
||||
Device[deviceCount].SendDataOption = true;
|
||||
Device[deviceCount].TimerOption = true;
|
||||
Device[deviceCount].TimerOptional = true;
|
||||
Device[deviceCount].GlobalSyncOption = true;
|
||||
break;
|
||||
}
|
||||
|
||||
case PLUGIN_GET_DEVICENAME:
|
||||
{
|
||||
string = F(PLUGIN_NAME_059);
|
||||
break;
|
||||
}
|
||||
|
||||
case PLUGIN_GET_DEVICEVALUENAMES:
|
||||
{
|
||||
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_059));
|
||||
break;
|
||||
}
|
||||
|
||||
case PLUGIN_WEBFORM_LOAD:
|
||||
{
|
||||
// default values
|
||||
if (CONFIG_L(0) == 0 && CONFIG_L(1) == 0)
|
||||
CONFIG_L(1) = 100;
|
||||
|
||||
String options[3] = { F("1 pulse per cycle"), F("2 pulses per cycle"), F("4 pulses per cycle") };
|
||||
int optionValues[3] = { 1, 2, 4 };
|
||||
addFormSelector(string, F("Mode"), F("qei_mode"), 3, options, optionValues, CONFIG(0));
|
||||
|
||||
addFormNumericBox(string, F("Limit min."), F("qei_limitmin"), CONFIG_L(0));
|
||||
addFormNumericBox(string, F("Limit max."), F("qei_limitmax"), CONFIG_L(1));
|
||||
|
||||
success = true;
|
||||
break;
|
||||
}
|
||||
|
||||
case PLUGIN_WEBFORM_SAVE:
|
||||
{
|
||||
CONFIG(0) = getFormItemInt(F("qei_mode"));
|
||||
|
||||
CONFIG_L(0) = getFormItemInt(F("qei_limitmin"));
|
||||
CONFIG_L(1) = getFormItemInt(F("qei_limitmax"));
|
||||
|
||||
success = true;
|
||||
break;
|
||||
}
|
||||
|
||||
case PLUGIN_INIT:
|
||||
{
|
||||
if (!Plugin_059_QE)
|
||||
Plugin_059_QE = new QEIx4;
|
||||
|
||||
Plugin_059_QE->begin(PIN(0),PIN(1),PIN(2),CONFIG(0));
|
||||
Plugin_059_QE->setLimit(CONFIG_L(0), CONFIG_L(1));
|
||||
Plugin_059_QE->setIndexTrigger(true);
|
||||
|
||||
ExtraTaskSettings.TaskDeviceValueDecimals[event->BaseVarIndex] = 0;
|
||||
|
||||
String log = F("QEI : GPIO: ");
|
||||
for (byte i=0; i<3; i++)
|
||||
{
|
||||
int pin = PIN(i);
|
||||
if (pin >= 0)
|
||||
{
|
||||
//pinMode(pin, (Settings.TaskDevicePin1PullUp[event->TaskIndex]) ? INPUT_PULLUP : INPUT);
|
||||
setPinState(PLUGIN_ID_059, pin, PIN_MODE_INPUT, 0);
|
||||
}
|
||||
log += pin;
|
||||
log += F(" ");
|
||||
}
|
||||
addLog(LOG_LEVEL_INFO, log);
|
||||
|
||||
success = true;
|
||||
break;
|
||||
}
|
||||
|
||||
case PLUGIN_TEN_PER_SECOND:
|
||||
{
|
||||
if (Plugin_059_QE)
|
||||
{
|
||||
if (Plugin_059_QE->hasChanged())
|
||||
{
|
||||
long c = Plugin_059_QE->read();
|
||||
UserVar[event->BaseVarIndex] = (float)c;
|
||||
event->sensorType = SENSOR_TYPE_SWITCH;
|
||||
|
||||
String log = F("QEI : ");
|
||||
log += c;
|
||||
addLog(LOG_LEVEL_INFO, log);
|
||||
|
||||
sendData(event);
|
||||
}
|
||||
|
||||
}
|
||||
success = true;
|
||||
break;
|
||||
}
|
||||
|
||||
case PLUGIN_READ:
|
||||
{
|
||||
if (Plugin_059_QE)
|
||||
{
|
||||
UserVar[event->BaseVarIndex] = (float)Plugin_059_QE->read();
|
||||
}
|
||||
success = true;
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user