There is a servo attached to the throttle. The RPM signal is taken from the alternator before the rectifier bridge. It maintains a constant rpm, regardless of load. Settings selected experimentally.
CODE:
#include <Arduino.h>
#include <Servo.h>
#include <PID_v1.h>
#define TACH_PIN 3
#include <Tachometer.h>
Tachometer tacho;
double Setpoint, Input, Output;
PID myPID(&Input, &Output, &Setpoint,1,1,0.125, REVERSE);
#define ERPM 800
#define MAX_SERVO 140
#define MIN_SERVO 70
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
int currentFreq = 0;
double sum = 0;
double nanoseconds = 0;
int count = 0;
int currentServo = (MAX_SERVO+MIN_SERVO)/2;
double currentRPM = 0;
double LRPM = 0;
int bck1, bck2, bck3, bck4;
Servo myservo; // create servo object to control a servo
void isr() {
tacho.tick(); // сообщаем библиотеке об этом
}
void setup() {
// пин тахометра вентилятора подтягиваем к VCC
pinMode(TACH_PIN, 0);
// настраиваем прерывание
attachInterrupt(digitalPinToInterrupt(3), isr, FALLING);
Setpoint = 800;
//turn the PID on
myPID.SetMode(AUTOMATIC);
Serial.begin(9600);
Serial.println("start setuu");
Serial.println("pinMode 13 1");
pinMode(13,1);
Serial.println("zapalam diode");
digitalWrite(13,1);
Serial.println("attaching servo");
myservo.attach(11); // attaches the servo on pin to the servo object
Serial.println("servo min");
myservo.write(MIN_SERVO);
delay(500);
Serial.println("servo center");
myservo.write((MAX_SERVO+MIN_SERVO)/2);
delay(500);
Serial.println("diode gasze");
digitalWrite(13,0);
Serial.println("koniec setupu");
bck1 = TCCR0A;
bck2 = TCCR0B;
bck3 = TCCR1A;
bck4 = TCCR1B;
}
void loop() {
//Serial.println(tacho.getRPM()); // RPM
//Serial.println(tacho.getHz()); // Hz
//Serial.println(tacho.Us()); // us
currentRPM = (3*currentRPM + (tacho.getHz()*2.0))/4.0;
//if(currentRPM < 750 && val > MIN_SERVO) val--;
//if(currentRPM > 850 && val < MAX_SERVO) val++;
//if(currentRPM < 20) val = (MAX_SERVO+MIN_SERVO)/2;
//if(currentRPM > 1800) val = MAX_SERVO;
Input = (currentRPM+LRPM)/2.0;
myPID.Compute();
val = map(Output, 0, 255, MIN_SERVO, MAX_SERVO);
if(val > MAX_SERVO) val = MAX_SERVO;
if(val < MIN_SERVO) val = MIN_SERVO;
Serial.print(Input);
Serial.print(" ");
Serial.print(Output);
Serial.print(" ");
Serial.println(val);
myservo.write(val); // sets the servo position according to the scaled value
digitalWrite(13,1);
delay(50); // waits for the servo to get there
digitalWrite(13,0);
delay(50);
LRPM = currentRPM;
}
Brak komentarzy:
Prześlij komentarz