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

Servo
Last change on this file since b094d6c was b094d6c, checked in by Steve Castellotti <sc@…>, 8 years ago
  • Fine-tuning of Throttle/Yaw/Pitch? thresholds and range of settings
  • Inverted calculations for Yaw so that left on the slider rotates left/counter-clockwise (when viewed from above) and vice versa
  • 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//@interface AdvancedViewController ()
17//
18//@end
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   //   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];
33   //   [alert show];
34   
35   self.motionManager = [[CMMotionManager alloc] init];
36   self.motionManager.accelerometerUpdateInterval = .2;
37   self.motionManager.gyroUpdateInterval = .2;
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)]; // between -100% and 100%
89}
90
91- (void) adjustValues
92{
93   [signalConverter setControlSettings:[self yawValue]throttle:[self throttleValue] pitch:[self pitchValue]];
94}
95
96- (int) yawValue
97{
98   // We subtract the current Yaw position from the maximum slider value
99   // because smaller values instruct the helicopter to spin to the right
100   // (clockwise if looking down from above) whereas intuitively moving
101   // the slider to the left should cause it to spin left
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   // With thanks to Kyro Apps
148   // http://kyroapps.weebly.com/xcode-tutorial-452---uiswitch.html
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   // With thanks to NSCookbook
179   // http://nscookbook.com/2013/03/ios-programming-recipe-19-using-core-motion-to-access-gyro-and-accelerometer/
180   
181   
182   
183   // TODO Add routing to smooth out changes
184   // Abrupt movements is breaking signal transmission
185   
186   
187   
188   if ([_switchTiltSensorControl isOn]) {
189     
190      if (! [_switchTiltSensorControlThrottle isOn]) {
191         
192         // Pitch/Yaw Tilt Control
193         
194         double tiltX = acceleration.x;
195         double tiltY = acceleration.y * -1;  // invert the Y axis so that negative values equal left
196         
197         //   NSLog(@"DEBUG: tiltX:%f tiltY:%f", tiltX, tiltY);
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         //   NSLog(@"DEBUG: yaw:%f pitch:%f", yaw.value, pitch.value);
208         //   NSLog(@"DEBUG: newYaw:%f newPitch:%f", newYaw, newPitch);
209         
210         // Filter changes below titleSensorMinimumThreshold
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      } // Pitch/Yaw Tilt Control
223     
224      else {
225         
226         // Throttle Tilt Control
227         
228         double tiltY = acceleration.y * 1;  // invert the Y axis so that negative values equal forward
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      } // Throttle Tilt Control
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   // Dispose of any resources that can be recreated.
259}
260
261
262@end
Note: See TracBrowser for help on using the repository browser.