From dde0e82eb7a5a5500e27071e344e8afe4e336049 Mon Sep 17 00:00:00 2001 From: cnlohr Date: Sat, 11 Mar 2017 22:45:31 -0500 Subject: Update with almost working poser information stuff. This has been long stream to live. Goobye. --- src/poser_daveortho.c | 457 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/survive_cal.c | 150 ++++++++++++++++- src/survive_cal.h | 4 +- 3 files changed, 603 insertions(+), 8 deletions(-) create mode 100644 src/poser_daveortho.c (limited to 'src') diff --git a/src/poser_daveortho.c b/src/poser_daveortho.c new file mode 100644 index 0000000..80ddd90 --- /dev/null +++ b/src/poser_daveortho.c @@ -0,0 +1,457 @@ +#include "survive_cal.h" +#include +#include +#include "linmath.h" +#include +#include +#include +#include +#include + +static int LH_ID; + +void OrthoSolve( + FLT T[4][4], // OUTPUT: 4x4 transformation matrix + FLT S_out[2][SENSORS_PER_OBJECT], // OUTPUT: array of screenspace points + FLT S_in[2][SENSORS_PER_OBJECT], // INPUT: array of screenspace points + FLT X_in[3][SENSORS_PER_OBJECT], // INPUT: array of offsets + int nPoints); + + +typedef struct +{ + int something; + //Stuff +} DummyData; + +int PoserDaveOrtho( SurviveObject * so, PoserData * pd ) +{ + PoserType pt = pd->pt; + SurviveContext * ctx = so->ctx; + DummyData * dd = so->PoserData; + + if( !dd ) so->PoserData = dd = malloc( sizeof( DummyData ) ); + + switch( pt ) + { + case POSERDATA_IMU: + { + PoserDataIMU * imu = (PoserDataIMU*)pd; + //printf( "IMU:%s (%f %f %f) (%f %f %f)\n", so->codename, imu->accel[0], imu->accel[1], imu->accel[2], imu->gyro[0], imu->gyro[1], imu->gyro[2] ); + break; + } + case POSERDATA_LIGHT: + { + PoserDataLight * l = (PoserDataLight*)pd; + //printf( "LIG:%s %d @ %f rad, %f s (AC %d) (TC %d)\n", so->codename, l->sensor_id, l->angle, l->length, l->acode, l->timecode ); + break; + } + case POSERDATA_FULL_SCENE: + { + PoserDataFullScene * fs = (PoserDataFullScene*)pd; + + for( LH_ID = 0; LH_ID < 2; LH_ID++ ) + { + int i; + int max_hits = 0; + FLT S_in[2][SENSORS_PER_OBJECT]; + FLT X_in[3][SENSORS_PER_OBJECT]; + for( i = 0; i < SENSORS_PER_OBJECT; i++ ) + { + //Load all our valid points into something the LHFinder can use. + if( fs->lengths[i][LH_ID][0] > 0 ) + { + S_in[0][max_hits] = fs->angles[i][LH_ID][0]; + S_in[1][max_hits] = fs->angles[i][LH_ID][1]; + X_in[0][max_hits] = so->sensor_locations[i*3+0]; + X_in[1][max_hits] = so->sensor_locations[i*3+1]; + X_in[2][max_hits] = so->sensor_locations[i*3+2]; + max_hits++; + } + + } + FLT tOut[4][4]; + FLT S_out[2][SENSORS_PER_OBJECT]; + OrthoSolve( tOut, S_out, S_in, X_in, max_hits ); + + //Now, we need to solve where we are as a function of where + //the lighthouses are. + FLT quat[4]; + FLT posoff[3] = { tOut[0][3], tOut[1][3], tOut[2][3] }; + FLT MT[4][4]; + //matrix44transpose( MT, &T[0][0] ); + matrix44copy( &MT[0][0], &tOut[0][0] ); + + quatfrommatrix( quat, &MT[0][0] ); + //printf( "QUAT: %f %f %f %f = %f\n", quat[0], quat[1], quat[2], quat[3], quatmagnitude(quat) ); + //quat[2] -= 0.005; //fixes up lh0 in test data set. + quatnormalize( quat, quat ); + printf( "QUAT: %f %f %f %f = %f [%f %f %f]\n", quat[0], quat[1], quat[2], quat[3], quatmagnitude(quat), posoff[0], posoff[1], posoff[2] ); + + for( i = 0; i < max_hits;i++ ) + { + FLT pt[3] = { X_in[0][i], X_in[1][i], X_in[2][i] }; + quatrotatevector( pt, quat, pt ); + add3d( pt, pt, posoff ); + printf( "OUT %f %f %f ANGLE %f %f AOUT %f %f\n", + pt[0], pt[1], pt[2], + S_in[0][i], S_in[1][i], atan2( pt[0], pt[1] ), atan2( pt[2], pt[1] ) ); + } + + so->FromLHPose[LH_ID].Pos[0] = posoff[0]; + so->FromLHPose[LH_ID].Pos[1] = posoff[1]; + so->FromLHPose[LH_ID].Pos[2] = posoff[2]; + so->FromLHPose[LH_ID].Rot[0] = quat[0]; + so->FromLHPose[LH_ID].Rot[1] = quat[1]; + so->FromLHPose[LH_ID].Rot[2] = quat[2]; + so->FromLHPose[LH_ID].Rot[3] = quat[3]; + } + + break; + } + case POSERDATA_DISASSOCIATE: + { + free( dd ); + so->PoserData = 0; + //printf( "Need to disassociate.\n" ); + break; + } + } + +} + + +REGISTER_LINKTIME( PoserDaveOrtho ); + + + + + +#define PRINT_MAT(A,M,N) { \ + int m,n; \ + printf(#A "\n"); \ + for (m=0; m 0 && err < bestErr) { x[1][0]=x_y; y[1][0]=y_y; z[1][0]=z_y; bestErr=err; } + if ( i == 0 && j == 1 && k == 0) { x[1][0]=x_y; y[1][0]=y_y; z[1][0]=z_y; bestErr=err; } + z_y = -z_y; + } + y_y = -y_y; + } + x_y = -x_y; + } + printf("bestErr %f\n", bestErr); +*/ + + //------------------------- + // A test version of the rescaling to the proper length + //------------------------- + FLT ydist2 = ydist; + FLT bestBestErr = 9999.0; + FLT bestYdist = 0; + //for (ydist2=ydist-0.1; ydist2 %f %f %f\n", x_y, y_y, z_y ); + + // Exhaustively flip the minus sign of the z axis until we find the right one . . . + FLT bestErr = 9999.0; + FLT xy_dot2 = x2[0][0]*y2[0][0] + x2[2][0]*y2[2][0]; + FLT yz_dot2 = y2[0][0]*z2[0][0] + y2[2][0]*z2[2][0]; + FLT zx_dot2 = z2[0][0]*x2[0][0] + z2[2][0]*x2[2][0]; + for (i=0;i<2;i++) { + for (j=0;j<2;j++) { + for(k=0;k<2;k++) { + + // Calculate the error term + FLT xy_dot = xy_dot2 + x_y*y_y; + FLT yz_dot = yz_dot2 + y_y*z_y; + FLT zx_dot = zx_dot2 + z_y*x_y; + FLT err = _ABS(xy_dot) + _ABS(yz_dot) + _ABS(zx_dot); + + // Calculate the handedness + FLT cx,cy,cz; + CrossProduct(cx,cy,cz,x2[0][0],x_y,x2[2][0],y2[0][0],y_y,y2[2][0]); + FLT hand = cx*z2[0][0] + cy*z_y + cz*z2[2][0]; + printf("err %f hand %f\n", err, hand); + + // If we are the best right-handed frame so far + if (hand > 0 && err < bestErr) { x2[1][0]=x_y; y2[1][0]=y_y; z2[1][0]=z_y; bestErr=err; } + z_y = -z_y; + } + y_y = -y_y; + } + x_y = -x_y; + } + printf("ydist2 %f bestErr %f\n",ydist2,bestErr); + + if (bestErr < bestBestErr) { + memcpy(x,x2,3*sizeof(FLT)); + memcpy(y,y2,3*sizeof(FLT)); + memcpy(z,z2,3*sizeof(FLT)); + bestBestErr = bestErr; + bestYdist = ydist2; + } + } + ydist = bestYdist; + +/* + for (i=0; i #include #include +#include #include "survive_config.h" @@ -22,6 +23,9 @@ #define NEEDED_TIMES_OF_COMMON 5 #define DRPTS_NEEDED_FOR_AVG ((int)(DRPTS*3/4)) + + + static void handle_calibration( struct SurviveCalData *cd ); static void reset_calibration( struct SurviveCalData * cd ); @@ -109,6 +113,23 @@ void survive_cal_install( struct SurviveContext * ctx ) return; } + + + const char * DriverName; + const char * PreferredPoser = config_read_str( ctx->global_config_values, "ConfigPoser", "PoserDaveOrtho" ); + PoserCB PreferredPoserCB = 0; + const char * FirstPoser = 0; + printf( "Available posers:\n" ); + i = 0; + while( ( DriverName = GetDriverNameMatching( "Poser", i++ ) ) ) + { + PoserCB p = GetDriver( DriverName ); + if( !PreferredPoserCB ) PreferredPoserCB = p; + int ThisPoser = strcmp( DriverName, PreferredPoser ) == 0; + if( ThisPoser ) PreferredPoserCB = p; + } + cd->ConfigPoserFn = PreferredPoserCB; + printf( "Got config poser: %p\n", cd->ConfigPoserFn ); ootx_packet_clbk = ootx_packet_clbk_d; ctx->calptr = cd; @@ -201,7 +222,7 @@ void survive_cal_angle( struct SurviveObject * so, int sensor_id, int acode, uin if( cd->peak_counts >= PTS_BEFORE_COMMON ) { - int tfc = cd->times_found_common; +/* int tfc = cd->times_found_common; if( cd->found_common ) { if( tfc >= NEEDED_TIMES_OF_COMMON ) @@ -223,7 +244,12 @@ void survive_cal_angle( struct SurviveObject * so, int sensor_id, int acode, uin SV_INFO( "Stage 2 bad - redoing. %d %d %d", cd->peak_counts, cd->found_common, tfc ); reset_calibration( cd ); cd->times_found_common = 0; - } + }*/ + + SV_INFO( "Stage 2 moving to stage 3. %d %d", cd->peak_counts, cd->found_common ); + reset_calibration( cd ); + cd->stage = 3; + cd->found_common = 1; } break; @@ -410,8 +436,10 @@ static void handle_calibration( struct SurviveCalData *cd ) } fclose( hists ); fclose( ptinfo ); - +/* //Comb through data and make sure we still have a sensor on a WM that + //We don't do this anymore. + int bcp_senid = 0; int bcp_count = 0; for( sen = 0; sen < MAX_SENSORS_TO_CAL; sen++ ) @@ -434,13 +462,121 @@ static void handle_calibration( struct SurviveCalData *cd ) reset_calibration( cd ); return; } - cd->senid_of_checkpt = bcp_senid; +*/ + + int i, j; + PoserDataFullScene fsd; + fsd.pt = POSERDATA_FULL_SCENE; + for( j = 0; j < NUM_LIGHTHOUSES; j++ ) + for( i = 0; i < SENSORS_PER_OBJECT; i++ ) + { + int gotdata = 0; - if( survive_cal_lhfind( cd ) == 0 ) + int dataindex = i*(2*NUM_LIGHTHOUSES)+j*2+0; + + if( cd->ctsweeps[dataindex+0] < DRPTS_NEEDED_FOR_AVG || + cd->ctsweeps[dataindex+1] < DRPTS_NEEDED_FOR_AVG ) + { + fsd.lengths[i][j][0] = -1; + fsd.lengths[i][j][1] = -1; + continue; + } + fsd.lengths[i][j][0] = cd->avglens[dataindex+0]; + fsd.lengths[i][j][1] = cd->avglens[dataindex+1]; + fsd.angles[i][j][0] = cd->avgsweeps[dataindex+0]; + fsd.angles[i][j][1] = cd->avgsweeps[dataindex+1]; + } + + cd->ConfigPoserFn( cd->hmd, (PoserData*)&fsd ); + if( 1 ) { - SV_INFO( "Stage 4 succeeded." ); - cd->stage = 5; + static int notfirstcal = 0; + SV_INFO( "Stage 4 succeeded. Inverting %d", notfirstcal ); + + if( !notfirstcal ) + { + // XXX This part is /all/ wrong. + // XXX This part is /all/ wrong. + // XXX This part is /all/ wrong. + + //OK! We've arrived. Now, we have to get the LH's pose from. + int lh; + for( lh = 0; lh < NUM_LIGHTHOUSES; lh++ ) + { + SurvivePose * objfromlh = &cd->hmd->FromLHPose[lh]; + SurvivePose * lhp = &ctx->bsd[lh].Pose; + + + lhp->Pos[0] = objfromlh->Pos[0]; + lhp->Pos[1] = objfromlh->Pos[1]; + lhp->Pos[2] = objfromlh->Pos[2]; + + lhp->Rot[0] = objfromlh->Rot[0]*-1; + lhp->Rot[1] = objfromlh->Rot[1]; + lhp->Rot[2] = objfromlh->Rot[2]; + lhp->Rot[3] = objfromlh->Rot[3]; + + //Write lhp from the inverse of objfromlh + quatrotatevector( lhp->Pos, lhp->Rot, lhp->Pos ); + + + fprintf( stderr, "%f %f %f\n", objfromlh->Pos[0], objfromlh->Pos[1], objfromlh->Pos[2] ); + fprintf( stderr, "%f %f %f %f\n", objfromlh->Rot[0], objfromlh->Rot[1], objfromlh->Rot[2], objfromlh->Rot[3] ); + fprintf( stderr, "%f %f %f\n", lhp->Pos[0], lhp->Pos[1], lhp->Pos[2] ); + /* + -0.074179 2.793859 0.519508 + -0.092802 0.087361 0.872115 0.472409 + 0.379494 1.906039 2.074617 + + 0.695987 3.346798 -0.776169 + -0.258207 0.966008 -0.008604 0.009116 + 0.614156 -2.521008 2.356952 + + */ + + printf( "\n" ); + + } + notfirstcal = 1; + } + else + { + for( lh = 0; lh < NUM_LIGHTHOUSES; lh++ ) + { + SurvivePose * objfromlh = &cd->hmd->FromLHPose[lh]; + SurvivePose * lhp = &ctx->bsd[lh].Pose; + + FLT pos[3] = { objfromlh->Pos[0], + objfromlh->Pos[1], + objfromlh->Pos[2] }; + + FLT rot[4] = { objfromlh->Rot[0]*-1, + objfromlh->Rot[1], + objfromlh->Rot[2], + objfromlh->Rot[3] }; + + fprintf( stderr, "====> %f %f %f ", + pos[0], pos[1], pos[2] ); + + quatrotatevector( pos, rot, pos ); + pos[0] -= lhp->Pos[0]; + pos[1] -= lhp->Pos[1]; + pos[2] -= lhp->Pos[2]; + + + quatrotatevector( lhp->Pos, objfromlh->Rot, lhp->Pos ); + + fprintf( stderr, "====> %f %f %f ", + pos[0], pos[1], pos[2] ); + + } + } + fprintf( stderr, "\n" ); + + + reset_calibration( cd ); +// cd->stage = 5; } else { diff --git a/src/survive_cal.h b/src/survive_cal.h index dd2a1e2..d21ba36 100644 --- a/src/survive_cal.h +++ b/src/survive_cal.h @@ -33,7 +33,7 @@ void survive_cal_light( SurviveObject * so, int sensor_id, int acode, int timein void survive_cal_angle( SurviveObject * so, int sensor_id, int acode, uint32_t timecode, FLT length, FLT angle ); #define MAX_SENSORS_TO_CAL 96 -#define DRPTS 1024 +#define DRPTS 128 #define MAX_CAL_PT_DAT (MAX_SENSORS_TO_CAL*NUM_LIGHTHOUSES*2) struct SurviveCalData { @@ -61,6 +61,8 @@ struct SurviveCalData SurviveObject * hmd; + PoserCB ConfigPoserFn; + //Stage: // 0: Idle // 1: Collecting OOTX data. -- cgit v1.2.3