Wanna hang together?
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 04-02-2017, 17:45
BetaBlues's Avatar
BetaBlues BetaBlues is offline
Registered User
FRC #5975 (Beta Blues)
Team Role: Programmer
 
Join Date: Jan 2017
Rookie Year: 2016
Location: Portland, Oregon
Posts: 13
BetaBlues is an unknown quantity at this point
Code for Ultrasonic Sensor

We have our sensor set up as digital but we are having trouble understanding how to use it. Could someone give us a run down on how to use it? Thanks
Reply With Quote
  #2   Spotlight this post!  
Unread 04-02-2017, 19:40
MuskieProgramme MuskieProgramme is offline
Registered User
FRC #6420
Team Role: Programmer
 
Join Date: Dec 2016
Rookie Year: 2014
Location: Muscatine, IA
Posts: 45
MuskieProgramme is an unknown quantity at this point
Re: Code for Ultrasonic Sensor

In general, an ultrasonic sensor gives you a number. The number corresponds to distance. You can use that distance to (for example) calculate how far the robot needs to move, then move that distance.

For more specific instructions, it might be helpful if you specified which ultrasonic sensor you are using.
Reply With Quote
  #3   Spotlight this post!  
Unread 06-02-2017, 20:31
BetaBlues's Avatar
BetaBlues BetaBlues is offline
Registered User
FRC #5975 (Beta Blues)
Team Role: Programmer
 
Join Date: Jan 2017
Rookie Year: 2016
Location: Portland, Oregon
Posts: 13
BetaBlues is an unknown quantity at this point
Re: Code for Ultrasonic Sensor

Quote:
Originally Posted by MuskieProgramme View Post
In general, an ultrasonic sensor gives you a number. The number corresponds to distance. You can use that distance to (for example) calculate how far the robot needs to move, then move that distance.

For more specific instructions, it might be helpful if you specified which ultrasonic sensor you are using.
Our ultrasonic sensor is by Rockwell Automation: HRVL-MaxSonary-EZ MB1013
Reply With Quote
  #4   Spotlight this post!  
Unread 07-02-2017, 07:23
pblankenbaker pblankenbaker is offline
Registered User
FRC #0868
 
Join Date: Feb 2012
Location: Carmel, IN, USA
Posts: 124
pblankenbaker is a glorious beacon of lightpblankenbaker is a glorious beacon of lightpblankenbaker is a glorious beacon of lightpblankenbaker is a glorious beacon of lightpblankenbaker is a glorious beacon of light
Re: Code for Ultrasonic Sensor

I'm assuming that your sensor is a MaxBotix MB1013 and that the following is the data sheet for your sensor.

http://www.maxbotix.com/documents/HR..._Datasheet.pdf

Assuming that is your sensor, I would recommend that you start with pins 3, 6 and 7 on the sensor and connect them to an analog input on the roboRIO (refer to the data sheet to determine how they should be connected). The following class could then be used to display readings from the sensor on the SmartDashboard to help you determine how the voltage output from the sensor corresponds to distance.

You will need to determine the constant (VOLTS_TO_DIST) to convert the voltage to a real world distance (if you want to work in real world units instead of voltage).

Code:
package org.usfirst.frc.team868.robot.sensors;

import edu.wpi.first.wpilibj.AnalogInput;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;

public class DistanceSensor {
  // A MB1013 distance sensor - http://www.maxbotix.com/documents/HRLV-MaxSonar-EZ_Datasheet.pdf
  // (pins 3, 6 and 7 from sensor to analog input 0)
  private static final AnalogInput mb1013 = new AnalogInput(0);
  
  // TODO - You will need to determine how to convert voltage to distance
  // (use information from the data sheet, or your own measurements)
  private static final double VOLTS_TO_DIST = 1.0;

  public static double getVoltage() {
    return mb1013.getVoltage();
  }
  
  public static double getDistance() {
    return getVoltage() * VOLTS_TO_DIST;
  }
  
  public static void updateDashboard() {
    SmartDashboard.putNumber("Distance (volts)", getVoltage());
    SmartDashboard.putNumber("Distance (real)", getDistance());
  }
}
NOTE: You will need to periodically call the Distance.updateDashboard() command to see the values on the dashboard.
Reply With Quote
  #5   Spotlight this post!  
Unread 07-02-2017, 07:28
Coach Seb's Avatar
Coach Seb Coach Seb is offline
Registered User
AKA: Sebastien Cournoyer
FRC #5860 (Full Metal Muskrats)
Team Role: Coach
 
Join Date: Sep 2015
Rookie Year: 2015
Location: Algonac, MI
Posts: 132
Coach Seb is an unknown quantity at this point
Re: Code for Ultrasonic Sensor

This is a great example, thank you!

in a command based situation, would you add this code as its own class, like a subsystem or would you add this to an existing class...?
Reply With Quote
  #6   Spotlight this post!  
Unread 07-02-2017, 08:14
pblankenbaker pblankenbaker is offline
Registered User
FRC #0868
 
Join Date: Feb 2012
Location: Carmel, IN, USA
Posts: 124
pblankenbaker is a glorious beacon of lightpblankenbaker is a glorious beacon of lightpblankenbaker is a glorious beacon of lightpblankenbaker is a glorious beacon of lightpblankenbaker is a glorious beacon of light
Re: Code for Ultrasonic Sensor

In a command based robot, I would be tempted to refactor the example into its own Subsystem.

However, this could be thought of as a special "read only" type of subsystems. Commands that made use of this subsystem would not need to require() it in their constructors as there isn't a problem if multiple commands take distance measurements at the same time. If two commands both want to get distance measurements, it isn't a problem like it is if two commands both want to manipulate drive motors.

Alternatively, I might be tempted to leave it as its own stand alone class (not make it a subsystem) and just use it as is.
Reply With Quote
  #7   Spotlight this post!  
Unread 07-02-2017, 10:19
rich2202 rich2202 is offline
Registered User
FRC #2202 (BEAST Robotics)
Team Role: Mentor
 
Join Date: Jan 2012
Rookie Year: 2012
Location: Wisconsin
Posts: 1,304
rich2202 has a reputation beyond reputerich2202 has a reputation beyond reputerich2202 has a reputation beyond reputerich2202 has a reputation beyond reputerich2202 has a reputation beyond reputerich2202 has a reputation beyond reputerich2202 has a reputation beyond reputerich2202 has a reputation beyond reputerich2202 has a reputation beyond reputerich2202 has a reputation beyond reputerich2202 has a reputation beyond repute
Re: Code for Ultrasonic Sensor

Quote:
Originally Posted by BetaBlues View Post
We have our sensor set up as digital...

Quote:
Originally Posted by pblankenbaker View Post
connect them to an analog input
Just want to make sure you saw this.

You can do digital via RS232, but it is much easier to do analog.
__________________

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 15:35.

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