Go to Post The GDC probably took bets on how many posts they could get off of a random CAD render, and bonus points for how many people go insane from thinking about it! :yikes: - gallo26 [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
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 18-02-2011, 17:45
MisterL's Avatar
MisterL MisterL is offline
Robot C Master and Mentor
AKA: Derrick Canfield
FRC #1544 (One Byte Short)
Team Role: Mentor
 
Join Date: Feb 2011
Rookie Year: 2006
Location: Alaska
Posts: 30
MisterL is an unknown quantity at this point
Autonomous Photosensor Programming

So my team wants me to program a photosensor in Java for our Autonomous code, but no matter where I look I can not find anything on any sample code that can help me. Is there anybody out there who can help me in the slightest? You help would be much appreciated
__________________
"Doing Java from scratch with no previous experience with it? Piece of cake!" Famous Last Words
One Byte Short Mentor
Reply With Quote
  #2   Spotlight this post!  
Unread 18-02-2011, 18:09
Patrickwhite's Avatar
Patrickwhite Patrickwhite is offline
May the North be with you
FRC #0610 (The Coyotes)
Team Role: Programmer
 
Join Date: Dec 2008
Rookie Year: 2008
Location: Toronto
Posts: 88
Patrickwhite is a glorious beacon of lightPatrickwhite is a glorious beacon of lightPatrickwhite is a glorious beacon of lightPatrickwhite is a glorious beacon of lightPatrickwhite is a glorious beacon of lightPatrickwhite is a glorious beacon of light
Re: Autonomous Photosensor Programming

In NetBeans, if you create a new FRC Project, and choose from the FRC Sample Projects, you can see one that is a line following project. If you create that project, you can see the example code. It's a bit hard to follow, but it works on the a few basic premises:
1) Using the driver station's digital inputs to choose whether to follow the straight or narrow, and whether to go left or right at the fork
2) Changing the motor's speed based on the time that has elapsed
3) Getting the values from the light sensors and storing it in an integer as a single binary string, i.e. 111 (7) is all sensors on, 000 (0) is all sensors off, 010 (2) is the middle sensor on, etc.
4) Running a switch statement based on the binary string to determine how much to turn.

Take a look through it, and if you have any questions, feel free to come back and ask.
__________________
while(!going.isTough());
tough.exit();

What will we do tonight, Warfa?
The same thing we do every night, Patrick. Sit and wait for Electrical.
Reply With Quote
  #3   Spotlight this post!  
Unread 18-02-2011, 19:46
Robby Unruh's Avatar
Robby Unruh Robby Unruh is offline
*insert random dial-up tone here*
FRC #3266 (Robots R Us)
Team Role: Coach
 
Join Date: Feb 2010
Rookie Year: 2010
Location: Eaton, OH
Posts: 338
Robby Unruh will become famous soon enough
Re: Autonomous Photosensor Programming

Quote:
Originally Posted by MisterL View Post
So my team wants me to program a photosensor in Java for our Autonomous code, but no matter where I look I can not find anything on any sample code that can help me. Is there anybody out there who can help me in the slightest? You help would be much appreciated
Let me begin by saying the provided sensor code is garbage! I basically ended up ripping it apart and pretty much coding my own.

The sensors are DigitalInputs, and they have a get() function that returns true if the tape is sensed, false if not. See the example below.

Code:
DigitalInput middle, right, left;
public Robot() {
    middle = new DigitalInput(1);
    right = new DigitalInput(2);
    left = new DigitalInput(3);
}
public void autonomous() {
    if(middle.get()) {
        // drive straight
    } else if(right.get()) {
        // drive left
    } else if(left.get()) {
        // drive right
    }
}
__________________
[Robots R Us #3266]
2015: Georgia Southern Classic (Winners / Thanks 1319 & 1648!), Queen City
2014: Crossroads, Queen City
2013: Buckeye, Queen City, Crossroads
2012: Buckeye, Queen City

2011: Buckeye
2010: Buckeye
Reply With Quote
  #4   Spotlight this post!  
Unread 18-02-2011, 21:26
Patrickwhite's Avatar
Patrickwhite Patrickwhite is offline
May the North be with you
FRC #0610 (The Coyotes)
Team Role: Programmer
 
Join Date: Dec 2008
Rookie Year: 2008
Location: Toronto
Posts: 88
Patrickwhite is a glorious beacon of lightPatrickwhite is a glorious beacon of lightPatrickwhite is a glorious beacon of lightPatrickwhite is a glorious beacon of lightPatrickwhite is a glorious beacon of lightPatrickwhite is a glorious beacon of light
Re: Autonomous Photosensor Programming

In the best of worlds (you starting off right on the line) that will probably keep you on the line, though in any case you'll be wobbling back and forth (and if you can't count on your orientation, good luck hanging a tube!), and if you happen to pass the line by a bit (as will likely happen) you'll just stop.
That's a really good concept to start with, but it needs more fleshing out.
__________________
while(!going.isTough());
tough.exit();

