blob: 20609564ce52332f8a9925797d94e20caededf70 [file] [log] [blame]
Brian Silverman68a5a012013-03-29 23:37:04 -07001#include "vision/SensorProcessor.h"
Brian Silverman6ae77dd2013-03-29 22:28:08 -07002
Brian Silverman6ae77dd2013-03-29 22:28:08 -07003#include <stdio.h>
4
Brian Silverman68a5a012013-03-29 23:37:04 -07005namespace frc971 {
6
Brian Silverman6ae77dd2013-03-29 22:28:08 -07007// give a set of x -> fx pairs find a range for our value
8// then interpolate between and return an interpolated fx.
9// If the value is off the end just extend the line to
10// meet our point. If something does go wrong (and it
11// never should) it will return -1.
12double interpolate(int num_interp_vals,
13 const Interpolation *interp, double value) {
14 double dy;
15 double dx;
16 double a;
17 double intercept;
18 //printf("for val %.1f\n", value);
19 if (value < interp[0].x) {
20 // if closer than nearest
21 dy = interp[1].fx - interp[0].fx;
22 dx = interp[1].x - interp[0].x;
23 a = value - interp[0].x;
24 intercept = interp[0].fx;
25 //printf("LESS THAN\n");
26 } else if (value > interp[num_interp_vals-1].x){
27 // if further than furthest
28 dy = interp[num_interp_vals-1].fx - interp[num_interp_vals-2].fx;
29 dx = interp[num_interp_vals-1].x - interp[num_interp_vals-2].x;
30 a = value - interp[num_interp_vals-2].x;
31 intercept = interp[num_interp_vals-2].fx;
32 //printf("GT THAN\n");
33 } else {
34 //printf("gh0\n");
35 // scan for range
36 for(int i=0; i<num_interp_vals-1; i++){
37 if(value >= interp[i].x && value <= interp[i+1].x){
38 // printf("(%.1f,%.1f)=(%.1f,%.1f)\n",
39 // interp[i].x, interp[i+1].x,
40 // interp[i].fx, interp[i+1].fx);
41 double lambda =
42 (value - interp[i].x)/(interp[i+1].x - interp[i].x);
43 return (1-lambda)*interp[i].fx + lambda*interp[i+1].fx;
44 }
45 }
46 // this should maybe be an assert
47 return -1;
48 }
49
50 return ( (dy/dx)*a + intercept );
51}
52
Brian Silverman68a5a012013-03-29 23:37:04 -070053} // namespace frc971