#include "QuadDecode.h"
#include "AD9837.h"
#define pin_motor_direction 9 // for toggling the direction (left and right) of the motor
#define pin_enable_pushpull_converter 17 // to turn the push pull converters on and off
#define pin_test 20 // test pin for various debugging
const long interval = 107000; //Intervall in MIKROSEKUNDEN!
const int cts = 19; //cts pro Intervall - 1 weniger als angegeben! (!!!Schon vorher prüfen?!!!)
volatile bool motorTurning = false;
bool dutyCycleReached = true;
const double minfrequency = 48000;
double frequency = 55000;
double initialfrequency;
float dutyCycle = 0.25;
AD9837 ad9837(frequency*4);
QuadDecode_t QuadDecode(cts);
IntervalTimer motorTimer; //Intervaltimer des Motors
IntervalTimer dutyCycleTimer; //Hilfsintervall zur Überprüfung vom eingestellten Duty Cycle
elapsedMillis changeCTS;
bool switched = false;
void setup() {
tone(2, 2000, 200);
// TURN OFF PUSH PULL CONVERTER
pinMode(pin_enable_pushpull_converter,OUTPUT);
digitalWrite(pin_enable_pushpull_converter, LOW);
pinMode(pin_test, OUTPUT);
digitalWrite(pin_test, LOW);
// SETUP MOTOR DIRECTION
pinMode(pin_motor_direction,OUTPUT); // set TOGGLE to OUTPUT
digitalWrite(pin_motor_direction, LOW); // set high for one motor direction
Serial.begin(115200);
delay(1000);
ad9837.setfrequency(frequency * 4);
// TURN ON PUSH PULL CONVERTER
digitalWrite(pin_enable_pushpull_converter, HIGH);
motorTurning = false;
while(!motorTurning){
if(frequency < minfrequency){
frequency = 49000;
}
frequency = frequency - 100;
ad9837.setfrequency(frequency * 4);
delay(300);
Serial.println(frequency);
}
digitalWrite(pin_enable_pushpull_converter, LOW);
frequency = frequency - 1000;
Serial.println(frequency);
ad9837.setfrequency(frequency * 4);
initialfrequency = frequency;
tone(2, 400, 200);
delay(200);
tone(2, 800, 200);
delay(200);
tone(2, 2000, 200);
Serial.println("Start");
digitalWrite(pin_enable_pushpull_converter, HIGH);
motorTimer.begin(motorLoop, interval);
dutyCycleTimer.begin(dutyCycleLoop, interval*dutyCycle);
changeCTS = 0;
}
void loop() {
if(changeCTS >= 10000){
changeCTS = 0;
if(switched){
QuadDecode.call_changeCTS(50);
}
else{
QuadDecode.call_changeCTS(10);
}
switched = !switched;
}
}
//ISR FUNKTIONEN
//ISR Counter-Overflow: Erforderliche Anzahl Flanken erreicht
void ftm2_isr(void) {
digitalWrite(pin_enable_pushpull_converter, LOW);
motorTurning = true;
// clear overflow flag
FTM2_SC&=0x7F;
//digitalWrite(pin_test, LOW);
}
//Duty Cycle Überprüfung
void dutyCycleLoop(){
if(motorTurning) {
dutyCycleReached = true;
}
dutyCycleTimer.end();
}
//Motor Cycle Loop
void motorLoop(){
//falls Flanken NICHT erreicht!
if(!motorTurning){
Serial.println("Dreht nicht! Dreht zu langsam! Erreicht erforderliche Flanken nicht! Frequenz herabsetzen.");
frequency -= 250;
ad9837.setfrequency(frequency * 4);
}
//falls Duty Cycle nicht erreicht!
else if(!dutyCycleReached){
Serial.println("Dreht, aber Duty Cycle wurde nicht erreicht! Frequenz herabsetzen.");
frequency -= 250;
ad9837.setfrequency(frequency * 4);
}
//falls unter die Mindestfrequenz gerutscht - setze Duty Cycle rauf, nochmal von vorne!
if(frequency <= minfrequency){
Serial.println("Innerhalb des vorgegebenen Duty Cycle nicht möglich! Erlaube höheren Duty Cycle.");
frequency = initialfrequency;
dutyCycle += 0.1;
ad9837.setfrequency(frequency * 4);
if(dutyCycle > 0.95){
while(1){
tone(2, 4500, 100);
delay(200);
}
}
}
//ansonsten: "ganz normaler" Durchlauf, setze Variablen zurück, "starte" Motor erneut
motorTurning = false;
dutyCycleReached = false;
dutyCycleTimer.begin(dutyCycleLoop, interval * dutyCycle);
digitalWrite(pin_enable_pushpull_converter, HIGH);
}