NXC: NXT 2.0 color sensor in lamp mode

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
gloomyandy
Posts: 323
Joined: 29 Sep 2010, 05:03

Re: NXC: NXT 2.0 color sensor in lamp mode

Post 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
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: NXC: NXT 2.0 color sensor in lamp mode

Post 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?
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: NXC: NXT 2.0 color sensor in lamp mode

Post 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
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: NXC: NXT 2.0 color sensor in lamp mode

Post 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?
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
mightor
Site Admin
Posts: 1079
Joined: 25 Sep 2010, 15:02
Location: Rotterdam, Netherlands
Contact:

Re: NXC: NXT 2.0 color sensor in lamp mode

Post 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
| My Blog: I'd Rather Be Building Robots (http://botbench.com)
| RobotC 3rd Party Driver Suite: (http://rdpartyrobotcdr.sourceforge.net)
| Some people, when confronted with a problem, think, "I know, I'll use threads,"
| and then two they hav erpoblesms. (@nedbat)
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: NXC: NXT 2.0 color sensor in lamp mode

Post by mattallen37 »

Ok, i get it. Thanks.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: NXC: NXT 2.0 color sensor in lamp mode

Post 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.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
mightor
Site Admin
Posts: 1079
Joined: 25 Sep 2010, 15:02
Location: Rotterdam, Netherlands
Contact:

Re: NXC: NXT 2.0 color sensor in lamp mode

Post 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
| My Blog: I'd Rather Be Building Robots (http://botbench.com)
| RobotC 3rd Party Driver Suite: (http://rdpartyrobotcdr.sourceforge.net)
| Some people, when confronted with a problem, think, "I know, I'll use threads,"
| and then two they hav erpoblesms. (@nedbat)
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXC: NXT 2.0 color sensor in lamp mode

Post 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
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
ricardocrl
Posts: 117
Joined: 27 Dec 2010, 19:27

Re: NXC: NXT 2.0 color sensor in lamp mode

Post 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
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 1 guest