-
Notifications
You must be signed in to change notification settings - Fork 0
/
Oscillator.h
55 lines (50 loc) · 1.48 KB
/
Oscillator.h
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
43
44
45
46
47
48
49
50
51
52
53
54
55
// Oscillator.pde Generate sinusoidal oscillations in the servos
// GPL license (c) Juan Gonzalez-Gomez (Obijuan), Dec 2011
// OttDIY Arduino Project, 2020 | sfranzyshen
#ifndef Oscillator_h
#define Oscillator_h
#if defined(ESP32)
# include <ESP32Servo.h>
#else
# include <Servo.h>
#endif
#ifndef DEG2RAD // Macro for converting from degrees to radians
#define DEG2RAD(g) ((g) * M_PI) / 180
#endif
class Oscillator {
public:
Oscillator(int trim = 0);
void attach(int pin, bool rev = false);
void detach();
void setA(int A);
void setO(int O);
void setPh(double Ph);
void setT(unsigned int T);
void setTrim(int trim);
int getTrim();
void setPosition(int position);
void stop();
void play();
void reset();
void refresh();
private:
bool _next_sample();
Servo _servo; // Servo that is attached to the oscillator
// Oscillators parameters
int _A; // Amplitude (degrees)
int _O; // Offset (degrees)
unsigned int _T; // Period (miliseconds)
double _phase0; // Phase (radians)
// Internal variables
int _pos; // Current servo pos
int _trim; // Calibration offset
double _phase; // Current phase
double _inc; // Increment of phase
double _NS; // Number of samples
unsigned int _TS; // sampling period (ms)
long _previousMillis;
long _currentMillis;
bool _stop; // Oscillation mode. If true, the servo is stopped
bool _rev; // Reverse mode
};
#endif