-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduino_Arm
More file actions
191 lines (157 loc) · 4.19 KB
/
Arduino_Arm
File metadata and controls
191 lines (157 loc) · 4.19 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <Servo.h>
Servo bldc_Aileron;
Servo bldc_Elevator;
Servo bldc_Throttle;
Servo bldc_Rudder;
Servo bldc_Aux;
String string= "";
//To send a command use this format : e.g. TH_1850
//for Aileron AI
//for Elevator EL
//for Throttle TH
//for Throttle RD
//for Auxiliary use AUX_ON or AUX_OFF
int defAileron = 1500;//Max value is 1900 and Min value is 1100
int defElevator = 1500;//Max value is 1900 and Min value is 1100
int defThrottle = 1000;//Max value is 2400 and Min value is 1100
int defRudder = 1500;//Max value is 1900 and Min value is 1100
int AuxOn = 2000;
int AuxOff = 1000;
void setup(){
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
bldc_Aileron.attach(5);
bldc_Elevator.attach(6);
bldc_Throttle.attach(9);//A2);
bldc_Rudder.attach(10);
bldc_Aux.attach(11);
Serial.begin(9600);
delay(100);
initializePulse();
Serial.println("System Ready");
}
void loop(){
string = "";
while(Serial.available() > 0)
{
string += (char) Serial.read();
Serial.flush();
delay(10);
}
if(string != "")
{
String word1 = getValue(string, '_', 0);
String word2 = getValue(string, '_', 1);
Serial.println(word1);
// if(word1 == "+"){
// fullThrottle();
// }
//
// if(word1 == "-"){
// IdleThrottle();
// }
if(word1=="TH"){
int th = word2.toInt();
bldc_Throttle.writeMicroseconds(th); //actually write the value to the motor
Serial.print("Throttle speed :");Serial.println(bldc_Throttle.read());
}
if(word1=="RD"){
int th = word2.toInt();
bldc_Rudder.writeMicroseconds(th); //actually write the value to the motor
Serial.print("Rudder :");Serial.println(word2);
}
if(word1=="AI"){
int th = word2.toInt();
bldc_Aileron.writeMicroseconds(th); //actually write the value to the motor
Serial.print("Aileron :");Serial.println(word2);
}
if(word1=="EL"){
int th = word2.toInt();
bldc_Elevator.writeMicroseconds(th); //actually write the value to the motor
Serial.print("Elevator :");Serial.println(word2);
}
// if(word1=="AUX"){
// if(word2=="ON"){
// Auxiliary(true);
// Serial.println("Self-Level is ON");
// }
// else if(word2=="OFF"){
// Auxiliary(false);
// Serial.println("Self-Level is OFF");
// }
// }
//
if(word1=="ARM"){
bldc_Aux.writeMicroseconds(AuxOn);
Serial.println("Model armed");
}
if(word1=="DARM"){
bldc_Aux.writeMicroseconds(AuxOff);
Serial.println("Model Disarmed");
}
}
delay(10);
}
//void fullThrottle(){
// bldc_Throttle.writeMicroseconds(2390);
//}
//
//void IdleThrottle(){
// bldc_Throttle.writeMicroseconds(defThrottle);
//}
//
//void ArmModel(){
// bldc_Rudder.writeMicroseconds(1100);
// delay(2000);
// bldc_Rudder.writeMicroseconds(defRudder);
//
// Serial.println("Model Armed and Ready to Fly");
// delay(1000);
//}
//
//void DisArmModel(){
// bldc_Rudder.writeMicroseconds(1900);
// delay(2000);
// bldc_Rudder.writeMicroseconds(defRudder);
//
// Serial.println("Model Disarmed");
// delay(1000);
//}
void initializePulse(){
bldc_Aileron.writeMicroseconds(defAileron);
delay(10);
bldc_Elevator.writeMicroseconds(defElevator);
delay(10);
bldc_Throttle.writeMicroseconds(defThrottle);
delay(10);
bldc_Rudder.writeMicroseconds(defRudder);
delay(10);
Auxiliary("ON");
delay(10);
Serial.println("Pulse Initialized");
}
void Auxiliary(bool SW){
if(SW){
bldc_Aux.writeMicroseconds(AuxOn);
}
else{
bldc_Aux.writeMicroseconds(AuxOff);
}
}
String getValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = {0, -1};
int maxIndex = data.length()-1;
for(int i=0; i<=maxIndex && found<=index; i++){
if(data.charAt(i)==separator || i==maxIndex){
found++;
strIndex[0] = strIndex[1]+1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
}
}
return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
}