2017-11-17 12:20:48 +00:00
|
|
|
//--------------------------------------------------------------
|
|
|
|
//-- Oscillator.pde
|
|
|
|
//-- Generate sinusoidal oscillations in the servos
|
|
|
|
//--------------------------------------------------------------
|
|
|
|
//-- (c) Juan Gonzalez-Gomez (Obijuan), Dec 2011
|
|
|
|
//-- GPL license
|
|
|
|
//--------------------------------------------------------------
|
|
|
|
#ifndef Oscillator_h
|
|
|
|
#define Oscillator_h
|
|
|
|
|
2017-12-03 21:04:39 +00:00
|
|
|
#if defined(ESP32)
|
|
|
|
#include <ESP32_Servo.h>
|
|
|
|
#else
|
|
|
|
#include <Servo.h>
|
|
|
|
#endif
|
2017-11-17 12:20:48 +00:00
|
|
|
|
|
|
|
//-- Macro for converting from degrees to radians
|
|
|
|
#ifndef DEG2RAD
|
|
|
|
#define DEG2RAD(g) ((g)*M_PI)/180
|
|
|
|
#endif
|
|
|
|
|
|
|
|
class Oscillator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Oscillator(int trim=0) {_trim=trim;};
|
|
|
|
void attach(int pin, bool rev =false);
|
|
|
|
void detach();
|
2017-12-03 21:04:39 +00:00
|
|
|
|
2017-11-17 12:20:48 +00:00
|
|
|
void SetA(unsigned int A) {_A=A;};
|
|
|
|
void SetO(unsigned int O) {_O=O;};
|
|
|
|
void SetPh(double Ph) {_phase0=Ph;};
|
|
|
|
void SetT(unsigned int T);
|
|
|
|
void SetTrim(int trim){_trim=trim;};
|
|
|
|
int getTrim() {return _trim;};
|
2017-12-03 21:04:39 +00:00
|
|
|
void SetPosition(int position);
|
2017-11-17 12:20:48 +00:00
|
|
|
void Stop() {_stop=true;};
|
|
|
|
void Play() {_stop=false;};
|
|
|
|
void Reset() {_phase=0;};
|
|
|
|
void refresh();
|
2017-12-03 21:04:39 +00:00
|
|
|
|
2017-11-17 12:20:48 +00:00
|
|
|
private:
|
2017-12-03 21:04:39 +00:00
|
|
|
bool next_sample();
|
|
|
|
|
2017-11-17 12:20:48 +00:00
|
|
|
private:
|
|
|
|
//-- Servo that is attached to the oscillator
|
|
|
|
Servo _servo;
|
2017-12-03 21:04:39 +00:00
|
|
|
|
2017-11-17 12:20:48 +00:00
|
|
|
//-- Oscillators parameters
|
|
|
|
unsigned int _A; //-- Amplitude (degrees)
|
|
|
|
unsigned int _O; //-- Offset (degrees)
|
|
|
|
unsigned int _T; //-- Period (miliseconds)
|
|
|
|
double _phase0; //-- Phase (radians)
|
2017-12-03 21:04:39 +00:00
|
|
|
|
2017-11-17 12:20:48 +00:00
|
|
|
//-- Internal variables
|
|
|
|
int _pos; //-- Current servo pos
|
|
|
|
int _trim; //-- Calibration offset
|
|
|
|
double _phase; //-- Current phase
|
|
|
|
double _inc; //-- Increment of phase
|
|
|
|
double __N; //-- Number of samples
|
|
|
|
unsigned int _TS; //-- sampling period (ms)
|
2017-12-03 21:04:39 +00:00
|
|
|
|
|
|
|
long _previousMillis;
|
2017-11-17 12:20:48 +00:00
|
|
|
long _currentMillis;
|
2017-12-03 21:04:39 +00:00
|
|
|
|
2017-11-17 12:20:48 +00:00
|
|
|
//-- Oscillation mode. If true, the servo is stopped
|
|
|
|
bool _stop;
|
|
|
|
|
|
|
|
//-- Reverse mode
|
|
|
|
bool _rev;
|
|
|
|
};
|
|
|
|
|
2017-12-03 21:04:39 +00:00
|
|
|
#endif
|