Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   C/C++ (http://www.chiefdelphi.com/forums/forumdisplay.php?f=183)
-   -   Programming Light Sensor (http://www.chiefdelphi.com/forums/showthread.php?t=92140)

dratewka2 17-02-2011 20:07

Programming Light Sensor
 
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

Re: Programming Light Sensor
 
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

Re: Programming Light Sensor
 
Quote:

Originally Posted by dratewka2 (Post 1025465)
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
Code:

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

if (LightWhite->Get()) {}
.

Strictly speaking,
Code:

if (LightWhite->Get())
is identical to
Code:

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

PranavSathy 20-02-2011 13:01

Re: Programming Light Sensor
 
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

Re: Programming Light Sensor
 
To debug the light sensors:
  1. Make sure the light sensors are powered (i.e. any of the LEDs are lit -green, yellow or orange).
  2. 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.
  3. 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.
  4. 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

Re: Programming Light Sensor
 
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

Re: Programming Light Sensor
 
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.
Code:

#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".


All times are GMT -5. The time now is 17:43.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi