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 | |
---|
37 | |
---|
38 | self.motionManager.accelerometerUpdateInterval = .1; |
---|
39 | self.motionManager.gyroUpdateInterval = .1; |
---|
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 setYaw:[self yawValue]throttle:[self throttleValue] pitch:[self pitchValue]]; |
---|
94 | } |
---|
95 | |
---|
96 | - (int) yawValue |
---|
97 | { |
---|
98 | return round(yaw.value * 100); |
---|
99 | } |
---|
100 | |
---|
101 | - (int) pitchValue |
---|
102 | { |
---|
103 | return round(pitch.value * 100); |
---|
104 | } |
---|
105 | |
---|
106 | - (int) throttleValue |
---|
107 | { |
---|
108 | return round(throttle.value * 100); |
---|
109 | } |
---|
110 | |
---|
111 | - (IBAction) hoverButtonPressed:(id)sender { |
---|
112 | [self resetButtonPressed:(sender)]; |
---|
113 | |
---|
114 | } |
---|
115 | |
---|
116 | - (IBAction) forwardButtonPressed:(id)sender { |
---|
117 | pitch.value = 0.8; |
---|
118 | yaw.value = 0.5; |
---|
119 | throttle.value = 0.5; |
---|
120 | [self adjustValues]; |
---|
121 | |
---|
122 | } |
---|
123 | |
---|
124 | - (IBAction) leftButtonPressed:(id)sender { |
---|
125 | pitch.value = 0.5; |
---|
126 | yaw.value = 0.1; |
---|
127 | throttle.value = 0.5; |
---|
128 | [self adjustValues]; |
---|
129 | |
---|
130 | } |
---|
131 | |
---|
132 | - (IBAction) rightButtonPressed:(id)sender { |
---|
133 | pitch.value = 0.5; |
---|
134 | yaw.value = 0.9; |
---|
135 | throttle.value = 0.5; |
---|
136 | [self adjustValues]; |
---|
137 | |
---|
138 | } |
---|
139 | |
---|
140 | - (IBAction)changeSwitchTiltSensorControl:(id)sender{ |
---|
141 | |
---|
142 | |
---|
143 | |
---|
144 | |
---|
145 | if([sender isOn]){ |
---|
146 | NSLog(@"DEBUG: Tilt Sensor Enabled"); |
---|
147 | [_switchTiltSensorControlThrottle setEnabled:YES]; |
---|
148 | } else{ |
---|
149 | NSLog(@"DEBUG: Tilt Sensor Disabled"); |
---|
150 | [_switchTiltSensorControlThrottle setOn:NO animated:YES]; |
---|
151 | [_switchTiltSensorControlThrottle setEnabled:NO]; |
---|
152 | } |
---|
153 | |
---|
154 | } |
---|
155 | |
---|
156 | - (IBAction)changeSwitchTiltSensorControlThrottle:(id)sender{ |
---|
157 | |
---|
158 | if([sender isOn]){ |
---|
159 | NSLog(@"DEBUG: Throttle Tilt Sensor Enabled"); |
---|
160 | } else{ |
---|
161 | NSLog(@"DEBUG: Throttle Tilt Sensor Disabled"); |
---|
162 | } |
---|
163 | |
---|
164 | } |
---|
165 | |
---|
166 | -(void)outputAccelertionData:(CMAcceleration)acceleration |
---|
167 | { |
---|
168 | |
---|
169 | |
---|
170 | |
---|
171 | if ([_switchTiltSensorControl isOn]) { |
---|
172 | |
---|
173 | if (! [_switchTiltSensorControlThrottle isOn]) { |
---|
174 | |
---|
175 | |
---|
176 | |
---|
177 | double tiltX = acceleration.x; |
---|
178 | double tiltY = acceleration.y * -1; |
---|
179 | |
---|
180 | |
---|
181 | |
---|
182 | double newYaw = defaultControlYaw + (tiltX - referenceTiltX); |
---|
183 | double newPitch = defaultControlPitch - (tiltY - referenceTiltY); |
---|
184 | |
---|
185 | |
---|
186 | |
---|
187 | |
---|
188 | yaw.value = newYaw; |
---|
189 | pitch.value = newPitch; |
---|
190 | |
---|
191 | [self adjustValues]; |
---|
192 | |
---|
193 | } |
---|
194 | |
---|
195 | else { |
---|
196 | |
---|
197 | |
---|
198 | |
---|
199 | double tiltY = acceleration.y * -1; |
---|
200 | |
---|
201 | throttle.value = defaultControlThrottle - (tiltY - referenceTiltY); |
---|
202 | |
---|
203 | } |
---|
204 | |
---|
205 | } |
---|
206 | |
---|
207 | } |
---|
208 | |
---|
209 | -(void)outputRotationData:(CMRotationRate)rotation |
---|
210 | { |
---|
211 | |
---|
212 | } |
---|
213 | |
---|
214 | - (void)didReceiveMemoryWarning |
---|
215 | { |
---|
216 | [super didReceiveMemoryWarning]; |
---|
217 | |
---|
218 | } |
---|
219 | |
---|
220 | |
---|
221 | @end |
---|