aboutsummaryrefslogtreecommitdiff
path: root/src/survive_cal.c
blob: febd506b8896a3cc5e8d513bf83e07a01e03c217 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
// (C) 2016, 2017 Joshua Allen, MIT/x11 License.
// (C) 2016, 2017 <>< C. N. Lohr, Under MIT/x11 License.

// All OOTX code was written by J. Allen. Rest of the code is probably mostly CNLohr.
//
// This file is primarily geared to the calibration phase, to produce the world cal information.
// Once world cal is produced, it's unlikely you will need this file at all.  The plan is
// to not include it at all on any stripped-down versions of libsurvive.
//


#include "survive_cal.h"
#include "survive_internal.h"
#include "survive_reproject.h"

#include <assert.h>
#include <linmath.h>
#include <math.h>
#include <poser.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>

#include "survive_config.h"

#ifdef WINDOWS
int mkdir(const char *);
#endif


STATIC_CONFIG_ITEM( REQ_TRACK_FOR_CAL, "requiredtrackersforcal", 's', "Which devices will be used, i.e. HMD,WM0,WM1", "" );
STATIC_CONFIG_ITEM( ALLOW_TRACK_FOR_CAL, "allowalltrackersforcal", 'i', "Allow use of additional connected devices for calibration", 0 );
STATIC_CONFIG_ITEM( CONFIG_POSER, "configposer", 's', "Poser used for calibration step", "SBA" );
STATIC_CONFIG_ITEM(OOTX_IGNORE_SYNC_ERROR, "ootx-ignore-sync-error", 'i', "Ignore sync errors on ootx packets", 0);

#define PTS_BEFORE_COMMON 32
#define NEEDED_COMMON_POINTS 10
#define MIN_SENSORS_VISIBLE_PER_LH_FOR_CAL 4

#define NEEDED_TIMES_OF_COMMON 5
#define DRPTS_NEEDED_FOR_AVG ((int)(DRPTS*3/4))


	//at stage 1, is when it branches to stage two or stage 7
	//stage 2 checks for the presence of two watchmen and an HMD visible to both lighthouses.
	//Stage 3 collects a bunch of data for statistical averageing
	//stage 4 does the calculation for the poses (NOT DONE!)
	//Stage 5 = System Calibrate.d


static void handle_calibration( struct SurviveCalData *cd );
static void reset_calibration( struct SurviveCalData * cd );

void ootx_error_clbk_d(ootx_decoder_context *ct, const char *msg) {
	SurviveContext *ctx = (SurviveContext *)(ct->user);
	SurviveCalData *cd = ctx->calptr;
	int id = ct->user1;
	SV_INFO("(%s %d) %s", cd->poseobjects[0]->codename, id, msg);
}

void ootx_packet_clbk_d(ootx_decoder_context *ct, ootx_packet* packet)
{
	static uint8_t lighthouses_completed = 0;

	SurviveContext * ctx = (SurviveContext*)(ct->user);
	SurviveCalData * cd = ctx->calptr;
	int id = ct->user1;

	SV_INFO( "Got OOTX packet %d %p", id, cd );

	lighthouse_info_v6 v6;
	init_lighthouse_info_v6(&v6, packet->data);

	BaseStationData * b = &ctx->bsd[id];
	//print_lighthouse_info_v6(&v6);

	b->BaseStationID = v6.id;
	b->fcal.phase[0] = v6.fcal_0_phase;
	b->fcal.phase[1] = v6.fcal_1_phase;
	b->fcal.tilt[0] = (v6.fcal_0_tilt);
	b->fcal.tilt[1] = (v6.fcal_1_tilt); // XXX??? Is this right? See https://github.com/cnlohr/libsurvive/issues/18
	b->fcal.curve[0] = v6.fcal_0_curve;
	b->fcal.curve[1] = v6.fcal_1_curve;
	b->fcal.gibpha[0] = v6.fcal_0_gibphase;
	b->fcal.gibpha[1] = v6.fcal_1_gibphase;
	b->fcal.gibmag[0] = v6.fcal_0_gibmag;
	b->fcal.gibmag[1] = v6.fcal_1_gibmag;
	b->accel[0] = v6.accel_dir_x;
	b->accel[1] = v6.accel_dir_y;
	b->accel[2] = v6.accel_dir_z;
	b->mode = v6.mode_current;
	b->OOTXSet = 1;

	config_set_lighthouse(ctx->lh_config,b,id);
	lighthouses_completed++;

	if (lighthouses_completed >= ctx->activeLighthouses) {
		config_save(ctx, survive_configs(ctx, "configfile", SC_GET, "config.json"));
	}
}

