Page 3 of 4

Re: NXC: NXT 2.0 color sensor in lamp mode

Posted: 04 Jan 2011, 17:06
by gloomyandy
OK but the actual mode change process goes something like this...
1. Reset the device (this has a 100ms delay as part of the process)
2. Set operating mode
3. Read calibration data
The light does not turn on/off at the end of the process, it actually turns off pretty much at the beginning (when you reset the device), and turns on midway through (when you set the mode, but before you read the calibration data). So I was wondering how with RobotC you know if the process has completed fully or not, if there is no way of telling this then your code may be initiating a new mode setting before it has completed the previous one and this may be having an impact on the timing. Not that it really matters if all you want to do is turn the light on and off as fast you can...

Andy

Re: NXC: NXT 2.0 color sensor in lamp mode

Posted: 04 Jan 2011, 18:20
by mattallen37
mightor wrote:
mattallen37 wrote:Ok. Is there anything like that in NXC?
Is there no way to get the current tick count?

- Xander
Not that I remember hearing of, but hopefully there is a way. John, is there?

Re: NXC: NXT 2.0 color sensor in lamp mode

Posted: 04 Jan 2011, 19:13
by afanofosc
In NXC you can time things by storing the CurrentTick into an unsigned long and then comparing the stored value with the new value returned by this same function.

http://bricxcc.sourceforge.net/nbc/nxcd ... df9a0ef21a

John Hansen

Re: NXC: NXT 2.0 color sensor in lamp mode

Posted: 04 Jan 2011, 19:29
by mattallen37
Ok cool. Thanks John.

How fast does it "Tick"? Is it 48mhz (clock)? If so, does it reset back to 0 (because there only 32 bits available) every 89 (if I did the math right) seconds?

Re: NXC: NXT 2.0 color sensor in lamp mode

Posted: 04 Jan 2011, 19:35
by mightor
A tick in this context is not the same as a CPU clock tick. It's a 1ms time slice. You'll be OK for a little while with using a 32 bit number for that.

- Xander

Re: NXC: NXT 2.0 color sensor in lamp mode

Posted: 04 Jan 2011, 19:49
by mattallen37
Ok, i get it. Thanks.

Re: NXC: NXT 2.0 color sensor in lamp mode

Posted: 04 Jan 2011, 20:02
by mattallen37
Turning on or off always returns 168 or 169 (using NXC, with enhanced FW). Here is the code that I used for timing turning off (I tried other initial colors as well).

Code: Select all

unsigned long First_Tick;
unsigned long Second_Tick;
unsigned long Total_Tick;

task main()
{
  SetSensorLight(S2);                 //set up the light sensor
  SetSensorMode(S2, IN_MODE_RAW);     //set light sensor in RAW mode
  SetSensorType(S2, IN_TYPE_LIGHT_INACTIVE);  //have light sensor light off
  SetSensorColorRed(S1);              //turn on light
  Wait(500);                          //wait for light to be on
  First_Tick=CurrentTick(		 );    //get the initial tick
  SetSensorColorNone(S1);             //turn off color light
  until(SENSOR_2<=37);                //until the light sensor senses dark
  Second_Tick=CurrentTick(		 );   //get the final tick
  Total_Tick=Second_Tick-First_Tick;  //calculate the difference in ticks
  NumOut(0,LCD_LINE1,Total_Tick);     //display the result
  while(true);                        //wait for you to read it
}
Here is the code for timing the light coming on (again, I tried all the color options).

Code: Select all

unsigned long First_Tick;
unsigned long Second_Tick;
unsigned long Total_Tick;

task main()
{
  SetSensorLight(S2);                 //set up the light sensor
  SetSensorMode(S2, IN_MODE_RAW);     //set light sensor in RAW mode
  SetSensorType(S2, IN_TYPE_LIGHT_INACTIVE);  //have light sensor light off
  First_Tick=CurrentTick(		 );    //get the initial tick
  SetSensorColorFull(S1);             //turn on light
  until(SENSOR_2>=37);                //until the light sensor senses dark
  Second_Tick=CurrentTick(		 );   //get the final tick
  Total_Tick=Second_Tick-First_Tick;  //calculate the difference in ticks
  NumOut(0,LCD_LINE1,Total_Tick);     //display the result
  while(true);                        //wait for you to read it
}
Again, both programs always return 168 or 169.

Re: NXC: NXT 2.0 color sensor in lamp mode

Posted: 04 Jan 2011, 20:08
by mightor
That means that either:
  • My code is flawed
  • The ROBOTC firmware does something funny
  • All of the above
I'll ask the ROBOTC devs sometime :)

- Xander

Re: NXC: NXT 2.0 color sensor in lamp mode

Posted: 06 Jan 2011, 17:58
by afanofosc
The SetSensorColor* functions in NXC all wait for the firmware to reset the Invalid flag back to false. This may not exactly equal the time it takes for the light to turn on or off. I would time this by using SetSensorType instead of SetSensorColor*. The mode should be set to raw for Full color operation and percent for single color operation.

So you could call SetSensorMode(port, SENSOR_MODE_PERCENT) and then when switching from Red to None you could call SetSensorType(port, SENSOR_TYPE_COLORRED) or SetSensorType(port, SENSOR_TYPE_COLORNONE). The mode doesn't really matter, though, if you aren't needing to actually read sensor values from the color sensor.

John Hansen

Re: NXC: NXT 2.0 color sensor in lamp mode

Posted: 15 Feb 2011, 12:47
by ricardocrl
I found this interesting and did the test like John suggested.

I used two LEGO color sensors. The placement of the sensors looks like in the picture.
Image

Code: Select all

#define SAMPLES 50
#define COLOR SENSOR_TYPE_COLORRED

task main(void){
     unsigned long tic;
     unsigned long totalTurnOffTime;
     unsigned long totalTurnOnTime;
     int i;
     
     SetSensorColorNone(S2);
     SetSensorMode(S2, SENSOR_MODE_RAW);
     
     for (i = 0; i < SAMPLES; i++){
         tic = CurrentTick();
         SetSensorType(S1, COLOR);
         while(SENSOR_2 < 500);
         totalTurnOnTime += CurrentTick()-tic;
         
         Wait(500); // Just in case the FW needs to finish any setup, when turning on the light

         tic = CurrentTick();
         SetSensorType(S1, SENSOR_TYPE_COLORNONE);
         while(SENSOR_2 > 500);
         totalTurnOffTime += CurrentTick()-tic;
         Wait(500);
     }
     
     TextOut(0, LCD_LINE1, "Turn On:");
     TextOut(0, LCD_LINE2, "Turn Off:");
     NumOut(60, LCD_LINE1, totalTurnOnTime/SAMPLES);
     NumOut(60, LCD_LINE2, totalTurnOffTime/SAMPLES);
     while(ButtonCount(BTNCENTER, 0) == 0);
}
I got for all 3 individual colors 160ms for turning on and 107ms to turn off.

For Red and Blue, I got the value 1023 for turned on and 0 for turned off (as opposed to higher values with Light sensor).
With RGB, I couldn't perform the test. I don't understand why, but I got about 0 for turned off and between 0-10 for turned on.

Ricardo