Go to Post I'll just be happy if they give us a great game. - Koko Ed [more]
Home
Go Back   Chief Delphi > Technical > Programming > C/C++
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 17-02-2011, 20:07
dratewka2 dratewka2 is offline
Registered User
FRC #2625
 
Join Date: Jan 2011
Location: Missisauga
Posts: 11
dratewka2 is an unknown quantity at this point
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???
Reply With Quote
  #2   Spotlight this post!  
Unread 17-02-2011, 20:24
speceottar's Avatar
speceottar speceottar is offline
Registered User
FRC #1504
 
Join Date: Jan 2010
Location: Michigan
Posts: 5
speceottar is an unknown quantity at this point
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();
Reply With Quote
  #3   Spotlight this post!  
Unread 20-02-2011, 01:15
garyk garyk is offline
Programming Mentor: 668, 972, 2643
FRC #0668 (Apes of Wrath)
Team Role: Mentor
 
Join Date: Dec 2006
Rookie Year: 2005
Location: Santa Clara (Silicon Valley) Calif.
Posts: 93
garyk is a jewel in the roughgaryk is a jewel in the roughgaryk is a jewel in the roughgaryk is a jewel in the rough
Re: Programming Light Sensor

Quote:
Originally Posted by dratewka2 View Post
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.
__________________

Silicon Valley Regional 2005, 2006 972
Silicon Valley Regional 2007 668 Xerox Creativity Award
Championship Event 2007 668
Portland Regional 2008 668
Silicon Valley Regional 2008 668, 972
Beta Test Team 2008 668 (with 100 & 254)
Silicon Valley Regional 2009 668 Regional Chairman's Award; 2643
Sacramento Regional 2009 668 Winning Alliance (thanks, 1717 & 2473!), 2010 Winning Alliance 3256
CalGames 2006, 2007, 2008, 2009, 2010, 2011 Field Tech
NorCal FTC Regional 2008, 2009 Inspector
Championship Event 2009
San Diego, Silicon Valley Regionals; Champ. Event 2010 668, 2643, 3256
Silicon Valley, Madera Regional 2012 2643
WRRF Programming Instructor 2006-2016
Regional Woodie Flowers Award 2014 2643 Utah Regional

Reply With Quote
  #4   Spotlight this post!  
Unread 20-02-2011, 13:01
PranavSathy PranavSathy is offline
Team Captain
AKA: Pranav Sathyanarayanan
FRC #0263 (Sachem Aftershock)
Team Role: Programmer
 
Join Date: Jan 2010
Rookie Year: 2010
Location: Holbrook, New York
Posts: 27
PranavSathy is an unknown quantity at this point
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!
Reply With Quote
  #5   Spotlight this post!  
Unread 21-02-2011, 15:30
mikets's Avatar
mikets mikets is offline
Software Engineer
FRC #0492 (Titan Robotics)
Team Role: Mentor
 
Join Date: Jan 2010
Rookie Year: 2008
Location: Bellevue, WA
Posts: 667
mikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of light
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.
__________________
Reply With Quote
  #6   Spotlight this post!  
Unread 27-01-2012, 18:25
joshkuruvilla's Avatar
joshkuruvilla joshkuruvilla is offline
Registered User
FRC #0027
 
Join Date: Jan 2012
Location: MI
Posts: 1
joshkuruvilla is an unknown quantity at this point
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.
Reply With Quote
  #7   Spotlight this post!  
Unread 27-01-2012, 19:09
mikets's Avatar
mikets mikets is offline
Software Engineer
FRC #0492 (Titan Robotics)
Team Role: Mentor
 
Join Date: Jan 2010
Rookie Year: 2008
Location: Bellevue, WA
Posts: 667
mikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of light
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".
__________________
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 15:00.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


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