Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Java (http://www.chiefdelphi.com/forums/forumdisplay.php?f=184)
-   -   A relatively dumb question... (http://www.chiefdelphi.com/forums/showthread.php?t=132973)

N00bfirst 15-01-2015 21:16

A relatively dumb question...
 
How do I program an ultrasonic sensor with an analog input and convert it into digital so I can use it for programming?

Let's say that the ultrasonic sensor is in port # 0 in the analog input part of the controller.

How would I set up the Ultrasonic class in a way that takes the analog port as the input.

Right now, it's only giving me an error.

AlexBrinister 15-01-2015 21:23

Re: A relatively dumb question...
 
Could you post your code?

Alex Brinister

TFleig78 15-01-2015 21:26

Re: A relatively dumb question...
 
Use:

Code:

AnalogInput ultraSonic = new AnalogInput(0);
Then to get a reading:

Code:

ultrasonic.getVoltage();
Then you need to scale the voltage (0-5) to a distance, using a constant which should be in the documentation for the ultrasonic sensor.

N00bfirst 16-01-2015 10:03

Re: A relatively dumb question...
 
Quote:

Originally Posted by TFleig78 (Post 1429007)
Use:

Code:

AnalogInput ultraSonic = new AnalogInput(0);
Then to get a reading:

Code:

ultrasonic.getVoltage();
Then you need to scale the voltage (0-5) to a distance, using a constant which should be in the documentation for the ultrasonic sensor.

How would I use that for the input and output?
This is our code.

AnalogInput inUltra = new AnalogInput(0);
AnalogOutput outUltra = new AnalogOutput(0);
inUltra.getVoltage();


Ultrasonic ultra = new Ultrasonic(0,0);
ultra.setAutomaticMode(true);
double range = ultra.getRangeInches();
String what = Double.toString(range);

for(int i = 1; i < 10; i++){
SmartDashboard.putNumber(what, range);
}

catacon 16-01-2015 10:53

Re: A relatively dumb question...
 
You have to convert the voltage to a distance value. The sensor should have a spec for mV/in or mV/m or something relating voltage and distance.

For example, the MaxBotics MB1010 LV-EZ1 has a spec of 9.8mV/in with a 5V supply. So, if you read 0.392V from the sensor, this would result in a distance of 40 inches. (0.392 / 0.0098)

Some simple code you could use.

Code:

AnalogInput ultrasonic(0);

float voltage(0);
float distance(0);
const float volts_to_inches(0.0098);

voltage = ultrasonic.GetVoltage();
distance = voltage / volts_to_inches;

These sensors tend to be pretty noisy, so I would suggest a simple low pass filter.

Code:


const float filter_alpha = 0.5;

voltage = ultrasonic.GetVoltage();
distance = distance + filter_alpha * ((voltage / volts_to_inches) - distance);



Hope that helps!


EDIT: Didn't realize this was in the Java forum and I wrote C++. :) Should still give you an idea of what to do.

TFleig78 16-01-2015 11:11

Re: A relatively dumb question...
 
What model ultrasonic sensor do you have? We use a Maxbotix sensor and there is an analog output pin. We don't need an analog output, just analog in.

Also, you can also use this method to reduce noise.

Code:

ultraSonic.getAverageVoltage()


All times are GMT -5. The time now is 03:41.

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