/* * Copyright (C) 2009 by Matthias Ringwald * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the copyright holders nor the names of * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * */ // // WiiMoteOpenGLDemoAppDelegate.m // // Created by Matthias Ringwald on 8/2/09. // #import "WiiMoteOpenGLDemoAppDelegate.h" #import "BTDevice.h" #import "EAGLView.h" #import "EAGLViewController.h" #import "BTInquiryViewController.h" #import #import #import BTDevice *device; uint16_t wiiMoteConHandle = 0; WiiMoteOpenGLDemoAppDelegate * theMainApp; @implementation WiiMoteOpenGLDemoAppDelegate @synthesize window; @synthesize glView; @synthesize navControl; @synthesize glViewControl; @synthesize inqViewControl; #define SIZE 5 int counter; static void bt_data_cb(uint8_t x, uint8_t y, uint8_t z){ // NSLog(@"BT data: %u %u %u", x , y ,z); // [[theMainApp status] setText:[NSString stringWithFormat:@"X:%03u Y:%03u Z:%03u", x, y, z]]; float ax = x - 128; float ay = y - 128; float az = z - 128; // mini calib // az /= 2; #if 0 // normalize vector float length = sqrt( ax*ax + ay*ay + az*az); ax /= length; ay /= length; az /= length; #endif #if 0 // cross product between A and (0,0,1) float crossX = ay; float crossY = -ax; float crossZ = 0; float omega = acos( az ); // normalize quat float quatSum = crossX * crossX + crossY * crossY + omega * omega; crossX /= quatSum; crossY /= quatSum; crossZ /= quatSum; omega /= quatSum; // int pitch = atan2( 2*(omega*crossX), omega*omega - crossX*crossX - crossY*crossY)* 180 / M_PI; int roll = atan2( 2*(crossX * crossY), omega*omega + crossX*crossX - crossY*crossY) * 180 / M_PI; int theta = 0; #endif #if 0 int roll = atan2(ax, sqrt(ay*ay+az*az)) * 180 / M_PI; int pitch = atan2(ay, sqrt(ax*ax+az*az)) * 180 / M_PI; int theta = atan2(sqrt(ax*ax+ay*ay), az) * 180 / M_PI; if (az < 0) { pitch = 180 - pitch; } #endif // sort axes float h = az; az = -ay; ay = h; // calc int roll = atan2(ax, ay) * 180 / M_PI; int pitch = atan2(ay, az) * 180 / M_PI; int theta = 0; #if 0 if (roll >= 90 || roll <= -90) { pitch = 360 - pitch; } // if ( (++counter & 15) == 0) // NSLog(@"BT data: %f %f %f: pitch %i, roll %i, yaw %i", ax , ay ,az, pitch, roll, theta); #endif pitch = 0; #if 1 static int lastPitch; static int lastRoll; if (abs(lastPitch - pitch) > 180) { if (pitch > lastPitch) { pitch -= 360; } else { pitch += 360; } } if (abs(lastRoll - roll) > 180) { if (roll > lastRoll) { roll -= 360; } else { roll += 360; } } // moving average of size SIZE static int pos = 0; static int historyRoll[SIZE]; static int historyPitch[SIZE]; static int historyTheta[SIZE]; historyRoll[pos] = roll; historyPitch[pos] = pitch; historyTheta[pos] = theta; pos++; if (pos==SIZE) pos = 0; pitch = roll = 0; int i; for (i=0;i