int survive_cal_get_status( struct SurviveContext * ctx, char * description, int description_length )
{
	struct SurviveCalData * cd = ctx->calptr;

	switch( cd->stage )
	{
	case 0:
		return snprintf( description, description_length, "0 Not calibrating" );
	case 1:
		return snprintf( description, description_length, "1 Collecting OOTX Data (%d:%d)", cd->ootx_decoders[0].buf_offset, cd->ootx_decoders[1].buf_offset );
	case 2:
	case 3:
		if( cd->found_common )
		{
			return snprintf( description, description_length, "%d Collecting Sweep Data %d/%d", cd->stage, cd->peak_counts, DRPTS );
		}
		else
		{
			return snprintf( description, description_length, "%d Searching for common watchman cal %d/%d (%d/%d)", cd->stage, cd->peak_counts, PTS_BEFORE_COMMON, cd->times_found_common, NEEDED_TIMES_OF_COMMON );
		}

	case 5:
		return snprintf( description, description_length, "%d LH Find complete.", cd->stage );

	case 4:
	default:
		return snprintf( description, description_length, "%d Unkown calibration state", cd->stage );
	}
}

void survive_cal_install( struct SurviveContext * ctx )
{
	if (ctx->calptr)
		return;

	int i;
	struct SurviveCalData * cd = ctx->calptr = calloc( 1, sizeof( struct SurviveCalData ) );

	if( ctx->state != SURVIVE_RUNNING )
	{
		SV_ERROR( "Error: You cannot install a calibrator until the system is running." );
	}

	for( i = 0; i < NUM_LIGHTHOUSES; i++ )
	{
		ootx_init_decoder_context(&cd->ootx_decoders[i]);
		cd->ootx_decoders[i].ignore_sync_bit_error = survive_configi(ctx, "ootx-ignore-sync-error", SC_SETCONFIG, 0);
		cd->ootx_decoders[i].user = ctx;
		cd->ootx_decoders[i].user1 = i;
	}

	cd->stage = 1;
	cd->ctx = ctx;

	cd->numPoseObjects = 0;

	// setting the required trackers for calibration to be permissive to make it easier for a newbie to start-- 
	// basically, libsurvive will detect whatever they have plugged in and start using that.  
	const char *RequiredTrackersForCal = survive_configs(ctx, "requiredtrackersforcal", SC_SETCONFIG, "");

	// If there are no mandatory trackers for calibration; by default just accept whatever it is that the person has.
	const uint32_t AllowAllTrackersForCal =
		survive_configi(ctx, "allowalltrackersforcal", SC_SETCONFIG, 0) || (strlen(RequiredTrackersForCal) == 0);

	size_t requiredTrackersFound = 0;

	for (int j=0; j < ctx->objs_ct; j++)
	{
		// Add the tracker if we allow all trackers for calibration, or if it's in the list 
		// of required trackers.
		int isRequiredTracker = strstr(RequiredTrackersForCal, ctx->objs[j]->codename) != NULL;

		if (isRequiredTracker)
		{
			requiredTrackersFound++;
		}

		if (AllowAllTrackersForCal || isRequiredTracker)
		{
			if (MAX_DEVICES_TO_CAL > cd->numPoseObjects)
			{
				cd->poseobjects[cd->numPoseObjects] = ctx->objs[j];
				SV_INFO("Calibration is using %s", cd->poseobjects[cd->numPoseObjects]->codename);
				cd->numPoseObjects++;
			}
			else
			{
				SV_INFO("Calibration is NOT using %s; device count exceeds MAX_DEVICES_TO_CAL", ctx->objs[j]->codename);
			}
		}	

	}

	// If we want to mandate that certain devices have been found

	if (strlen(RequiredTrackersForCal) > 0)
	{
		if (requiredTrackersFound != ((strlen(RequiredTrackersForCal) + 1) / 4))
		{
			SV_ERROR( "Error: Did not find all devices required for calibration." );
			free( cd );
			return;
		}
	}

	cd->ConfigPoserFn = GetDriverByConfig(ctx, "Poser", "configposer", "SBA", 0);

	ootx_packet_clbk = ootx_packet_clbk_d;
	ootx_error_clbk = ootx_error_clbk_d;
	ctx->calptr = cd;
}


