-
Notifications
You must be signed in to change notification settings - Fork 0
/
q3.ino
63 lines (59 loc) · 1.5 KB
/
q3.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const int fanPin = 9; //fan
const int trigPin = 4; //ultrasonic sensor
const int echoPin = 3; //ultrasonic sensor
const int minDistance = 45; // in cm
const int maxDistance = 55; // in cm
float duration, distance;
const int kp = 2; //proportional parameter
const int ki = 5; //integral parameter
const int kd = 1; //differential parameter
float setPoint = 50; //hover drone at 50m
float error,derivative;
int fanSpeed = 0;
int PWM;
int previousError = 0;
int integral = 0;
float getDistance(); //get distance from ground
void setup()
{
pinMode(echoPin, INPUT);
pinMode(trigPin, OUTPUT);
pinMode(fanPin, OUTPUT);
}
void loop()
{
distance = getDistance();
if (distance >= minDistance && distance <= maxDistance)
{
error = setPoint - distance;
derivative = error - previousError;
integral += error;
previousError = error;
fanSpeed = kp * error + ki * integral + kd * derivative;
if(distance >= 50)
{
PWM = abs(map(fanSpeed,0,76,0,150));
}
else
{
PWM = abs(map(fanSpeed,0,51,0,150));
}
analogWrite(fanPin, PWM);
}
else
{
PWM = 0;
analogWrite(fanPin, PWM);
}
delay(1000);
}
//to get distance above ground level
float getDistance() {
digitalWrite(trigPin, LOW); //send trigger signal
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); //calculate duration
return (duration / 2) / 29.1; //return distance
}