source: orbit/iOS/Orbit/Orbit/controllers/AdvancedViewController.m @ c6dde93

Servo
Last change on this file since c6dde93 was f05ba79, checked in by Steve Castellotti <sc@…>, 8 years ago
  • Core Motion re-enabled for deployment
  • Property mode set to 100644
File size: 7.7 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   self.motionManager = [[CMMotionManager alloc] init];
34   self.motionManager.accelerometerUpdateInterval = .2;
35   self.motionManager.gyroUpdateInterval = .2;
36   //   self.motionManager.accelerometerUpdateInterval = .1;
37   //   self.motionManager.gyroUpdateInterval = .1;
38   
39   [self.motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue]
40                                            withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
41                                               [self outputAccelertionData:accelerometerData.acceleration];
42                                               if(error){
43                                                  NSLog(@"%@", error);
44                                               }
45                                            }];
46   
47   [self.motionManager startGyroUpdatesToQueue:[NSOperationQueue currentQueue]
48                                   withHandler:^(CMGyroData *gyroData, NSError *error) {
49                                      [self outputRotationData:gyroData.rotationRate];
50                                   }];
51   
52}
53
54- (void) resetValues:(id) sender
55{
56   pitch.value = defaultControlPitch;
57   yaw.value = defaultControlYaw;
58   throttle.value = defaultControlThrottle;
59   NSString *zero = @"0%";
60   throttlePercent.text = zero;
61   yawPercent.text = zero;
62   pitchPercent.text = zero;
63   [self adjustValues];
64}
65
66- (IBAction) resetButtonPressed:(id) button
67{
68   [self resetValues:(button)];
69}
70
71- (IBAction) pitchChanged:(id) sender
72{
73   pitchPercent.text = [self percentStringFromSlider:pitch];
74   [self adjustValues];
75}
76
77- (IBAction) yawChanged:(id) sender
78{
79   yawPercent.text = [self percentStringFromSlider:yaw];
80   [self adjustValues];
81}
82
83- (IBAction) throttleChanged:(id) sender
84{
85   throttlePercent.text = [self percentStringFromSlider:throttle];
86   [self adjustValues];
87}
88
89- (NSString *) percentStringFromSlider:(UISlider *) slider
90{
91   return [NSString stringWithFormat:@"%i%%", (int)((slider.value - 0.5) * 200)]; // between -100% and 100%
92}
93
94- (void) adjustValues
95{
96   [signalConverter setControlSettings:[self yawValue]throttle:[self throttleValue] pitch:[self pitchValue]];
97}
98
99- (int) yawValue
100{
101   // We subtract the current Yaw position from the maximum slider value
102   // because smaller values instruct the helicopter to spin to the right
103   // (clockwise if looking down from above) whereas intuitively moving
104   // the slider to the left should cause it to spin left
105   
106   return round((yaw.maximumValue - yaw.value) * 100);
107}
108
109- (int) pitchValue
110{
111   return round(pitch.value * 100);
112}
113
114- (int) throttleValue
115{
116   return round(throttle.value * 100);
117}
118
119- (IBAction) hoverButtonPressed:(id)sender {
120   [self resetValues:(sender)];
121   
122}
123
124- (IBAction) forwardButtonPressed:(id)sender {
125   throttle.value = defaultControlThrottle;
126   pitch.value = defaultControlPitch + 0.2;
127   yaw.value = defaultControlYaw;
128   [self adjustValues];
129   
130}
131
132- (IBAction) leftButtonPressed:(id)sender {
133   throttle.value = defaultControlThrottle;
134   pitch.value = defaultControlPitch;
135   yaw.value = defaultControlYaw - 0.5;
136   [self adjustValues];
137   
138}
139
140- (IBAction) rightButtonPressed:(id)sender {
141   throttle.value = defaultControlThrottle;
142   pitch.value = defaultControlPitch;
143   yaw.value = defaultControlYaw + 0.3;
144   [self adjustValues];
145   
146}
147
148- (IBAction)changeSwitchTiltSensorControl:(id)sender{
149   
150   // With thanks to Kyro Apps
151   // http://kyroapps.weebly.com/xcode-tutorial-452---uiswitch.html
152   
153   if([sender isOn]){
154      //      NSLog(@"DEBUG: Tilt Sensor Enabled");
155      [_switchTiltSensorControlThrottle setEnabled:YES];
156   } else{
157      //      NSLog(@"DEBUG: Tilt Sensor Disabled");
158      [_switchTiltSensorControlThrottle setOn:NO animated:YES];
159      [_switchTiltSensorControlThrottle setEnabled:NO];
160      referenceTiltX = 0;
161                referenceTiltY = 0;
162   }
163   
164}
165
166- (IBAction)changeSwitchTiltSensorControlThrottle:(id)sender{
167   
168   if([sender isOn]){
169      //      NSLog(@"DEBUG: Throttle Tilt Sensor Enabled");
170      referenceTiltY = 0;
171   } else{
172      //      NSLog(@"DEBUG: Throttle Tilt Sensor Disabled");
173      referenceTiltX = 0;
174                referenceTiltY = 0;
175   }
176   
177}
178
179-(void)outputAccelertionData:(CMAcceleration)acceleration
180{
181   // With thanks to NSCookbook
182   // http://nscookbook.com/2013/03/ios-programming-recipe-19-using-core-motion-to-access-gyro-and-accelerometer/
183   
184   
185   if ([_switchTiltSensorControl isOn]) {
186     
187      if (! [_switchTiltSensorControlThrottle isOn]) {
188         
189         // Pitch/Yaw Tilt Control
190         
191         double tiltX = acceleration.x;
192         double tiltY = acceleration.y * -1;  // invert the Y axis so that negative values equal left
193         
194         //   NSLog(@"DEBUG: tiltX:%f tiltY:%f", tiltX, tiltY);
195         
196         if (referenceTiltX == 0) {
197            referenceTiltX = tiltX;
198            referenceTiltY = tiltY;
199         }
200         
201         double newYaw = defaultControlYaw + (tiltX - referenceTiltX);
202         double newPitch = defaultControlPitch - (tiltY - referenceTiltY);
203         
204         //   NSLog(@"DEBUG: yaw:%f pitch:%f", yaw.value, pitch.value);
205         //   NSLog(@"DEBUG: newYaw:%f newPitch:%f", newYaw, newPitch);
206         
207         // Filter changes below titleSensorMinimumThreshold
208         if (((newYaw > yaw.value) && (newYaw - yaw.value > titleSensorMinimumThreshold)) ||
209             ((newYaw < yaw.value) && (yaw.value - newYaw > titleSensorMinimumThreshold))) {
210           
211            yaw.value = newYaw;
212         }
213         
214         if (((newPitch > pitch.value) && (newPitch - pitch.value > titleSensorMinimumThreshold)) ||
215             ((newPitch < pitch.value) && (pitch.value - newPitch > titleSensorMinimumThreshold))) {
216            pitch.value = newPitch;
217         }
218         
219      } // Pitch/Yaw Tilt Control
220     
221      else {
222         
223         // Throttle Tilt Control
224         
225         double tiltY = acceleration.y * 1;  // invert the Y axis so that negative values equal forward
226         
227         if (referenceTiltY == 0) {
228            referenceTiltY = tiltY;
229         }
230         
231         double newThrottle = defaultControlThrottle - (tiltY - referenceTiltY);
232         
233         if (((newThrottle > throttle.value) && (newThrottle - throttle.value > titleSensorMinimumThreshold)) ||
234             ((newThrottle < throttle.value) && (throttle.value - newThrottle > titleSensorMinimumThreshold))) {
235            throttle.value = newThrottle;
236         }
237         
238      } // Throttle Tilt Control
239     
240     
241      [self adjustValues];
242     
243   }
244   
245}
246
247-(void)outputRotationData:(CMRotationRate)rotation
248{
249   
250}
251
252- (void)didReceiveMemoryWarning
253{
254   [super didReceiveMemoryWarning];
255   // Dispose of any resources that can be recreated.
256}
257
258
259@end
Note: See TracBrowser for help on using the repository browser.