Log in

View Full Version : Programming Light Sensor


dratewka2
17-02-2011, 20:07
We are declaring the LightWhite as a DigitalInput;
then LightWhite = new DigitalInput(5);
we do... if (LightWhite->Get()==1)
{...do something...}

but we do not get the if to be entered....
any ideas???

speceottar
17-02-2011, 20:24
The light sensor will return true if light is reflected back into it, and false if no light is reflected. In other words, when the light on it is green, it is false, and if the light is orange, it is a true. All you need is a simple if statement:

if(LightWhite->Get()) {
//the sensor is on the line
}

or

if(!LightWhite->Get()) {
//The sensor is not on the line
}

Depending on which sensors are on and which are off, you can find out where the robot is relative to the line, and move accordingly.

Another useful feature of c++ is that you have the ability to use booleans as ints (either a 1 or a 0) and add them together. The following would set "Lines" to the number of sensors that are triggered:
int Lines = WhiteLightOne->Get() + WhiteLightTwo->Get();

garyk
20-02-2011, 01:15
We are declaring the LightWhite as a DigitalInput;
then LightWhite = new DigitalInput(5);
we do... if (LightWhite->Get()==1)
{...do something...}

but we do not get the if to be entered....
any ideas???

Are the indicators on the body of the Light Sensor (assuming it's the KOP sensor) showing it's detecting light or dark? The interpretation of the indicators is among the information on the data sheet that came in each sensor bag. If the indicators aren't showing that an object is seen, the cRIO isn't going to see it either.

And the sensor's brown wire is wired to +12v, the blue wire to - ?. And according to your code, the white wire is connected to a DI and the black wire is cut and taped so it won't short to anything?

It's puzzling, but DigitalInput->Get() does not return a boolean, but an UINT32. Please see "WPILib C++ Reference" in c:\windriver\docs\extensions\FRC\.

An UINT32 is an unsigned 32 bit int, but all we care about is that it returns 1 or 0. In c++, as in c, an int can be interpreted as a boolean in an if().

Your if (LightWhite->Get()==1) {} should work, the old-school C way of doing it is if (LightWhite->Get()) {} .

Strictly speaking, if (LightWhite->Get()) is identical to if (LightWhite->Get() != 0) but the effect is the same since the DI returns 0 0r 1.

PranavSathy
20-02-2011, 13:01
I just want to point out though that your signal return, a.k.a the DigitalInput->Get() on the sensor will vary depending on which wire you put for the digital input, the Black (light out), or White(dark out), the code above would work for the white wire, Dark out, which will return 1 if it is on the line, and 0 if it is not. Good luck!

mikets
21-02-2011, 15:30
To debug the light sensors:

Make sure the light sensors are powered (i.e. any of the LEDs are lit -green, yellow or orange).
Make sure the light sensors are calibrated. The green LED should lit up if the sensor is on carpet. The yellow LED should lit up if the sensor is on the line. If the orange LED is lit up along side the yellow, it's even better (good contrast margin). If the LEDs don't lit up as described above, you need to tune the screw on top of each sesnor until it behaves as above.
Make sure the sensor signal wires are connected to a separate digital input channel for each sensor. If you want to read a 1 for on-the-line and 0 for on-the-carpet, use the white wire as the signal wire. If you want the reverse, use the black wire.
Assuming you are connecting using the white wire, test your setup so that when the light sensor has the green LED lit, you should read a 0. When the yellow or yellow+orange LEDs are lit, you should read a 1.

joshkuruvilla
27-01-2012, 18:25
Hey can you give me like the code for when an object passes through the light sensor. Like everything. I am so new to programming, its not even funny.

mikets
27-01-2012, 19:09
Are you a rookie team or do you have last year's code you can base on? If you are totally new to programming, you probably need a programming mentor to guide you through it. What are you trying to do with the light sensors? That will affect how the code is written. Last year, we used the light sensors to follow the line. If you are not doing line following then last year's code may not be too useful. But you can still look at how to read the light sensor value. If you are talking about the same light sensor that was used last year, it is connected to a digital input channel. So you basically instantiate a DigitalInput object and call its Get() method to read the light sensor value (1 or 0). That's the basic operation of using the light sensor. Now how do you use the light sensor reading is a whole new story. Here is a simple example.

#define DIN_LIGHT_SENSOR 1 //Digital input channel 1

class MyRobot: public SimpleRobot
{
//
// This is to declare the light sensor object to be a digital input object.
//
DigitalInput lightSensor;
DriverStationLCD *dsLCD;

//
// This initialize the light sensor object specifying the digital input channel.
//
MyRobot():
lightSensor(DIN_LIGHT_SENSOR),
dsLCD(DriverStationLCD::GetInstance())
{
}

void OperatorControl()
{
while (IsEnabled() && IsOperatorControl())
{
UINT32 value = lightSensor.Get();
dsLCD->PrintfLine(DriverStationLCD::kUserLine1, "value=%d", value);
Wait(0.1);
}
}
};

If your light sensor is powered by a solenoid channel, then you need to add code to instantiate a solenoid object and "turn the power ON".