source: orbit/iOS/Orbit/Orbit/controllers/AdvancedViewController.m @ 52de6cb

Servo
Last change on this file since 52de6cb was 52de6cb, checked in by Steve Castellotti <sc@…>, 7 years ago
  • Restored tilt control in preparation of update release
  • Property mode set to 100644
File size: 7.8 KB
Line 
1//
2//  AdvancedViewController.m
3//  orbit
4//
5//  Copyright (c) 2013 Puzzlebox Productions, LLC. All rights reserved.
6//  Originally created by Jonathon Horsman.
7//
8//  This code is released under the GNU Public License (GPL) version 2
9//  For more information please refer to http://www.gnu.org/copyleft/gpl.html
10//
11
12#import "AdvancedViewController.h"
13#import "SignalConverter.h"
14#import "AppDelegate.h"
15
16
17@implementation AdvancedViewController {
18   SignalConverter *signalConverter;
19}
20
21@synthesize pitch, pitchPercent, yaw, yawPercent, throttle, throttlePercent;
22
23- (void)viewDidLoad
24{
25   [super viewDidLoad];
26   AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
27   signalConverter = appDelegate.signalConverter;
28   
29   //   UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Help Wanted" message:@"Please join our Open Source development community to help us port this feature! See link to Forums under Support tab" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
30   //   [alert show];
31   
32   // Core Motion
33   @try {
34      self.motionManager = [[CMMotionManager alloc] init];
35      self.motionManager.accelerometerUpdateInterval = .2;
36      self.motionManager.gyroUpdateInterval = .2;
37      //   self.motionManager.accelerometerUpdateInterval = .1;
38      //   self.motionManager.gyroUpdateInterval = .1;
39     
40      [self.motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue]
41                                               withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
42                                                  [self outputAccelertionData:accelerometerData.acceleration];
43                                                  if(error){
44                                                     NSLog(@"%@", error);
45                                                  }
46                                               }];
47     
48      [self.motionManager startGyroUpdatesToQueue:[NSOperationQueue currentQueue]
49                                      withHandler:^(CMGyroData *gyroData, NSError *error) {
50                                         [self outputRotationData:gyroData.rotationRate];
51                                      }];
52   }
53   @catch (NSException * e) {
54      NSLog(@"Exception: %@", e);
55   }
56
57     
58}
59
60- (void) resetValues:(id) sender
61{
62   pitch.value = defaultControlPitch;
63   yaw.value = defaultControlYaw;
64   throttle.value = defaultControlThrottle;
65   NSString *zero = @"0%";
66   throttlePercent.text = zero;
67   yawPercent.text = zero;
68   pitchPercent.text = zero;
69   [self adjustValues];
70}
71
72- (IBAction) resetButtonPressed:(id) button
73{
74   [self resetValues:(button)];
75}
76
77- (IBAction) pitchChanged:(id) sender
78{
79   pitchPercent.text = [self percentStringFromSlider:pitch];
80   [self adjustValues];
81}
82
83- (IBAction) yawChanged:(id) sender
84{
85   yawPercent.text = [self percentStringFromSlider:yaw];
86   [self adjustValues];
87}
88
89- (IBAction) throttleChanged:(id) sender
90{
91   throttlePercent.text = [self percentStringFromSlider:throttle];
92   [self adjustValues];
93}
94
95- (NSString *) percentStringFromSlider:(UISlider *) slider
96{
97   return [NSString stringWithFormat:@"%i%%", (int)((slider.value - 0.5) * 200)]; // between -100% and 100%
98}
99
100- (void) adjustValues
101{
102   [signalConverter setControlSettings:[self yawValue]throttle:[self throttleValue] pitch:[self pitchValue]];
103}
104
105- (int) yawValue
106{
107   // We subtract the current Yaw position from the maximum slider value
108   // because smaller values instruct the helicopter to spin to the right
109   // (clockwise if looking down from above) whereas intuitively moving
110   // the slider to the left should cause it to spin left
111   
112   return round((yaw.maximumValue - yaw.value) * 100);
113}
114
115- (int) pitchValue
116{
117   return round(pitch.value * 100);
118}
119
120- (int) throttleValue
121{
122   return round(throttle.value * 100);
123}
124
125- (IBAction) hoverButtonPressed:(id)sender {
126   [self resetValues:(sender)];
127   
128}
129
130- (IBAction) forwardButtonPressed:(id)sender {
131   throttle.value = defaultControlThrottle;
132   pitch.value = defaultControlPitch + 0.2;
133   yaw.value = defaultControlYaw;
134   [self adjustValues];
135   
136}
137
138- (IBAction) leftButtonPressed:(id)sender {
139   throttle.value = defaultControlThrottle;
140   pitch.value = defaultControlPitch;
141   yaw.value = defaultControlYaw - 0.5;
142   [self adjustValues];
143   
144}
145
146- (IBAction) rightButtonPressed:(id)sender {
147   throttle.value = defaultControlThrottle;
148   pitch.value = defaultControlPitch;
149   yaw.value = defaultControlYaw + 0.3;
150   [self adjustValues];
151   
152}
153
154- (IBAction)changeSwitchTiltSensorControl:(id)sender{
155   
156   // With thanks to Kyro Apps
157   // http://kyroapps.weebly.com/xcode-tutorial-452---uiswitch.html
158   
159   if([sender isOn]){
160      //      NSLog(@"DEBUG: Tilt Sensor Enabled");
161      [_switchTiltSensorControlThrottle setEnabled:YES];
162   } else{
163      //      NSLog(@"DEBUG: Tilt Sensor Disabled");
164      [_switchTiltSensorControlThrottle setOn:NO animated:YES];
165      [_switchTiltSensorControlThrottle setEnabled:NO];
166      referenceTiltX = 0;
167                referenceTiltY = 0;
168   }
169   
170}
171
172- (IBAction)changeSwitchTiltSensorControlThrottle:(id)sender{
173   
174   if([sender isOn]){
175      //      NSLog(@"DEBUG: Throttle Tilt Sensor Enabled");
176      referenceTiltY = 0;
177   } else{
178      //      NSLog(@"DEBUG: Throttle Tilt Sensor Disabled");
179      referenceTiltX = 0;
180                referenceTiltY = 0;
181   }
182   
183}
184
185-(void)outputAccelertionData:(CMAcceleration)acceleration
186{
187   // With thanks to NSCookbook
188   // http://nscookbook.com/2013/03/ios-programming-recipe-19-using-core-motion-to-access-gyro-and-accelerometer/
189   
190   
191   if ([_switchTiltSensorControl isOn]) {
192     
193      if (! [_switchTiltSensorControlThrottle isOn]) {
194         
195         // Pitch/Yaw Tilt Control
196         
197         double tiltX = acceleration.x;
198         double tiltY = acceleration.y * -1;  // invert the Y axis so that negative values equal left
199         
200         //   NSLog(@"DEBUG: tiltX:%f tiltY:%f", tiltX, tiltY);
201         
202         if (referenceTiltX == 0) {
203            referenceTiltX = tiltX;
204            referenceTiltY = tiltY;
205         }
206         
207         double newYaw = defaultControlYaw + (tiltX - referenceTiltX);
208         double newPitch = defaultControlPitch - (tiltY - referenceTiltY);
209         
210         //   NSLog(@"DEBUG: yaw:%f pitch:%f", yaw.value, pitch.value);
211         //   NSLog(@"DEBUG: newYaw:%f newPitch:%f", newYaw, newPitch);
212         
213         // Filter changes below titleSensorMinimumThreshold
214         if (((newYaw > yaw.value) && (newYaw - yaw.value > titleSensorMinimumThreshold)) ||
215             ((newYaw < yaw.value) && (yaw.value - newYaw > titleSensorMinimumThreshold))) {
216           
217            yaw.value = newYaw;
218         }
219         
220         if (((newPitch > pitch.value) && (newPitch - pitch.value > titleSensorMinimumThreshold)) ||
221             ((newPitch < pitch.value) && (pitch.value - newPitch > titleSensorMinimumThreshold))) {
222            pitch.value = newPitch;
223         }
224         
225      } // Pitch/Yaw Tilt Control
226     
227      else {
228         
229         // Throttle Tilt Control
230         
231         double tiltY = acceleration.y * 1;  // invert the Y axis so that negative values equal forward
232         
233         if (referenceTiltY == 0) {
234            referenceTiltY = tiltY;
235         }
236         
237         double newThrottle = defaultControlThrottle - (tiltY - referenceTiltY);
238         
239         if (((newThrottle > throttle.value) && (newThrottle - throttle.value > titleSensorMinimumThreshold)) ||
240             ((newThrottle < throttle.value) && (throttle.value - newThrottle > titleSensorMinimumThreshold))) {
241            throttle.value = newThrottle;
242         }
243         
244      } // Throttle Tilt Control
245     
246     
247      [self adjustValues];
248     
249   }
250   
251}
252
253-(void)outputRotationData:(CMRotationRate)rotation
254{
255   
256}
257
258- (void)didReceiveMemoryWarning
259{
260   [super didReceiveMemoryWarning];
261   // Dispose of any resources that can be recreated.
262}
263
264
265@end
Note: See TracBrowser for help on using the repository browser.