-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessing.pde
96 lines (83 loc) · 3.19 KB
/
processing.pde
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
#if PROCESSING == 1
/********************************************
* Serial Communication functions / helpers
********************************************/
union { // This Data structure lets
byte asBytes[24]; // us take the byte array
float asFloat[6]; // sent from processing and
} // easily convert it to a
foo; // float array
// getting float values from processing into the arduino
// was no small task. the way this program does it is
// as follows:
// * a float takes up 4 bytes. in processing, convert
// the array of floats we want to send, into an array
// of bytes.
// * send the bytes to the arduino
// * use a data structure known as a union to convert
// the array of bytes back into an array of floats
// the bytes coming from the arduino follow the following
// format:
// 0: 0=Manual, 1=Auto, else = ? error ?
// 1-4: float setpoint
// 5-8: float input
// 9-12: float output
// 13-16: float P_Param
// 17-20: float I_Param
// 21-24: float D_Param
void SerialReceive()
{
// read the bytes sent from Processing
int index=0;
byte Auto_Man = -1;
while(Serial.available()&&index<25)
{
if(index==0) Auto_Man = Serial.read();
else foo.asBytes[index-1] = Serial.read();
index++;
}
// if the information we got was in the correct format,
// read it into the system
if(index==25 && (Auto_Man==0 || Auto_Man==1))
{
pitchSetPoint=double(foo.asFloat[0]);
//Input=double(foo.asFloat[1]); // * the user has the ability to send the
// value of "Input" in most cases (as
// in this one) this is not needed.
if(Auto_Man==0) // * only change the output if we are in
{ // manual mode. otherwise we'll get an
outputElevator=double(foo.asFloat[2]); // output blip, then the controller will
} // overwrite.
double p, i, d; // * read in and set the controller tunings
p = double(foo.asFloat[3]); //
i = double(foo.asFloat[4]); //
d = double(foo.asFloat[5]); //
pitchPID.SetTunings(p, i, d); //
if(Auto_Man==0) pitchPID.SetMode(MANUAL);// * set the controller mode
else pitchPID.SetMode(AUTO); //
}
Serial.flush(); // * clear any random data from the serial buffer
}
// unlike our tiny microprocessor, the processing ap
// has no problem converting strings into floats, so
// we can just send strings. much easier than getting
// floats from processing to here no?
void SerialSend()
{
Serial.print("PID ");
Serial.print(pitchSetPoint);
Serial.print(" ");
Serial.print(pitchAngle);
Serial.print(" ");
Serial.print(outputElevator);
Serial.print(" ");
Serial.print(pitchPID.GetP_Param());
Serial.print(" ");
Serial.print(pitchPID.GetI_Param());
Serial.print(" ");
Serial.print(pitchPID.GetD_Param());
Serial.print(" ");
if(pitchPID.GetMode()==AUTO) Serial.println("Automatic");
else Serial.println("Manual");
}
#endif