source: orbit/iOS/Orbit/Orbit/controllers/AdvancedViewController.m @ 876e42b

Servo
Last change on this file since 876e42b was 876e42b, checked in by Steve Castellotti <sc@…>, 7 years ago
  • Property mode set to 100644
File size: 5.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   
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- (IBAction) resetButtonPressed:(id)button
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) pitchChanged:(id) sender
67{
68   pitchPercent.text = [self percentStringFromSlider:pitch];
69   [self adjustValues];
70}
71
72- (IBAction) yawChanged:(id) sender
73{
74   yawPercent.text = [self percentStringFromSlider:yaw];
75   [self adjustValues];
76}
77
78- (IBAction) throttleChanged:(id) sender
79{
80   throttlePercent.text = [self percentStringFromSlider:throttle];
81   [self adjustValues];
82}
83
84- (NSString *) percentStringFromSlider:(UISlider *) slider
85{
86   return [NSString stringWithFormat:@"%i%%", (int)((slider.value - 0.5) * 200)]; // between -100% and 100%
87}
88
89- (void) adjustValues
90{
91   [signalConverter setYaw:[self yawValue]throttle:[self throttleValue] pitch:[self pitchValue]];
92}
93
94- (int) yawValue
95{
96   return round(yaw.value * 100);
97}
98
99- (int) pitchValue
100{
101   return round(pitch.value * 100);
102}
103
104- (int) throttleValue
105{
106   return round(throttle.value * 100);
107}
108
109- (IBAction) hoverButtonPressed:(id)sender {
110   [self resetButtonPressed:(sender)];
111   
112}
113
114- (IBAction) forwardButtonPressed:(id)sender {
115   pitch.value = 0.8;
116   yaw.value = 0.5;
117   throttle.value = 0.5;
118   [self adjustValues];
119   
120}
121
122- (IBAction) leftButtonPressed:(id)sender {
123   pitch.value = 0.5;
124   yaw.value = 0.1;
125   throttle.value = 0.5;
126   [self adjustValues];
127   
128}
129
130- (IBAction) rightButtonPressed:(id)sender {
131   pitch.value = 0.5;
132   yaw.value = 0.9;
133   throttle.value = 0.5;
134   [self adjustValues];
135   
136}
137
138- (IBAction)changeSwitchTiltSensorControl:(id)sender{
139   
140   // With thanks to Kyro Apps
141   // http://kyroapps.weebly.com/xcode-tutorial-452---uiswitch.html
142   
143   if([sender isOn]){
144      NSLog(@"DEBUG: Tilt Sensor Enabled");
145      [_switchTiltSensorControlThrottle setEnabled:YES];
146   } else{
147      NSLog(@"DEBUG: Tilt Sensor Disabled");
148      [_switchTiltSensorControlThrottle setOn:NO animated:YES];
149      [_switchTiltSensorControlThrottle setEnabled:NO];
150   }
151   
152}
153
154- (IBAction)changeSwitchTiltSensorControlThrottle:(id)sender{
155   
156   if([sender isOn]){
157      NSLog(@"DEBUG: Throttle Tilt Sensor Enabled");
158   } else{
159      NSLog(@"DEBUG: Throttle Tilt Sensor Disabled");
160   }
161   
162}
163
164-(void)outputAccelertionData:(CMAcceleration)acceleration
165{
166   // With thanks to NSCookbook
167   // http://nscookbook.com/2013/03/ios-programming-recipe-19-using-core-motion-to-access-gyro-and-accelerometer/
168   
169   //   // Sensor.TYPE_GRAVITY
170   //   if (checkBoxTiltSensorControlThrottle.isChecked()) {
171   //      seekBarThrottle.setProgress(defaultControlThrottle - (int) ((tiltX * 2) - (referenceTiltX * 2)));
172   //   } else {
173   //      seekBarYaw.setProgress(defaultControlYaw + (int) (tiltY - referenceTiltY));
174   //      seekBarPitch.setProgress(defaultControlPitch + (int) (tiltX - referenceTiltX));
175   //   }
176   
177   double tiltX = acceleration.x;
178   double tiltY = acceleration.y * -1;  // invert the Y axis so that negative values equal left
179   
180   NSLog(@"DEBUG: tiltX:%f tiltY:%f", tiltX, tiltY);
181
182   double newYaw = defaultControlYaw + (tiltX - referenceTiltX);
183   double newPitch = defaultControlPitch - (tiltY - referenceTiltY);
184   
185   NSLog(@"DEBUG: yaw:%f pitch:%f", yaw.value, pitch.value);
186   NSLog(@"DEBUG: newYaw:%f newPitch:%f", newYaw, newPitch);
187   
188   yaw.value = newYaw;
189   pitch.value = newPitch;
190   
191   [self adjustValues];
192   
193}
194
195-(void)outputRotationData:(CMRotationRate)rotation
196{
197   
198}
199
200- (void)didReceiveMemoryWarning
201{
202   [super didReceiveMemoryWarning];
203   // Dispose of any resources that can be recreated.
204}
205
206
207@end
Note: See TracBrowser for help on using the repository browser.