-
Notifications
You must be signed in to change notification settings - Fork 0
/
pid.cpp
54 lines (47 loc) · 1.34 KB
/
pid.cpp
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
#include "quadcopter.h"
#include <iostream>
#include <unistd.h>
#include <algorithm>
int main(int argc, char* argv[])
{
int desiredPitch = 0;
int desiredRoll = 0;
int desiredHeight;
std::cout << "Enter hover height:\n";
std::cin >> desiredHeight;
int desiredPower = 100; //This needs to change based on how high it goes / power
float pwrNum = 2; //This number decides how much the copter should correct. It changes\
based on how far the copter overshoots and stuff.
int pH = 0;
int pP = 0;
int cH = 0;
int cP = 0;
//for(int i = 0; i < 100; i++)
while(true)
{
move();
pH = cH;
pP = cP;
cH = getHeight();
if ((cH - pH) <= 0 && cH < desiredHeight)
{
desiredPower += (desiredHeight - cH) * 0.5;
}
else if((cH - pH) >= 0 && cH > desiredHeight)
{
desiredPower -= (cH - desiredHeight) * 0.5;
}
/*
else if((cH - pH) > 0 && cH < desiredHeight)
{
}
*/
int pDiff = getPitch() - desiredPitch;
int rDiff = getRoll() - desiredRoll;
setFR(std::min(std::max((int)(desiredPower + pDiff*pwrNum + rDiff*pwrNum), 0), 255));
setFL(std::min(std::max((int)(desiredPower + pDiff*pwrNum - rDiff*pwrNum), 0), 255));
setBR(std::min(std::max((int)(desiredPower - pDiff*pwrNum + rDiff*pwrNum), 0), 255));
setBL(std::min(std::max((int)(desiredPower - pDiff*pwrNum - rDiff*pwrNum), 0), 255));
usleep(50000);
}
}