What will we do tonight, Warfa?
The same thing we do every night, Patrick. Sit and wait for Electrical.
Reply With Quote
  #5   Spotlight this post!  
Unread 19-02-2011, 16:30
MisterL's Avatar
MisterL MisterL is offline
Robot C Master and Mentor
AKA: Derrick Canfield
FRC #1544 (One Byte Short)
Team Role: Mentor
 
Join Date: Feb 2011
Rookie Year: 2006
Location: Alaska
Posts: 30
MisterL is an unknown quantity at this point
Re: Autonomous Photosensor Programming

This is all helpful, I only wish I had more expertise in Java. This is still kind of a new thing for me to learn so I am completely out of my league. Thank you for your help! If you are able to help a little more on the
if(middle.get())
concept that would help. I can't help but feel I am missing something here.
__________________
"Doing Java from scratch with no previous experience with it? Piece of cake!" Famous Last Words
One Byte Short Mentor
Reply With Quote
  #6   Spotlight this post!  
Unread 19-02-2011, 16:47
ArchVince's Avatar
ArchVince ArchVince is offline
Registered User
FRC #1290 (Si Se Puede)
Team Role: Alumni
 
Join Date: Jan 2011
Rookie Year: 2010
Location: Chandler High School
Posts: 26
ArchVince will become famous soon enough
Re: Autonomous Photosensor Programming

Quote:
Originally Posted by MisterL View Post
This is all helpful, I only wish I had more expertise in Java. This is still kind of a new thing for me to learn so I am completely out of my league. Thank you for your help! If you are able to help a little more on the
if(middle.get())
concept that would help. I can't help but feel I am missing something here.
The line sensors send true if a line is underneath and false if there is not a line underneath. The get method return that value. Basically, middle is the digital input plugged in to slot 1. if(middle.get()) checks to see if there is a line underneath that sensor. That's my impression of it, anyway. We only used line sensors temporarily, since we aren't sure if we'll even have a functioning arm to use with them.
__________________
PETA wants your help finding Schrödinger.
Reply With Quote
  #7   Spotlight this post!  
Unread 19-02-2011, 19:41
MisterL's Avatar
MisterL MisterL is offline
Robot C Master and Mentor
AKA: Derrick Canfield
FRC #1544 (One Byte Short)
Team Role: Mentor
 
Join Date: Feb 2011
Rookie Year: 2006
Location: Alaska
Posts: 30
MisterL is an unknown quantity at this point
Re: Autonomous Photosensor Programming

Well heres a new development, we only own one photosensor, so should I only have the middle part of the code? Or should I use a whole new code?
__________________
"Doing Java from scratch with no previous experience with it? Piece of cake!" Famous Last Words
One Byte Short Mentor
Reply With Quote
  #8   Spotlight this post!  
Unread 19-02-2011, 20:36
Bot190's Avatar
Bot190 Bot190 is offline
Registered User
FRC #0166 (ChopShop)
Team Role: Programmer
 
Join Date: Sep 2009
Rookie Year: 2009
Location: Merrimack NH
Posts: 105
Bot190 will become famous soon enough
Re: Autonomous Photosensor Programming

You should have gotten three of the photoelectic sensors in the Kit of Parts.
As others have said, you want to put the three line sensors in a row, spaced apart a little. Then you have to adjust them such that the LED's on the sensors change colors as you move the robot past the tape.

Then, depending on how you wired them, you will get a value from the sensor with a digitalinput. The Get method returns a value, 1 or 0 when the sensor sees the line, or doesn't, depending on how you wired them.

From there, you basically go forward while the center one is on the line, then if either side sees the tape, turn a bit until only the center sees the line.
__________________

Reply With Quote
  #9   Spotlight this post!  
Unread 20-02-2011, 15:26
Hadyn Fitzgeral Hadyn Fitzgeral is offline
Registered User
None #1234
 
Join Date: Feb 2011
Rookie Year: 2013
Location: No where
Posts: 3
Hadyn Fitzgeral is an unknown quantity at this point
Re: Autonomous Photosensor Programming

Here's my teams code that I wrote for this. We can do the Y + the straight profiles of the line. Since this is fairly outdated I am not to worried about leeching.

Code:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.freelance.logic;

import edu.wpi.first.wpilibj.Timer;
import org.freelance.sensors.Photoswitch;
import org.freelance.sys.MecanumDrive;
import org.freelance.util.Constants;

/**
 *
 * @author Freelance Robotics (Hadyn Fitzgerald)
 */
public class Linetracking {

    private boolean done = false;
    private boolean left = false;
    private int profile = 1; //1 == Y, 0 == Straight

    private Photoswitch leftSwitch;
    private Photoswitch centerSwitch;
    private Photoswitch rightSwitch;

    private final byte NONE_OPCODE = 0, LEFT_OPCODE = 1, CENTER_OPCODE = 2,
                       RIGHT_OPCODE = 4, ALL_OPCODE = 7;

    private double FORWARD = -.5, BACKWARD = .5, LEFT =  -.425, RIGHT = .425;

    private int stage = 0;

    public Linetracking() {
        initLineTracking();
    }

    private void initLineTracking() {
        /**
         * Construct Photoswitches
         */
        leftSwitch = new Photoswitch(Constants.LEFT_SLOT);
        centerSwitch = new Photoswitch(Constants.CENTER_SLOT);
        rightSwitch = new Photoswitch(Constants.RIGHT_SLOT);
    }

    public byte getType() {
        byte type = 0;

        if(leftSwitch.get())
            type += 1;

        if(centerSwitch.get())
            type += 2;

        if(rightSwitch.get())
            type += 4;

        System.out.println(type);

        return type;
    }

    public boolean process(MecanumDrive drive) {
        byte type = getType();
        switch(type) {

            case NONE_OPCODE:
                if(profile == 0) {
                    drive.controllerDrive(0, 0, 0);
                } else
                    if(stage == 0) {
                        for(int a = 0; a < 50; a++) {
                            if(left)
                                drive.controllerDrive(0, 0, -.3);
                            else
                                drive.controllerDrive(0, 0, .3);
                        }
                        while(getType() != CENTER_OPCODE) {
                            if(getType() == ALL_OPCODE)
                                break;
                            if(left)
                                drive.controllerDrive(0, LEFT, 0);
                            else
                                drive.controllerDrive(0, LEFT, 0);
                        }
                        stage++;
                    } else if(stage == 1) {
                        for(int a = 0; a < 35; a++) {
                            if(left)
                                drive.controllerDrive(0, 0, .3);
                            else
                                drive.controllerDrive(0, 0, -.3);
                        }
                        done = true;
                    } else {
                        drive.controllerDrive(0, 0, 0);
                    }
                break;

            case LEFT_OPCODE:
                while(getType() != CENTER_OPCODE) {
                    if(getType() == NONE_OPCODE || getType() == ALL_OPCODE)
                        break;
                    drive.controllerDrive(0, RIGHT, 0);
                }
                break;

            case CENTER_OPCODE:
                drive.controllerDrive(FORWARD, 0, 0);
                break;

            case RIGHT_OPCODE:
                while(getType() != CENTER_OPCODE) {
                    if(getType() == NONE_OPCODE || getType() == ALL_OPCODE)
                        break;
                    drive.controllerDrive(0, LEFT, 0);
                }
                break;

            case ALL_OPCODE:
                drive.controllerDrive(0, 0, 0);
                if(profile == 0) {
                    done = true;
                }
                break;
        }
        return done;
    }

    public void reset() {
        stage = 0;
    }
}
Reply With Quote
  #10   Spotlight this post!  
Unread 20-02-2011, 20:17
Robby Unruh's Avatar
Robby Unruh Robby Unruh is offline
*insert random dial-up tone here*
FRC #3266 (Robots R Us)
Team Role: Coach
 
Join Date: Feb 2010
Rookie Year: 2010
Location: Eaton, OH
Posts: 338
Robby Unruh will become famous soon enough
Re: Autonomous Photosensor Programming

How are you guys programming your Y's? I'm having some trouble with mine. Any tips would be really appreciated.
__________________
[Robots R Us #3266]
2015: Georgia Southern Classic (Winners / Thanks 1319 & 1648!), Queen City
2014: Crossroads, Queen City
2013: Buckeye, Queen City, Crossroads
2012: Buckeye, Queen City

2011: Buckeye
2010: Buckeye
Reply With Quote
  #11   Spotlight this post!  
Unread 20-02-2011, 21:21
Patrickwhite's Avatar
Patrickwhite Patrickwhite is offline
May the North be with you
FRC #0610 (The Coyotes)
Team Role: Programmer
 
Join Date: Dec 2008
Rookie Year: 2008
Location: Toronto
Posts: 88
Patrickwhite is a glorious beacon of lightPatrickwhite is a glorious beacon of lightPatrickwhite is a glorious beacon of lightPatrickwhite is a glorious beacon of lightPatrickwhite is a glorious beacon of lightPatrickwhite is a glorious beacon of light
Re: Autonomous Photosensor Programming

Quote:
Originally Posted by Robby Unruh View Post
How are you guys programming your Y's? I'm having some trouble with mine. Any tips would be really appreciated.
Our robot drives straight on the line, so it notices when the middle sensor is no longer over the tape, but the left and right both are. At that point, we begin turning in the direction we want to go. Using the code you've already posted as a template:
Code:
if(left.get() && !middle.get() && right.get()) {
        // turn left or right, depending on what you want
    }
It wouldn't go on as another else, it would be a separate if, or it would never be triggered.
__________________
while(!going.isTough());
tough.exit();

What will we do tonight, Warfa?
The same thing we do every night, Patrick. Sit and wait for Electrical.

Last edited by Patrickwhite : 20-02-2011 at 21:29. Reason: forgot to write the point of the post
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 22:18.

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