Challenge 3.4: Robots

Robot

Goals

In this challenge you will practice the following computer science concepts in a team or small group environment:

  • Creating Objects
  • Using conditional statements and looping statements

Computer Science Principles Curriculum

  • Big Idea: Creativity: EU 1.2, LO 1.2.3, EK 1.2.3A, EU 1.3, LO 1.3.1, EK 1.3.1A
  • Big Idea: Algorithms: EU 4.1, LO 4.1.1, EK 4.1.1B
  • Big Idea: Programming: EU 5.1, LO 5.1.1, EK 5.1.1B, EU 5.2, LO 5.2.1, EK 5.2.1C, EK 5.2.1D

Common Core Standards

  • English Language Arts Standards » Science & Technical Subjects: CCSS.ELA-Literacy.RST.9-10.2, CCSS.ELA-Literacy.RST.9-10.3, CCSS.ELA-Literacy.RST.9-10.4, CCSS.ELA-Literacy.RST.9-10.5, CCSS.ELA-Literacy.RST.9-10.6, CCSS.ELA-Literacy.RST.11-12.2, CCSS.ELA-Literacy.RST.11-12.3, CCSS.ELA-Literacy.RST.9-10.1, CCSS.ELA-Literacy.RST.9-10.2, CCSS.ELA-Literacy.RST.9-10.7, CCSS.ELA-Literacy.RST.9-10.8, CCSS.ELA-Literacy.RST.9-10.9
  • Standards For Mathmatical Practice: CCSS.Math.Practice.MP1, CCSS.Math.Practice.MP2, CCSS.Math.Practice.MP5, CCSS.Math.Practice.MP6, CCSS.Math.Practice.MP7, CCSS.Math.Practice.MP8, CCSS.Math.Content.HSF.IF.A.1

Vocabulary

  • Control Structures
  • If Statement
  • Loops
  • Input Manager
  • Infrared Sensor

Overview

This challenge will use the InfraredSensor Library. You will use conditions with the Infrared Sensor in order to avoid the wall.

Goal 1: Creating Objects

Create a new Robot project. Name it properly and open the main.quorum file. In the top of the file, type the appropriate code to use the Quorum InfraredSensor and Motor libraries inside the Lego library.

Activity 1: Instantiate the Motor and the InfraredSensor objects.

Call the Motor object motor and the InfraredSensor object infraredSensor.

Now you must set the port to work with. That will be the port that the Infrared Sensor is connected to on the robot. You will do that using the SetPort statement.

Example: Set the port your Infrared Sensor is connected to

// This line of code will set the port that your robot will work with
infraredSensor:SetPort(4) //This line is a looping condition; once the condition doesn’t match anymore the program stops 
repeat while true 

After that you should set the speed of the motors, so despite any condition the motors will work at this speed when they’re active.

Activity 2: Write the code to set the speed of motors B and C.

Goal 2: Using conditional and looping statements

The robot should go forward and, if the infrared sensor detects a wall, it will turn to the right first. If there is a free space the robot will move forward, but if there is also a wall after turning right, it will turn to the left and move forward. If there is also a the wall there, the robot will stop because it has nowhere to go!

Let’s start with the conditions now. In order to get the distance between the robot and an obstacle you will use the GetDistance command. To make sure we have enough space to turn, let’s use 25 cm as the maximum distance.

Example: Create the conditions that will check the distance between the sensor and the wall and then make the robot move accordingly

//Before the conditions, to make it easier, let's set the distance values the sensor will take
distance = infraredSensor:GetDistance()
//This line states that in case the distance is greater than 40 cm
if infraredSensor:GetDistance() > 40
//the robot should move forward
motor:RotateBackward("B")
motor:RotateBackward("C")
//This line states that in case the distance isn’t greater than 25 cm
else
//the robot will stop and turn right
motor:Stop("B")
motor:Stop("C")
motor:RotateByDegrees("C", -655)
motor:Wait("C")
//You will also use an if statement inside an if statement to measure the distance again after turning right
//This line of code will do that again
distance = infraredSensor:GetDistance()

Activity 3: Using if and else statements, create the other conditions.

Don’t forget that you can (and in this case will need to) have an if inside another. You’ll do these conditions now: If after the robot turns to the right, there is a free space, the robot should move forward. Else the robot should turn 180 degrees to the left. If there is free space the robot should move forward. After these conditions we still need one more: in case the left also has a wall.

Example: Last condition: stop the motors

Example: Last condition: stop the motors.

//This line of code states that in case there is no free space to the left 
else
//These lines will make the robot stop
motor:Stop("B")
motor:Stop("C")
//These lines finish each one of the conditional (if) statements and the looping (repeat) statement
end
end
end
end

Hopefully your program is running great at this point! Run it and see if you get any errors. If you do, go back and fix them. Otherwise, you just finished your challenge! Congrats!

Next Tutorial

In the next tutorial, we will discuss Actions, which describes how work actions in Quorum..