Hey thanks so far.
We have combined Audio Codec & PID & are facing an issue.
Whenever we run the code the output is calculated as 0 no matter what!
However, if we comment out Audiocodecinit() (in void setup) & Audiocodecdata() (in ISR) then our pid calculates output values correctly i.e. it starts from 0 and increases till max output limits (we've given hardcoded input value for experimenting)
If we uncomment Audiocodecinit() (in void setup) & Audiocodecdata() (in ISR) then our pid calculates wrong output values i.e. output value = 0
/*
dc_controller.pde
guest openmusiclabs 9.23.12
this takes in a dc value from the codecshield, processes it, and
sends out a dc value on the codecshield.
*/
// setup codec parameters
// must be done before #includes
// see readme file in libraries folder for explanations
#define SAMPLE_RATE 88 // 44.1Khz
#define ADCS 2 // no ADCs are being used
#define ADCHPD 1 // set the codecshield to DC input
// include necessary libraries
#include <Wire.h>
#include <SPI.h>
#include <AudioCodec.h>
// create data variables for audio transfer
int left_in;// = 0x0000;
int left_out;// = 0x0000;
int right_in;// = 0x0000;
int right_out;// = 0x0000;
//TEMP Serial Buffer
String SerialBuffer, temp;
char *buffer;
char TempSerial[39];
int si;
// so much for audio codec
// now for PID
#include <PID_v1.h>
//Define the aggressive and conservative Tuning Parameters
double aggKp=4, aggKi=0.2, aggKd=1;
double consKp=2, consKi=5, consKd=1;
double gap;
double Setpoint, Input, Output, Temp = 0.0;
//Specify the links and initial tuning parameters
//PID myPID(&Input, &Output, &Setpoint, consKp, consKi, consKd, DIRECT);
PID myPID(&Input, &Output, &Setpoint,2,5,1, DIRECT);
void setup() {
Serial.begin(115200);
AudioCodec_init();
Setpoint = 50000;
myPID.SetMode(AUTOMATIC);
//myPID.SetSampleTime(1000);
myPID.SetOutputLimits(0, 65535);
//myPID.SetTunings(consKp, consKi, consKd);
}
void loop() {
//Input = 32768 + left_in;
Input = 45000; //32768 - left_in;
//Input = 5.0; //left_in;
myPID.Compute();
Serial.println(Output);
/*
left_out = Output;
if(Serial.available() > 38) {
for(si=0; si<40; si++){
TempSerial[si] = Serial.read();
}
Serial.flush();
SerialBuffer = TempSerial;
if (SerialBuffer.startsWith("START ") || SerialBuffer.endsWith(" END")) {
temp = SerialBuffer.substring(6,9);
buffer = &temp[0];
consKi = strtod (buffer, NULL);
temp = SerialBuffer.substring(10,13);
buffer = &temp[0];
consKp = strtod (buffer, NULL);
temp = SerialBuffer.substring(14,17);
buffer = &temp[0];
consKd = strtod (buffer, NULL);
temp = SerialBuffer.substring(18,21);
buffer = &temp[0];
aggKi = strtod (buffer, NULL);
temp = SerialBuffer.substring(22,25);
buffer = &temp[0];
aggKp = strtod (buffer, NULL);
temp = SerialBuffer.substring(26,29);
buffer = &temp[0];
aggKd = strtod (buffer, NULL);
temp = SerialBuffer.substring(30,35);
buffer = &temp[0];
Setpoint = strtod (buffer, NULL);
Serial.println("OUTPUT");
}
else {
Serial.println("Err1");
}
}
/*
gap = abs(Setpoint-Input); //distance away from setpoint
if(gap<10)
{ //we're close to setpoint, use conservative tuning parameters
myPID.SetTunings(consKp, consKi, consKd);
}
else
{
//we're far from setpoint, use aggressive tuning parameters
myPID.SetTunings(aggKp, aggKi, aggKd);
}
//AudioCodec_data(&left_in, &right_in, left_out, right_out);
Serial.print("Input : ");
Serial.print(Input);
Serial.print(" Output : ");
Serial.println(Output);
delay(300);
*/
}
ISR(TIMER1_COMPA_vect) {
//AudioCodec_data(&left_in, &right_in, left_out, right_out);
//Input = left_in*3.3/32767;
//myPID.Compute();
//left_out = Output;
}
Any thoughts why?