Bricxcc, NXC, Color Sensor, NXT 2.0

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: Bricxcc, NXC, Color Sensor, NXT 2.0

Post by mattallen37 »

roboteer wrote:Does anyone know if it matters which way the color sensor is facing? Does it matter if it is put at the front of the robot versus the rear of the robot?
It is almost essential that it is in the front; it would be probably hundreds of times harder to program if it's in the back! If it's in the front, all you have to do is keep it over the line. I won't even begin to explain what you'd have to do to keep the robot following the line if the sensor was at the back (compared to the direction of travel).
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: Bricxcc, NXC, Color Sensor, NXT 2.0

Post by afanofosc »

ggrinton wrote: I found that if I use SetSensorColorFull(S3); then I can read the light value with code like
  • int light, RawColor[];
    ReadSensorColorRaw(S3, RawColor);
    light = RawColor[3];
but if I try to use light = SensorRaw(S3); I don't get any value returned.

On the other hand, if I use SetSensorColorRed(S3); then to get a meaningful light value, I need to use light = SensorRaw(S3); and I don't get any meaningful values from ReadSensorColorRaw.
By design, you read full color values from a special area in the Input module's IOMap structure and with a special system call function. If you have the sensor configured as a full color device then you only get values via this system call function or in the SensorValue field which simply returns the color number. If you switch the sensor into a single color device using SetSensorColorRed, SetSensorColorBlue, or SetSensorColorGreen then it acts like the old light sensor and does not update the full color IOMap fields so you can't read any useful values via the special system call function (which ReadSensorColorRaw uses under the hood). Instead you get sensor values from the same functions that you would use for the old light sensor, aka SensorValue, SensorRaw, etc... That is simply the way that the NXT firmware is designed to work with the new color sensor. You use it as a light sensor in single color mode (red, green, or blue) or you use it as a full color sensor and depending on which mode you choose to use you access the values returned by the sensor differently (as you have found through testing and as described above).

Here are a couple examples of using the new color sensor:

http://www.philohome.com/fbs/fbs.nxc

http://tiltedtwister.com/download/tt2/tiltedtwister.nxc

https://sourceforge.net/apps/phpbb/mind ... rRed#p1532

Searching these forums for "SetSensorColorFull" or "SetSensorColorRed" will return a number of threads where this device has been discussed a lot in the past.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
ggrinton
Posts: 16
Joined: 24 Feb 2012, 12:18

Re: Bricxcc, NXC, Color Sensor, NXT 2.0

Post by ggrinton »

Thanks for the helpful information, John, and for the pointers.
jimbobert94
Posts: 2
Joined: 16 Mar 2012, 06:26

Re: Bricxcc, NXC, Color Sensor, NXT 2.0

Post by jimbobert94 »

Hey, James here. When using the switch statement to decide what to do when the color sensor detects blue, it performs the same action over and over, even when I use break. Here is my code and I would appreciate any help:

Code: Select all

ClearScreen();
switch(Color)
{
             
            case 2:
                TextOut(0, LCD_LINE3, "BLUE- Let's park!");
                Off(OUT_BC);
                Wait(2500);
                OnRev(OUT_B, turn);
                OnFwd(OUT_C, turn);
                Wait(angle);
                Off(OUT_BC);
                OnRev(OUT_BC, full);
                Wait(1500);
                Off(OUT_BC);
                Wait(5000);
                OnFwd(OUT_BC, full);
                Wait(1500);
                Off(OUT_BC);
                OnFwd(OUT_B, turn);
                OnRev(OUT_C, turn);
                Wait(angle);
                Off(OUT_BC);
                ClearSensor(IN_3);
                break;
            default:
                break;
            }
            OnRev(OUT_BC, full);
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: Bricxcc, NXC, Color Sensor, NXT 2.0

Post by afanofosc »

I can't tell from the partial snippet of code that you posted what is going on. You should try to avoid "magic numbers" in your source code, such as case 2: or Wait(2500). #define a meaningful name for your numbers at the start of your code or use one that already exists, such as the ones defined on this page:

http://bricxcc.sourceforge.net/nbc/nxcd ... tants.html

Can you post a bit of code that shows the switch statement in context?

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
jimbobert94
Posts: 2
Joined: 16 Mar 2012, 06:26

Re: Bricxcc, NXC, Color Sensor, NXT 2.0

Post by jimbobert94 »

Yes and thank you for the first advice. Much appreciated! Here is the whole script:

Code: Select all

//setting turn speed, full speed, and rotate time.
#define turn 100
#define full 45
#define angle 400
#define INPUT_BLUECOLOR 2

sub color_check()
{
    //Turn on color sensor in sensor input 3
    SetSensorColorFull(IN_3);

    while(true)
        {
        int Color;
        //If the color sensor picks up blue, assign that value to color
        if(Sensor(IN_3) == INPUT_BLUECOLOR) Color = INPUT_BLUECOLOR;
        {       
            //clear to lcd screen on the physical robot
            ClearScreen();
            switch(Color)
            {    
                //case 2 (two is the default number given to blue) which tells the robot to park when it senses blue.
            case 2:         
            	TextOut(0, LCD_LINE3, "BLUE- Let's park!");
                Off(OUT_BC);    
                Wait(2500);     
                OnRev(OUT_B, turn);
                OnFwd(OUT_C, turn);
                Wait(angle);    
                Off(OUT_BC);    
                OnRev(OUT_BC, full);
                Wait(1500);     
                Off(OUT_BC);    
                Wait(5000);     
                OnFwd(OUT_BC, full);
                Wait(1500);     
                Off(OUT_BC);    
                OnFwd(OUT_B, turn);
                OnRev(OUT_C, turn);
                Wait(angle);
                Off(OUT_BC);
       	        ClearSensor(IN_3);
                break;
	    default:
	    	break;
            }
            OnRev(OUT_BC, full);
        }
    }
}

task main()
{
    OnRev(OUT_BC, full);
    while(true)
    {
    	color_check();
    }
}
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: Bricxcc, NXC, Color Sensor, NXT 2.0

Post by afanofosc »

Here's what I would use:

Code: Select all

//setting turn speed, full speed, and rotate time.
#define turn 100
#define full 45
#define angle 400
// don't redefine INPUT_BLUECOLOR

void color_check() // use void instead of sub (which is inherited from NQC but frowned upon for NXC use)
{
    while(true)
    {
        int Color;
        // assign sensor value to Color (regardless of its value)
        Color = Sensor(IN_3);
//        //If the color sensor picks up blue, assign that value to color
//        if(Sensor(IN_3) == INPUT_BLUECOLOR)
//            Color = INPUT_BLUECOLOR;
        // clear the lcd screen on the physical robot
        ClearScreen();
        switch(Color)
        {
            //case 2 (two is the default number given to blue) which tells the robot to park when it senses blue.
            case INPUT_BLUECOLOR:
                TextOut(0, LCD_LINE3, "BLUE- Let's park!");
                Off(OUT_BC);
                Wait(2500);
                OnRev(OUT_B, turn);
                OnFwd(OUT_C, turn);
                Wait(angle);
                Off(OUT_BC);
                OnRev(OUT_BC, full);
                Wait(1500);
                Off(OUT_BC);
                Wait(5000);
                OnFwd(OUT_BC, full);
                Wait(1500);
                Off(OUT_BC);
                OnFwd(OUT_B, turn);
                OnRev(OUT_C, turn);
                Wait(angle);
                Off(OUT_BC);
//                ClearSensor(IN_3); // no reason for this
                break;
            default:
                break;
        }
        OnRev(OUT_BC, full);
    }
}

task main()
{
    //Turn on color sensor in sensor input 3 (only do this one time in your program)
    SetSensorColorFull(IN_3);

    OnRev(OUT_BC, full);
    while(true)
    {
       color_check();
    }
}
John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest