Go to Post "But from a real life standpoint, that looks like something the evil villains in a superhero movie would use to get around in" Reading the phrases "real life" and "evil villains in a superhero movie" in the same sentence makes me giggle. - Alan Anderson [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 13-02-2015, 10:38
nfusselman1 nfusselman1 is offline
Registered User
FRC #4076
 
Join Date: Jan 2015
Location: Justin TX
Posts: 26
nfusselman1 is an unknown quantity at this point
VEX Ultrasonic Sensor

I have been trying to use this ultrasonic sensor for our robot but it never works. I am wondering if any one could help me out with the code to run this sensor using Ultrasonic Output in DIO "0" and Ultrasonic Input in DIO "1".
Reply With Quote
  #2   Spotlight this post!  
Unread 13-02-2015, 10:48
Oromus's Avatar
Oromus Oromus is offline
Lead Programmer, Community Liason
AKA: Ryan
FRC #1902 (Exploding Bacon)
Team Role: Programmer
 
Join Date: Jan 2015
Rookie Year: 2013
Location: Florida
Posts: 82
Oromus is a splendid one to beholdOromus is a splendid one to beholdOromus is a splendid one to beholdOromus is a splendid one to beholdOromus is a splendid one to beholdOromus is a splendid one to behold
Re: VEX Ultrasonic Sensor

You may want to take a look at the WPILib Java documentation on Ultrasonic sensors. I personally haven't used them, but it looks like (based off of the Javadocs) that you code should look similiar to this:

Code:
//Setting up the sensor
Ultrasonic ultra = new Ultrasonic(0, 1); //Make a new Ultrasonic Sensor on "pingChannel" 0 and "echoChannel" 1.
ultra.setAutomaticMode(true); //If you don't want to have to call ultra.ping() whenever you want to check for values
ultra.setEnabled(true); //Enable the Ultrasonic sensor

//Whenever you want to read values:
double millis = ultra.getRangeMM(); //Get distance in millimeters
double inches = ultra.getRangeInches(); //Get distance in inches
Reply With Quote
  #3   Spotlight this post!  
Unread 13-02-2015, 10:53
nfusselman1 nfusselman1 is offline
Registered User
FRC #4076
 
Join Date: Jan 2015
Location: Justin TX
Posts: 26
nfusselman1 is an unknown quantity at this point
Re: VEX Ultrasonic Sensor

Ok thanks I will try that I was not always having the Enable command.
Reply With Quote
  #4   Spotlight this post!  
Unread 13-02-2015, 10:59
nfusselman1 nfusselman1 is offline
Registered User
FRC #4076
 
Join Date: Jan 2015
Location: Justin TX
Posts: 26
nfusselman1 is an unknown quantity at this point
Re: VEX Ultrasonic Sensor

Because you seem to be good at code and we are a 2 year team and have never used java. what would the code be like the track the totes with a HD Microsoft Webcam and make the robot center up with the totes and drive up to them till the ultrasonic sensor says like 6 inches.
Reply With Quote
  #5   Spotlight this post!  
Unread 13-02-2015, 11:39
Oromus's Avatar
Oromus Oromus is offline
Lead Programmer, Community Liason
AKA: Ryan
FRC #1902 (Exploding Bacon)
Team Role: Programmer
 
Join Date: Jan 2015
Rookie Year: 2013
Location: Florida
Posts: 82
Oromus is a splendid one to beholdOromus is a splendid one to beholdOromus is a splendid one to beholdOromus is a splendid one to beholdOromus is a splendid one to beholdOromus is a splendid one to behold
Re: VEX Ultrasonic Sensor

I can't help you very much with the vision tracking part of the code due to never EVER working with the darn stuff, but I can help with the 6 inch spacing part.

Code:
//After you've found the stack and lined up
double kP = .2
double minSpeed = 0.3;
double maxSpeed = 0.5;
distance = ultra.getRangeInches();
if (distance != 6) {
     while (true) {
          distance = 6 - ultra.getRangeInches(); //Update the distance from our goal (6 inches).
          double p = distance * kP; //Calculate p, which is the speed we want to move at.
          
          //Making sure p is within maxSpeed and minSpeed and correcting it if it isn't.
          if (Math.abs(p) > maxSpeed) {
               p = maxSpeed * (Math.abs(p)/p); //p equals maxSpeed multiplied by the sign of p
          } else if (Math.abs(p) < minSpeed) {
               p = 0; //We're moving too slow, so we're saying we've stopped and hit our goal.
          }

          //Set our motors to p.
          motor1.set(p);
          motor2.set(p);

         //If we're not moving, we've made it to our goal and want to exit this loop.
          if (p == 0) {
               break;
          }
     }
}
Now let me explain what this is and how it works. This is a very simple P loop. How P loops work is a speed is calculated based off of how far you are away from your target. The farther you are away, the faster you move. The speed is calculated by multiplying distance by kP, which you'll need to fine-tune the value of to get it working well. The lower kP is, the more speed scaling you'll see (i.e. the robot slowing down much more as it approaches 6 inches). You'll also want to adjust maxSpeed and minSpeed. maxSpeed is the fastest the robot is allowed to go, and minSpeed is how slow the robot can be going before we say we're arrived at our destination.

This may be all you need, or you may need to expand on this and make it a PI, PD, or PID loop. If you have any issues with this, feel free to ask. I'm happy to help

Last edited by Oromus : 13-02-2015 at 11:46.
Reply With Quote
  #6   Spotlight this post!  
Unread 13-02-2015, 13:46
nfusselman1 nfusselman1 is offline
Registered User
FRC #4076
 
Join Date: Jan 2015
Location: Justin TX
Posts: 26
nfusselman1 is an unknown quantity at this point
Re: VEX Ultrasonic Sensor

Here is my current code that is not working for some reason.

Code:
package org.usfirst.frc.team4076.robot;

import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.SampleRobot;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.Ultrasonic;

public class Robot extends SampleRobot {

    RobotDrive myRobot;
    Joystick stick;
    Ultrasonic ultra;

    

    public void robotInit() {
        myRobot = new RobotDrive(0, 1);
        myRobot.setExpiration(0.1);
        stick = new Joystick(0);
        ultra = new Ultrasonic(0, 1);
        ultra.setAutomaticMode(isEnabled());
        ultra.setEnabled(isEnabled());
    }
	public void operatorControl() {
        while (isOperatorControl() && isEnabled()) {
        	myRobot.arcadeDrive(stick);
            while (stick.getRawButton(4)) {
            	myRobot.tankDrive(1, -1);
            	Timer.delay(0.005);
            }
            while (stick.getRawButton(5)) {
            	myRobot.tankDrive(-1, 1);
            	Timer.delay(0.005);
            }
            Timer.delay(0.005);
        }
    }
	public void Test() {
		ultra.getTable();
		while (isTest() && isEnabled()) {
			myRobot.arcadeDrive(stick);
			ultra.updateTable();
		}
	}
}
Reply With Quote
  #7   Spotlight this post!  
Unread 13-02-2015, 14:38
Oromus's Avatar
Oromus Oromus is offline
Lead Programmer, Community Liason
AKA: Ryan
FRC #1902 (Exploding Bacon)
Team Role: Programmer
 
Join Date: Jan 2015
Rookie Year: 2013
Location: Florida
Posts: 82
Oromus is a splendid one to beholdOromus is a splendid one to beholdOromus is a splendid one to beholdOromus is a splendid one to beholdOromus is a splendid one to beholdOromus is a splendid one to behold
Re: VEX Ultrasonic Sensor

Here's your problem:

Code:
public void robotInit() {
        ...stuff...
        ultra.setAutomaticMode(isEnabled()); <---
        ultra.setEnabled(isEnabled()); <---
        ...other stuff...
}
When robotInit is run, the robot is disabled. That means that isEnabled() will return false, which means your ultrasonic sensor is never turned on, along with automatic mode being set to false. I'd suggest just changing the lines to ultra.setEnabled(true) and ultra.setAutomaticMode(true).

Last edited by Oromus : 13-02-2015 at 14:43.
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 09:11.

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