Arduino-PID-Library icon indicating copy to clipboard operation
Arduino-PID-Library copied to clipboard

Use arrays with PID library

Open simeon9696 opened this issue 3 years ago • 6 comments

Would it be possible to use arrays with the constructors? I have six valves with different input, output and setpoints so I wanted to know if could do something like below:

//Specify the links and initial tuning parameters
double Kp=10, Ki=2, Kd=5;

double temperatures[] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
double setPoints[] = {25.0, 25.0, 25.0, 25.0, 25.0, 25.0};
double valveDutyCycle[] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};


//Define Variables we'll be connecting to


PID aNeckPID(&temperatures[0] + 0, &valveDutyCycle[0] + 0, &setPoints[0] + 5, Kp, Ki, Kd, REVERSE);
PID aBodyPID(&temperatures[0] + 1, &valveDutyCycle[0] + 1, &setPoints[0] + 5, Kp, Ki, Kd, REVERSE);
PID aBasePID(&temperatures[0] + 2, &valveDutyCycle[0] + 2, &setPoints[0] + 5, Kp, Ki, Kd, REVERSE);
PID bNeckPID(&temperatures[0] + 3, &valveDutyCycle[0] + 3, &setPoints[0] + 5, Kp, Ki, Kd, REVERSE);
PID bBodyPID(&temperatures[0] + 4, &valveDutyCycle[0] + 4, &setPoints[0] + 5, Kp, Ki, Kd, REVERSE);
PID bBasePID(&temperatures[0] + 5, &valveDutyCycle[0] + 5, &setPoints[0] + 5, Kp, Ki, Kd, REVERSE);

PID valves[] = {aNeckPID, aBodyPID, aBasePID, bNeckPID, bBodyPID, bBasePID};

Then in loop() do:

 for (int i = 0; i < NUM_THERMOCOUPLES; i++)
  {
    valves[i].Compute();
   //pwm1.setPWM(i, 0, valveDutyCycle[i]); 
    Serial.print("VALVE: ");
    Serial.print(i);
    Serial.print(" IN: ");
    Serial.print(temperatures[i]);
    Serial.print(" SET: ");
    Serial.print(setPoints[i]);
    Serial.print(" C ");
    Serial.print(" OUT: ");
    Serial.print(valveDutyCycle[i]);
    Serial.println(" ");
  }; 

The valves are controlled via a PWM signal and I have 6 valves

simeon9696 avatar Jun 29 '21 19:06 simeon9696