PID SOS

Hello everybody,
I am fairly new to arduino, and new to this forum. I am making a demo stand where I am suppost to control a throttle body of a car with arduino.
This seemed easy enough at first, a servo or stepper motor is easy to control. However car manufacturers use DC motors with some gears to control the throttle valve.

So I figured I should use a H bridge and a PID code to make the valve to my biding.
I have gotten so far that the valve is not flapping like crazy anymore, but it is still flapping, I have been playing around with the PID settings for tuning, but honestly, I think there is a problem with the rest of the code as well. The mapping of the pot I am using to controle the valve seems off, since the valve only starts flapping when the pot is completely open, and the rest of the time the valve does not move at all.

If anybody has any expierience with diy servo, or projects like this, help would be much apprieciated.

/********************************************************
 * PID Adaptive Tuning Example
 * One of the benefits of the PID library is that you can
 * change the tuning parameters at any time.  this can be
 * helpful if we want the controller to be agressive at some
 * times, and conservative at others.   in the example below
 * we set the controller to use Conservative Tuning Parameters
 * when we're near setpoint and more agressive Tuning
 * Parameters when we're farther away.
 ********************************************************/

#include <PID_v1.h>
#include <CytronMotorDriver.h>
CytronMD motor(PWM_DIR, 3, 4);  // PWM = Pin 3, DIR = Pin 4.

#define PIN_INPUT A0
#define PIN_OUTPUT 3
#define PIN_SETPOINT A1

//Define Variables we'll be connecting to
double Setpoint, Input, Output;

//Define the aggressive and conservative Tuning Parameters
double aggKp=0.25, aggKi=0.2, aggKd=0.25;
double consKp=0.0625, consKi=0.05, consKd=0.0625;

//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint, consKp, consKi, consKd, DIRECT);

void setup()
{
  //initialize the variables we're linked to
  Input = map(analogRead(PIN_INPUT), 1023, 0, 0, 255);
  Setpoint = map(analogRead(PIN_SETPOINT), 470, 920, 0, 255);
  pinMode (3, OUTPUT);
  //turn the PID on
  myPID.SetMode(AUTOMATIC);
}

void loop()
{
  Input = analogRead(PIN_INPUT);

  double 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);
  }
  if (Input < Setpoint)
   {
    motor.setSpeed(10);
    digitalWrite(4, LOW);
   }
    else if (Input > Setpoint)
   {
     motor.setSpeed(10);
     digitalWrite(4, HIGH);
   }
    else
   {
     motor.setSpeed(0);
   }  
    
  myPID.Compute();
  analogWrite(PIN_OUTPUT, Output);
}

If you think there is a problem with the pot, put that code to one side and write a new tester for just the pot.

The worst problem is a glitch from a speck of dust inside the pot. I would take 10 or 100 readings as fast as possible and then print the highest and lowest. Exercise the pot through the whole range and check the two numbers don't get too far apart.

Alter thr PID settings for the different conditions

Take the two sets of settings and write your code so the PID settings are correct for the different conditions

MorganS:
If you think there is a problem with the pot, put that code to one side and write a new tester for just the pot.

The worst problem is a glitch from a speck of dust inside the pot. I would take 10 or 100 readings as fast as possible and then print the highest and lowest. Exercise the pot through the whole range and check the two numbers don't get too far apart.

Ok, I will try that. I have been testing the pot with my multimeter, but your idea seems better. Thank you!

Remove any and all D gain from your PID. You do not need but more importantly, you do not want any derivative action in your control loop for a simple position application. This is probably the source of the instability you are seeing.

Any derivative action in your loop will amplify any noise from the feedback source and create instability. In the process control world, it is said that the D in PID stands for disaster.

Multi-meters all have dampening built into the inputs so the readings do not jump around too much. Your Arduino typically reads much faster, that is why you usually do some sort of debounce on switches.

WattsThat:
Remove any and all D gain from your PID. You do not need but more importantly, you do not want any derivative action in your control loop for a simple position application. This is probably the source of the instability you are seeing.

Any derivative action in your loop will amplify any noise from the feedback source and create instability. In the process control world, it is said that the D in PID stands for disaster.

Removing the disaster seems to help, I still don't have control of the valve, but at least it is not going out of control. Thank you.

look at the mecanical.
you cannot control a thing that will alter the process too fast

if you change your output to the motor by 1 unit, but the process moves 200
and then reducing your output by 1 drops the process by 200 means you mechanical devices are not controllable to anything less. if you want to to control with an resolution of say, 50, it is not possible.

this is why they make proportional valves and proportional linkages. CAM's on belt drives, etc.

once you have confirmed that the mechanics can control your system, only then is it possible to add computer control.
computers cannot fix things that will not work.

PID is not well understood by many and as noted the D is only used in some applications.

if you can alter the P and I near the max and get stability and control.

then log those numbers

then alter them in the mid band, and log those
then alter them yet again for the low end, and log those

you can alter the vaules of P and I and D depending on where you are in the process.

I am using the mechanics of the actual throttle body, it works like this in a car, so it should work with the right software. I managed to rip all the gears though... so the project is stuck for now.