View unanswered posts | View active topics It is currently Fri Aug 22, 2014 7:57 am






Reply to topic  [ 3 posts ] 
Handling Lego colour sensor sensitivity 
Author Message
Rookie

Joined: Wed Jun 12, 2013 8:09 pm
Posts: 2
Post Handling Lego colour sensor sensitivity
Hello

I'm new to RobotC but I'm quickly warming to it as a way of programming Lego NXT robots. I'm experimenting with a few things using the Lego colour sensor, including a robot that rolls about over a floor with coloured patches on it (the floor, not the robot :-) )and does various actions depending on colour it encounters. I have a program that looks like this:

Code:
#pragma config(Sensor, S3,     colorSensor,    sensorCOLORFULL)
#pragma config(Motor,  motorB,           ,             tmotorNXT, PIDControl, reversed, encoder)
#pragma config(Motor,  motorC,           ,             tmotorNXT, PIDControl, reversed, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

void TurnRight();
void TurnLeft();
void Reverse();
void ChangeSpeed();
void TurnAround();

const int cFast = 60;
const int cNormal = 30;

int nPower = cNormal;
int nDirection = 0;   // 0 = forwards, 1 = reverse
int nSpeed = 0;       // 0 = normal, 1 = fast

task main()
{
   // wait a couple of seconds before starting
   wait1Msec(2000);

   // all motors off
   motor(motorC) = 0;
   motor(motorB) = 0;

   nDirection = 0;
   nSpeed = 0;

   motor(motorC) = nPower;
   motor(motorB) = nPower;

   while(true)
   {
      switch(SensorValue(colorSensor))
      {
         case REDCOLOR:
            TurnRight();
            break;
         case BLUECOLOR:
            TurnLeft();
            break;
         case GREENCOLOR:
            Reverse();
            break;
         case YELLOWCOLOR:
            ChangeSpeed();
            break;
         case BLACKCOLOR:
            TurnAround();
            break;
         case WHITECOLOR:
            break;
         default:
            nxtDisplayCenteredBigTextLine(0, "????????");
      }  // end switch

   }  // end of main while loop

}  // end of task main


The other functions, TurnLeft, TurnRight, Reverse, ChangeSpeed, TurnAround contain commands that control the motors. Hopefully the names are self-explanatory :D

So my questions so far are:

- Is this design more or less how others do similar things? Several of the sample programs look similar.

- I guess the constants REDCOLOR etc effectively match ranges of the raw values returned by the colour sensor? I notice that sometimes it does the wrong thing, for example it gives blue when rolling over a green patch (I am in a well lit room, using fairly even, mid-range colour patches). I guess this might be because as it rolls over the border between two colours, the sensor value is the average of the white area it is leaving and the green area it is rolling on to, and it is interpreting that as blue? What's a good way to accommodate that in the code?


Thanks for any suggestions


Wed Jun 12, 2013 8:39 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3190
Location: Rotterdam, The Netherlands
Post Re: Handling Lego colour sensor sensitivity
EvilTwin wrote:
- Is this design more or less how others do similar things? Several of the sample programs look similar.

Yup, this is pretty much how you do it.

Quote:
- I guess the constants REDCOLOR etc effectively match ranges of the raw values returned by the colour sensor? I notice that sometimes it does the wrong thing, for example it gives blue when rolling over a green patch (I am in a well lit room, using fairly even, mid-range colour patches). I guess this might be because as it rolls over the border between two colours, the sensor value is the average of the white area it is leaving and the green area it is rolling on to, and it is interpreting that as blue? What's a good way to accommodate that in the code?

You could do the colour matching yourself but the easiest way I've found is to make a small shroud around colour sensor so that very little outside light can interfere with its readings. I tend to use a small piece of paper and simple wrap it around the sensor and have it extend a little beyond the end of it.

Btw, may I congratulate you on writing readable code AND using code tags on your first post?

= 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]


Thu Jun 13, 2013 1:23 am
Profile WWW
Rookie

Joined: Wed Jun 12, 2013 8:09 pm
Posts: 2
Post Re: Handling Lego colour sensor sensitivity
Hello Xander

Thanks for replying. A bit of paper wrapped round the sensor... good engineering solution :D I would never have thought of that!

The paper helps, but a bit of further experimentation shows that it is definitely the boundary between different coloured areas that causes the sensor to sometimes do the wrong thing. Recognising red, yellow and white seems OK, but green, blue and black sometimes get mixed up. I noticed another site where you made a comment that the colour sensor is a bit flaky and I am beginning to agree :-)

I'm still working on getting it to do what I want. Currently, I'm experimenting with using a timer of a few milliseconds between a sensor value change and then doing something, by which time the sensor value will be the right one (e.g. if the sensor detects green when it should be black, because it is on a boundary, when the timer times out it should have moved completely on to the colour block and the value will hopefully be correct). Not really a nice solution but I am hoping it will do for now... any further ideas appreciated :D


Fri Jun 14, 2013 4:01 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 3 posts ] 

Who is online

Users browsing this forum: No registered users and 2 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.