View unanswered posts | View active topics It is currently Mon Dec 22, 2014 11:46 am






Reply to topic  [ 10 posts ] 
Lego RGB Color Sensor 
Author Message
Rookie

Joined: Thu Aug 12, 2010 1:39 pm
Posts: 4
Post Lego RGB Color Sensor
I have the version of RobotC that supports the Lego color sensor. Can anyone give me the code for a simple line following program that uses the color sensor instead of the light sensor?
Also, with NXT-G, you could use the color sensor as a light sensor (get numbers from 1 to 100 from the color sensor). Can you do something similar to that in RobotC.


Thu Aug 12, 2010 1:51 pm
Profile
Rookie

Joined: Thu Aug 12, 2010 1:39 pm
Posts: 4
Post Re: Lego RGB Color Sensor
Never mind... I got it


Mon Sep 06, 2010 10:11 am
Profile
Rookie

Joined: Fri Jul 15, 2011 1:07 am
Posts: 1
Post Re: Lego RGB Color Sensor
Could you please share with us how?


Fri Jul 15, 2011 1:10 am
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3297
Location: Rotterdam, The Netherlands
Post Re: Lego RGB Color Sensor
Code:
#pragma config(Sensor, S1,     colorPort,           sensorCOLORFULL)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

task main()
{
  string sColor;

  while (true) {
    switch (SensorValue[colorPort])
    {
      case BLACKCOLOR:    sColor = "Black";     break;
      case BLUECOLOR:     sColor = "Blue";      break;
      case GREENCOLOR:    sColor = "Green";     break;
      case YELLOWCOLOR:   sColor = "Yellow";    break;
      case REDCOLOR:      sColor = "Red";       break;
      case WHITECOLOR:    sColor = "White";     break;
      default:            sColor = "???";       break;
    }
    nxtDisplayCenteredTextLine(2, sColor);
    wait1Msec(50);
  }
}


You will need ROBOTC version 2.26 beta.

- Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Fri Jul 15, 2011 1:36 am
Profile WWW
Rookie

Joined: Sun Aug 21, 2011 12:55 am
Posts: 2
Post Re: Lego RGB Color Sensor
Hello every one! I'm new to this board and I have a question. I just updated to 2.26 to be able to use my RGB Lego sensor. All I need to do is figure out how to asign to three individual variables the raw value of each color. I used your snip above and it works great but the colors are predetermined.
In pseudo:

int RedRaw = (Something meaning Raw Red Value)
int GreenRaw = (Something meaning Raw Green Value)
int BlueRaw = (Something meaning Raw Blue Value)

Thanks,


Mon Aug 22, 2011 12:40 am
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3297
Location: Rotterdam, The Netherlands
Post Re: Lego RGB Color Sensor
Hiya Very New Guy!

You're in luck, I wasn't sure it was possible but if you open the ColorSensor.c sample program that came with your ROBOTC 2.26 installation, take a look around line 215:

Code:
void displayRGBColorValues()
{
   short nAtoDValues[4];
   short nRawValues[4];
   short nColorValues[4];
   string sTemp;

  getColorSensorData(colorPort, colorAtoD,   &nAtoDValues);
  getColorSensorData(colorPort, colorRaw,    &nRawValues);
  getColorSensorData(colorPort, colorValue,  &nColorValues);

  displayColorIndex();

  nxtDisplayTextLine(3, "   AtoD  Raw   %%");
  StringFormat(sTemp, "R  %4i%5i", nAtoDValues[0], nRawValues[0]);      nxtDisplayTextLine(4, "%s%4i", sTemp, nColorValues[0]);
  StringFormat(sTemp, "G  %4i%5i", nAtoDValues[1], nRawValues[1]);      nxtDisplayTextLine(5, "%s%4i", sTemp, nColorValues[1]);
  StringFormat(sTemp, "B  %4i%5i", nAtoDValues[2], nRawValues[2]);      nxtDisplayTextLine(6, "%s%4i", sTemp, nColorValues[2]);
  StringFormat(sTemp, "Amb%4i%5i", nAtoDValues[3], nRawValues[3]);      nxtDisplayTextLine(7, "%s%4i", sTemp, nColorValues[3]);
}


I think this is pretty much what you were looking for, no?

For your colour comparing pleasure, I also have for you on special offer an RGB to HSV conversion function, courtesy of one of my driver suite users:
Code:
/**
 * Convert RGB colors to HSV
 * @param red the red input value
 * @param green the green input value
 * @param blue the blue input value
 * @param hue the hue output value (from 0 to 365, or -1 if n/a)
 * @param sat the saruration output value (from 0 to 100, or -1 if n/a)
 * @param value the value output value (from 0 to 100)
 * @return void
 */
void RGBtoHSV(float red, float green, float blue, float &hue, float &sat, float &value)
{
   hue = 0;
   sat = 0;
   value = 0;

  //   Value
  float rgb_max = max3(red, green, blue);
  float rgb_min;
  value = rgb_max / 2.56;
  if (value == 0){
    hue = -1;
    sat = -1;
    return;
  }

  //   Saturation
  red /= rgb_max;
  green /= rgb_max;
  blue /= rgb_max;

  rgb_max = max3(red, green, blue);
  rgb_min = min3(red, green, blue);
  sat = (rgb_max - rgb_min) * 100;
  if (sat == 0){
    hue = -1;
    return;
  }

  //   Hue

  red = (red - rgb_min) / (rgb_max - rgb_min);
  green = (green - rgb_min) / (rgb_max - rgb_min);
  blue = (blue - rgb_min) / (rgb_max - rgb_min);

  rgb_max = max3(red, green,blue);
  rgb_min = min3(red, green,blue);

  if (rgb_max == red){
    hue = 0.0 + 60.0*(green-blue);
    if (hue < 0.0){
      hue += 360.0;
    }
  } else if (rgb_max == green){
    hue = 120.0 + 60.0 * (blue-red);
  } else {
    hue = 240.0 + 60.0 * (red-green);
  }
}


This function can also be found in light-common.h in my driver suite which you can download from my signature below.

- Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Mon Aug 22, 2011 1:06 am
Profile WWW
Rookie

Joined: Sun Aug 21, 2011 12:55 am
Posts: 2
Post Re: Lego RGB Color Sensor
Ok, this is the bare-bones version. All I've done is erased all that wasn't raw-color code. There's no print to display just simply a way to access the raw data from each color short-variables in what I would call brightness levels. Hope this helps someone else out there. I surely learned a lot. Thanks Xander!

Code:
#pragma config(Sensor, S1,     colorPort,           sensorCOLORFULL)

short nAtoDValues[3]={0,0,0};
short red = 0;
short green = 0;
short blue = 0;

task main()
{
   wait1Msec(1000);  // Give sensor time to initialize
   eraseDisplay();
   
   while (true)
      {
        getColorSensorData(colorPort, colorAtoD, &nAtoDValues );
        red = nAtoDValues[0];
        green = nAtoDValues[1];
        blue = nAtoDValues[2];
         wait1Msec(150);         
      }
}


Mon Sep 05, 2011 3:41 am
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3297
Location: Rotterdam, The Netherlands
Post Re: Lego RGB Color Sensor
No problems. I edited your post to put the code inside [ code] tags. Next time just use them to preserve the spacing and all that.
Code:
[code]your code goes here[/code]


- Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Mon Sep 05, 2011 4:24 am
Profile WWW
Rookie

Joined: Wed Nov 02, 2011 5:23 pm
Posts: 1
Location: Breckerfeld, Germany
Post Re: Lego RGB Color Sensor
Hello everybody,

I'm new to this board and to the whole Mindstorms world and it seems that I'm also a bit late for the party but anyway, I'll give it a try and post my question to this thread instead creating a new one. Apologies if that is an unwanted behavior here. And apologies for my clumsy english, it's obviously not my native language.
I want to use the Lego RGB Sensor coming with the 8547 Set as a simple light detector. I'm using ROBOTC 3.04 with FW 9.04. I played around with the different possibilities to use the RGB Sensor but when I try to use the measured numbers (I monitor them via the Debugging window) e.g. with the Lego Color Ambient settings, it seems that there is no way to read the correct data and use it e.g. in a loop. I can see data in the "Raw" and in the "Value" fields but it seems that I can't reference these values correctly. When using the sonar sensor, everything works like a charm if I use the raw values. Maybe this is a typical newbie nobrainer question but I searched the threads in this forum but up to now to no avail. I would be very thankful for a hint...

Regards
HDC


Wed Nov 02, 2011 5:47 pm
Profile WWW
Rookie

Joined: Thu Jan 24, 2013 4:29 pm
Posts: 1
Post Re: Lego RGB Color Sensor
Hi everyone...well me and my group have to find out a way to make our robot read the colors,we are using RGB lights and we cant find a way to match the number for the color sensor since we get a reading from the light sensor when we go to the view section of the NXT. Can anyone help?


Thu Jan 24, 2013 5:20 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 10 posts ] 

Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.