Changes between Initial Version and Version 1 of ControlSystems/Sensors/Ultrasonic


Ignore:
Timestamp:
Oct 11, 2018, 12:59:18 PM (7 years ago)
Author:
David Albert
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • ControlSystems/Sensors/Ultrasonic

    v1 v1  
     1Inexpensive HC-SR04 ultrasonic sensors [[Image(https://www.robotshop.com/media/catalog/product/cache/image/625x625/9df78eab33525d08d6e5fb8d27136e95/h/c/hc-sr04-ultrasonic-range-finder-2.png,240px,right)]] are helpful for learning about sensors and making basic robots more interactive.  Every team should have a box of sensors and cables ready to connect to a roboRIO.  The sensors are cheap and plentiful; the main obstacle is usually cabling and although students can crimp nice new cables with locking connectors, for many purposes, it is quicker and easier to make cables using off-the-shelf materials; it takes only a few minutes to wire them as needed and this is something rookie students can do without special training and minimal supervision to get comfortable with wiring.  It also avoids most of the hassle associated with poor crimps.
     2
     3Materials Needed:
     4
     5    - One HC-SR04 sensor (cost: [https://www.aliexpress.com/item/Free-shipping-1pcs-Ultrasonic-Module-HC-SR04-Distance-Measuring-Transducer-Sensor-for-Arduino-Samples-Best-prices/690139020.html $0.70] - [https://www.amazon.com/Ultrasonic-LGDehome-Measuring-Distance-Transmitter/dp/B07558LH2P $1.50])
     6    - One 4-pin female-to-female dupont jumper wires  (cost: [https://www.aliexpress.com/item/10PCS-LOT-2P-3P-5P-6P-7P-8P-9P-10P-Pin-Female-To-Female-Connector-with/32850724255.html $0.38] - [https://www.amazon.com/gp/product/B00KWA1UWK $1.20])
     7    - Two 3-pin female dupont shell/housings (less than [https://www.aliexpress.com/item/For-dupont-connector-6pin-single-row-2-54mm-for-dupont-plastic-shell-through-hole-100pcs-lot/32637991458.html $0.02] each or from an [https://www.amazon.com/100pcs-Dupont-Jumper-Housing-Connector/dp/B07H19SX83 amazon/US supplier])
     8
     9Everything is available from Amazon, [https://www.aliexpress.com AliExpress], Gearbest, Banggood, etc. depending on how fast you need it.
     10Instructions for students:
     11
     12    Using [https://www.amazon.com/Marrywindix-Precision-Anti-static-Electronics-Jewelry-making/dp/B00DVIEJ14 sharp tweezers] or the tip of a pointy knife or small screwdriver, remove the two black plastic shell/housings from the 4-wire cable: slightly lift the black tabs that retain each wire in the connector and slide the wires out so you're left with a 4-wire bundle and two 4-pin shells.  Watch [https://www.youtube.com/watch?v=-InoAbkNVdQ this video] to see how it's done.
     13    1. Reinsert the wires from one end of the 4-wire bundle into one of the 4-pin shells in this order: Red(Vcc), White(Trig), Yellow(Echo), Black(Gnd).
     14    2. Reinsert 3 of the 4 wires on the other end of the bundle into a 3-pin shell in this order: Black (Gnd), Red (5v), Yellow (S) and label it A
     15    3. Reinsert the last wire into one end of another 3-pin shell: White (S), Empty, Empty and label it B
     16    4. You can discard or save the left-over 4-pin shell housing.
     17
     18Use the new cable to connect the ultrasonic sensor to the RoboRIO as follows:
     19
     20    1. Plug the 4-pin connector into the HC-SR04 with the Red wire on the Vcc pin and the Black wire on the Gnd pin - this is important because the sensor will be destroyed if you connect it backwards.
     21    2. Plug connector A into one of the DIO ports (let's say DIO 0) with the black wire on the OUTSIDE edge of the roboRIO
     22    3. Plug connector B into another DIO port (let's say DIO 1)  with the white wire towards the INSIDE edge of the roboRIO
     23
     24You can then use the ultrasonic sensor in your programs by just creating an Ultrasonic class (everything is already done for you by WPILib).  A sample snippet is below:
     25{{{
     26import edu.wpi.first.wpilibj.Ultrasonic;
     27
     28public class Robot extends IterativeRobot {
     29  private Ultrasonic ultrasonic;
     30  @Override
     31  public void robotInit() {
     32    ultrasonic = new Ultrasonic(1,0); // trig, echo
     33    ultrasonic.setAutomaticMode(true);
     34    ultrasonic.setEnabled(true);
     35  }
     36
     37  @Override
     38  public void teleopPeriodic() {
     39    double range = ultrasonic.getRangeInches();
     40    // do something with the range
     41  }
     42}
     43}}}
     44
     45Students should first get comfortable using the sensor and displaying the range readings on the console, a good next exercise is to add collision avoidance to a robot.