#if 0
//Regulácia rýchlosti ťahania filamentu
#include <Arduino.h>
#include <driver/gpio.h>

template<class T> inline Print &operator <<(Print &obj, T arg) {obj.print(arg); return obj;} //Serial <<

#define FILAMENT_DIAMETER 1.750 //[mm]
#define PUL GPIO_NUM_14 //motor pulz
#define DIR GPIO_NUM_12 //motor smer
#define ENA GPIO_NUM_13 //motor zapnutie/vypnutie
#define RESOLUTION 800 // kolko pulzov/otacku -> RESOLUTION OPTIONS: {200, 400, 800, 1600, 3200, 6400}

enum Direction {RIGHT = 1, LEFT = 0};
enum Enable {ON = 1, OFF = 0};

int extrusion_speed = 70; //[RPM] po odladeni bude rychlost extruzie konstantna, preto ju teraz stanovime na 70 otacok/minutu

//PID konstanty
//Ladenie bude prebiehat na realnych zariadeniach
float kp = 1;
float ki= 0;
float kd = 0;
float PID_p = 0;
float PID_i = 0;
float PID_d = 0;

float k_omega2 = 0; //zosilnenie pre rychlost extruzie

//PID premenne
int x = 0; //akcna_velicina
float e = 0; //regulacna_odchylka
float e_prev = 0; //predchadzajuca regulacna odchylka
float currentTime, prevTime, elapsedTime; //aktualny, predchadzajuci a uplynuly cas

//dalsie premenne
float omega1 = 0; //riadiaca velicina 1
float omega2 = 0; //riadiaca velicina 2
int y = 0; //y1 - regulovana velicina 1
float y2 = 0; //regulovana velicina 2
float delta = 5.0; //[s] dopravne oneskorenie
int x_total = 0; //vysledna(celkova) akcna velicina

unsigned long currentMillis = 0;
unsigned long previousMillis = 0;
int pauseTime = 0; //cas medzi impulzmi -> reguluje rychlost otacania

//hlavicky funkcii
void MotorSpeedRPM(int RPM);

void setup()
{
    Serial.begin(115200);
    omega1 = FILAMENT_DIAMETER;
    omega2 = extrusion_speed;
    
    pinMode(PUL,OUTPUT);
    pinMode(DIR,OUTPUT);
    pinMode(ENA, OUTPUT);

    digitalWrite(DIR, RIGHT); // nastavenie smeru otacania motora
    digitalWrite(ENA, ON); // zapnutie motora
}

void loop()
{
    //nacitanie aktualnej regulovanej veliciny 2 (priemer filamentu)
    //pre simulaciu je snimac priemeru vynechany a je za pomoci konzoly zadavana regulovana (snimana) velicina 2
    if(Serial.available()>0) 
    {
        y2 = Serial.parseInt();
    }

    e = omega1 - y2; //Vypocet regulacnej odchylky
    //e *= exp(-delta)//pridanie dopravneho oneskorenia


    //vypis
    Serial << "Požadovaný priemer filamentu = " << omega1 << "\n";
    Serial << "Aktuálny priemer filamentu = " << y2 << "\n\n";

    PID_p = kp * e; //Vypocet P zlozky
    PID_i = PID_i + (ki * e); //vypocet I zlozky
    //if(PID_i > Max) PID_i = Max;
    //else if(PID_i < Min) PID_i = Min;
    prevTime = currentTime; //zapametanie si predchadzajuceho casu
    currentTime = millis(); //nacitanie aktualneho casu
    elapsedTime = (currentTime - prevTime); //vypocet uplynuleho casu
    PID_d = kd*((e - e_prev)/elapsedTime); //vypocet D zlozky
    x = PID_p + PID_i + PID_d; //vypocet akcnej veliciny
    e_prev = e; //zapametanie si aktualnej chyby

    //Vypocet obmedzenia akcnej veliciny na rozsah v RPM
    //5000 [mm/s] - maximalna rychlost tahania
    //300 000 [mm/min]
    //1 otacka tahaca = 88 mm filamentu
    //3409 RPM - maximalna rychlost tahania v ot/min
    //obmedzenie akcnej veliciny na rozsah 0-3409 RPM
    if(x > 3409) x = 3409;
    if(x < 0) x = 0;

    //vypocet celkovej akcnej veliciny pridanim riadiacej veliciny 1 (rychlost extruzie RPM)
    x_total = x + (k_omega2*omega2); //RPM
    MotorSpeedRPM(x_total);
}

void MotorSpeedRPM(int RPM)
{
    //pulz = 5 us
    //1 ot/min = 1 ot/60 000 000us
    //1 ot = 5us * RESOLUTION [us]
    //Pr.
    //RESOLUTION = 1600 -> minimalna doba otacky 8000us
    //60 000 000 - 8000 = 59 992 000us
    //pauseTime = 59 992 000 / RESOLUTION

    pauseTime=(60000000/RPM*RESOLUTION)-5; //doba medzi pulzmi

    currentMillis = millis();
    if(currentMillis - previousMillis > pauseTime)
    {
        previousMillis = currentMillis;
        //zachovanie podmienky = pulz > ( 2.5Î¼S = 2.5us)
        //pulz - aktivna uroven v nule
        digitalWrite(PUL, LOW);
        delayMicroseconds(5); //5us pulz
        digitalWrite(PUL, HIGH);
        y = RPM; // v tomto bode sa predpoklada ze rychlost otacania uz bola nadobudnuta
        //je predpokladane prilis male oneskorenie drivera motora a preto ho zanedbame
    }
}
#endif