Page 1 of 3

How to calibrate light sensor using nxc?

Posted: 12 Jun 2013, 03:32
by felix2ch
Hi, guys,
I want to calibrate my light sensor before using it, like the calibrate block in the NXT.
But I can't found similar method in the NXC.

Thanks for your help!

Re: How to calibrate light sensor using nxc?

Posted: 12 Jun 2013, 20:02
by tabbycatrobots
Here's some ideas that may help. I build various race car and dancing robots that follow a black line on a
light surface. To account for various robot designs, and parts, and especially room lighting, I do a short
"calibration" routine at the start of each program. The attached file, Ty_Dncr_31_Line_Contrast_Discover.nxc,
has code for 2 tasks that run concurrently to measure the lightness and darkness of the dark line and the
light background. The first task, task line_wiper(), rotates the robot about 60 degrees, in either direction,
over the light and dark areas. The second task, task line_contrast_discover(), measures the light intensity,
the minimum and maximum, as the robot moves back and forth. (I got some of the ideas for this code from
the site <http://www.nxtprograms.com/>. This site shows NXT-G code, but the ideas are great.) I then calculate
the mid or average, and the range, and can use this in the main program as needed. I display the min, max,
mid, and range with the following code.

Code: Select all

void display_light_min_max()
{
    unsigned short   Bat_Lev;

    TextOut(0, LCD_LINE1, "Volts = ", DRAW_OPT_CLEAR_WHOLE_SCREEN);
    Bat_Lev = BatteryLevel();
    NumOut((6 * StrLen("Volts = ")), LCD_LINE1, Bat_Lev);

    TextOut(0, LCD_LINE4, "Current = ");
    NumOut((6 * StrLen("Current = ")), LCD_LINE4, current_reflected_light_intensity);
    TextOut(0, LCD_LINE5, "lite_min = ");
    NumOut((6 * StrLen("lite_min = ")), LCD_LINE5, reflected_light_min);
    TextOut(0, LCD_LINE6, "lite_max = ");
    NumOut((6 * StrLen("lite_max = ")), LCD_LINE6, reflected_light_max);
    TextOut(0, LCD_LINE7, "lite_mid = ");
    NumOut((6 * StrLen("lite_mid = ")), LCD_LINE7, reflected_light_mid);
    TextOut(0, LCD_LINE8, "lite_range = ");
    NumOut((6 * StrLen("lite_range = ")), LCD_LINE8, reflected_light_range);

}  //  End  -  function  -  display_light_min_max
It takes some experimentation to determine how much to run the motor as the robots moves over the light and
dark, depending on whether your robot has wheels or treads, or whatever. Hope this helps.

Re: How to calibrate light sensor using nxc?

Posted: 14 Jun 2013, 05:50
by felix2ch
Thanks for your reply.
I should explain my question more clearly.
I can calibrate or recaculate the value of light senor by some method defined by myself.
But i want some nxc API to do it.
Like calibrate("SensorType", minRaw, maxRaw), and read the calibrated value by call normal Sensor(PORT).

In that way,don't need to known the calibating thing when you using Sensor(PORT). If you want calibration , just add a API to the top of code.
Any way, like NXT-G, just add a block to do it, nothing else need to change.

btw:
In your code, maybe uing SensorRaw insdead of SensorScale more efficacious.
Sometimes(depend on the lightness of environment mostly), the range of SensorScale will shrink, need calibrate to expand the range to 0~100 (or a bit less), to obtain more precision.

Re: How to calibrate light sensor using nxc?

Posted: 14 Jun 2013, 07:02
by HaWe
hey,
as far as I know there is not such a calibration function.
I think you'd consider that NXC and NXT-G (or, e.g., Labview as well) are completely different in their functionality, the only common element is the sharable firmware base.
NXC does not claim to mimic NXT-G functions or procedures or blocks by it's API at all.
If any compatibility is largely aimed by NXC then it's the syntax rules of plain C.

So the bad news is: you must do lot of things by yourself.
But the good news is: you can do lot of things by yourself.
:D

(John, CMIIW)
;)

Re: How to calibrate light sensor using nxc?

Posted: 14 Jun 2013, 14:43
by tabbycatrobots
Thanks for the ideas about SensorRaw vs. SensorScale. I'll try it.

Re: How to calibrate light sensor using nxc?

Posted: 14 Jun 2013, 15:30
by afanofosc
If you post a zip containing an RXE file built using NXT-G that does nothing more than calibrate the light sensor, read the calibrated value, and show it on the screen before ending then I can convert it into an NXC function that does the same thing.

Whether that function will be made an official standard NXC API function or not is not certain but it might.

At the moment I am primarily focused on quietly adding support for the new LEGO MINDSTORMS EV3 in BricxCC. My plan is to have a firmware-targeting compiler integrated into BricxCC as well as to have support for launching GNU C/C++ and Free Pascal cross compilers from within BricxCC to build native executables.

John Hansen

Re: How to calibrate light sensor using nxc?

Posted: 15 Jun 2013, 15:39
by felix2ch
Thanks everyone!

Here is the rxe and screen shot for the nxt-g.
I'm expecting EV3, especially NXC for EV3.
Did you known EV3 run with byte code for virtual machine or native binary on linux?

Re: How to calibrate light sensor using nxc?

Posted: 16 Jun 2013, 18:28
by mightor
The VM runs as a native application on Linux, but the code you compile with the EV3 Programming Environment is byte code. It's a lot faster than NXT-G, though.

= Xander

Re: How to calibrate light sensor using nxc?

Posted: 17 Jun 2013, 01:43
by felix2ch
Is that mean EV3 will be compatible with NXT at byte code level?

Re: How to calibrate light sensor using nxc?

Posted: 18 Jun 2013, 20:10
by tabbycatrobots
afanofosc wrote:At the moment I am primarily focused on quietly adding support for the new LEGO MINDSTORMS EV3
in BricxCC.
John, Do you have any idea on a timetable, or a date you can publish, for 'EV3-NXC'? (I'm sure it will
have a new name.) I'm debating whether to get an Education EV3 sooner or a retail unit when they ship. Don't worry, I
won't hold you to the date. (My manager used to double the times I gave him before publishing them.) Thanks.
Howard