Lab 4.3: Robotic Actions

Using Actions with Robots

Robotic Actions

Goals

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

  • Creating actions
  • Using actions

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

  • Actions
  • Color Sensor
  • Conditionals
  • Control Structures
  • If Statement
  • Loops
  • Parameter

Overview

This lab demonstrates how to create and use actions with robots. You will move the robot and use the Color Sensor to tell what color it is reading, indicating that on the screen and with different sounds.

First, open the provided template and click on the main.quorum file. You will see that the libraries are all declared and also a ColorSensor object called colorSensor. The ColorSensor object is before the action because all the objects that are going to be used in more than one action should be declared in the class before all the actions. Inside the class Main, is the action called Main and two other actions that you are going to created.

Activity: Declare a Motor, Sound, Screen objects under the ColorSensor object.
You will use these objects later in your program.

Goal 1: Creating Actions

The Main action is where the principal code is going to be, so we will leave it for after you've created the other two actions. We will start creating the second action, on line 21, that we will call Move and it will take two text parameters, that we will call motor1 and motor2.

Example: Create the action Move and declare the parameters

//This line will create an action called Move.
//The parentheses declare two parameters
action Move(text motor1, text motor2)

We will call this action to move the robot, so you will have to write the code to move the robot inside action Move.

Activity: Write the proper code to move the robot forward.
Use the RotateByDegrees statement to move your robot.

The robot should stop before reading the color. Add this line to your action, so the program will wait for the motor to stop before continuing.

motor:Wait(motor1)

You have completed the first action. Let's create the action ColorSensor. This action won't take a parameter.

Activity: Create action ColorSensor.
Create the action. Remember to use an end.

To read the colors using the Color Sensor we will need to use the GetColor command. Remember that the sensor can return only 8 colors options: none, black, white, brown, red, green, blue and yellow. In the example below you can see the first two conditional statements.

Example: Start the conditional statements.

//This line gives the text returned from GetColor to the variable color
text color = colorSensor:GetColor()
//This is the first conditional statement. It checks the color "none."
if color = "none"
     sound:Buzz()
     screen:Output("No color read!"
//This line is a second conditional statement to check if the color is "black".
elseif color = "black"
     sound:PlayTone(20, 250)
     screen:Output("The color is:" + color, 4)

Activity: Write six more conditional statements.
Add elseif statements for the six other color possibilities. Make each sound unique, and remember to output information to the screen. Also, remember to end the conditional.

Activity: Create a Utility object. Also use SetPort to set the Color Sensor.

Goal 2: Using Actions

Go to action Main to write the code to run the robot. You will use actions to make the robot move, stop, read and tell the color 5 times.
To call an action, you write the action name with any parameters in the parentheses. If there are no parameters, you leve the parentheses empty. () Use the name of your Motor objects in the parentheses of the Move action. You can use a repeat statement to simplify your program.

Activity: Use actions to make the robot move, stop, read and report color five times.
You may want to use the Utility object with the DelayMilliseconds() action to make your robot pause.

Next Tutorial

In the next tutorial, we will discuss Assignment 4.2, which describes an introduction to file saving and action overloading..