Introduction to Sonar

Objectives

  1. Understand the differences between analog and digital input
  2. Be able to sample the IntelliBrain Ping sensor to obtain a value
  3. Use the IntelliBrain Ping sensor to calculate distance
  4. Use the IntelliBrain Ping sensor as an input for the motors

Important Words

  • digital sensor
  • exception
  • throws
  • thread.sleep
  • comments
  • double


Before you start

Know how to create a project in Java IDE and RoboJDE.

Have basic familiarity with the IntelliBrain bot.

Complete the Introduction to IR Sensors Tutorial.



Digital Input VS Analog Input

Digital inputs operate quite differently from the analog input you used before. As previously discussed in the Introduction to IR Sensors tutorial, analog inputs work based on a continuous range of numbers. For an example of the difference, let's take a look at a clock. On an analog clock, the second, minute, and hour hands are constantly moving to show you the current time. As long as the batteries are providing electricity, the clock will be in a continuous state of motion. Now take a look at a digital clock like the one that probably sits by your bed at home. Are the numbers constantly changing? No! The numbers only change after 60 seconds of waiting. Digital input works with a discontinuous set of data much like you see with a digital clock.


You are also probably familiar with CD’s, which are a digital way to store music. However, this poses an interesting problem because music is analog - constantly changing wavelengths that produce the sounds you hear. How do we go from analog output(music) to digital input (the CD)? This is accomplished by indenting the CD with 1's(indent) and 0's(no indent) to represent the analog sounds (remember from earlier modules the concept of binary and using 1s and 0s). When we put that CD into our car stereo system, the digital input is translated back into analog output as the disc spins around inside the player and eventually output as sound we can hear.

The Sonar Sensor

The sonar sensor is positioned on the front of the robot and resembles a pair of bug-like eyes. In order for it to work properly, it must be hooked up to one of the four digital IO ports numbered 3 - 6. The setup we are using has the sensor plugged into IOPort(3). The sensor works by emitting a brief sound pulse then listening for the returning echo. The values read by the sensor are the amount of time it takes for the echo to return to the sensor. The range reads between 115 and 18500, standing for the amount of time in microseconds that it takes for the sound to bounce back.


Using the Ping Sensor

Let's make a new program to use the sonar sensor on the IntelliBrain called SonarSensor. Just like we have done before, let's begin by importing the libraries we need to be able to use the sonar and the LCD display.



	import com.ridgesoft.intellibrain.IntelliBrain;

	import com.ridgesoft.intellibrain.IntelliBrainDigitalIO;

	import com.ridgesoft.io.Display;

	

	public class SonarSensor {

	

		public static void main(String[] args)  {

	

			

		}

	}



Now we need to create the LCD and the sonar sensor for the program to use on the robot. We will also need to tell the program that we want to allow for the measurement to be allowed (this is on line 13)



	import com.ridgesoft.intellibrain.IntelliBrain;

	import com.ridgesoft.intellibrain.IntelliBrainDigitalIO;

	import com.ridgesoft.io.Display;

	

	public class SonarSensor {

	

		public static void main(String[] args) {

	

			

			Display display = IntelliBrain.getLcdDisplay();

			IntelliBrainDigitalIO ping = IntelliBrain.getDigitalIO(3);

	

			ping.enablePulseMeasurement(true);

	

			

		}

	}




In order to be able to use the sonar sensor appropriately, we need to activate the sensor using a method we imported in the library - ping.pulse(20). Because we also have to wait for the sound to travel out, hit and object and reflect, and then travel back to the sensor, we have to momentarily "pause" the program to allow for this to happen. We do this using the Thread.sleep(50) method on line 18. Let's implement that now.


	import com.ridgesoft.intellibrain.IntelliBrain;

	import com.ridgesoft.intellibrain.IntelliBrainDigitalIO;

	import com.ridgesoft.io.Display;

	

	public class SonarSensor {

	

		public static void main(String[] args) throws InterruptedException {

	

			

			Display display = IntelliBrain.getLcdDisplay();

			IntelliBrainDigitalIO ping = IntelliBrain.getDigitalIO(3);

	

			ping.enablePulseMeasurement(true);

	

				ping.pulse(20);

				Thread.sleep(50);	

			

		}

	}



