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

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