### c#

#### Calculating Length Based on Sensor Data

I've got an IR sensor which writes its current information to a token which I then interpret in a C# application. That's all good -- no problems there, heres my code: SetLabelText(tokens [1],label_sensorValue); sensorreading = Int32.Parse(tokens[0]); sensordistance = (mathfunctionhere); Great. So the further away the IR sensor is from an object, the lower the sensor reading (as less light is reflected back and received by the sensor). My problem is in interpreting that length. I can go ahead and get lets say "110" as a value when an object is 5 inches away, and then "70" as a value when an object is 6 inches away. Now I want to be able to calculate the distance of an object using these constants for any length. Any ideas?

Well, the first thing that I would do is take data at fixed distances, i.e., 1 inch, 1 foot, 2 feet, 5 feet, etc. I would then plot that data in a program like Excel and find a best fit curve from which you can derive a function. Use that function in your code and start testing at varying distances. Now, it may not be that simple. The reflective properties of the object you are trying to measure will change your readings, as could other factors. Since I don't know what your requirements are it is hard for me to give more specific advice.

It more of a physics question than a math question! Ed Swangren suggestion to create tables with the recording from various experiments is the way to go, unless you can also rely on information from the manufacturer of the sensor. Aside from the intrinsic precision and fidelity of the device, so many factors may interfere with the effective amount of energy (or whatever is returned and measured); reading up on this particular device or even on similar items and/or the fundamental of the physical effects/dimensions in play may provide you additional idea for the calibration. Once you have a table associating measurements with distance (and maybe additional criteria temperature, reflective factor...), it becomes a true math issue, for example to: confirm the statistical relevance of the precision targeted extrapolate real reading with the discrete table (seems to be the original question by the OP) find the regression function, and do away with the table approach (I doubt this would be easy, as hinted in comments and such this will be far from linear...

Reflected IR is often used for object detection. But, if the shape, angle, reflective properties of your target(s) aren't always the same, then measuring the intensity of reflected IR is not a reliable method for estimating distance. Any ideas? Different sensing methodology. How about, parallax sensor such as this one: http://www.acroname.com/robotics/info/articles/sharp/sharp.html .

elaborating a bit further on the square function proposed by tom10 ... we asume your devices function is a square curve , i.e. distance = A + B * reading + C * reading^2 Now we need to find out A, B and C to convert your reading into a distance, so what we need is a kind of regression analysis. A square curve is defined by exactly 3 points, so you measure at 3 points (r1..r3) and note distances (d1..d3) Now you have 3 equations with three unknowns which you can solve by any means, i.e. A + r1 * B + r1^2 * C = d1 A + r2 * B + r2^2 * C = d2 A + r3 * B + r3^2 * C = d3 You solve once for A, B, and C which will be your "calibration curve" and will be able to calculate any unknown distance using the first formula above. Of course if you change hardware you will need to recalibrate your gear within the limits of any device variations. You can extend this mechanism to a cubical and even higher order curve by making a fourth measurement and expanding all above equations by a variable D, i.e. A + rx*B + rx^2*C + rx^3*D + .... etc. but it will not add much of accuracy. You will find that factor D for a rx^3 and onwards will be very small. Hope this helps Good luck MikeD

The energy from the light source will fall off as 1/r2 (for a source that's relatively small). Beyond this, if everything else is held constant, the only problem could be non-linearity in the sensor. To check this with your data, you would expect E x r2=const, and this roughly holds for your data: 110 x 52 = 2750, and 70 x 62 = 2520, so these are within 10% which seems fairly close, so it looks like the basic rule will hold. Non-linear sensors are common, so you should be sure to check this over the full range that you'll be using it. But if it's a linear sensor, the other issues that people are mentioning (e.g. reflective surfaces) won't be a problem because, for light transmission and reflection, everything (almost) is linear and will therefore be intrinsically compensated for by a single calibration constant. The angle of the light source, absorbing materials, etc, all won't matter as long as they don't change. If you test a few points, including the extremes of the range you're interested in, and it follows the 1/r2 rule, you're good to go. Then, of course, calculate what the const is, and r = sqrt(const/E).

### Related Links

C# Code doesn’t compile verifying files before process start

VS-2012 : Breakpoint will not currently be hit. No symbols have been loaded for this document

C# read XML File and select Nodes

Serial port and multithreading issue

Where to place application settings for a WPF application

Prism DelgateComand throws an exception

XML class Error : Object reference not set to an instance of an object [duplicate]

RegEx - Version in filename

C# dictionary key split to JSON key value?

Injecting C++ DLLs into notepad on windows 10 using C#

DLL Could not be found in the project folder

C# Append text with Encoding.UTF8

abstract class with more then one parameterized constructor and derived class with parameterized constructor

image are not view in full screen in convert pdf time

New controller validation error due to no PK on inherited classes ASP.NET MVC VS2015

C# Console Application: Catching Some Cases of White Spaces