JMotor
Loading...
Searching...
No Matches
JServoCurrentSensor.h
Go to the documentation of this file.
1#ifndef J_SERVO_CURRENT_SENSOR_H
2#define J_SERVO_CURRENT_SENSOR_H
4#include <Arduino.h>
14template <uint16_t N>
16protected:
22 int readings[N];
23 byte readIndex; // the index of the current reading
24 long total; // the running total
25 unsigned long lastMeasurementMillis;
29
30public:
38 JServoCurrentSensor(int _measurePin, int _maxRange = 1000, int _minRange = 0)
39 {
40 measurePin = _measurePin;
42 justStarted = true;
43 total = 0;
44 readIndex = 0;
46 minRange = _minRange;
47 maxRange = _maxRange;
48 }
49 float getMeasurement(bool _run = true)
50 {
51 if (_run) {
52 run();
53 }
54 return constrain(map(total, (long)minRange * N, (long)maxRange * N, 0, 10000), 0, 10000) / 10000.0;
55 }
57 {
58 return measurementRaw;
59 }
61 {
62 return justStarted;
63 }
65 {
66 return minRange;
67 }
69 {
70 return maxRange;
71 }
72 void setMinRange(int min)
73 {
74 minRange = min;
75 }
76 void setMaxRange(int max)
77 {
78 maxRange = max;
79 }
80 void setMeasurementPin(byte _newPin)
81 {
82 measurePin = _newPin;
83 }
85 {
86 return measurePin;
87 }
88 void run()
89 {
90 if (millis() - lastMeasurementMillis > 2) {
91 lastMeasurementMillis = millis();
92 if (!justStarted) {
93 // subtract the last reading:
95 }
96 // read from the sensor:
97 readings[readIndex] = analogRead(measurePin);
98 // add the reading to the total:
100 // advance to the next position in the array:
101 readIndex = readIndex + 1;
102
103 // if we're at the end of the array...
104 if (readIndex >= N) {
105 justStarted = false;
106 // ...wrap around to the beginning:
107 readIndex = 0;
108 }
109 if (!justStarted) {
110 // calculate the average:
112 } else {
113 measurementRaw = 0;
114 }
115 }
116 }
117};
118#endif
implements JServoStallSensing interface.
Definition JServoCurrentSensor.h:15
void setMinRange(int min)
Definition JServoCurrentSensor.h:72
void run()
Definition JServoCurrentSensor.h:88
void setMeasurementPin(byte _newPin)
Definition JServoCurrentSensor.h:80
void setMaxRange(int max)
Definition JServoCurrentSensor.h:76
float getMeasurement(bool _run=true)
Definition JServoCurrentSensor.h:49
int getMaxRange()
Definition JServoCurrentSensor.h:68
bool justStarted
Definition JServoCurrentSensor.h:18
int readings[N]
Definition JServoCurrentSensor.h:22
JServoCurrentSensor(int _measurePin, int _maxRange=1000, int _minRange=0)
constructor for JServoCurrentSensor
Definition JServoCurrentSensor.h:38
unsigned long lastMeasurementMillis
Definition JServoCurrentSensor.h:25
int getMinRange()
Definition JServoCurrentSensor.h:64
int getUnscaledMeasurement()
Definition JServoCurrentSensor.h:56
int maxRange
Definition JServoCurrentSensor.h:28
bool getJustStarted()
Definition JServoCurrentSensor.h:60
long measurementRaw
Definition JServoCurrentSensor.h:26
byte getMeasurementPin()
Definition JServoCurrentSensor.h:84
int measurePin
Definition JServoCurrentSensor.h:17
long total
Definition JServoCurrentSensor.h:24
int minRange
Definition JServoCurrentSensor.h:27
byte readIndex
Definition JServoCurrentSensor.h:23
defines interface for classes that can measure how hard a servo is working
Definition JServoStallSensing.h:7