|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||
|
|||
|
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".
|
|
#2
|
|||||
|
|||||
|
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 |
|
#3
|
|||
|
|||
|
Re: VEX Ultrasonic Sensor
Ok thanks I will try that I was not always having the Enable command.
|
|
#4
|
|||
|
|||
|
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.
|
|
#5
|
|||||
|
|||||
|
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;
}
}
}
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. |
|
#6
|
|||
|
|||
|
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();
}
}
}
|
|
#7
|
|||||
|
|||||
|
Re: VEX Ultrasonic Sensor
Here's your problem:
Code:
public void robotInit() {
...stuff...
ultra.setAutomaticMode(isEnabled()); <---
ultra.setEnabled(isEnabled()); <---
...other stuff...
}
Last edited by Oromus : 13-02-2015 at 14:43. |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|