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);
}