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

Servo
Last change on this file since ac59750 was ac59750, checked in by Steve Castellotti <sc@…>, 7 years ago
  • referenceTiltX and Y processing added
  • Property mode set to 100644
File size: 6.3 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 setYaw:[self yawValue]throttle:[self throttleValue] pitch:[self pitchValue]];
94}
95
96- (int) yawValue
97{
98   return round(yaw.value * 100);
99}
100
101- (int) pitchValue
102{
103   return round(pitch.value * 100);
104}
105
106- (int) throttleValue
107{
108   return round(throttle.value * 100);
109}
110
111- (IBAction) hoverButtonPressed:(id)sender {
112   [self resetButtonPressed:(sender)];
113   
114}
115
116- (IBAction) forwardButtonPressed:(id)sender {
117   pitch.value = 0.8;
118   yaw.value = 0.5;
119   throttle.value = 0.5;
120   [self adjustValues];
121   
122}
123
124- (IBAction) leftButtonPressed:(id)sender {
125   pitch.value = 0.5;
126   yaw.value = 0.1;
127   throttle.value = 0.5;
128   [self adjustValues];
129   
130}
131
132- (IBAction) rightButtonPressed:(id)sender {
133   pitch.value = 0.5;
134   yaw.value = 0.9;
135   throttle.value = 0.5;
136   [self adjustValues];
137   
138}
139
140- (IBAction)changeSwitchTiltSensorControl:(id)sender{
141   
142   // With thanks to Kyro Apps
143   // http://kyroapps.weebly.com/xcode-tutorial-452---uiswitch.html
144   
145   if([sender isOn]){
146      NSLog(@"DEBUG: Tilt Sensor Enabled");
147      [_switchTiltSensorControlThrottle setEnabled:YES];
148   } else{
149      NSLog(@"DEBUG: Tilt Sensor Disabled");
150      [_switchTiltSensorControlThrottle setOn:NO animated:YES];
151      [_switchTiltSensorControlThrottle setEnabled:NO];
152      referenceTiltX = 0;
153                referenceTiltY = 0;
154   }
155   
156}
157
158- (IBAction)changeSwitchTiltSensorControlThrottle:(id)sender{
159   
160   if([sender isOn]){
161      NSLog(@"DEBUG: Throttle Tilt Sensor Enabled");
162   } else{
163      NSLog(@"DEBUG: Throttle Tilt Sensor Disabled");
164   }
165   
166}
167
168-(void)outputAccelertionData:(CMAcceleration)acceleration
169{
170   // With thanks to NSCookbook
171   // http://nscookbook.com/2013/03/ios-programming-recipe-19-using-core-motion-to-access-gyro-and-accelerometer/
172   
173   
174   
175   // TODO Add routing to smooth out changes
176   // Abrupt movements is breaking signal transmission
177   
178   
179   
180   if ([_switchTiltSensorControl isOn]) {
181     
182      if (! [_switchTiltSensorControlThrottle isOn]) {
183         
184         // Pitch/Yaw Tilt Control
185         
186         double tiltX = acceleration.x;
187         double tiltY = acceleration.y * -1;  // invert the Y axis so that negative values equal left
188         
189         //   NSLog(@"DEBUG: tiltX:%f tiltY:%f", tiltX, tiltY);
190         
191         if (referenceTiltX == 0) {
192            referenceTiltX = tiltX;
193            referenceTiltY = tiltY;
194         }
195         
196         double newYaw = defaultControlYaw + (tiltX - referenceTiltX);
197         double newPitch = defaultControlPitch - (tiltY - referenceTiltY);
198         
199         //   NSLog(@"DEBUG: yaw:%f pitch:%f", yaw.value, pitch.value);
200         //   NSLog(@"DEBUG: newYaw:%f newPitch:%f", newYaw, newPitch);
201         
202         yaw.value = newYaw;
203         pitch.value = newPitch;
204         
205      } // Pitch/Yaw Tilt Control
206     
207      else {
208         
209         // Throttle Tilt Control
210         
211         double tiltY = acceleration.y * -1;  // invert the Y axis so that negative values equal forward
212         
213         throttle.value = defaultControlThrottle - (tiltY - referenceTiltY);
214         
215      } // Throttle Tilt Control
216     
217
218      [self adjustValues];
219     
220   }
221   
222}
223
224-(void)outputRotationData:(CMRotationRate)rotation
225{
226   
227}
228
229- (void)didReceiveMemoryWarning
230{
231   [super didReceiveMemoryWarning];
232   // Dispose of any resources that can be recreated.
233}
234
235
236@end
Note: See TracBrowser for help on using the repository browser.