JMotor
Loading...
Searching...
No Matches
JEncoder.h
Go to the documentation of this file.
1#ifndef J_ENCODER_H
2#define J_ENCODER_H
3#include <Arduino.h>
4//**********SEE BOTTOM OF THIS FILE FOR #INCLUDES OF SUBCLASSES**********//
5
6/* \def
7 * Functions called by interrupts can't expect parameters or be functions of classes so a workaround is needed to use interrupts in a class.
8 * The workaround that's used here is global functions need to be made when a new instance of this class is made.
9 * The global function(s) get used when attaching interrupts, and the global function(s) can call a function inside the class.
10 * The following macros makes it easy to make the global function(s).
11 *
12 * Put IRAM_ATTR keyword before macro when calling on esp32
13 */
14#define jENCODER_MAKE_ISR_MACRO(name) \
15 void name##_jENCODER_ISR() { name.encoderISR(); }
16
17/* \def
18 * Functions called by interrupts can't expect parameters or be functions of classes so a workaround is needed to use interrupts in a class.
19 * The workaround that's used here is global functions need to be made when a new instance of this class is made.
20 * The global function(s) get used when attaching interrupts, and the global function(s) can call a function inside the class.
21 * The following macros makes it easy to make the global function(s).
22 *
23 * Put IRAM_ATTR keyword before macro when calling on esp32
24 */
25#define jENCODER_MAKE_ISRS_MACRO(name) \
26 void name##_jENCODER_ISR_A() { name.ISRA(); } \
27 void name##_jENCODER_ISR_B() { name.ISRB(); }
28
33class JEncoder {
34public:
38 virtual void setRev(bool _rev)
39 {
40 }
45 virtual long zeroCounter();
46
51 virtual float getVel();
52
58 virtual long getCounter();
59
65 virtual float getPos();
66
72 virtual float getDistPerCountFactor();
73
78 virtual void setDistPerCountFactor(float _factor);
79
84 virtual bool hasDirection();
85
90 virtual bool isVelNew();
91
96 virtual void run();
97};
102#if (defined(ESP32) || defined(CORE_TEENSY) || defined(SAMD_SERIES) || defined(ESP8266) || defined(JMOTOR_ENCODER_FORCE_ATTACHINTERRUPT)) && !defined(JMOTOR_ENCODER_FORCE_PINCHANGE)
106#else
110#endif
111#include "JEncoderAS5048bI2C.h"
112#include "JEncoderAS5600I2C.h"
113#include "JEncoderBSED.h"
114
115#endif // J_ENCODER_H
defines common interface for JEncoder
Definition JEncoder.h:33
virtual float getDistPerCountFactor()
returns a conversion factor between encoder ticks and distance that can be set for the encoder
virtual long getCounter()
returns how far the encoder has turned from the zero position
virtual void setRev(bool _rev)
empty function for directionless encoders to override
Definition JEncoder.h:38
virtual void run()
if an encoder needs to have some code called each loop (like absolute encoder polling encoder and cal...
virtual float getPos()
returns how far the encoder has turned from the zero position converted to distance
virtual float getVel()
calculates velocity in distance per second where distance was set by setdistPerCountFactor()
virtual bool isVelNew()
could be useful for only recalculating a control loop if there's new velocity data
virtual void setDistPerCountFactor(float _factor)
set the conversion factor between encoder ticks and distance
virtual bool hasDirection()
can this encoder measure direction or just speed
virtual long zeroCounter()
reset the counter of how far the encoder has turned