-
Notifications
You must be signed in to change notification settings - Fork 0
/
4withCPF.cpp
98 lines (71 loc) · 1.71 KB
/
4withCPF.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Do not remove the include below
#include "PlutoPilot.h"
#include "Xshield.h"
#include "Control.h"
#include "Led.h"
#include "Print.h"
float e_1 = 0;//Disable default LED behaviour
int array_left[10];
int array_right[10];
int sumL =0;
int sumR =0;
//The setup function is called once at Pluto's hardware startup
void plutoInit()
{
// Add your hardware initialization code here
Xshield.init();
}
//The function is called once before plutoPilot() when you activate Developer Mode
void onPilotStart()
{
// do your one time stuffs here
Xshield.startRanging();
}
// The loop function is called in an endless loop
void plutoPilot()
{
//Add your repeated code here
for (int i=0; i<10; i++){
Print.monitor(" L RANGE: ", Xshield.getRange(LS_LEFT));
Print.monitor(" R RANGE: ", Xshield.getRange(LS_RIGHT));
if(Xshield.getRange(LS_LEFT)<0 || Xshield.getRange(LS_LEFT)>2000)
{
array_left[i]=0;
}
else
{array_left[i] = Xshield.getRange(LS_LEFT);}
sumL+= array_left[i];
if(Xshield.getRange(LS_RIGHT)<0 || Xshield.getRange(LS_RIGHT)>2000)
{
array_right[i]= 0;
}
else
{array_right[i] = Xshield.getRange(LS_RIGHT);}
sumR = sumR + array_right[i];
}
Print.monitor("\n");
int L = (sumL)/10;
Print.monitor("average L:", L);
int R = (sumR)/10;
Print.monitor("average R:", R);
int kp,kd;
float e_2 = (L-R)/2;
kp= 4;
kd = 11;
float del_e = e_2 - e_1;
e_1 = e_2;
float u = kp*e_2 + kd*del_e;
Control.setRcCommand(RC_ROLL, 1500+u);
// Print.monitor(" RC ROLL :", u);
for(int k=0; k<10;k++){
array_left[k] = 0;
array_right[k] = 0;
}
sumR=0;
sumL=0;
}
void onPilotFinish()
{
// do your cleanup stuffs here
Xshield.stopRanging();
}