void survive_cal_light( struct SurviveObject * so, int sensor_id, int acode, int timeinsweep, uint32_t timecode, uint32_t length, uint32_t lh)
{
	struct SurviveContext * ctx = so->ctx;
	struct SurviveCalData * cd = ctx->calptr;

	if( !cd ) return;

	switch( cd->stage )
	{
	default:
	case 2: //Taking in angle data. We don't care about light data anymore.
	case 0: //Default, inactive.
		break;

	case 1:
		//Collecting OOTX data.
		if( sensor_id < 0 )
		{
				//fprintf(stderr, "%s\n", so->codename);
			int lhid = -sensor_id-1;
			// Take the OOTX data from the first device.  (if using HMD, WM0, WM1 only, this will be HMD)

			if( lhid < NUM_LIGHTHOUSES && so == cd->poseobjects[0]  ) 
			{
				uint8_t dbit = (acode & 2)>>1;
				ootx_pump_bit( &cd->ootx_decoders[lhid], dbit );
			}
			int i;
			for( i = 0; i < ctx->activeLighthouses; i++ )
				if( ctx->bsd[i].OOTXSet == 0 ) break;
			if( i == ctx->activeLighthouses ) cd->stage = 2;  //TODO: Make this configuratble to allow single lighthouse.
		}
		break;
	case 3: //Look for light sync lengths.
	{
		if( acode >= -2 ) break;
		else if( acode < -4 ) break;
		int lh = (-acode) - 3;

		for (int i=0; i < cd->numPoseObjects; i++)
		{
			if( strcmp( so->codename, cd->poseobjects[i]->codename ) == 0 )
			{
				sensor_id += i*32;
			}
		}

		cd->all_sync_times[sensor_id][lh][cd->all_sync_counts[sensor_id][lh]++] = length;
		break;
	}

	}
	

}

