Presentation is loading. Please wait.

Presentation is loading. Please wait.

Compiled from various Internet sources Presented by Mr. Hatfield

Similar presentations


Presentation on theme: "Compiled from various Internet sources Presented by Mr. Hatfield"— Presentation transcript:

1 Compiled from various Internet sources Presented by Mr. Hatfield
Robotics Week 5 Sensors

2 Previously In Robotics
We learned to use the display We learned to use the motors We learned about the sleep command We learned to make the robot move a specific distance at a specific power We learned to encapsulate code in functions

3 Motor and Sensor Setup In order to use sensor we have to set them up in ROBOTC Click on the Motor and Sensor Setup button

4 Motor Setup A new dialog box pops up with several tabs. Choose the Motors tab. Our standard setup is with motorB driving the left wheel and motorC driving the right wheel with the Large motors. Set yours up like this:

5 Sensor Setup The next tab over is the sensors tab, click it and set up the sensors like this: Select OK at the bottom of the dialog box.

6 We Pause Now To: Set up your motors and sensors

7 Back to the Code When we go back to the code we find that ROBOTC has modified the top 7 lines. Leave these alone. We can use the new names we have given the motors and sensors in our code.

8 Quickly Using Motor Names
Instead of saying motor[motorB] = pwr; // or motorC We can say: motor[left] = pwr; // or right This is more descriptive and if we change where we put our motors we don’t have to change the code.

9 Now it is Time to Learn Sensors
Sensor Types Color Ultrasonic Gyro Touch Infrared – not covered The following pages are from: Visit this site for more information.

10 Color Sensor (from legoengineering.com)
The color (or color) sensor can detect either the color or intensity of light. The color sensor has three different modes: color, reflected light intensity, and ambient light intensity. color – In this mode, the color sensor can differentiate up to seven different colors: black, blue, green, yellow, red, white, and brown. Each color is also represented by a value (see “color and light data table” below). Note: For best results, the color sensor needs to be 1-2 cm away from the color you are trying to detect, and have consistent lighting. Reflected light intensity – In this mode, the color sensor emits a red light and measures the amount reflected back into itself from the surface you are testing. The intensity of the light is measured as a percentage from 0 to 100, with 0 being very dark, and 100 being very bright. Ambient light intensity –  In this mode, the color sensor measures the amount of light in its environment, without producing its own light source. Ambient light intensity is measured as a percentage from 0 to 100, with 0 being very dark, and 100 being very bright.

11 Ultrasonic Sensor (from legoengineering.com)
The ultrasonic sensor measures distance to an object up to a maximum of 255cm (or inches) away. It does this by sending out high frequency sound waves that bounce off any object in range, and measuring how long it takes the sound to return to the sensor. In the software, you can select whether the distance is given in centimeters or inches. The ultrasonic sensor also has a “listen only” mode that can detect whether another robot is using an ultrasonic sensor nearby. In this mode, the sensor listens for signals but does not send them.

12 Gyro Sensor (from legoengineering.com)
The gyro sensor detects rotational motion in the plane indicated by the arrows on the top of the sensor housing. The sensor measures the rate of rotation in degrees per second and keeps track of the total angle of rotation in degrees. Note: When connecting the gyro sensor to your EV3 brick, you should hold it completely still in order to minimize drift. For best results, reset the angle using the reset mode of the gyro sensor block before every angle of motion you want to measure.

13 Infrared Sensor (from legoengineering.com)
The infrared sensor can measure distance or detect signals that are sent from the infrared beacon (see below). The infrared sensor can be used in three different modes: proximity, beacon, and remote. Proximity – The infrared sensor sends an infrared signal and detects the reflection of this signal by an object in front of the sensor. The strength of the reflected signal can be used to estimate the distance to the object. Its maximum range is approximately 100 cm. Beacon – In this mode, the infrared sensor can detect an infrared beacon, set to beacon mode. The infrared sensor can detect the beacon’s proximity (relative distance from the sensor) and its heading (angle from the direction the sensor is pointing). See the table below for more information. Remote – The infrared sensor can detect button presses on the IR beacon. The infrared sensor can detect which button on the remote infrared beacon is pressed. You can also detect when certain combinations of two buttons are pressed at the same time.

14 Touch Sensor The touch sensor gives your robot a sense of touch. The touch sensor detects when it is being pressed or released. It can even be programmed to wait until it is both pressed and released (we call this bumped).

