본문 바로가기

교육 자료/Raduino mini

11. SBUS 비행제어


  • 준비물


  • 회로도



  • 코드

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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include "raduino_sbus.h"
 
#include "raduino_fc.h"
 
#include <Wire.h>                     // i2C 통신을 위한 라이브러리
 
SBUS sbus(Serial2);
 
FC Flight(Serial1);
 
 
 
boolean startFlag = false;
 
 
 
unsigned long currentMillis, previousMillis = 0;
 
 
 
int ledPin = PC13;
 
 
 
// We'll use timer 1
 
HardwareTimer timer(1);
 
 
 
void setup() {
 
 
 
    sbus.begin();
 
    sbus.SBUS_OUT= true//enable SBUS_OUT
 
    
 
    // Pause the timer while we're configuring it
 
    timer.pause();
 
 
 
    // Set up period
 
    timer.setPeriod(10000); // in 10 microseconds
 
 
 
    // Set up an interrupt on channel 1
 
    timer.setChannel1Mode(TIMER_OUTPUT_COMPARE);
 
    timer.setCompare(TIMER_CH1, 1);  // Interrupt 1 count after each update
 
    timer.attachCompare1Interrupt(handler_10ms);
 
 
 
    // Refresh the timer's count, prescale, and overflow
 
    timer.refresh();
 
 
 
    // Start the timer counting
 
    timer.resume();
 
 
 
    Flight.begin();
 
    Serial.begin(115200);  
 
 
 
    pinMode(LED_BUILTIN, OUTPUT);
 
    digitalWrite(LED_BUILTIN, HIGH);
 
 
 
    delay(100);
 
 
 
    // roll positive right
 
    // pitch positive forward
 
    //Flight.Trim(20,10); //roll: 0.0 , pith: -5.0
 
    Serial.println("Triming...");
 
 
 
    delay(5000);
 
    
 
    // binding.....
 
    Flight.Bind();
 
    Serial.println("binding...");
 
 
 
//     delay(100);
 
 //   Flight.Cali();  
 
 //   Serial.println("Calibration...");
 
      
 
}
 
 
 
int ChannelValue[4];
 
 
 
void loop() 
 
{
 
  int ret = Flight.Event();
 
  if(ret==1)digitalWrite(LED_BUILTIN,LOW); 
 
  if(ret>0)Serial.println(Flight.retString);
 
  
 
 
 
  if ( startFlag == true ) {
 
    ChannelValue[3+= 80;
 
  }
 
 
 
  if( ChannelValue[0< 30 && ChannelValue[1< 30 && ChannelValue[2> 1000 && ChannelValue[3< 110 ){   // 시동 걸때
 
      currentMillis = millis();
 
      if (currentMillis - previousMillis >= 1500){
 
        previousMillis = currentMillis;
 
        startFlag = 1;
 
      }
 
  }
 
 
 
  if( ChannelValue[0> 1000 && ChannelValue[1< 30 && ChannelValue[2< 30 && ChannelValue[3< 110 ){   // 시동 끌때
 
      currentMillis = millis();
 
      if (currentMillis - previousMillis >= 1500){
 
        previousMillis = currentMillis;
 
        startFlag = 0;
 
      }
 
  }
 
 
 
  Serial.print(ChannelValue[0]);  //312 ~1690 우스틱 좌우 roll
 
  Serial.print("   |   ");
 
  Serial.print(ChannelValue[1]); //325 ~1673 좌스틱 좌우 pitch
 
  Serial.print("   |   ");
 
  Serial.print(ChannelValue[2]); //352 ~1698 좌스틱 상하 yaw
 
  Serial.print("   |   ");
 
  Serial.println(ChannelValue[3]);  // 330 ~1680 우스틱 상하 Throttle  
 
}
 
 
 
void handler_10ms(void) {
 
    sbus.process();
 
  
 
  int ch1  = sbus.getChannel(1); 
 
  int ch2 = sbus.getChannel(2);
 
  int ch3 = sbus.getChannel(3);
 
  int ch4  = sbus.getChannel(4);  
 
 
 
  ChannelValue[0]= map(ch1,300,1700,0,1023); //roll
 
  ChannelValue[1]= map(ch2,1700,300,0,1023); //pitch
 
  ChannelValue[2]= map(ch4,300,1700,0,1023); //yaw
 
  ChannelValue[3]= map(ch3,300,1700,0,1023); //throttle
 
 
 
   if ( startFlag == true ) Flight.Tx(ChannelValue[0],ChannelValue[1],ChannelValue[2],ChannelValue[3]);
 
}
 
 
cs

'교육 자료 > Raduino mini' 카테고리의 다른 글

13. 적외선 고도제어  (0) 2018.10.08
12. BLE4.0(HM-10) 비행제어  (0) 2018.10.08
8. Dot Matrix LED  (0) 2018.10.01
6. SOUND  (0) 2018.10.01
5. RGB  (0) 2018.10.01