source: orbit/iOS/Orbit/Orbit/controllers/AdvancedViewController.m @ 67d9807

Servo
Last change on this file since 67d9807 was 67d9807, checked in by Steve Castellotti <sc@…>, 9 years ago
  • Tilt Sensor fine tuning include fllter for sudden changes
  • Property mode set to 100644
File size: 7.5 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   NSLog(@"DEBUG: yaw:%f pitch:%f", yaw.value, pitch.value);
94   [signalConverter setYaw:[self yawValue]throttle:[self throttleValue] pitch:[self pitchValue]];
95}
96
97- (int) yawValue
98{
99   return round(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.3;
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   
179   // TODO Add routing to smooth out changes
180   // Abrupt movements is breaking signal transmission
181   
182   
183   
184   if ([_switchTiltSensorControl isOn]) {
185     
186      if (! [_switchTiltSensorControlThrottle isOn]) {
187         
188         // Pitch/Yaw Tilt Control
189         
190         double tiltX = acceleration.x;
191         double tiltY = acceleration.y * -1;  // invert the Y axis so that negative values equal left
192         
193         //   NSLog(@"DEBUG: tiltX:%f tiltY:%f", tiltX, tiltY);
194         
195         if (referenceTiltX == 0) {
196            referenceTiltX = tiltX;
197            referenceTiltY = tiltY;
198         }
199         
200         double newYaw = defaultControlYaw + (tiltX - referenceTiltX);
201         double newPitch = defaultControlPitch - (tiltY - referenceTiltY);
202         
203         //   NSLog(@"DEBUG: yaw:%f pitch:%f", yaw.value, pitch.value);
204         //   NSLog(@"DEBUG: newYaw:%f newPitch:%f", newYaw, newPitch);
205         
206         // Filter changes below titleSensorMinimumThreshold
207         if (((newYaw > yaw.value) && (newYaw - yaw.value > titleSensorMinimumThreshold)) ||
208             ((newYaw < yaw.value) && (yaw.value - newYaw > titleSensorMinimumThreshold))) {
209            yaw.value = newYaw;
210         }
211         
212         if (((newPitch > pitch.value) && (newPitch - pitch.value > titleSensorMinimumThreshold)) ||
213             ((newPitch < pitch.value) && (pitch.value - newPitch > titleSensorMinimumThreshold))) {
214            pitch.value = newPitch;
215         }
216         
217      } // Pitch/Yaw Tilt Control
218     
219      else {
220         
221         // Throttle Tilt Control
222         
223         double tiltY = acceleration.y * 1;  // invert the Y axis so that negative values equal forward
224         
225         if (referenceTiltY == 0) {
226            referenceTiltY = tiltY;
227         }
228         
229         double newThrottle = defaultControlThrottle - (tiltY - referenceTiltY);
230         
231         if (((newThrottle > throttle.value) && (newThrottle - throttle.value > titleSensorMinimumThreshold)) ||
232             ((newThrottle < throttle.value) && (throttle.value - newThrottle > titleSensorMinimumThreshold))) {
233            throttle.value = newThrottle;
234         }
235         
236      } // Throttle Tilt Control
237     
238     
239      [self adjustValues];
240     
241   }
242   
243}
244
245-(void)outputRotationData:(CMRotationRate)rotation
246{
247   
248}
249
250- (void)didReceiveMemoryWarning
251{
252   [super didReceiveMemoryWarning];
253   // Dispose of any resources that can be recreated.
254}
255
256
257@end
Note: See TracBrowser for help on using the repository browser.