Log in

View Full Version : get distance from cmucam


ModMan24
21-01-2006, 17:03
Does anyone know how to retrive the distance from what ever your tracking from the CMUcam?

I need a way to determine distance to target.

if someone knows how to get the distance to target from the CMUcam please post a copy of the code.

Thanks
Ben
übergeeks Programmer

Andrew Blair
21-01-2006, 17:53
Haven't written the code yet, but just so you know, you can't get the distance to target *directly* from the camera (without some elegant code that I don't know about). However, it is still rather easy.

Using Kevin Watson's Default camera code, there should be a variable called something like tilt_value. Using this value, after converting the PWM value into a useable angle, you can employ a bit of triganometry to derive the distance to target.

(Vert_goal_hight/(tan(angle_to_goal)))= distnce to goal

I would use a lookup table, it'll be much faster than constantly calculating the distance.

ModMan24
21-01-2006, 18:40
I need one that gives me the distance from the camera to the target.. not to the base of the target..

does this do it? or does it give the distance from the base of the robot to the base of the target?

Andrew Blair
21-01-2006, 19:00
From the base of the robot to the base of the target. However, you can easily modify it so as to give you the distance to the target, directly.

To derive the hypotenuse of our virtual triangle:

(height_of_camera_to_goal)/(sin(angle_to_goal))= distance, directly to goal.

For anyone who hasn't had trigonometry, I would highly recommend it; it's a very applicable math course! For those who have it coming up, but haven't had it yet, have someone on the team teach you the basics! It really is the most useful math you'll ever learn!

ModMan24
21-01-2006, 19:20
Thanks... do you know how to get the angle using easyc? its what my team has decided to use to code it this year instead of mplab.


Thanks a bunch

Andrew Blair
21-01-2006, 19:22
No idea. There should be a way that you can code functions in it; you'll need to code a function to do what you want. I haven't used EasyC yet myself.

Chris_Elston
21-01-2006, 20:06
I might be way off base here...

but what we did was make a global varible called cam_Size. Then in the "Capture Tracking Data", there is a pull down box called Region Size. We selected cam_Size in that box. cam_Size needs to be declared as an unsigned char in your globals.

Region size is the size of your "blob" from the CMU cam. The closer you are to your color tracking parmeters, the larger the "blob" will appear on the screen. The futher away you are, the smaller the region will be on the screen.

This relationship is almost linear.

Let's assume that you get a region size of 255. That would mean your camera lens is right ontop of your light or let's just say 1 foot away.

Then let's assume you get a region size of 1, and you note that your camera is 30 feet away.

Everything inbetween can be calculated on a linear scale with some kind of a conversion factor.

(note, I didn't crunch the numbers, I just assumed for this topic of discussion)

1 foot = 255
1.1 feet = 254 etc...

29.9 feet = 2
30 feet = 1

Andrew Blair
21-01-2006, 20:11
Yeah, that was what I was reffering to with the elegant bit of code I didn't know about! I had been trying to find out a good way to look at the size of the blob to determine distance, but it looks like you've done it! Good job!

ModMan24
21-01-2006, 21:09
does anyone actually know the numbers and equations for this? my team is still building stuff so as yet I have nothing to test this on.

im sure someone else is trying to do this. if anyone out there is willing to help it would be greatly apprciated.

Dillon Compton
22-01-2006, 18:51
does anyone actually know the numbers and equations for this? my team is still building stuff so as yet I have nothing to test this on.

im sure someone else is trying to do this. if anyone out there is willing to help it would be greatly apprciated.


Hi,
I'll be using last years Robot and some printF statements to do this sometime in the next week. I can give you the values I get, but no garauntee that your camera will respond the same as mine; one can find minor changes in mass produced products, and people have indeed found such variances in the CMU CAM responses.

Good luck!

ModMan24
22-01-2006, 18:55
Thanks.. it will at least be a decent starting point.

aaronm_k
05-02-2006, 07:39
I might be way off base here...

but what we did was make a global varible called cam_Size. Then in the "Capture Tracking Data", there is a pull down box called Region Size. We selected cam_Size in that box. cam_Size needs to be declared as an unsigned char in your globals.

Region size is the size of your "blob" from the CMU cam. The closer you are to your color tracking parmeters, the larger the "blob" will appear on the screen. The futher away you are, the smaller the region will be on the screen.

This relationship is almost linear.

Let's assume that you get a region size of 255. That would mean your camera lens is right ontop of your light or let's just say 1 foot away.

Then let's assume you get a region size of 1, and you note that your camera is 30 feet away.

Everything inbetween can be calculated on a linear scale with some kind of a conversion factor.

(note, I didn't crunch the numbers, I just assumed for this topic of discussion)

1 foot = 255
1.1 feet = 254 etc...

29.9 feet = 2
30 feet = 1

Actually, the relationship is hyperbolic (inversely proportional), NOT linear. You can set up a proportion as follows:
(target distance)/(image distance (i.e. camera focal length)) = (target height (illuminated part))/(blob height)
This is the standard formula for a single lens (which is what the CMU Cam has). Of course, experimental factors (such as amount of the target that's illuminated) make using the above formula unrealistic. Therefore, it's more accurate to calculate a best-fit line. Solving for the target distance, you get the simplified formula:
(target distance) = (some constant)(1/(blob height))
The blob size is an area, so it's proportional to the blob height squared:
(blob size) = (some constant)(blob height)^2
or
(blob height) = (some constant)squareRoot(blob size)
Therefore, the formula in terms of blob size is:
(target distance) = (some constant)(1/squareRoot(blob size))
| | |
y = m x
The graph of target distance vs. blob size is a hyperbola, so in order to get a straight line, you have to compare target distance to 1/squareRoot(blob size).

Using this information, my team's programming group took some data using the blob sizes printed to the terminal screen (with Kevin Watson's code) when the camera is locked on to the target. We took data every 4in from 6ft (simulating when the robot is right below the target) to 25ft (simulating when the robot is at half court), and analyzed the data. Fortunately for us (and any team wanting distance data from the camera), the graph of target distance vs. 1/squareRoot(blob size) is a very straight line.

I analyzed the data using some fancy PHP code, and put the results, along with the graph and formula, in an Excel spreadsheet (http://www.dpengineering.org/robotics/files/student/programming/06203CMU`CamBlobSizeVs.DistanceData/06204CMUCamBlobSizeVs.DistanceDataAnalysisW'Graph&Formula.xls) on Team 1717's website. (Links to the raw data can be found under "Programming Team" > "FRC robot code" on our downloads (http://www.dpengineering.org/robotics/files/student/) page.)

The final formula is then:
(target distance) = 798.48871156253/squareRoot(blob size)
The formula can be easily converted to code:
#define TARGET_DISTANCE_FORMULA_CONSTANT 798.48871156253
targetDistance = TARGET_DISTANCE_FORMULA_CONSTANT / sqrt((double)T_Packet_Data.pixels);
So, one formula down, several more to go for an accurate ball launcher! :)

ModMan24
05-02-2006, 13:57
the whole trig thing works pretty well.

sampath
18-09-2010, 08:34
Hi,

It's not clear for me whether how are you getting the blob size from the following parameter

"T_Packet_Data.pixels"

Can anybody please explain it to me? I'm new to CMUcam2 sensor.

Thanks!

Sampath

steve d
29-10-2010, 17:51
"T_Packet_Data.pixels" = number of pixels in tracked area. Attached is the CMUcam manual if you need it.