|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||
|
|||
|
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??? |
|
#2
|
||||
|
||||
|
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(); |
|
#3
|
||||
|
||||
|
Re: Programming Light Sensor
Quote:
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) {}
Code:
if (LightWhite->Get()) {}
Strictly speaking, Code:
if (LightWhite->Get()) Code:
if (LightWhite->Get() != 0) |
|
#4
|
|||
|
|||
|
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!
|
|
#5
|
||||
|
||||
|
Re: Programming Light Sensor
To debug the light sensors:
|
|
#6
|
||||
|
||||
|
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.
|
|
#7
|
||||
|
||||
|
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);
}
}
};
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|