#include <Arduino.h>

#include "motor.h"
#include "integrator.h"
#include "PID.h"
#include "Controller.h"
#include "display.h"

Rotax915iS motor(600.0);
Rotax915iS motor_nn(600.0);
Integrator throttle(1.5);
Integrator throttle_nn(1.5);
PID pid;
CONTROLLER controller;

float fordulatszam = 0.0f;
float fordulatszam_nn = 0.0f;

static void IRAM_ATTR pid_task(void* arg)
{
  Serial.begin(115200);
  //for (int i = 0; i < 1000; i++)
  //  Serial.println("0.0");

  TickType_t xLastWakeTime = xTaskGetTickCount();

  for (;;) {
    xTaskDelayUntil(&xLastWakeTime, 100);

    static float teher = 2.0; //2.625; //Nm

    static float thr, thr_nn;

    static float z = 0;
    static bool p = 0;

    if (digitalRead(14) == LOW)
      controller.print();

    if (digitalRead(4) == LOW) {
      /*static double x = 0;
        x += .2;
        double s = sin(x);
        teher = 140 + s * 20;*/
      float cc = random(0, 20);
      teher += random(-cc, cc) + z;
    }
    if (digitalRead(17) == LOW)
      teher += 10;
    if (digitalRead(18) == LOW)
      teher -= 10;

    if (teher < 2.0)
      teher = 2.0;
    if (teher > 160)
      teher = 160;

    if (z < 1.0) {
      if (p == true)
        z += 0.01;
    } else p = false;
    if (z > 0.0) {
      if (p == false)
        z -= 0.01;
    } else p = true;

    float governor = 0;
    float governor_nn = 0;
    float error = 1.0 - fordulatszam / 5500.0;
    float error_nn = 1.0 - fordulatszam_nn / 5500.0;

    governor = pid.update(error);

    if (digitalRead(10) == LOW) {
      controller.setMode(PID_TRAINING);
      governor_nn = controller.update(error, governor);
    } else {
      controller.setMode(SELF_LEARNING);
      governor_nn = controller.update(error_nn);
    }

    thr = throttle.update(governor);

    if (digitalRead(10) == LOW) {
      thr_nn = throttle_nn.update(governor);
    } else
      thr_nn = throttle_nn.update(governor_nn);

    if (thr < 0.39)
      thr = 0.39;
    if (thr_nn < 0.39)
      thr_nn = 0.39;

    fordulatszam = motor.rpm(thr * 100.0, teher);
    fordulatszam_nn = motor_nn.rpm(thr_nn * 100.0, teher);

    Serial.print( (fordulatszam / 5500 - 1) * 10 + 1.2, 2);
    Serial.print( "," );
    Serial.print( (fordulatszam_nn / 5500 - 1) * 10 + 1.2, 2);
    Serial.print( "," );
    Serial.print( thr, 2);
    Serial.print( "," );
    Serial.print( governor, 2);
    Serial.print( "," );
    Serial.print( governor_nn, 2);
    Serial.print( "," );
    Serial.print( thr_nn, 2);
    Serial.print( "," );
    Serial.print( teher / 170, 2);
    Serial.print( "," );
    Serial.print( "1.2");
    Serial.print( "," );
    Serial.println( w[10]* 10 + 1.2, 2);
  }
}


void setup() {
  setCpuFrequencyMhz(240);

  pid.set_params(6.5, 0, 1.23);

  pinMode(4, INPUT_PULLUP);
  pinMode(17, INPUT_PULLUP);
  pinMode(18, INPUT_PULLUP);
  pinMode(10, INPUT_PULLUP);
  pinMode(14, INPUT_PULLUP);

  delay(1000);

  xTaskCreatePinnedToCore(pid_task, "pid_task", 65536, NULL, 2, NULL, 1);
  xTaskCreatePinnedToCore(display_task, "display_task", 4096, NULL, 0, NULL, 0);

  vTaskDelete(NULL);
}

void loop() {}
Loading
esp32-s3-devkitc-1