PID shunt regulator

Hi guys,
For a project I use a mosfet and a PWM signal to control voltage over a train (by using a different duty cycle I can change the resistance of the mosfet -> different voltage).
But my code doesn't seem to work, it only gives 0 as output.

#include <PID_v1.h>

const int SpanningTreinLezer= A0;
const int MosfetPin = 6;
const int WensSpanningLezer = A2;
double alpha;
double Kp=-5, Ki=0.5, Kd=0;
double WerkelijkeTreinspanning;
double WensTreinspanning;
PID myPID(&WerkelijkeTreinspanning,&alpha,&WensTreinspanning,Kp,Ki,Kd,DIRECT); //Normally this should attain voltage we want in 6sec
int PIDtijd = -6000;

void setup() {
  pinMode(MosfetPin,OUTPUT);
  myPID.SetMode(AUTOMATIC);
  Serial.begin(19200);


}

void loop() {
  double WerkelijkeTreinspanning = analogRead(SpanningTreinLezer)*20.0/1023.0; // Voltage that the train has on the moment.
  int beta;
  double WensTreinspanning = 4+analogRead(WensSpanningLezer)*5/1023.0; //Voltage we want the train to have
  if (WerkelijkeTreinspanning<4 && (millis()-PIDtijd)>6000)) {
     analogWrite(MosfetPin,beta);
  }
  if (WerkelijkeTreinspanning >4 && (millis()-PIDtijd)>6000) { // We give PID 6secs to calculate
    PIDtijd = millis();
    myPID.Compute(); // This should compute alpha but only gives 0!!
    int beta = alpha*255;
    analogWrite(MosfetPin,beta);
  Serial.println(alpha);
  }
}

The PID only gives alpha=0, and I don't know what's wrong.
I will also add system. Btw I can change wenstreinspanning (by turning a potentiometer).
Here's the circuit

Hello
well at least the calculations using the millis() function requires the datatype unsigned long.

unsigned long PIDtijd = 0;

Thanks,
I'm using this library btw:
https://playground.arduino.cc/Code/PIDLibrary/

Which controller is used? Why *255?
Some Arduino only uses byte, 8 bits in analogWrite....

PI, I gave a Kc and Ti not equal to 0 and Td=0.
I use the *255, because the PI controller should give a duty ratio between 0 and 1
Which I will convert to a PWM signal (which needs the *255)

Okey, I misread one declaration.
Add some temporary Serial.println of the key variables just before analogWrite and watch Serial monitor to see where things go wrong.
Which controller?

Hi,
What model Arduino are you using?

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

Uno.
Which controller? I guess a PI one.
I changed my code to the following:

#include <PID_v1.h>

const int SpanningTreinLezer= A0;
const int MosfetPin = 6;
const int WensSpanningLezer = A2;
double alpha;
double WerkelijkeTreinspanning;
double WensTreinspanning;
double Input,Output,Setpoint;
PID myPID(&Input,&Output,&Setpoint,5,0.5,0,REVERSE); //Normally this should attain voltage we want in 6sec
unsigned long PIDtijd = -6000;

void setup() {
  pinMode(MosfetPin,OUTPUT);
  myPID.SetMode(AUTOMATIC);
  myPID.SetSampleTime(6000);
  Serial.begin(19200);


}

void loop() {
  double WerkelijkeTreinspanning = analogRead(SpanningTreinLezer)*20.0/1023.0; // Voltage that the train has on the moment.
  int beta;
  double WensTreinspanning = 4+analogRead(WensSpanningLezer)*5/1023.0; //Voltage we want the train to have
  Input = analogRead(SpanningTreinLezer)*20.0/1023.0;
  Setpoint = 4+analogRead(WensSpanningLezer)*5/1023.0;
  if (WerkelijkeTreinspanning<4) {
     analogWrite(MosfetPin,255);
  }
  if (WerkelijkeTreinspanning >4 && (millis()-PIDtijd)>6000) { // We give PID 6secs to calculate
    PIDtijd = millis();
    myPID.Compute(); // This should compute alpha but only gives 0!!
    int beta = alpha*255;
    analogWrite(MosfetPin,beta);
  }
  else {
    analogWrite(MosfetPin,beta);
  }
  Serial.println(Output);
  }

As you can see my PID library generates an output (which I checked with serialprint) it indeed calculates something. Which is a duty cycle 'alpha' of a pwm so 0.8 means 80% of the time I want a high pwm pulse to my mosfetpin. (it's after the line myPID.compute())
But I don't how i can change the output signal in beta, can I do something like this: beta=Output*255?
Thanks, snor

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.