source: orbit/iOS/Orbit/Orbit/controllers/AdvancedViewController.m @ 9a24f48

Servo
Last change on this file since 9a24f48 was 9a24f48, checked in by Steve Castellotti <sc@…>, 7 years ago
  • realtime Tilt Sensor control updates fixed
  • Property mode set to 100644
File size: 6.0 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   }
153   
154}
155
156- (IBAction)changeSwitchTiltSensorControlThrottle:(id)sender{
157   
158   if([sender isOn]){
159      NSLog(@"DEBUG: Throttle Tilt Sensor Enabled");
160   } else{
161      NSLog(@"DEBUG: Throttle Tilt Sensor Disabled");
162   }
163   
164}
165
166-(void)outputAccelertionData:(CMAcceleration)acceleration
167{
168   // With thanks to NSCookbook
169   // http://nscookbook.com/2013/03/ios-programming-recipe-19-using-core-motion-to-access-gyro-and-accelerometer/
170   
171   if ([_switchTiltSensorControl isOn]) {
172     
173      if (! [_switchTiltSensorControlThrottle isOn]) {
174         
175         // Pitch/Yaw Tilt Control
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      } // Pitch/Yaw Tilt Control
192     
193      else {
194         
195         // Throttle Tilt Control
196         
197         double tiltY = acceleration.y * -1;  // invert the Y axis so that negative values equal forward
198         
199         throttle.value = defaultControlThrottle - (tiltY - referenceTiltY);
200         
201      } // Throttle Tilt Control
202     
203
204      [self adjustValues];
205     
206   }
207   
208}
209
210-(void)outputRotationData:(CMRotationRate)rotation
211{
212   
213}
214
215- (void)didReceiveMemoryWarning
216{
217   [super didReceiveMemoryWarning];
218   // Dispose of any resources that can be recreated.
219}
220
221
222@end
Note: See TracBrowser for help on using the repository browser.