Closed Loop DC motor position control

Hi guys…so basicly i’m ultra new to programing…and managed somehow to get this far. I am bulding a small controler for a electronic throttle, basicly, i have a 0-5 v potentiometer instead of mu throttle pedal, and a dc motor with a 0-5 v potentiometer on it…i have built a little code that works pretty well, but the butterfly is opened by the vacum and the boost, being driven just up, and drove back down with a spring in it, making it close relatively slow. I curretly have a l298n h bridge driver, but since the motor is rated at 2amp peak, it get’s hot when i try to make it work in h bridge mode. I have ordered a 10amp dc motor controler, wich runs either in Sign-Magnitude PWM or Locked-Antiphase PWM, so, when my digital output is low, the butterfly drives down, when my output is high, it goes the other up. I was thinking of making it work with pid, can you guys shom me an example code on how to do that, i,m a bit confused of what i found on the net. Here is where i’m standing at the moment:
Thanks a bunch!

int pot = A0; //pedal position sensor
int tps = A1; //motor position sensor
int potin;
int tpsin;
int led = 13;

void setup() {

TCCR2B = TCCR2B & 0b11111000 | 0x01; //sets PWM frequency

digitalWrite(led, HIGH);

}

void loop() {

potin = analogRead(pot);
tpsin = analogRead(tps);
potin = map(potin, 240, 400, 0, 1023);
tpsin = map(tpsin, 400, 600, 0, 1023);

if(tpsin < potin) {

if (potin < 100) {

analogWrite(3, 80);
}
if (potin > 100 && potin < 300) {
analogWrite(3, 100);
}
if (potin > 300) {
analogWrite(3, 120);
}
potin = analogRead(pot);
tpsin = analogRead(tps);
potin = map(potin, 240, 400, 0, 1023);
tpsin = map(tpsin, 400, 600, 0, 1023);
}

if(tpsin > potin) {

analogWrite(3, 0);

potin = analogRead(pot);
tpsin = analogRead(tps);
potin = map(potin, 240, 400, 0, 1023);
tpsin = map(tpsin, 400, 600, 0, 1023);
}
}

With a PID you need to identify the error signal, which must be a signed value you want to drive towards zero. Typically you have a setpoint and an measured value, and the error is the setpoint minus the measured value.

The PID then processes this error difference and gives an output value, which needs converting to your control output value, here the magnitude of PWM and direction signals in a smooth transfer characteristic.

You should be processing new values at a fixed rate, and probably quite frequently, perhaps 1 to 10 kHz for
motor control, or the latency will make it very unstable.

In the real world you'll often need a PID library or code that supports suppression of integral wind-up, but
that's a secondary consideration from actually closing a loop and getting it stable.

well, my error would be the difference between the pedal 0-1023 and the motors 0-1023 position, right ? this new motor driver is driven as shown in this video: https://www.youtube.com/watch?v=QRMdA-43v84

one pin determines the speed via pwm, and the other determines the motors direction...

if i have an example in my situation, with pid, i can after work put what's going on and how, it would be way more easy for me to understand...that's why o was asking.

So you've got an error value, you just need to map the output value to direction + pwm value, plug in a PID library and call it regularly and start playing with P, I and D values.

i had a look ever some pid examples, but none i found look silimlar with what i need...i am playing with this for just 3 days....this is all the time i had to read and learn about programing...i am not sure how to how to write the code....my digital output 'high' will dirve the motor up, when it's 'low' it will come by default back, because that's what the sign magnitude mode dose. so my pid will just have to drive the motor up, high?

would something like this work ?

void loop() {
setpoint = analogRead(pedal position) * 5;
input = motor position ;
myPID.Compute();
pwmOut(output);
}

void pwmOut(int out) {
if (out > 0) {
digitalWrite(8, HiGH); //motor direction left
analogWrite(3, 100); // pwm motor speed
}
else {
digitalWrite8, LOW); //motor direction right
analogWrite(3, 100); // pwm motor speed
}
}

so…after reading a bit…i came up with this…should it work?
double pedal = 0;
double pwm = 3;
const int dir1 = 8;

double tps = (100);
double Kp = 0.32;//
double Ki = 0.1;
double Kd = 0.3;

float last_error = 0;
float error = 0;
float changeError = 0;
float totalError = 0;
float pidTerm = 0;
float pidTerm_scaled = 0;

void setup() {

pinMode(dir1, OUTPUT);

}

void loop(){

PIDcalculation();// find PID value

analogRead(tps, 0, 1023, 0, 1023);
analogRead(pedal, 0, 1023, 0, 1023);

if (tps < pedal) {

digitalWrite(dir1, HIGH);// Forward motion
analogWrite(3, 100);

} else {
digitalWrite(dir1, LOW);//Reverse motion
analogWrite(3, 100);

}

delay(100);
}

void PIDcalculation(){

pedal = analogRead(pedal);
tps = analogRead(tps);
error = tps - pedal;

changeError = error - last_error; // derivative term
totalError += error; //accumalate errors to find integral term
pidTerm = (Kp * error) + (Ki * totalError) + (Kd * changeError);//total gain
pidTerm = constrain(pidTerm, -255, 255);//constraining to appropriate value
pidTerm_scaled = abs(pidTerm);//make sure it’s a positive value

last_error = error;
}

Please use code tags.

What happened when you tried it?

I suspect that you are using analogRead() incorrectly. The compiler will tell you that is an error. You seem to have mushed together a map() funtion call in there.

Why does your main loop do a PID calculation before reading the two inputs? Does that seem backwards to you? Then the calculation function dors its own analogRead() anyway.

  pedal = analogRead(pedal);

See anything wrong here? Which pin is it reading?

After doing the PID calculation, your main loop totally ignores the result and just does its own thing when it writes the output to the motor.

as i said...i've been playing with ardunios for 4 days now...never touched one in my life....that's why i was asking for a example, so i can learn and figure out what's going on...

4 days ??!
have you gone through all the basic tutorials ?
they may look simple, and you may not need to rig it up and go through it all physically, but at least you should be able to understand and know what each line of code is doing.

for eg. you used digitalWrite(8,HIGH);
do you know what that means ?

because;

potin = analogRead(pot);

does not mean you can do;

pedal = analogRead(pedal);