Professional Documents
Culture Documents
h>
#include <FreqMeasure.h>
Servo myservo;
//inputs
//int Ref_in = 3;
void setup() {
myservo.attach(9);
Serial2.begin(57600);
Serial.begin(9600);
FreqMeasure.begin();
pinMode(CHPWSW, INPUT);
pinMode(CHPWA, INPUT);
pinMode(Up,OUTPUT);
pinMode(Dn,OUTPUT);
pinMode(En,OUTPUT);
pinMode(M2n,OUTPUT);
pinMode(6,OUTPUT);
analogReadResolution(13);
attachInterrupt(digitalPinToInterrupt(3), Ref_26, CHANGE);
}
void Ref_26()
{
// delayMicroseconds(Time-Tdesface);
delayMicroseconds(Time-Tdesface);
valEle = analogRead(ELEV);
// valCHPWB = analogRead(CHPWB);
void loop() {
if (FreqMeasure.available()) {
// average several reading together
sum = sum + FreqMeasure.read();
count = count + 1;
if (count > 30) {
float frequency = FreqMeasure.countToFrequency(sum / count);
// Serial.println(frequency);
sum = 0;
count = 0;
Time = ((1/frequency)*1000000)/4; //tiempo en microsegundos de cada media
onda
// Serial.print(" ");
// Serial.print(frequency);
// Serial.print(" ");
// Serial.println(Time);
}
digitalWrite(En,LOW);
valPot = analogRead(PoT);
valPot = map(valPot, 0, 8192, 0, 512);
setpoint = valPot;
filteredEle = firstOrderFilter (valEle, filteredEle, 75);
// Serial.println(filteredEle);
val = 0;
val = map(filteredEle, 0, 8192, 0, 512);
Serial.print(angle);
Serial.print(" ");
Serial.print(error);
Serial.print(" ");
Serial.print(valPot);
Serial.print(" ");
Serial.println(pidTerm_scaled);
delayMicroseconds(100);
// Serial.println(valEle);
void PIDcalculation(){
angle = val ;//count to angle conversion
error = abs(setpoint - angle);
pidTerm_scaled = map(error, 0,512 , 50, 255);
// 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;
// delay(5);
}