1 | |
---|
2 | |
---|
3 | |
---|
4 | |
---|
5 | |
---|
6 | |
---|
7 | |
---|
8 | |
---|
9 | |
---|
10 | |
---|
11 | |
---|
12 | #import "AdvancedViewController.h" |
---|
13 | #import "SignalConverter.h" |
---|
14 | #import "AppDelegate.h" |
---|
15 | |
---|
16 | |
---|
17 | |
---|
18 | |
---|
19 | |
---|
20 | @implementation AdvancedViewController { |
---|
21 | SignalConverter *signalConverter; |
---|
22 | } |
---|
23 | |
---|
24 | @synthesize pitch, pitchPercent, yaw, yawPercent, throttle, throttlePercent; |
---|
25 | |
---|
26 | - (void)viewDidLoad |
---|
27 | { |
---|
28 | [super viewDidLoad]; |
---|
29 | AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; |
---|
30 | signalConverter = appDelegate.signalConverter; |
---|
31 | |
---|
32 | |
---|
33 | |
---|
34 | |
---|
35 | self.motionManager = [[CMMotionManager alloc] init]; |
---|
36 | self.motionManager.accelerometerUpdateInterval = .2; |
---|
37 | self.motionManager.gyroUpdateInterval = .2; |
---|
38 | |
---|
39 | |
---|
40 | |
---|
41 | [self.motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue] |
---|
42 | withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) { |
---|
43 | [self outputAccelertionData:accelerometerData.acceleration]; |
---|
44 | if(error){ |
---|
45 | NSLog(@"%@", error); |
---|
46 | } |
---|
47 | }]; |
---|
48 | |
---|
49 | [self.motionManager startGyroUpdatesToQueue:[NSOperationQueue currentQueue] |
---|
50 | withHandler:^(CMGyroData *gyroData, NSError *error) { |
---|
51 | [self outputRotationData:gyroData.rotationRate]; |
---|
52 | }]; |
---|
53 | |
---|
54 | } |
---|
55 | |
---|
56 | - (IBAction) resetButtonPressed:(id)button |
---|
57 | { |
---|
58 | pitch.value = defaultControlPitch; |
---|
59 | yaw.value = defaultControlYaw; |
---|
60 | throttle.value = defaultControlThrottle; |
---|
61 | NSString *zero = @"0%"; |
---|
62 | throttlePercent.text = zero; |
---|
63 | yawPercent.text = zero; |
---|
64 | pitchPercent.text = zero; |
---|
65 | [self adjustValues]; |
---|
66 | } |
---|
67 | |
---|
68 | - (IBAction) pitchChanged:(id) sender |
---|
69 | { |
---|
70 | pitchPercent.text = [self percentStringFromSlider:pitch]; |
---|
71 | [self adjustValues]; |
---|
72 | } |
---|
73 | |
---|
74 | - (IBAction) yawChanged:(id) sender |
---|
75 | { |
---|
76 | yawPercent.text = [self percentStringFromSlider:yaw]; |
---|
77 | [self adjustValues]; |
---|
78 | } |
---|
79 | |
---|
80 | - (IBAction) throttleChanged:(id) sender |
---|
81 | { |
---|
82 | throttlePercent.text = [self percentStringFromSlider:throttle]; |
---|
83 | [self adjustValues]; |
---|
84 | } |
---|
85 | |
---|
86 | - (NSString *) percentStringFromSlider:(UISlider *) slider |
---|
87 | { |
---|
88 | return [NSString stringWithFormat:@"%i%%", (int)((slider.value - 0.5) * 200)]; |
---|
89 | } |
---|
90 | |
---|
91 | - (void) adjustValues |
---|
92 | { |
---|
93 | [signalConverter setControlSettings:[self yawValue]throttle:[self throttleValue] pitch:[self pitchValue]]; |
---|
94 | } |
---|
95 | |
---|
96 | - (int) yawValue |
---|
97 | { |
---|
98 | |
---|
99 | |
---|
100 | |
---|
101 | |
---|
102 | |
---|
103 | return round((yaw.maximumValue - yaw.value) * 100); |
---|
104 | } |
---|
105 | |
---|
106 | - (int) pitchValue |
---|
107 | { |
---|
108 | return round(pitch.value * 100); |
---|
109 | } |
---|
110 | |
---|
111 | - (int) throttleValue |
---|
112 | { |
---|
113 | return round(throttle.value * 100); |
---|
114 | } |
---|
115 | |
---|
116 | - (IBAction) hoverButtonPressed:(id)sender { |
---|
117 | [self resetButtonPressed:(sender)]; |
---|
118 | |
---|
119 | } |
---|
120 | |
---|
121 | - (IBAction) forwardButtonPressed:(id)sender { |
---|
122 | throttle.value = defaultControlThrottle; |
---|
123 | pitch.value = defaultControlPitch + 0.2; |
---|
124 | yaw.value = defaultControlYaw; |
---|
125 | [self adjustValues]; |
---|
126 | |
---|
127 | } |
---|
128 | |
---|
129 | - (IBAction) leftButtonPressed:(id)sender { |
---|
130 | throttle.value = defaultControlThrottle; |
---|
131 | pitch.value = defaultControlPitch; |
---|
132 | yaw.value = defaultControlYaw - 0.5; |
---|
133 | [self adjustValues]; |
---|
134 | |
---|
135 | } |
---|
136 | |
---|
137 | - (IBAction) rightButtonPressed:(id)sender { |
---|
138 | throttle.value = defaultControlThrottle; |
---|
139 | pitch.value = defaultControlPitch; |
---|
140 | yaw.value = defaultControlYaw + 0.3; |
---|
141 | [self adjustValues]; |
---|
142 | |
---|
143 | } |
---|
144 | |
---|
145 | - (IBAction)changeSwitchTiltSensorControl:(id)sender{ |
---|
146 | |
---|
147 | |
---|
148 | |
---|
149 | |
---|
150 | if([sender isOn]){ |
---|
151 | NSLog(@"DEBUG: Tilt Sensor Enabled"); |
---|
152 | [_switchTiltSensorControlThrottle setEnabled:YES]; |
---|
153 | } else{ |
---|
154 | NSLog(@"DEBUG: Tilt Sensor Disabled"); |
---|
155 | [_switchTiltSensorControlThrottle setOn:NO animated:YES]; |
---|
156 | [_switchTiltSensorControlThrottle setEnabled:NO]; |
---|
157 | referenceTiltX = 0; |
---|
158 | referenceTiltY = 0; |
---|
159 | } |
---|
160 | |
---|
161 | } |
---|
162 | |
---|
163 | - (IBAction)changeSwitchTiltSensorControlThrottle:(id)sender{ |
---|
164 | |
---|
165 | if([sender isOn]){ |
---|
166 | NSLog(@"DEBUG: Throttle Tilt Sensor Enabled"); |
---|
167 | referenceTiltY = 0; |
---|
168 | } else{ |
---|
169 | NSLog(@"DEBUG: Throttle Tilt Sensor Disabled"); |
---|
170 | referenceTiltX = 0; |
---|
171 | referenceTiltY = 0; |
---|
172 | } |
---|
173 | |
---|
174 | } |
---|
175 | |
---|
176 | -(void)outputAccelertionData:(CMAcceleration)acceleration |
---|
177 | { |
---|
178 | |
---|
179 | |
---|
180 | |
---|
181 | |
---|
182 | |
---|
183 | |
---|
184 | |
---|
185 | |
---|
186 | |
---|
187 | |
---|
188 | if ([_switchTiltSensorControl isOn]) { |
---|
189 | |
---|
190 | if (! [_switchTiltSensorControlThrottle isOn]) { |
---|
191 | |
---|
192 | |
---|
193 | |
---|
194 | double tiltX = acceleration.x; |
---|
195 | double tiltY = acceleration.y * -1; |
---|
196 | |
---|
197 | |
---|
198 | |
---|
199 | if (referenceTiltX == 0) { |
---|
200 | referenceTiltX = tiltX; |
---|
201 | referenceTiltY = tiltY; |
---|
202 | } |
---|
203 | |
---|
204 | double newYaw = defaultControlYaw + (tiltX - referenceTiltX); |
---|
205 | double newPitch = defaultControlPitch - (tiltY - referenceTiltY); |
---|
206 | |
---|
207 | |
---|
208 | |
---|
209 | |
---|
210 | |
---|
211 | if (((newYaw > yaw.value) && (newYaw - yaw.value > titleSensorMinimumThreshold)) || |
---|
212 | ((newYaw < yaw.value) && (yaw.value - newYaw > titleSensorMinimumThreshold))) { |
---|
213 | |
---|
214 | yaw.value = newYaw; |
---|
215 | } |
---|
216 | |
---|
217 | if (((newPitch > pitch.value) && (newPitch - pitch.value > titleSensorMinimumThreshold)) || |
---|
218 | ((newPitch < pitch.value) && (pitch.value - newPitch > titleSensorMinimumThreshold))) { |
---|
219 | pitch.value = newPitch; |
---|
220 | } |
---|
221 | |
---|
222 | } |
---|
223 | |
---|
224 | else { |
---|
225 | |
---|
226 | |
---|
227 | |
---|
228 | double tiltY = acceleration.y * 1; |
---|
229 | |
---|
230 | if (referenceTiltY == 0) { |
---|
231 | referenceTiltY = tiltY; |
---|
232 | } |
---|
233 | |
---|
234 | double newThrottle = defaultControlThrottle - (tiltY - referenceTiltY); |
---|
235 | |
---|
236 | if (((newThrottle > throttle.value) && (newThrottle - throttle.value > titleSensorMinimumThreshold)) || |
---|
237 | ((newThrottle < throttle.value) && (throttle.value - newThrottle > titleSensorMinimumThreshold))) { |
---|
238 | throttle.value = newThrottle; |
---|
239 | } |
---|
240 | |
---|
241 | } |
---|
242 | |
---|
243 | |
---|
244 | [self adjustValues]; |
---|
245 | |
---|
246 | } |
---|
247 | |
---|
248 | } |
---|
249 | |
---|
250 | -(void)outputRotationData:(CMRotationRate)rotation |
---|
251 | { |
---|
252 | |
---|
253 | } |
---|
254 | |
---|
255 | - (void)didReceiveMemoryWarning |
---|
256 | { |
---|
257 | [super didReceiveMemoryWarning]; |
---|
258 | |
---|
259 | } |
---|
260 | |
---|
261 | |
---|
262 | @end |
---|