moved PID to seperate routine

This commit is contained in:
Jens Noack 2023-03-22 07:51:27 +01:00
parent 42c59a01b4
commit cdafb2a107
2 changed files with 27 additions and 10 deletions

View file

@ -55,7 +55,7 @@ Servo ESC; // Der ESC-Controller (Electronic Speed Controller bzw. elektroni
Oled display;
const unsigned long UPDATE_TURN_VALUES_EVERY_MS = 1000;
const unsigned long UPDATE_ESC_EVERY_MS = 0;
const unsigned long UPDATE_ESC_EVERY_MS = 500;
const uint8_t HALL_SENSORS_COUNT = 8; // input, sec, min, hour, day, week, month, year
const uint8_t ALL_DATA_COUNT = HALL_SENSORS_COUNT;
@ -101,10 +101,10 @@ const uint8_t HALL6_PIN = 26;
const uint8_t HALL7_PIN = 27;
const uint8_t HALL8_PIN = 14;
const uint16_t MIN_SPEED = 500;
const uint16_t MAX_SPEED = 3000;
const uint16_t MIN_SPEED = 800;
const uint16_t MAX_SPEED = 5000;
const uint8_t MAX_ESC_SPEED = 100;
const uint8_t MIN_ESC_SPEED = 36;
const uint8_t MIN_ESC_SPEED = 10;
const uint16_t MAX_POTI_VALUE = 4095;
const uint16_t JITTER_POTI_PERCENT = 10;

View file

@ -70,14 +70,16 @@ void setup()
Serial.println("Init ETC ...");
ESC.attach(ESC_PIN,1000,2000);
display.progressBar(0);
ESC.write(180);
delay(5000);
ESC.write(0);
for(uint8_t i=0;i<=100;i=i+10)
{
display.progressBar(i);
delay(500);
ESC.write(0);
delay(1000);
}
ESC.write(0);
data_init();
// Initialise timer
@ -170,7 +172,15 @@ void PID()
double int_integ = integ + int_err;
double int_derivative = (int_err - last_error);
double int_output = kp * int_err + ki * int_integ + kd * int_derivative;
if((int_output >= MIN_SPEED) && (int_output <= MAX_SPEED))
if( int_output <= MIN_SPEED)
{
output = MIN_SPEED;
}
else if( int_output > MAX_SPEED)
{
output = MAX_SPEED;
}
else if((int_output >= MIN_SPEED) && (int_output <= MAX_SPEED))
{
err = int_err;
integ = int_integ;
@ -402,8 +412,15 @@ void get_serial_cmd()
ser_esc_output = 0;
Serial.printf("Stop drive.\n");
}
else if(command.equals("pidreset")){
err = 0.0;
last_error = 0.0;
integ = 0.0;
Serial.printf("Reset PID variables.\n");
}
else if(command.equals("help")){
Serial.printf("Commands: kp,ki,kd=x.xxxx | params | control | max | min | start | stop | speed=x | esc=x help \n");
Serial.printf("Commands: kp,ki,kd=x.xxxx | params | control | max | min | start | stop | speed=x | esc=x | reset | help \n");
}
else{
Serial.printf("Invalid command.");