Color Sensors

Editor modeling : physics geoms, geoms shapes, render geoms
Post Reply
Martin
Posts: 25
Joined: Tue Mar 19, 2013 11:41 pm

Color Sensors

Post by Martin » Wed Mar 27, 2013 7:52 am

First of all , thanks for create Marilou is amazing program and i love it!

This is my first post, i hope my english will be good enough to be understood.

I'm trying to simulate SumoBots, which must fight each other without go out of the combat area. The limit of this area is a white line. I have done the Phx and Search/Attack software but i could achive this matter (keep inside the area).

I use a photo transistor and photo resistor in the real prototipes to sense the ambient light intensity and identify differences in colour but i read that they don't exist in Marilou. I tried to use a camera to take a picture and compare the pixels but i couldn't reach it.

Could someone help me with the software? I have been use the example called Camera... but is too complex for mi knowlege in c++.

Thank you.

Martin

LR
Posts: 305
Joined: Mon Feb 12, 2007 11:55 pm
Location: FR

Re: Color Sensors

Post by LR » Thu Mar 28, 2013 2:23 pm

Hi martin,

We did not yet code the color sensor, sorry for that.

- Place a camera on your robot (with a very low resolution : 16*16 or 32*32)
http://doc.anykode.com/cameras.html

- from your MODA based application, use the GetColor function from the received image to get the average color of the image
http://doc.anykode.com/ModaCPP__Image__ ... const.html

Cheers
LR

Martin
Posts: 25
Joined: Tue Mar 19, 2013 11:41 pm

Re: Color Sensors

Post by Martin » Sat Mar 30, 2013 4:32 pm

Hi LG! Thank you for reply!

I have been trying the code of the camera and i have problems with GetColor(). What kind of data it returns? I have read that is an int ( 0 for black and 15 for white) but when i tried to compare them with numbers, the compiler of Visual Studio 2010 say "error C2678: binary '==' : no operator found which takes a left-hand operand of type 'const std::string' (or there is no acceptable conversion)".

This is a simplified code of what i want to do:

void Step(void)
{
#define RotationSpeed 500
#define Attackspeed 1000
#define MIN 400

float mdt,mit,mdd,mid;
bool ld,rd;
int PerformWait=0;
ld=_pSensorI->GetMeasure();
rd=_pSensorD->GetMeasure();

ModaCPP::DeviceCamera *pCamera =_pPHX->QueryDeviceCamera("viewpoint0/cam0");

if(pCamera)
{
_cprintf("camera found\r\n");
ModaCPP::Image theimage;
pCamera->GetNextImage(&theimage);
if(theimage.IsValid())
{
if( theimage.GetColor() == 0)
{

//It's black, go ahead...
}
else
{
if(theimage.GetColor() == 15)
{
//It's white,go back
}
}

}
}

}

Thank you.

fabian
Posts: 36
Joined: Sun Jun 15, 2008 9:45 am

Re: Color Sensors

Post by fabian » Mon Apr 01, 2013 4:15 pm

Pixel GetColor(void) const;
The returned value is a RGBA quad.

So do something like:

Code: Select all

Pixel p=pMyImage->getColor();
cprintf(" R:%u G:%u B:%u", ((unsigned int)p.R)&0xFF, ((unsigned int)p.G)&0xFF, ((unsigned int)p.B)&0xFF);
if you want the luminosity (black and white) , simply sum the 3 components and divide by 3 ....

Cheers
Fab

Martin
Posts: 25
Joined: Tue Mar 19, 2013 11:41 pm

Re: Color Sensors

Post by Martin » Mon Apr 01, 2013 7:26 pm

Hi Fabian !!! Thank you too for reply! :)

Sorry but I really don't understand the concept of what i must do to identify the line. How your code could be introduced into my code above?. I relly don't know so much of c++ and it's hard for me to use of pointers and the syntaxis.

Cheers...

LR
Posts: 305
Joined: Mon Feb 12, 2007 11:55 pm
Location: FR

Re: Color Sensors

Post by LR » Tue Apr 02, 2013 10:24 am

Instead of doing :

Code: Select all

if(theimage.GetColor() == 15)
Do:

Code: Select all

Pixel p=pMyImage->getColor();

//get the image luminosity from 0 to 255
int luminosity= ((unsigned int)p.R)&0xFF + ((unsigned int)p.G)&0xFF + ((unsigned int)p.B)&0xFF);
luminosity/=3;

//you want some thing from 0 to 15:
luminosity = (luminosity * 15)/255;
if( luminosity >= 8) //it is white
...
if( luminosity < 8) //it is black
LR

Martin
Posts: 25
Joined: Tue Mar 19, 2013 11:41 pm

Re: Color Sensors

Post by Martin » Tue Apr 02, 2013 3:16 pm

Thank you very much for explain me and be so patient!!!! i could do it!! :D

Martin
Posts: 25
Joined: Tue Mar 19, 2013 11:41 pm

Re: Color Sensors

Post by Martin » Thu Sep 12, 2013 4:58 pm

Hi LR!!!

I don't fully understand this piece of code.

Code: Select all

(unsigned int)p.R)&0xFF
why using &0xFF? I don't see the point.

//you want some thing from 0 to 15:
luminosity = (luminosity * 15)/255;
if( luminosity >= 8) //it is white
...
if( luminosity < 8) //it is black
Why did you set 15% of luminosity and 8 as color range? which are apropieate values to generate the best performance in this color sensor (white - black)?

thank you!!

LR
Posts: 305
Joined: Mon Feb 12, 2007 11:55 pm
Location: FR

Re: Color Sensors

Post by LR » Fri Sep 13, 2013 5:47 pm

I don't fully understand this piece of code.
It is the usual way to convert a RGB color to a grey scale value.
Why did you set 15% of luminosity
It is a sample :) use 1000 if you want. The goal is to have a value that is near what you can have on a real sensor.
which are apropieate values to generate the best performance in this color sensor (white - black)?
It depends of wat you want to do. When you value is converted to 0 to 15 (for example), you can assume that values <=4 mean BLACK values >=10 mean WHITE, others are unknow ...
LR

Post Reply