-
Notifications
You must be signed in to change notification settings - Fork 690
Expand file tree
/
Copy pathMagneticSensorAnalog.cpp
More file actions
42 lines (32 loc) · 1.35 KB
/
MagneticSensorAnalog.cpp
File metadata and controls
42 lines (32 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "MagneticSensorAnalog.h"
/** MagneticSensorAnalog(uint8_t _pinAnalog, int _min, int _max)
* @param _pinAnalog the pin that is reading the pwm from magnetic sensor
* @param _min_raw_count the smallest expected reading. Whilst you might expect it to be 0 it is often ~15. Getting this wrong results in a small click once per revolution
* @param _max_raw_count the largest value read. whilst you might expect it to be 2^10 = 1023 it is often ~ 1020. Note: For ESP32 (with 12bit ADC the value will be nearer 4096)
*/
MagneticSensorAnalog::MagneticSensorAnalog(uint8_t _pinAnalog, int _min_raw_count, int _max_raw_count){
pinAnalog = _pinAnalog;
cpr = _max_raw_count - _min_raw_count;
min_raw_count = _min_raw_count;
max_raw_count = _max_raw_count;
if(pullup == Pullup::USE_INTERN){
pinMode(pinAnalog, INPUT_PULLUP);
}else{
pinMode(pinAnalog, INPUT);
}
}
void MagneticSensorAnalog::init(){
raw_count = getRawCount();
this->Sensor::init(); // call base class init
}
// Shaft angle calculation
// angle is in radians [rad]
float MagneticSensorAnalog::getSensorAngle(){
// raw data from the sensor
raw_count = getRawCount();
return ( (float) (raw_count - min_raw_count) / (float)cpr) * _2PI;
}
// function reading the raw counter of the magnetic sensor
int MagneticSensorAnalog::getRawCount(){
return analogRead(pinAnalog);
}