Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Java (http://www.chiefdelphi.com/forums/forumdisplay.php?f=184)
-   -   Updating Speed Using Ultrasonic Sensors (http://www.chiefdelphi.com/forums/showthread.php?t=136070)

DeeWBee 24-03-2015 13:16

Updating Speed Using Ultrasonic Sensors
 
I am trying to create code that will slow down the robot as it gets closer to an object.

So far I have been able to write code that will stop the robot using an "if" statement with the conditional being the range as measured by sonar.


Code:

if(sonar.getRangeInches() < 40) {
            motor.stop();

However, when I modify the code such that I want to change the speed on approach, I get "robotDrive...output not updated enough". Any ideas? I've gotten the code to run by disabling the motor safety, but the sensitivity doesn't change at all.


Code:

if(sonar.getRangeInches() < 40) {
            driveTrain.setSensitivity(0.5)
          elseif(sonar.....<30){
            driveTrain.setSensitivty(0.25)......


otherguy 24-03-2015 14:23

Re: Updating Speed Using Ultrasonic Sensors
 
* Put your robot on blocks so it wont drive.
* Add print statements to your code that lists the distance and motor output value for each loop itteration.
* Run your code while moving an object in front of the ultrasonic sensor.

There's got to be a case where your if/else logic doesn't result in the motor.drive() method being called.


Also, why are you using the setSensitiviy method, that affects turning not rate of travel.
See: http://team2168.org/javadoc/edu/wpi/...obotDrive.html

JacobD 24-03-2015 19:41

Re: Updating Speed Using Ultrasonic Sensors
 
You can also do something like this. It starts a linear deceleration of the magnitude for mecanum drive. Just change out the numbers and the drive base and it will be fine.

Code:

                    double counter, startDecelDistance, stopDistance, differenceInDistance;
                   
                    startDecelDistance = 20;
                    stopDistance = 40;
                   
                    differenceInDistance = Math.abs(stopDistance-startDecelDistance);
                   
                    while(sonar.getRangeInches() < startDecelDistance && sonar.getRangeInches() > stopDistance) {
                            counter= Math.abs(startDecelDistance - sonar.getRangeInches()) / differenceInDistance;
                            myRobot.mecanumDrivePolar(counter, 0, 0);
                    }
                            myRobot.mecanumDrivePolar(0,0,0);


Djur 29-03-2015 14:43

Re: Updating Speed Using Ultrasonic Sensors
 
If you're using CammandBased java, you should take a look at Gearsbot. It uses PID control to move to a certain distance away from an object and stop, which seems to be exactly what you're trying here.

GeeTwo 29-03-2015 15:00

Re: Updating Speed Using Ultrasonic Sensors
 
You might be better off overriding the set method of your motor controller. If you do it there, you only have to do it once, and not worry about it anywhere else.

You may also want to only reduce the throttle if the applied voltage will push you towards the wall, but allow full throttle to push you away from the wall.

engunneer 29-03-2015 15:32

Re: Updating Speed Using Ultrasonic Sensors
 
In addition to everyone else's suggestions, the problem you are having is mostly from the structure of your if statement
Code:

if(sonar.getRangeInches() < 40) {
            driveTrain.setSensitivity(0.5)
          elseif(sonar.....<30){
            driveTrain.setSensitivty(0.25)......

remember that if an if statement returns true, the other elses aren't always evaluated
What you wrote
Code:

If it's <40, then do something
If it's not <40 but is <30, do something else (this can't happen!)

try instead building from one direction to the other
Code:

if(sonar.getRangeInches() >= 40)
{
            driveTrain.setSensitivity(1);
}
else if(sonar.....>= 30)
{
            driveTrain.setSensitivity(0.5);
}
else /*if(sonar.....< 30)*/
{
            driveTrain.setSensitivty(0.25);
}

you can also do it mathematically (y=mx+b)
Code:

driveTrain.setSensitivty(sonar.getRangeInches() / 40)


All times are GMT -5. The time now is 10:48.

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