Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use arrays with PID library #113

Open
simeon9696 opened this issue Jun 29, 2021 · 6 comments
Open

Use arrays with PID library #113

simeon9696 opened this issue Jun 29, 2021 · 6 comments

Comments

@simeon9696
Copy link

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

@br3ttb
Copy link
Owner

br3ttb commented Jun 29, 2021 via email

@simeon9696
Copy link
Author

@br3ttb wouldn't that array of pid objects be my valves array?

@br3ttb
Copy link
Owner

br3ttb commented Jun 30, 2021 via email

@simeon9696
Copy link
Author

@br3ttb your first question, that's a typo and I believe that's what led me to believe that it wasn't working because I was changing the first set point in the array and nothing changed so I thought I was passing it wrong in the first place.

Yes, I'd need to add the size of the actual data type, I think I'll determine that at using sizeof rather than hardcoding it

&output[2] I myself don't know if this will decay into a pointer to the first element in the array or the 3rd element in the array

Though, according to this link under the section 'Passing array to function using call by reference' it should work.

If you have any ideas please let me know! I'll comment here with the working code

@joba-1
Copy link

joba-1 commented Jun 30, 2021 via email

@simeon9696
Copy link
Author

Much thanks to @br3ttb and @joba-1. For some reason, I had to create an array of references to the PID objects.

//Specify the links and initial tuning parameters, these are global variables
double Kp=10, Ki=3, Kd=7;

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], &valveDutyCycle[0], &setPoints[0], Kp, Ki, Kd, DIRECT);
PID aBodyPID(&temperatures[1], &valveDutyCycle[1], &setPoints[1], Kp, Ki, Kd, DIRECT);
PID aBasePID(&temperatures[2], &valveDutyCycle[2], &setPoints[2], Kp, Ki, Kd, DIRECT);
PID bNeckPID(&temperatures[3], &valveDutyCycle[3], &setPoints[3], Kp, Ki, Kd, DIRECT);
PID bBodyPID(&temperatures[4], &valveDutyCycle[4], &setPoints[4], Kp, Ki, Kd, DIRECT);
PID bBasePID(&temperatures[5], &valveDutyCycle[5], &setPoints[5], Kp, Ki, Kd, DIRECT);

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

loop()

// NUM_THERMOCOUPLES is 6
 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(" ");
  }; 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants