Prototyp V2
Dependencies: PM2_Libary
mapping.cpp@49:c2ebc11520f9, 2022-04-20 (annotated)
- Committer:
- lupomic
- Date:
- Wed Apr 20 08:33:31 2022 +0200
- Revision:
- 49:c2ebc11520f9
- Child:
- 54:86a90ce4e412
mapping.cpp
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lupomic | 49:c2ebc11520f9 | 1 | //pow function is here so we dont have to use the math.h library |
lupomic | 49:c2ebc11520f9 | 2 | //it takes 2 arguments the base can be any negative or positive floating point number the power has to be a hos to be an "integer" defined as a double |
lupomic | 49:c2ebc11520f9 | 3 | double powerx(double base, double pow2){ |
lupomic | 49:c2ebc11520f9 | 4 | double result = -1; |
lupomic | 49:c2ebc11520f9 | 5 | double power = pow2; |
lupomic | 49:c2ebc11520f9 | 6 | double basis = base; |
lupomic | 49:c2ebc11520f9 | 7 | result = 1; |
lupomic | 49:c2ebc11520f9 | 8 | //handling negative exponents |
lupomic | 49:c2ebc11520f9 | 9 | if(power<0){ |
lupomic | 49:c2ebc11520f9 | 10 | for(double i=1; i<=(power*(-1.0)); i++) { |
lupomic | 49:c2ebc11520f9 | 11 | result *= basis; |
lupomic | 49:c2ebc11520f9 | 12 | } |
lupomic | 49:c2ebc11520f9 | 13 | result = 1.0/result; |
lupomic | 49:c2ebc11520f9 | 14 | } |
lupomic | 49:c2ebc11520f9 | 15 | //handling positive exponents |
lupomic | 49:c2ebc11520f9 | 16 | else{ |
lupomic | 49:c2ebc11520f9 | 17 | for(double i=1; i<=power; i++){ |
lupomic | 49:c2ebc11520f9 | 18 | result *= basis;}} |
lupomic | 49:c2ebc11520f9 | 19 | |
lupomic | 49:c2ebc11520f9 | 20 | return result; |
lupomic | 49:c2ebc11520f9 | 21 | } |
lupomic | 49:c2ebc11520f9 | 22 | |
lupomic | 49:c2ebc11520f9 | 23 | double mapping(float adc_value_mV){ |
lupomic | 49:c2ebc11520f9 | 24 | double distance = 0.0f; //distance in mm |
lupomic | 49:c2ebc11520f9 | 25 | double infY =360 , supY = 2360; //Window for sensor values |
lupomic | 49:c2ebc11520f9 | 26 | double voltage_mV = adc_value_mV; |
lupomic | 49:c2ebc11520f9 | 27 | double p1 = -1.127*powerx(10,-14), p2 = 8.881*powerx(10,-11), p3 = -2.76*powerx(10,-7), p4 = 0.0004262, p5 = -0.3363, p6 = 120.1 ; //faktoren für polynomkurve -> von matlab exportiert |
lupomic | 49:c2ebc11520f9 | 28 | if(voltage_mV > infY && voltage_mV < supY){ |
lupomic | 49:c2ebc11520f9 | 29 | distance = p1*powerx(voltage_mV,5) + p2*powerx(voltage_mV,4) + p3*powerx(voltage_mV,3) + p4*powerx(voltage_mV,2) + p5*voltage_mV + p6; |
lupomic | 49:c2ebc11520f9 | 30 | } |
lupomic | 49:c2ebc11520f9 | 31 | return (distance); |
lupomic | 49:c2ebc11520f9 | 32 | } |