15 So How Do I Use Sensors? The commands for the sensors are located in the list of text functions under ‘Sensors’ The main commands we will use will involve resetting and getting the values of sensors.

16 Give Me Code!!! (Gyro Sensor)
Getting a sensor value, there is a single command that can be used to get any sensor value: SensorValue(namedSensor) //Resets the motor encoder using the resetMotorEncoder command resetGyro(gyro); //Using our gyro setup from earlier //Run motors until the gyro reaches 90 or -90 degrees (abs value) while(abs(SensorValue(gyro)) < deg) {//move until requested deg motor[motorC] = 25; motor[motorB] = 0; //one of the motors needs to be still } motor[motorC] = 0; motor[motorB] = 0;

17 Give Me More Code!!! (Still Gyro)
To do the same with the gyro specific command. getGyroDegrees(gyro) //Resets the motor encoder using the resetMotorEncoder command resetGyro(gyro); //Using our gyro setup from earlier //Run motors until the gyro reaches 90 or -90 degrees (abs value) while(abs(getGyroDegrees(gyro)) < deg) {//move until requested deg motor[motorC] = 25; motor[motorB] = 0; //one of the motors needs to be still } motor[motorC] = 0; motor[motorB] = 0;

18 Touch Sensor This will cause the program to wait until the touch sensor is pressed. Uses getTouchValue(touch), also works with SensorValue(touch) Notice == means equals for conditions //No need to reset the touch sensor //Waits while the touch sensor reads 0, 1 means pressed while(getTouchValue(gyro) == 0) { //Do nothing here… } //Do the rest of the code here…

19 Color Sensor Let’s make the robot move until the Color Sensor senses Dark Uses getColorAmbient, works with SensorValue //Again, no need to reset a color sensor int gray = 25; //Set a middle light/dark value, depends on room //While the Color Sensor senses light color while(getColorAmbient(color) > gray) { motor[motorC] = pwr; motor[motorB] = pwr; } motor[motorC] = 0; motor[motorB] = 0;

20 Ultrasonic Sensor (US Sensor)
Let’s make the robot move until the Ultrasonic Sensor is 10cm from something Uses getUSDistance, works with SensorValue //Again, no need to reset a US sensor //While the US Sensor is > 10 cm while(getUSDistance(sonic) > 10) { motor[motorC] = pwr; motor[motorB] = pwr; } motor[motorC] = 0; motor[motorB] = 0;

21 Use these example in Functions
For Example: The Ultrasonic Sensor void moveToObject(float d, int pwr) { //Desc: moves the robot until the distance specified in ‘d’ // at a power specified by ‘pwr’ while(getUSDistance(sonic) > d) {//runs until US reads distance motor[left] = pwr; //Uses the new left motor designation motor[right] = pwr; //Uses the new right motor designation } motor[left] = 0; motor[right] = 0;

22 Different Settings The different commands used by the sensors may require different sensor settings An example would be to ‘SEE’ the color ‘RED’ the color sensor would need to be set to ‘COLOR’ in the Motor and Sensor Setup Search online for more specific information concerning setup, or Just Try It for yourself

23 Good Function Comments
When you make functions use good comments to help yourself and others later… For example: void brake () { // Name: brake // Author: Andy Hatfield // Desc: Stops the wheels for a duration of 1/10 sec. // This is just enough for the wheels to stop. //Notes: Nov 1, 2018 – changed motor[] commands to // setMotorSync command setMotorSync(left, right, 0, 0); //New function sleep(100); }

24 The Beauty of Sensors Using the sleep command can give varied results due to battery power Using distance based functions can be tricky because of small differences in the game board and wheel slippage (will be addressed later) SENSORS ARE SUPER COOL

25 Now It’s Your Turn Create and test the following functions:
waitUntilTouch(){} moveToObject(float dist, int pwr){} moveFromObject(float dist, int pwr){} moveToLine(int pwr){} turnRight(int deg, int pwr){} turnLeft(int deg, int pwr){} Modify the functions to display the function name and the parameters when they are called

26 Congratulations!!! I am assuming that you have a functioning EV3 with ROBOTC I am assuming that you were able to use the sensors and create the new usable functions


Download ppt "Compiled from various Internet sources Presented by Mr. Hatfield"

Similar presentations


Ads by Google