Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********************************************************
- * PID Basic Example
- * Reading analog input 0 to control analog PWM output 3
- ********************************************************/
- #include <PID_v1.h>
- #include <ModbusRtu.h>
- #define PIN_INPUT 1
- #define PIN_OUTPUT 3
- #define E2 3
- #define I3 3
- #define I4 13
- // data array for modbus network sharing
- uint16_t au16data[16];
- //Define Variables we'll be connecting to
- double Setpoint, Input, Output;
- //Specify the links and initial tuning parameters
- //double Kp=1.0, Ki=0.5, Kd=1.0;
- double Kp=1.4,Ki=0, Kd=0;
- //double Kp=1, Ki=0.18, Kd=0;
- PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
- Modbus slave(1,1,0); // this is slave @1 and RS-232 or USB-FTDI
- void setup()
- {
- //initialize the variables we're linked to
- Setpoint = analogRead(A4);
- Input = analogRead(A5);
- pinMode(E2,OUTPUT);
- pinMode(I3,OUTPUT);
- pinMode(I4,OUTPUT);
- //turn the PID on
- myPID.SetMode(AUTOMATIC);
- myPID.SetSampleTime(50); //200ms
- myPID.SetOutputLimits(0, 255);
- Serial.begin(9600);
- slave.begin( 19200 ); // baud-rate at 19200
- }
- void loop()
- {
- Setpoint = analogRead(A4);
- Input = analogRead(A5);
- myPID.Compute();
- digitalWrite(I3,HIGH);
- digitalWrite(I4,LOW);
- analogWrite(PIN_OUTPUT, Output);
- Serial.print("\n");
- Serial.print(Setpoint);
- Serial.print("\t");
- Serial.print(Input);
- //Serial.print("\t");
- //Serial.print( Output);
- Serial.print("\t");
- Serial.print( 700);
- Serial.print("\t");
- Serial.print( 300);
- au16data[1]= Input;
- au16data[2]= Setpoint-Input;
- //Setpoint = au16data[3];
- Kp = au16data[4];
- Ki = au16data[5];
- Kd = au16data[6];
- myPID.SetMode(AUTOMATIC);
- au16data[7]= Kp;//myPID.GetKp();
- au16data[8]= Ki;//myPID.GetKi();
- au16data[9]= Kd;//myPID.GetKd();
- PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
- slave.poll( au16data, 16 );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement