Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 10 Sensors.

Similar presentations


Presentation on theme: "Lecture 10 Sensors."— Presentation transcript:

1 Lecture 10 Sensors

2 Objectives To learn the modes and interface for the remaining sensors.
Touch sensor Gyro sensor Ultrasonic sensor Infrared sensor

3 Sensor API Setting up any of the sensors is similar to setting up the color sensor. Create an object for the sensor you are using. Create a SampleProvider object. Create an array to store the resulting sample. Call the fetchSample method from the SampleProvider to collect a sample. The differences are The type of the object to be created. The number of modes and their descriptions. The sample size and interpretation for each mode.

4 Touch Sensor The touch sensor is the simplest sensor – it is essentially a button. It only has one mode called “Touch”. The result of each sample is one floating point number. The value will be 0.0 if the button is not pushed and 1.0 when the button is pushed.

5 Touch Sensor Demo Here is code to sample the touch sensor 20 times.
EV3TouchSensor ts = new EV3TouchSensor(SensorPort.S2); SampleProvider touch = ts.getMode("Touch"); float[] sample = new float[touch.sampleSize()]; for (int i = 0; i < 20; i++){ touch.fetchSample(sample, 0); System.out.printf("%d. %f%n", i, sample[0]); Delay.msDelay(250); }

6 Gyro Sensor The gyro sensor measures rotation around the axis shown on the top of the sensor. Counter clockwise rotation results in positive angles. Clockwise results in negative angles.

7 Gyro Sensor The gyro sensor has three modes.
Rate – the rate of change of the angle, measured in degrees per second. Angle – the size of the angle Angle and Rate – returns both the angle and the rate (sample size is 2 floating point numbers).

8 Gyro Sensor The angles are measured for a zero angle that is set when the program initializes. This angle can be reset – making the current angle the zero point. It is a good idea to do this at the beginning of the program. The robot is often bumped or moved right at the beginning of a program. An EV3GyroSensor object has a reset method which is use to do this.

9 Gyro Sensor Demo The following is a program that assumes the following: Large motors are plugged into ports B and C Motor B is on the left and motor C is on the right. The gyro sensor is plugged into port 3. The program causes the robot to turn right to approximately 90 degrees (-90o). Note the advantages of using the rotation sensor. It is easier with no trial and error. If the robot is bumped or the wheels slip it is still possible to make the correct turn.

10 Gyro Sensor Demo import lejos.hardware.motor.*; import lejos.hardware.port.MotorPort; import lejos.hardware.port.SensorPort; import lejos.hardware.sensor.EV3GyroSensor; import lejos.robotics.SampleProvider; public class EV3TurnRight { public static void main(String[] args){ EV3LargeRegulatedMotor b = new EV3LargeRegulatedMotor(MotorPort.B); EV3LargeRegulatedMotor c = new EV3LargeRegulatedMotor(MotorPort.C); EV3GyroSensor gs = new EV3GyroSensor(SensorPort.S3); SampleProvider angle = gs.getMode("Angle"); float[] sample = new float[angle.sampleSize()];

11 Gyro Sensor Demo b.setSpeed(200); c.setSpeed(200); b.forward(); gs.reset(); angle.fetchSample(sample, 0); while(sample[0] > -90.0) b.stop(); gs.close(); b.close(); c.close(); }

12 Ultrasonic Sensor The ultrasonic sensor uses echolocation to determine its distance to objects. The sensor has two modes. Distance – measure the distance to an object in meters. Listen – listen for other ultrasonic sensors; 1 indicates the presence of an active ultrasonic sensor.

13 Ultrasonic Sensor Demo
The following program uses the ultrasonic sensor plugged into port 4 and the two large motors plugged into ports B and C. The program will use the ultrasonic sensor to spin the robot when an object is NOT within 10 cm of the sensor. Start the robot with an object in front of the sensor – remove the object and watch it spin.

14 Ultrasonic Sensor Demo
import lejos.hardware.motor.*; import lejos.hardware.port.MotorPort; import lejos.hardware.port.SensorPort; import lejos.hardware.sensor.EV3UltrasonicSensor; import lejos.robotics.SampleProvider; import lejos.utility.Delay; public class EV3Spin2 { public static void main(String[] args){ EV3LargeRegulatedMotor b = new EV3LargeRegulatedMotor(MotorPort.B); EV3LargeRegulatedMotor c = new EV3LargeRegulatedMotor(MotorPort.C); EV3UltrasonicSensor us = new EV3UltrasonicSensor(SensorPort.S4); SampleProvider distance = us.getMode("Distance"); float[] meters = new float[distance.sampleSize()];

15 Ultrasonic Sensor Demo
distance.fetchSample(meters, 0); while(meters[0] < 0.1) b.forward(); Delay.msDelay(2000); b.stop(); us.close(); b.close(); c.close(); } Notice that the only thing the while loop does is repeatedly query the ultrasonic sensor for distance measurements.

16 Infrared Sensor The infrared sensor has two modes.
Distance Seek In some ways the infrared sensor in distance mode is similar to the ultrasonic sensor in that it can estimate the distance to an object. The distance estimate is very rough and only works at close range (approximately 10 cm). The return value is in cm rounded to the nearest integer. If no object is detected the distance returned is infinity.

17 Infrared Beacon In the seek mode the IR sensor detects an infrared beacon. The beacon comes with the home edition. The beacon can send out a constant signal that can be located with the IR sensor. The beacon can also be used like a remote control to send messages that can be received by the IR sensor. The beacon can broadcast on one of 4 channels.

18 Infrared Sensor Seek Mode
The sample size in seek mode is 8. It can detect 4 beacons simultaneously – one broadcasting on each channel. It measures the angle (in degrees) and the distance (in cm) to the beacon. The angles are measured from -25o to 25o with values increasing clockwise in the point-of-view of the sensor. An angle of 0 indicates the beacon is straight ahead. The format for the sample is [angle 1, distance 1, angle 2, distance 2, angle 3, distance 3, angle 4, distance 4] If no beacon is detected the distance is set to infinity.

19 getCommand Method The IR sensor can also receive commands from a beacon. int getRemoteCommand(int chan) chan is the channel of the beacon (1-4). The return values are as follows: TOP-LEFT BOTTOM-LEFT TOP-RIGHT BOTTOM-RIGHT TOP-LEFT + TOP-RIGHT TOP-LEFT + BOTTOM-RIGHT BOTTOM-LEFT + TOP-RIGHT BOTTOM-LEFT + BOTTOM-RIGHT CENTRE/BEACON BOTTOM-LEFT + TOP-LEFT TOP-RIGHT + BOTTOM-RIGHT

20 Homework Write a program which will have the robot drive up to a wall and stop when it gets within 5 cm. Modify your program to have the robot turn 90 when it gets within 5 cm of an obstruction. The gyro sensor might be helpful here. Write a program that attempts to solve a maze (made from 2x4 lumber) without touching the walls.


Download ppt "Lecture 10 Sensors."

Similar presentations


Ads by Google