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

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