Because we added the Thread.sleep(50) method to the program, this is going to cause our program to run "unexpectedly" because we forced it to pause in the middle. We have to add throws InterruptedException to the main method on line 7 in order for the program to run normally. Because we know that the program is going to bring up this issue, and we are ok with this issue because we are the ones that told the program to pause, we are telling the program that it is ok to ignore this "problem" (aka exception) and continue running normally.


The last things we need to do are create a variable to hold the distance, output the results to the LCD display, and to tell the program how long we want it to run. The variable needs to be a double type variable (which are basically just numbers that include decimal places) and again we will use a while(true) loop.



	import com.ridgesoft.intellibrain.IntelliBrain;

	import com.ridgesoft.intellibrain.IntelliBrainDigitalIO;

	import com.ridgesoft.io.Display;

	

	public class SonarSensor {

	

		public static void main(String[] args) throws InterruptedException {

	

			double distance = 0;

			Display display = IntelliBrain.getLcdDisplay();

			IntelliBrainDigitalIO ping = IntelliBrain.getDigitalIO(3);

	

			ping.enablePulseMeasurement(true);

	

			while (true) {

	

				ping.pulse(20);

				Thread.sleep(50);

	

				distance = ping.readPulseDuration();

	

				display.print(0, "Ping Value");

				display.print(1, " " + distance); 

	

			}

		}

	}



You've now built the program to use the sonar sensor on the front of the robot.



Words to know

digital
- this is a type of input that is discontinuous.
exception
- exceptions are issues that cause the program to not work properly but you can plan for them and let the program how to handle them. Think about a program that asks you to input two numbers then divides the first by the second. If you enter 0 for the second number, the program will not work because mathematically you cannot divide by 0. However, you know that someone can enter in any number and that number could be 0. You then plan accordingly and tell the program what to do if someone does enter 0 for the second number because the program will not function otherwise.
keyword
- a word in the programming language that is reserved for special tasks and will cause an error if used anywhere else.
throws
- a keyword used with exceptions, tells the program what types of exceptions/errors to expect
sleep
- this causes the program to pause for the specified amount of time (in milliseconds)


More Using the Ping Sensor


  1. What happens if you change the variable type from double to int on line X? Try it!

  2. What happens if you change true to false on line X? Try it! Why do you think it happened.

  3. What happens if you change the value on line X from 20 to a different number? Try it!

  4. What happens if you change the value on line X from 50 to a different number? Try it!

  5. Does the distance displayed on the screen make any sense (as a real distance)?

Using the Ping sensor to Calculate Distance

The value from the Ping sensor is not quite a distance, however, with one small modification we can get the distance (in inches) from the robot to whatever the sonar sensor is detecting. The following code will display the distance in inches on the LCD.


	import com.ridgesoft.intellibrain.IntelliBrain;

	import com.ridgesoft.intellibrain.IntelliBrainDigitalIO;

	import com.ridgesoft.io.Display;

	

	public class SonarSensor {

	

		public static void main(String[] args) throws InterruptedException {

	

			double distance = 0;

			Display display = IntelliBrain.getLcdDisplay();

			IntelliBrainDigitalIO ping = IntelliBrain.getDigitalIO(3);

	

			ping.enablePulseMeasurement(true);

	

			while (true) {

	

				ping.pulse(20);

				Thread.sleep(50);

	

				distance = ping.readPulseDuration() /150.0;

	

				display.print(0, "Ping Value");

				display.print(1, " " + distance); 

	

	

			}

		}

	}

More Using the Ping Sensor to Calculate Distance


  1. What is the difference between this code and the previous one?

  2. On what line does it occur?

  3. How accurate is the distance? Place an object at different distance and see what the distance it and compare to the actual distance!