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

softPwm --> ledcWrite #33

Open
pllagunos opened this issue Jun 20, 2023 · 1 comment
Open

softPwm --> ledcWrite #33

pllagunos opened this issue Jun 20, 2023 · 1 comment

Comments

@pllagunos
Copy link

Hi.

In my application I am using a ESP32 driving a thrystor using 8 bit PWM. According to the manufacturer the minimum PWM frequency should be 1kHz. Using softPwm, this would mean the windowSize should be < 1 (since millis is used). Since in the .cpp, time variables are uint32_t, the maximum frequency using softPwm would be 1kHz, right?

Then what I don't understand is what would happen with the following MAX31856 example:
`void loop() {
float optimumOutput = tuner.softPwm(relayPin, Input, Output, Setpoint, outputSpan, debounce);

switch (tuner.Run()) {
case tuner.sample: // active once per sample during test
if (!digitalRead(drdyPin)) Input = maxthermo.readThermocoupleTemperature();
tuner.plotter(Input, Output, Setpoint, 0.5f, 3); // output scale 0.5, plot every 3rd sample
break;

case tuner.tunings: // active just once when sTune is done
  tuner.GetAutoTunings(&Kp, &Ki, &Kd); // sketch variables updated by sTune
  myPID.SetOutputLimits(0, outputSpan * 0.1);
  myPID.SetSampleTime(outputSpan - 1);

...`

because when I set outputSpan = 1 (to get the necesarry 1kHz frequency of the PWM) then the PID output will be limited to a max of 0.1.

Would using a function like ledcWrite, which allows frequency to be specified, help solve this issue?

@Larsuu
Copy link

Larsuu commented Jan 21, 2025

i just put the ledcWrite in the tuner.sample, as it runs every n'th time (testTime/SampleTime?).

then, the tuner.tunings is ran after it completes.

but i was doing just simple inflection point with a step output.

In your question, the output is shown as it is outputspan * 0.1. Als, there is 0.5f in the tunePlotter.

You could do:

in the tuner.sample:
ledcWrite(OutPut, OptimumOutput)
tuner.tunings:
tuner.getAutoTunings();

Simply in arduino language: tuner.sample is the loop() and tuner.tunings is the setup() of your code after loop has been ran, so it kind of runs backwards so to speak, first loop then setup.. which is left for the new code with new tunings.

`void Battery::runTune() {

        optimumOutput = tuner.softPwm(  heaterPin, 
                                        battery.heater.pidInput, 
                                        battery.heater.pidOutput, 
                                        battery.heater.pidSetpoint, 
                                        battery.stune.outputSpan, 0);

        switch (tuner.Run()) {

            case tuner.sample:
                battery.heater.pidInput = battery.temperature;
                ledcWrite(PWM_CHANNEL, battery.stune.outputStep);
                tuner.plotter(battery.heater.pidInput, battery.heater.pidOutput, battery.heater.pidSetpoint, 0.1f, 10); // Plotting
                break;

            case tuner.tunings: 
                tuner.GetAutoTunings(&battery.stune.pidP, &battery.stune.pidI, &battery.stune.pidD); // Get tuned values

                Serial.print("heaterP: ");
                Serial.print(battery.heater.pidP);
                Serial.print("stuneP ");
                Serial.print(battery.stune.pidP);
                Serial.print("heaterI: ");
                Serial.print(battery.heater.pidI);
                Serial.print("stuneI ");
                Serial.print(battery.stune.pidI);
                Serial.print("heaterD: ");
                Serial.print(battery.heater.pidD);
                Serial.print("stuneD ");
                Serial.println(battery.stune.pidD);

                float contra = tuner.GetTau() / tuner.GetDeadTime();`

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

2 participants