[ab9d63b] | 1 | |
---|
| 2 | |
---|
| 3 | |
---|
| 4 | |
---|
| 5 | |
---|
[6db346c] | 6 | |
---|
| 7 | |
---|
| 8 | |
---|
| 9 | |
---|
[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 | |
---|
| 29 | |
---|
| 30 | |
---|
| 31 | self.motionManager = [[CMMotionManager alloc] init]; |
---|
[67d9807] | 32 | self.motionManager.accelerometerUpdateInterval = .2; |
---|
| 33 | self.motionManager.gyroUpdateInterval = .2; |
---|
| 34 | |
---|
| 35 | |
---|
[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)]; |
---|
[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 | |
---|
| 95 | |
---|
| 96 | |
---|
| 97 | |
---|
| 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 | |
---|
| 144 | |
---|
| 145 | |
---|
[6db346c] | 146 | if([sender isOn]){ |
---|
[16249e0] | 147 | |
---|
[d5f1a91] | 148 | [_switchTiltSensorControlThrottle setEnabled:YES]; |
---|
[6db346c] | 149 | } else{ |
---|
[16249e0] | 150 | |
---|
[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 | |
---|
[67d9807] | 163 | referenceTiltY = 0; |
---|
[6db346c] | 164 | } else{ |
---|
[16249e0] | 165 | |
---|
[395ea1a] | 166 | referenceTiltX = 0; |
---|
| 167 | referenceTiltY = 0; |
---|
[6db346c] | 168 | } |
---|
| 169 | |
---|
| 170 | } |
---|
| 171 | |
---|
[876e42b] | 172 | -(void)outputAccelertionData:(CMAcceleration)acceleration |
---|
| 173 | { |
---|
| 174 | |
---|
| 175 | |
---|
| 176 | |
---|
[ac59750] | 177 | |
---|
[bfa2ba1] | 178 | if ([_switchTiltSensorControl isOn]) { |
---|
| 179 | |
---|
| 180 | if (! [_switchTiltSensorControlThrottle isOn]) { |
---|
| 181 | |
---|
| 182 | |
---|
| 183 | |
---|
| 184 | double tiltX = acceleration.x; |
---|
| 185 | double tiltY = acceleration.y * -1; |
---|
| 186 | |
---|
| 187 | |
---|
| 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 | |
---|
| 198 | |
---|
| 199 | |
---|
[67d9807] | 200 | |
---|
| 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 | } |
---|
| 213 | |
---|
| 214 | else { |
---|
| 215 | |
---|
| 216 | |
---|
| 217 | |
---|
[67d9807] | 218 | double tiltY = acceleration.y * 1; |
---|
[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 | } |
---|
| 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 | |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | |
---|
[ab9d63b] | 252 | @end |
---|