Hello,
This is my first time experimenting with an Arduino. I have very minimal experience in electronic, and much less in writing any sort of programming. With much help from google and an acquaintance that has a lot of experience I managed to load a PID program and get it working with my setup. Now I need to adjust the output PWM frequency to work with the valve I am using. I have tried many different methods and suggestions that I have found through searches with no success.
The setup:
I have a closed loop pressure system using the Uno to read a 5v pressure sensor, a potentiometer to adjust pressure, and a pilot operated valve to regulate the pressure. The pressure is to be adjustable from 0-4000psi. This valve is from an automotive application and I do not have technical data on the valve, from what I gather the valve was designed to run on a 3300Hz frequency and a 15-85% duty cycle
The Uno output is fed to a SSR with a 12v power supply providing power to the valve. Currently the frequency is so low that the pressure pulses extremely hard, and becomes worse as the pressure is increased.
Question is - how can I incorporate a function to adjust the PWM frequency within my current sketch?
The sketch I am using thus far without any PWM changes is as follows:
#include <PID_v1.h>
const int ICP = A0; // ICP Sensor input
const int pot = A1; // Potentiometer input
const int IPR = 6; // IPR valve
double ICPLevel; // variable that stores the incoming ICP level
// Tuning parameters
float Kp=0; //Initial Proportional Gain
float Ki=5; //Initial Integral Gain
float Kd=0; //Initial Differential Gain
double Setpoint, Input, Output; //These are just variables for storingvalues
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
// This sets up our PDID Loop
//Input is our PV
//Output is our u(t)
//Setpoint is our SP
const int sampleRate = 1; // Variable that determines how fast our PID loop
// runs
// Communication setup
const long serialPing = 500; //This determines how often we ping our loop
// Serial pingback interval in milliseconds
unsigned long now = 0; //This variable is used to keep track of time
// placehodler for current timestamp
unsigned long lastMessage = 0; //This keeps track of when our loop last
// spoke to serial
// last message timestamp.
void setup(){
ICPLevel = analogRead(ICP); //Read pressure level
Input = map(ICPLevel, 0, 1024, 0, 255); //Change read scale to analog
// out scale
Setpoint = map(analogRead(pot), 0, 1024, 0, 255);
//get our setpoint from our pot
Serial.begin(9600); //Start a serial session
myPID.SetMode(AUTOMATIC); //Turn on the PID loop
myPID.SetSampleTime(sampleRate); //Sets the sample rate
Serial.println("Begin"); // Hello World!
lastMessage = millis(); // timestamp
}
void loop(){
Setpoint = map(analogRead(pot), 0, 1024, 0, 255); //Read our setpoint
ICPLevel = analogRead(ICP); //Get the pressure level
Input = map(ICPLevel, 0, 1024, 0, 255); //Map it to the right scale
myPID.Compute(); //Run the PID loop
analogWrite(IPR, Output); //Write out the output from the PID loop to our LED pin
now = millis(); //Keep track of time
if(now - lastMessage > serialPing) { //If it has been long enough give us
// some info on serial
// this should execute less frequently
// send a message back to the mother ship
Serial.print("Setpoint = ");
Serial.print(Setpoint);
Serial.print(" Input = ");
Serial.print(Input);
Serial.print(" Output = ");
Serial.print(Output);
Serial.print("\n");
if (Serial.available() > 0) { //If we sent the program a command deal
// with it
for (int x = 0; x < 4; x++) {
switch (x) {
case 0:
Kp = Serial.parseFloat();
break;
case 1:
Ki = Serial.parseFloat();
break;
case 2:
Kd = Serial.parseFloat();
break;
case 3:
for (int y = Serial.available(); y == 0; y--) {
Serial.read(); //Clear out any residual junk
}
break;
}
}
Serial.print(" Kp,Ki,Kd = ");
Serial.print(Kp);
Serial.print(",");
Serial.print(Ki);
Serial.print(",");
Serial.println(Kd); //Let us know what we just received
myPID.SetTunings(Kp, Ki, Kd); //Set the PID gain constants and start
// running
}
lastMessage = now;
//update the time stamp.
}
}
Thanks everyone!
-Jon