void survive_cal_angle( struct SurviveObject * so, int sensor_id, int acode, uint32_t timecode, FLT length, FLT angle, uint32_t lh )
{
	struct SurviveContext * ctx = so->ctx;
	struct SurviveCalData * cd = ctx->calptr;

	if( !cd ) return;

	int sensid = sensor_id;

	for (int i=0; i < cd->numPoseObjects; i++)
	{
		if( strcmp( so->codename, cd->poseobjects[i]->codename ) == 0 )
		{
			sensid += i*32;
		}
	}

	if( sensid >= MAX_SENSORS_TO_CAL || sensid < 0 ) return;

	int lighthouse = lh;
	int axis = acode & 1;

	switch( cd->stage )
	{
	default:
	case 1:	//Collecting OOTX data. (Don't do anything here, yet.)
	case 0: //Default, inactive.
		break;
	case 2:
	{
		int ct = cd->all_counts[sensid][lighthouse][axis]++;
		cd->all_lengths[sensid][lighthouse][axis][ct] = length;
		cd->all_angles[sensid][lighthouse][axis][ct] = angle;

		int dev = sensid / 32;

		if( ct > cd->peak_counts )
		{
			cd->peak_counts = ct;
		}


		//Determine if there is a sensor on a watchman visible from both lighthouses.
/*		if( sensid >= 32 )
		{
			int k;
			int ok = 1;
			for( k = 0; k < NUM_LIGHTHOUSES; k++ )
			{
				if( cd->all_counts[sensid][k][0] < NEEDED_COMMON_POINTS || cd->all_counts[sensid][k][1] < NEEDED_COMMON_POINTS )
				{
					ok = 0;
					break;
				}
			}
			if( ok ) cd->found_common = 1;
		}*/

		if( cd->peak_counts >= PTS_BEFORE_COMMON )
		{
			int min_peaks = PTS_BEFORE_COMMON;
			int i, j, k;
			cd->found_common = 1;
			for( i = 0; i < cd->numPoseObjects; i++ )
			{
				// for( i = 0; i < MAX_SENSORS_TO_CAL/SENSORS_PER_OBJECT; i++ )
				for (j = 0; j < ctx->activeLighthouses; j++) {
					int sensors_visible = 0;
					for (k = 0; k < SENSORS_PER_OBJECT; k++) {
						if (cd->all_counts[k + i * SENSORS_PER_OBJECT][j][0] > NEEDED_COMMON_POINTS &&
							cd->all_counts[k + i * SENSORS_PER_OBJECT][j][1] > NEEDED_COMMON_POINTS)
							sensors_visible++;
					}
					if (sensors_visible < MIN_SENSORS_VISIBLE_PER_LH_FOR_CAL) {
						// printf( "Dev %d, LH %d not enough visible points found.\n", i, j );
						reset_calibration(cd);
						cd->found_common = 0;
						return;
					}
				}
			}
			
			int tfc = cd->times_found_common;
			if( cd->found_common )
			{
				if( tfc >= NEEDED_TIMES_OF_COMMON )
				{
					SV_INFO( "Stage 2 moving to stage 3. %d %d %d", cd->peak_counts, cd->found_common, tfc );
					reset_calibration( cd );
					cd->stage = 3;
					cd->found_common = 1;
				}
				else
				{
					SV_INFO( "Stage 2 good - continuing. %d %d %d", cd->peak_counts, cd->found_common, tfc );
					reset_calibration( cd );
					cd->times_found_common = tfc+1;
				}
			}
			else
			{
				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;
	}
	case 3:
	{
		int ct = cd->all_counts[sensid][lighthouse][axis]++;
		cd->all_lengths[sensid][lighthouse][axis][ct] = length;
		cd->all_angles[sensid][lighthouse][axis][ct] = angle;
		if( ct > cd->peak_counts )
		{
			cd->peak_counts = ct;
			if( ct >= DRPTS )
				handle_calibration( cd ); //This must also reset all cals.
		}
		break;
	}
	}
}

static void reset_calibration( struct SurviveCalData * cd )
{
	memset( cd->all_counts, 0, sizeof( cd->all_counts ) );
	cd->peak_counts = 0;
	cd->found_common = 0;
	cd->times_found_common = 0;
	cd->stage = 2;

	memset( cd->all_sync_counts, 0, sizeof( cd->all_sync_counts ) );
}

static void handle_calibration( struct SurviveCalData *cd )
{
	struct SurviveContext * ctx = cd->ctx;

	#define MAX_CAL_PT_DAT (MAX_SENSORS_TO_CAL*NUM_LIGHTHOUSES*2)

/*
	FLT avgsweeps[MAX_CAL_PT_DAT];
	FLT avglens[MAX_CAL_PT_DAT];
	FLT stdsweeps[MAX_CAL_PT_DAT];
	FLT stdlens[MAX_CAL_PT_DAT];
	int ctsweeps[MAX_CAL_PT_DAT];
*/

	memset( cd->ctsweeps, 0, sizeof( cd->ctsweeps ) );

	//Either advance to stage 4 or go resetting will go back to stage 2.
	//What is stage 4?  Are we done then?
#ifdef WINDOWS
	mkdir( "calinfo" );
#else
	mkdir( "calinfo", 0755 );
#endif
	int sen, axis, lh;

	//Just to get it out of the way early, we'll calculate the sync-pulse-lengths here.
	FILE * sync_time_info = fopen( "calinfo/synctime.csv", "w" );

	for( sen = 0; sen < MAX_SENSORS_TO_CAL; sen++ )
	for( lh = 0; lh < NUM_LIGHTHOUSES; lh++ )
	{
		int count = cd->all_sync_counts[sen][lh];
		int i;
		double totaltime;

		totaltime = 0;

		if( count < 20 ) continue;
		for( i = 0; i < count; i++ )
		{
			totaltime += cd->all_sync_times[sen][lh][i];
		}
		FLT avg = totaltime/count;

		double stddev = 0.0;
		for( i = 0; i < count; i++ )
		{
			stddev += (cd->all_sync_times[sen][lh][i] - avg)*(cd->all_sync_times[sen][lh][i] - avg);
		}
		stddev /= count;

		fprintf( sync_time_info, "%d %d %f %d %f\n", sen, lh, totaltime/count, count, stddev );
	}

	fclose( sync_time_info );




	FILE * hists = fopen( "calinfo/histograms.csv", "w" );
	FILE * ptinfo = fopen( "calinfo/ptinfo.csv", "w" );
	for( sen = 0; sen < MAX_SENSORS_TO_CAL; sen++ )
	for( lh = 0; lh < NUM_LIGHTHOUSES; lh++ )
	for( axis = 0; axis < 2; axis++ )
	{
		int dpmax = cd->all_counts[sen][lh][axis];
		if( dpmax < MIN_PTS_BEFORE_CAL ) continue;
		int i;

		FLT sumsweepangle = 0;
		FLT sumlentime = 0;

		//Find initial guess at average
		for( i = 0; i < dpmax; i++ )
		{
			FLT sweepangle = cd->all_angles[sen][lh][axis][i];
			FLT datalen = cd->all_lengths[sen][lh][axis][i];
			sumsweepangle += sweepangle;
			sumlentime += datalen;
		}

		#define OUTLIER_ANGLE   0.001	//TODO: Tune
		#define OUTLIER_LENGTH	0.001	//TODO: Tune
#define ANGLE_STDEV_TOO_HIGH 0.001		// TODO: Tune

		FLT avgsweep = sumsweepangle / dpmax;
		FLT avglen = sumlentime / dpmax;
		int count = 0;

		FLT max_outlier_angle = 0;
		FLT max_outlier_length = 0;

		//Get rid of outliers
		for( i = 0; i < dpmax; i++ )
		{
			FLT sweepangle = cd->all_angles[sen][lh][axis][i];
			FLT datalen = cd->all_lengths[sen][lh][axis][i];
			FLT Sdiff = sweepangle - avgsweep;
			FLT Ldiff = datalen - avglen;
			FLT Sdiff2 = Sdiff * Sdiff;
			FLT Ldiff2 = Ldiff * Ldiff;


			if( Sdiff2 > OUTLIER_ANGLE || Ldiff2 > OUTLIER_LENGTH )
			{
				cd->all_lengths[sen][lh][axis][i] = -1;
			}
			else
			{
				if( Sdiff2 > max_outlier_angle ) max_outlier_angle = Sdiff2;
				if( Ldiff2 > max_outlier_length ) max_outlier_length = Ldiff2;
				count++;
			}
		}

		if( count < DRPTS_NEEDED_FOR_AVG )
		{
			printf( "DPAVG %d\n", count );
			//Not enough for this point to be considered.
			continue;
		}

		sumsweepangle = 0;
		sumlentime = 0;
		//Redo, finding new average:
		for( i = 0; i < dpmax; i++ )
		{
			FLT sweepangle = cd->all_angles[sen][lh][axis][i];
			FLT datalen = cd->all_lengths[sen][lh][axis][i];
			if( datalen < 0 ) continue;
			sumsweepangle += sweepangle;
			sumlentime += datalen;
		}

		avgsweep = sumsweepangle / count;
		avglen = sumlentime / count;

		FLT stddevang = 0;
		FLT stddevlen = 0;

		#define HISTOGRAMSIZE   31
		#define HISTOGRAMBINANG ((3.14159)/400000.0)  //TODO: Tune

		int histo[HISTOGRAMSIZE];
		memset( histo, 0, sizeof( histo ) );

		for( i = 0; i < dpmax; i++ )
		{
			FLT sweepangle = cd->all_angles[sen][lh][axis][i];
			FLT datalen = cd->all_lengths[sen][lh][axis][i];
			if( datalen < 0 ) continue;

			FLT Sdiff = sweepangle - avgsweep;
			FLT Ldiff = datalen - avglen;
			FLT Sdiff2 = Sdiff * Sdiff;
			FLT Ldiff2 = Ldiff * Ldiff;

			stddevang += Sdiff2;
			stddevlen += Ldiff2;

			int llm = (int)( Sdiff / HISTOGRAMBINANG + (HISTOGRAMSIZE/2.0) );
			if( llm < 0 ) llm = 0;
			if( llm >= HISTOGRAMSIZE ) llm = HISTOGRAMSIZE-1;

			histo[llm]++;
		}

		stddevang /= count;
		stddevlen /= count;

		if( stddevang > ANGLE_STDEV_TOO_HIGH )
		{
			SV_INFO( "DROPPED: %02d:%d:%d dropped because stddev (%f) was too high.", sen, lh, axis, stddevang );
			continue;
		}

		fprintf( hists, "%02d_%d_%d, ", sen, lh, axis );

		for( i = 0; i < HISTOGRAMSIZE; i++ )
		{
			fprintf( hists, "%d ", histo[i] );
		}
		fprintf( hists, "\n" );

		fprintf( ptinfo, "%d %d %d %d %f %f %f %f %f %f\n", sen, lh, axis, count, avgsweep, avglen*1000000, stddevang*1000000000, stddevlen*1000000000, max_outlier_length*1000000000, max_outlier_angle*1000000000 );

		int dataindex = sen*(2*NUM_LIGHTHOUSES)+lh*2+axis;
		cd->avgsweeps[dataindex] = avgsweep;
		cd->avglens[dataindex] = avglen;
		cd->stdsweeps[dataindex] = stddevang;
		cd->stdlens[dataindex] = stddevlen;
		cd->ctsweeps[dataindex] = count;
	}
	fclose( hists );
	fclose( ptinfo );

	int obj;

	//Poses of lighthouses relative to objects.
	SurvivePose  objphl[MAX_POSE_OBJECTS][NUM_LIGHTHOUSES];

	FILE * fobjp = fopen( "calinfo/objposes.csv", "w" );

	for( obj = 0; obj < cd->numPoseObjects; obj++ )
	{
		int i, j;
		PoserDataFullScene fsd = {0};
		fsd.hdr.pt = POSERDATA_FULL_SCENE;
		for( j = 0; j < NUM_LIGHTHOUSES; j++ )
		for( i = 0; i < SENSORS_PER_OBJECT; i++ )
		{
			int gotdata = 0;

			int dataindex = (i+obj*32)*(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];
		}

		int r = cd->ConfigPoserFn( cd->poseobjects[obj], (PoserData*)&fsd );
		if( r )
		{
			SV_INFO( "Failed calibration on dev %d\n", obj );
			reset_calibration( cd );
			cd->stage = 2;
			fclose( fobjp );
			return;
		}

		int compute_reprojection_error = config_read_uint32(
			ctx->global_config_values, "ComputeReprojectError", 0);

		int lh;
		for( lh = 0; lh < NUM_LIGHTHOUSES; lh++ )
		{
			SurvivePose * objfromlh = &cd->poseobjects[obj]->FromLHPose[lh];  //The pose is here
			SurvivePose * lhp = &ctx->bsd[lh].Pose; //Need to somehow put pose here.

			memcpy( &objphl[obj][lh], objfromlh, sizeof( SurvivePose ) );

			fprintf( fobjp, "%f %f %f\n", objfromlh->Pos[0], objfromlh->Pos[1], objfromlh->Pos[2] );
			fprintf( fobjp, "%f %f %f %f\n", objfromlh->Rot[0], objfromlh->Rot[1], objfromlh->Rot[2], objfromlh->Rot[3] );

			if (ctx->bsd[lh].PositionSet) {
				if (compute_reprojection_error) {
					FLT reproj_err = 0;
					size_t cnt = 0;
					SurviveObject *so = cd->poseobjects[obj];
					for (size_t idx = 0; idx < so->sensor_ct; idx++) {
						FLT *lengths = fsd.lengths[idx][lh];
						FLT *pt = fsd.angles[idx][lh];
						if (lengths[0] < 0 || lengths[1] < 0)
							continue;

						cnt++;
						FLT reproj_pt[2];
						survive_reproject(ctx, lh, so->sensor_locations,
										  reproj_pt);

						FLT err = 0;
						for (int dim = 0; dim < 2; dim++) {
							err += (reproj_pt[dim] - pt[dim]) *
								   (reproj_pt[dim] - pt[dim]);
						}
						reproj_err += sqrt(err);
					}

					// This represents the average distance we were off in our
					// reprojection.
					// Different libraries have slightly different variations on
					// this theme,
					// but this one has an intuitive meaning
					reproj_err = (reproj_err / cnt);

					SV_INFO("Reproject error was %.13g for lighthouse %d",
							reproj_err, lh);
				}
			}
		}

	}
	fclose( fobjp );



	SV_INFO( "Stage 4 succeeded." );
	reset_calibration( cd );
	cd->stage = 5;
}