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

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