JMotor
Loading...
Searching...
No Matches
JEncoderBSED.h
Go to the documentation of this file.
1#ifndef J_ENCODER_BSED_H
2#define J_ENCODER_BSED_H
3#include "JEncoder.h"
4#include <Arduino.h>
5#include <byte-sized-encoder-decoder.h>
10class JEncoderBSED : public JEncoder {
11
12protected:
13 ByteSizedEncoderDecoder& bsed;
16 int8_t reverse; // can be 1 or -1
17 int32_t zeroVal;
18
19public:
30 JEncoderBSED(ByteSizedEncoderDecoder& _bsed, byte _encoderChannel, bool _reverse = false, float _distPerCountFactor = 1.0, int16_t _slowestIntervalMicros = 0, int16_t _encoderEnoughCounts = 0)
31 : bsed(_bsed)
32 {
33 encoderChannel = constrain(_encoderChannel, 1, 8);
34 if (_reverse) {
35 reverse = -1;
36 } else {
37 reverse = 1;
38 }
39 zeroVal = 0;
40 distPerCountFactor = _distPerCountFactor;
41 bsed.setEncoderSlowestInterval(_encoderChannel, _slowestIntervalMicros);
42 bsed.setEncoderEnoughCounts(_encoderChannel, _encoderEnoughCounts);
43 }
44
46 {
47 zeroVal = bsed.getEncoderPosition(encoderChannel);
48 return zeroVal;
49 }
50 float getVel()
51 {
52 return bsed.getEncoderVelocity(encoderChannel) * distPerCountFactor * reverse;
53 }
55 {
56 return (bsed.getEncoderPosition(encoderChannel) - zeroVal) * reverse;
57 }
58 float getPos()
59 {
60 return (bsed.getEncoderPosition(encoderChannel) - zeroVal) * distPerCountFactor * reverse;
61 }
63 {
64 return distPerCountFactor;
65 }
66 void setDistPerCountFactor(float _factor)
67 {
68 distPerCountFactor = _factor;
69 }
75 void setReverse(bool _reverse)
76 {
77 if (_reverse) {
78 reverse = -1;
79 } else {
80 reverse = 1;
81 }
82 }
84 {
85 return true;
86 }
87 bool isVelNew()
88 {
89 return bsed.isVelNew(encoderChannel);
90 }
91 void run()
92 {
93 }
94};
95
96#endif // JENCODER_QUADRATURE_20COPY_H
reads a quadrature (incremental) encoder that is connected to a Byte Sized Encoder Decoder board conn...
Definition JEncoderBSED.h:10
long getCounter()
returns how far the encoder has turned from the zero position
Definition JEncoderBSED.h:54
float getDistPerCountFactor()
returns a conversion factor between encoder ticks and distance that can be set for the encoder
Definition JEncoderBSED.h:62
byte encoderChannel
Definition JEncoderBSED.h:14
void setReverse(bool _reverse)
reverse readings of encoder
Definition JEncoderBSED.h:75
void setDistPerCountFactor(float _factor)
set the conversion factor between encoder ticks and distance
Definition JEncoderBSED.h:66
JEncoderBSED(ByteSizedEncoderDecoder &_bsed, byte _encoderChannel, bool _reverse=false, float _distPerCountFactor=1.0, int16_t _slowestIntervalMicros=0, int16_t _encoderEnoughCounts=0)
constructor, sets pins and settings
Definition JEncoderBSED.h:30
void run()
if an encoder needs to have some code called each loop (like absolute encoder polling encoder and cal...
Definition JEncoderBSED.h:91
float getVel()
calculates velocity in distance per second where distance was set by setdistPerCountFactor()
Definition JEncoderBSED.h:50
int32_t zeroVal
Definition JEncoderBSED.h:17
float distPerCountFactor
Definition JEncoderBSED.h:15
long zeroCounter()
reset the counter of how far the encoder has turned
Definition JEncoderBSED.h:45
int8_t reverse
Definition JEncoderBSED.h:16
float getPos()
returns how far the encoder has turned from the zero position converted to distance
Definition JEncoderBSED.h:58
bool hasDirection()
can this encoder measure direction or just speed
Definition JEncoderBSED.h:83
ByteSizedEncoderDecoder & bsed
Definition JEncoderBSED.h:13
bool isVelNew()
could be useful for only recalculating a control loop if there's new velocity data
Definition JEncoderBSED.h:87
defines common interface for JEncoder
Definition JEncoder.h:33