Pages: [1]   Go Down
Author Topic: PID does not sense negative values.  (Read 969 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello. I am trying to set up a system of Servomotor and a combo board (gyro + accelerometers). I am using PID and the setups is such: setpoint is zero degrees, input is angle from the combo board and output is Amplitude of rotation of servo arm on my servo. I made it work, but it only works when incline the combo board in one direction and gives positive angles. however, when incline the other way and it gives negative angles PID does not do anything. Is there any solution to that?
Logged

Montreal
Offline Offline
Faraday Member
**
Karma: 27
Posts: 2564
Per aspera ad astra.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Looks as extract from encrypt comms in CERN, something related to Large Hadron Collider  smiley-cool
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is my question unclear?
Logged

Montreal
Offline Offline
Faraday Member
**
Karma: 27
Posts: 2564
Per aspera ad astra.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Obviously.   

http://arduino.cc/forum/index.php/topic,97455.0.html
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am working on a project, which involves PID controller, Arduino, IMU combo board (gyro + accelerometer) and a servomotor. The aim is to keep IMU horizontal at zero degrees, when it inclines amplitude of oscillation of a servo arm on my servomotor increases and when IMU is back in horizontal position amplitude is zero. I set my PID as such: Input = angles from IMU, Output = Amplitude, Setpoint = 0 degrees. It works fine in one direction when angles from IMU are positive, however, in the other direction when angles are negative PID does not respond. Could anyone help me to find a solution for that problem?

Here is a code to make it easier to understand:
Code:
#include <PID_v1.h>

#include <FreeSixIMU.h>
#include <FIMU_ADXL345.h>
#include <FIMU_ITG3200.h>

#include <Wire.h>


double Setpoint, Input, Output;
PID myPID(&Input, &Output, &Setpoint,2,5,1, REVERSE);

int Ampl = 0;

float angles[3]; // yaw pitch roll

// Set the FreeSixIMU object
FreeSixIMU sixDOF = FreeSixIMU();

void setup() {
  Serial.begin(9600);
  Wire.begin();
 
  Input = angles[2];
  Setpoint = 0.00;
  myPID.SetMode(AUTOMATIC);
 
  delay(5);
  sixDOF.init(); //begin the IMU
  delay(5);
}

void loop() {
 
  sixDOF.getEuler(angles);

  Input = angles[2]; 
  myPID.Compute();
  myPID.SetOutputLimits(0, 180);
  Ampl = Output;
 
 
  Serial.print(angles[0]);
  Serial.print(" | "); 
  Serial.print(angles[1]);
  Serial.print(" | ");
  Serial.print(angles[2]);
  Serial.print(" | ");
  Serial.println(Ampl);
 
  delay(100);
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I managed to solve this problem by introducing two PIDs in the code and multiplying input for one of them by (-1), however, it is not an elegant solution and I am still looking for a solution using single PID?
Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12534
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't think it's related to your problem, but I think the call to SetOutputLimits() should be in setup(), not in loop().

I can't see any reason why that wouldn't work for negative input values. Can you post some examples of the trace output with positive and negative input values?
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

For example:

Angle    Amplitude
10        10
20        20
80        80
0          0
-10       0
-30       0
-80       0

I made it up myself, but this is what I see is going on when reading a serial monitor.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12534
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well, you've set the output limits to 0, 180, so it can't go negative. What do you expect it to do in that case?
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I did not want getting any negative amplitudes. I was expecting it to increase amplitude when angle is not zero.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12534
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I did not want getting any negative amplitudes. I was expecting it to increase amplitude when angle is not zero.

It can't 'increase amplitude when angle is not zero' when the angle is negative, because you've prevented it from outputting a negative value. The PID has run into the end stops that you have defined, and it can't go any further.

Please show a real example of where it has produced the wrong behaviour and explain what you wanted it to do.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It would seem that you would want the output limits to be -90 to 90. Then, add 90 to the actual output to get a value between 0 and 180 to write to the servo.

What, exactly, is PID doing for you?
Logged

Pages: [1]   Go Up
Jump to: