Go to Post At this time of the season, there is only one response: BAAAAAAAAA-humbug! - dlavery [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 15-01-2015, 21:16
N00bfirst N00bfirst is offline
Registered User
FRC #4992
 
Join Date: Dec 2013
Location: Milton
Posts: 8
N00bfirst is an unknown quantity at this point
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.
Reply With Quote
  #2   Spotlight this post!  
Unread 15-01-2015, 21:23
AlexBrinister AlexBrinister is offline
Registered User
AKA: Alex Brinister
FRC #1768 (RoboChiefs)
Team Role: Alumni
 
Join Date: Jan 2013
Rookie Year: 2012
Location: Bolton, MA
Posts: 93
AlexBrinister will become famous soon enough
Re: A relatively dumb question...

Could you post your code?

Alex Brinister
Reply With Quote
  #3   Spotlight this post!  
Unread 15-01-2015, 21:26
TFleig78's Avatar
TFleig78 TFleig78 is offline
Registered User
AKA: Tyler
FRC #0078 (Air Strike)
Team Role: Programmer
 
Join Date: Jan 2013
Rookie Year: 2012
Location: Rhode Island
Posts: 58
TFleig78 will become famous soon enoughTFleig78 will become famous soon enough
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.
Reply With Quote
  #4   Spotlight this post!  
Unread 16-01-2015, 10:03
N00bfirst N00bfirst is offline
Registered User
FRC #4992
 
Join Date: Dec 2013
Location: Milton
Posts: 8
N00bfirst is an unknown quantity at this point
Re: A relatively dumb question...

Quote:
Originally Posted by TFleig78 View Post
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);
}
Reply With Quote
  #5   Spotlight this post!  
Unread 16-01-2015, 10:53
catacon catacon is offline
Registered User
FRC #1444 (Lightning Lancers)
Team Role: Mentor
 
Join Date: Jan 2009
Rookie Year: 2006
Location: St. Louis
Posts: 154
catacon is a splendid one to beholdcatacon is a splendid one to beholdcatacon is a splendid one to beholdcatacon is a splendid one to beholdcatacon is a splendid one to beholdcatacon is a splendid one to beholdcatacon is a splendid one to behold
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.

Last edited by catacon : 16-01-2015 at 11:01.
Reply With Quote
  #6   Spotlight this post!  
Unread 16-01-2015, 11:11
TFleig78's Avatar
TFleig78 TFleig78 is offline
Registered User
AKA: Tyler
FRC #0078 (Air Strike)
Team Role: Programmer
 
Join Date: Jan 2013
Rookie Year: 2012
Location: Rhode Island
Posts: 58
TFleig78 will become famous soon enoughTFleig78 will become famous soon enough
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()
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 08:38.

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