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

Servo
Last change on this file since af91708 was 16249e0, checked in by Steve Castellotti <sc@…>, 9 years ago
  • Updated version numbers
  • Trimmed debug output
  • Whitespace and comment cleanup
  • Property mode set to 100644
File size: 7.6 KB
RevLine 
[ab9d63b]1//
2//  AdvancedViewController.m
3//  orbit
4//
5//  Copyright (c) 2013 Puzzlebox Productions, LLC. All rights reserved.
[6db346c]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
[ab9d63b]10//
11
12#import "AdvancedViewController.h"
[ce0a7ee]13#import "SignalConverter.h"
14#import "AppDelegate.h"
[ab9d63b]15
[ce0a7ee]16@implementation AdvancedViewController {
[876e42b]17   SignalConverter *signalConverter;
[ce0a7ee]18}
[ab9d63b]19
20@synthesize pitch, pitchPercent, yaw, yawPercent, throttle, throttlePercent;
21
[ce0a7ee]22- (void)viewDidLoad
23{
[876e42b]24   [super viewDidLoad];
25   AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
26   signalConverter = appDelegate.signalConverter;
27   
28   //   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];
29   //   [alert show];
30   
31   self.motionManager = [[CMMotionManager alloc] init];
[67d9807]32   self.motionManager.accelerometerUpdateInterval = .2;
33   self.motionManager.gyroUpdateInterval = .2;
34   //   self.motionManager.accelerometerUpdateInterval = .1;
35   //   self.motionManager.gyroUpdateInterval = .1;
[876e42b]36   
37   [self.motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue]
38                                            withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
39                                               [self outputAccelertionData:accelerometerData.acceleration];
40                                               if(error){
41                                                  NSLog(@"%@", error);
42                                               }
43                                            }];
44   
45   [self.motionManager startGyroUpdatesToQueue:[NSOperationQueue currentQueue]
46                                   withHandler:^(CMGyroData *gyroData, NSError *error) {
47                                      [self outputRotationData:gyroData.rotationRate];
48                                   }];
[505e3d5]49   
[ce0a7ee]50}
51
[0a45bc8]52- (IBAction) resetButtonPressed:(id)button
53{
[876e42b]54   pitch.value = defaultControlPitch;
55   yaw.value = defaultControlYaw;
56   throttle.value = defaultControlThrottle;
[0a45bc8]57   NSString *zero = @"0%";
58   throttlePercent.text = zero;
59   yawPercent.text = zero;
60   pitchPercent.text = zero;
61   [self adjustValues];
62}
63
[ab9d63b]64- (IBAction) pitchChanged:(id) sender
65{
[876e42b]66   pitchPercent.text = [self percentStringFromSlider:pitch];
67   [self adjustValues];
[ab9d63b]68}
69
70- (IBAction) yawChanged:(id) sender
71{
[876e42b]72   yawPercent.text = [self percentStringFromSlider:yaw];
73   [self adjustValues];
[ab9d63b]74}
75
76- (IBAction) throttleChanged:(id) sender
77{
[876e42b]78   throttlePercent.text = [self percentStringFromSlider:throttle];
79   [self adjustValues];
[ab9d63b]80}
81
82- (NSString *) percentStringFromSlider:(UISlider *) slider
83{
[876e42b]84   return [NSString stringWithFormat:@"%i%%", (int)((slider.value - 0.5) * 200)]; // between -100% and 100%
[ab9d63b]85}
86
[ce0a7ee]87- (void) adjustValues
88{
[b094d6c]89   [signalConverter setControlSettings:[self yawValue]throttle:[self throttleValue] pitch:[self pitchValue]];
[de10cbc]90}
91
92- (int) yawValue
93{
[b094d6c]94   // We subtract the current Yaw position from the maximum slider value
95   // because smaller values instruct the helicopter to spin to the right
96   // (clockwise if looking down from above) whereas intuitively moving
97   // the slider to the left should cause it to spin left
98   
99   return round((yaw.maximumValue - yaw.value) * 100);
[de10cbc]100}
101
102- (int) pitchValue
103{
[876e42b]104   return round(pitch.value * 100);
[de10cbc]105}
106
107- (int) throttleValue
108{
[876e42b]109   return round(throttle.value * 100);
[de10cbc]110}
111
[0a45bc8]112- (IBAction) hoverButtonPressed:(id)sender {
[7f21a20]113   [self resetButtonPressed:(sender)];
114   
[ce0a7ee]115}
116
[0a45bc8]117- (IBAction) forwardButtonPressed:(id)sender {
[67d9807]118   throttle.value = defaultControlThrottle;
119   pitch.value = defaultControlPitch + 0.2;
120   yaw.value = defaultControlYaw;
[7f21a20]121   [self adjustValues];
122   
[0a45bc8]123}
124
125- (IBAction) leftButtonPressed:(id)sender {
[67d9807]126   throttle.value = defaultControlThrottle;
127   pitch.value = defaultControlPitch;
[b094d6c]128   yaw.value = defaultControlYaw - 0.5;
[7f21a20]129   [self adjustValues];
[876e42b]130   
[0a45bc8]131}
132
133- (IBAction) rightButtonPressed:(id)sender {
[67d9807]134   throttle.value = defaultControlThrottle;
135   pitch.value = defaultControlPitch;
136   yaw.value = defaultControlYaw + 0.3;
[7f21a20]137   [self adjustValues];
[876e42b]138   
[0a45bc8]139}
140
[d42b042]141- (IBAction)changeSwitchTiltSensorControl:(id)sender{
[876e42b]142   
143   // With thanks to Kyro Apps
144   // http://kyroapps.weebly.com/xcode-tutorial-452---uiswitch.html
145   
[6db346c]146   if([sender isOn]){
[16249e0]147      //      NSLog(@"DEBUG: Tilt Sensor Enabled");
[d5f1a91]148      [_switchTiltSensorControlThrottle setEnabled:YES];
[6db346c]149   } else{
[16249e0]150      //      NSLog(@"DEBUG: Tilt Sensor Disabled");
[d5f1a91]151      [_switchTiltSensorControlThrottle setOn:NO animated:YES];
152      [_switchTiltSensorControlThrottle setEnabled:NO];
[ac59750]153      referenceTiltX = 0;
154                referenceTiltY = 0;
[6db346c]155   }
156   
157}
158
[d42b042]159- (IBAction)changeSwitchTiltSensorControlThrottle:(id)sender{
[6db346c]160   
161   if([sender isOn]){
[16249e0]162      //      NSLog(@"DEBUG: Throttle Tilt Sensor Enabled");
[67d9807]163      referenceTiltY = 0;
[6db346c]164   } else{
[16249e0]165      //      NSLog(@"DEBUG: Throttle Tilt Sensor Disabled");
[395ea1a]166      referenceTiltX = 0;
167                referenceTiltY = 0;
[6db346c]168   }
169   
170}
171
[876e42b]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   
[ac59750]177   
[bfa2ba1]178   if ([_switchTiltSensorControl isOn]) {
179     
180      if (! [_switchTiltSensorControlThrottle isOn]) {
181         
182         // Pitch/Yaw Tilt Control
183         
184         double tiltX = acceleration.x;
185         double tiltY = acceleration.y * -1;  // invert the Y axis so that negative values equal left
186         
187         //   NSLog(@"DEBUG: tiltX:%f tiltY:%f", tiltX, tiltY);
188         
[ac59750]189         if (referenceTiltX == 0) {
190            referenceTiltX = tiltX;
191            referenceTiltY = tiltY;
192         }
193         
[bfa2ba1]194         double newYaw = defaultControlYaw + (tiltX - referenceTiltX);
195         double newPitch = defaultControlPitch - (tiltY - referenceTiltY);
196         
197         //   NSLog(@"DEBUG: yaw:%f pitch:%f", yaw.value, pitch.value);
198         //   NSLog(@"DEBUG: newYaw:%f newPitch:%f", newYaw, newPitch);
199         
[67d9807]200         // Filter changes below titleSensorMinimumThreshold
201         if (((newYaw > yaw.value) && (newYaw - yaw.value > titleSensorMinimumThreshold)) ||
202             ((newYaw < yaw.value) && (yaw.value - newYaw > titleSensorMinimumThreshold))) {
[b094d6c]203           
[67d9807]204            yaw.value = newYaw;
205         }
206         
207         if (((newPitch > pitch.value) && (newPitch - pitch.value > titleSensorMinimumThreshold)) ||
208             ((newPitch < pitch.value) && (pitch.value - newPitch > titleSensorMinimumThreshold))) {
209            pitch.value = newPitch;
210         }
[bfa2ba1]211         
212      } // Pitch/Yaw Tilt Control
213     
214      else {
215         
216         // Throttle Tilt Control
217         
[67d9807]218         double tiltY = acceleration.y * 1;  // invert the Y axis so that negative values equal forward
[bfa2ba1]219         
[395ea1a]220         if (referenceTiltY == 0) {
221            referenceTiltY = tiltY;
222         }
223         
[67d9807]224         double newThrottle = defaultControlThrottle - (tiltY - referenceTiltY);
225         
226         if (((newThrottle > throttle.value) && (newThrottle - throttle.value > titleSensorMinimumThreshold)) ||
227             ((newThrottle < throttle.value) && (throttle.value - newThrottle > titleSensorMinimumThreshold))) {
228            throttle.value = newThrottle;
229         }
[bfa2ba1]230         
231      } // Throttle Tilt Control
232     
[67d9807]233     
[9a24f48]234      [self adjustValues];
235     
[bfa2ba1]236   }
[876e42b]237   
238}
239
240-(void)outputRotationData:(CMRotationRate)rotation
241{
242   
243}
244
245- (void)didReceiveMemoryWarning
246{
247   [super didReceiveMemoryWarning];
248   // Dispose of any resources that can be recreated.
249}
250
251
[ab9d63b]252@end
Note: See TracBrowser for help on using the repository browser.