NXC: NXT 2.0 color sensor in lamp mode
-
- Posts: 323
- Joined: 29 Sep 2010, 05:03
Re: NXC: NXT 2.0 color sensor in lamp mode
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
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
-
- Posts: 1818
- Joined: 02 Oct 2010, 02:19
- Location: Michigan USA
- Contact:
Re: NXC: NXT 2.0 color sensor in lamp mode
Not that I remember hearing of, but hopefully there is a way. John, is there?mightor wrote:Is there no way to get the current tick count?mattallen37 wrote:Ok. Is there anything like that in NXC?
- Xander
Matt
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
Re: NXC: NXT 2.0 color sensor in lamp mode
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
http://bricxcc.sourceforge.net/nbc/nxcd ... df9a0ef21a
John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
http://bricxcc.sourceforge.net/
-
- Posts: 1818
- Joined: 02 Oct 2010, 02:19
- Location: Michigan USA
- Contact:
Re: NXC: NXT 2.0 color sensor in lamp mode
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?
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
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
Re: NXC: NXT 2.0 color sensor in lamp mode
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
- 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)
| 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)
-
- Posts: 1818
- Joined: 02 Oct 2010, 02:19
- Location: Michigan USA
- Contact:
Re: NXC: NXT 2.0 color sensor in lamp mode
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
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
-
- Posts: 1818
- Joined: 02 Oct 2010, 02:19
- Location: Michigan USA
- Contact:
Re: NXC: NXT 2.0 color sensor in lamp mode
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).Here is the code for timing the light coming on (again, I tried all the color options).Again, both programs always return 168 or 169.
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
}
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
}
Matt
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
Re: NXC: NXT 2.0 color sensor in lamp mode
That means that either:
- Xander
- My code is flawed
- The ROBOTC firmware does something funny
- All of the above
- 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)
| 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)
Re: NXC: NXT 2.0 color sensor in lamp mode
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
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/
http://bricxcc.sourceforge.net/
-
- Posts: 117
- Joined: 27 Dec 2010, 19:27
Re: NXC: NXT 2.0 color sensor in lamp mode
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.
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
I used two LEGO color sensors. The placement of the sensors looks like in the picture.
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);
}
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
Who is online
Users browsing this forum: Semrush [Bot] and 0 guests