Changes between Version 12 and Version 13 of ControlSystems/SoftwareTeam/Training/GettingStarted/UltrasonicRangefinder


Ignore:
Timestamp:
Mar 17, 2021, 12:42:08 PM (4 years ago)
Author:
Angelina Zhou
Comment:

changed f_ultrasonic.setAutomaticMode(true); to Ultrasonic.setAutomaticMode(true);

Legend:

Unmodified
Added
Removed
Modified
  • ControlSystems/SoftwareTeam/Training/GettingStarted/UltrasonicRangefinder

    v12 v13  
    1 == Ultrasonic Rangefinder
    2 
     1== Ultrasonic Rangefinder ==
    32Robots need to sense the environment around them, particularly when driving autonomously.  Ultrasonic sensors are like the sonar used by bats.  [[Image(UltrasonicSensor.jpg,right,250px,margin=10)]] They emit a chirp of sound and measure the time it takes to hear an echo of that chirp.  The longer the time, the further the sound traveled.  Since sound travels at a particular speed (about 1125 ft/s) through air, we can use the echo delay to calculate distance to the object was that reflected the sound.  You can read more about ultrasonic sensors on [https://wpilib.screenstepslive.com/s/currentCS/m/java/l/599715-ultrasonic-sensors-measuring-robot-distance-to-a-surface ScreenStepsLive]
    43
    54Ultrasonic sensors are connected to the RoboRIO using DIO ports.  In the example below, a sensor's !Ping/Trig and Echo wires are connected to DIO8 and DIO9 respectively.
    65
    7 Create another program using the !TimedRobot java template and name it !UltrasonicTest. Modify the auto-generated template code as follows:
    8 * import the [https://first.wpi.edu/FRC/roborio/beta/docs/java/edu/wpi/first/wpilibj/Ultrasonic.html Ultrasonic] class
    9 {{{
     6Create another program using the !TimedRobot java template and name it !UltrasonicTest. Modify the auto-generated template code in Robot.java as follows:
     7
     8 * import the [https://first.wpi.edu/FRC/roborio/beta/docs/java/edu/wpi/first/wpilibj/Ultrasonic.html Ultrasonic] class
     9{{{
    1010   import edu.wpi.first.wpilibj.Ultrasonic;
    1111}}}
    12 * declare a ultrasonic variable in the Robot class
    13 {{{ 
     12 * declare a ultrasonic variable in the Robot class
     13{{{
    1414   private Ultrasonic f_ultrasonic;
    1515}}}
    16 * in robotInit() instantiate an ultrasonic object and set it to start automatically ranging
    17   {{{
     16 * in robotInit() instantiate an ultrasonic object and set it to start automatically ranging
     17{{{
    1818    // Sensor is connected to DIO8 (trig), DIO9 (echo)
    1919    f_ultrasonic = new Ultrasonic(8,9);
    2020    // Start sensor continuously pinging
    21     f_ultrasonic.setAutomaticMode(true);
    22   }}}
    23 * in robotPeriodic() read and display the range
    24   {{{
     21    Ultrasonic.setAutomaticMode(true);
     22}}}
     23 * in robotPeriodic() read and display the range
     24{{{
    2525    if (f_ultrasonic.isRangeValid()) {
    2626       SmartDashboard.putNumber("Front range", f_ultrasonic.getRangeInches());
    2727    }
    28   }}}
     28}}}
    2929
    3030Build and Deploy the program to your robot as you did in the [wiki:ControlSystems/SoftwareTeam/Training/GettingStarted/XboxController previous example] and observe the "Front range" value in the [https://wpilib.screenstepslive.com/s/currentCS/m/java/l/599724-displaying-data-on-the-ds-dashboard-overview Smart Dashboard] as you move the robot towards and away from the wall.