Concept-based Lesson: Waddle Bot Unit 1 Lesson 3

Printer-friendly version
Objectives: 

Give the NXT the ability to make simple choices. Students should learn the following skills:

  • Use sensors to allow NXT to react to world around them.
  • Create a "logic branch" to control when certain actions occur.
  • Resolve basic conditional statements.
Lesson: 

There are many different types of programming languages out there. Each one operates under something called a programming paradigm. This paradigm dictates how the instructions that we write are interpreted by the NXT. RobotC uses the procedural programming paradigm. This is where commands are executed in order, one after another until it runs out of commands. You can interrupt this flow by adding logic branches to your program. Let's take a look at a simple example:


  1. #pragma config(Sensor, S3, Light, sensorLightActive)
  2. #pragma config(Motor,  motorA, Left, tmotorNormal, PIDControl, encoder)
  3. #pragma config(Motor,  motorC, Right, tmotorNormal, PIDControl, encoder)
  4. //*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
  5.  
  6. // Unit 1 - Lesson 3 - If statements and sensors
  7.  
  8. task main
  9. {
  10.   int threshold = 40; //Comparison Value
  11.   int motorspeed = 50; //Initial Motor speed
  12.  
  13.   //Logic Gate
  14.   if(SensorValue[Light] < threshold)
  15.   {
  16.     //On Black
  17.     motorspeed = -1 * motorspeed;
  18.   }
  19.  
  20.   //Result feedback
  21.   motor[Left] = motorspeed;
  22.   motor[Right] = motorspeed;
  23.   wait1Msec(1500);
  24. }

Sensor Configuration

Printer-friendly version

 

If motors are the output of our programs, then sensors are the input. Sensors give the NXT the ability to become aware of the world around it. We can use this awareness to grant the NXT the intelligence to navigate a dynamically changing world.

As with motors, before we can use a sensor, we must first configure it. This is done using the same tool we used to setup the motors.

SensorSetup1

In this case, we attached the light sensor to port 3 on the NXT. The type of sensors provided with the NXT kit are as follows:

image

Images From Lego.com

As with the motor configuration, the utility will create a #pragma command at the start of your program. Once configured, your NXT will be granted the ability to access the data generated by the sensors.

Sensor Value [ sLabel]

Printer-friendly version

 

Motors and Sensors are very similar in robotC. They are both special variables that are used to interface with the attached devices. Where motors accept data inputs, sensors only output data. This sensor integer value represents a real world condition that the NXT is sensing. In order to act on that information, we have to interpret the data for the NXT. We will do that below in conditional statements. For now, let's look at the elements of the variable.

SensorValue

Just like motors, each sensor has a static and dynamic name. The dynamic name is the label you gave the sensor in "motor and sensor setup". The Static named is based of what port you have the device plugged into. In this case, we have the sensor in Port 3 so the static name is "S3". If the device was in port 1, the static name would be "S1".

Booleans

Printer-friendly version

 

Our third major data type is called a Boolean. This data type deals with logic and is based in the binary language at the heart of every computer. Binary is defined as something "composed of two pieces or parts". Booleans are the same as in it only has two states: True or False. A Boolean can not have any other value.

While Booleans are important, you will rarely use them in programming. In the NXT environment, where we have ample memory space, we use integers instead. That doesn't means you can't or shouldn't use Booleans. If you want to use Booleans, use the following syntax:

  • bool MyVar = true; //Creates a Boolean and sets variable to true.
  • bool MyVar = false; //Sets variable to false.

Conditional Statements

Printer-friendly version

 

Computer logic is based around the ability of a computer to compare integers. Even the most complex choice can be boiled down to being able to determining if one number is greater than, less than, or equal to another number. This behavior forces us to form our logical instructions in a way that can be answered through evaluating two numbers. The result of this evaluation is a Boolean.

Let's take a look at the conditional statement in our example.

ConditionalStatements

In the example program, the NXT will determine if the robot is situated above a white region of the board or not. To do this, we use a light sensor pointed towards the ground. The light sensor will shine a light and monitor the brightness of the reflection. The sensor translates the brightness level to an integer. A black region would have a low number where a white number would have a high number. This behavior allows us to compare this value to another integer to determine if the NXT is over a dark region.

The light sensor will output a value from 1 to 100. In this instance the light sensor would output a number above 40 for a white area and below 40 for a black area. With this conditional statement, a white region would return a True value and a black region would return a False value.

View Sensor Values on NXT

Printer-friendly version

It's important to note that the value for the "threshold" variable was not picked at random. That number was picked because we tested the different regions on the field for what value the sensor will output. The NXT provides menu options that allow you to see the current sensor value. Use the arrow buttons to change your selection and the orange button to pick the selected menu option.

ViewSensor

The above example shows how to view a light sensor connected on port 3. Let's see what our sensor readings are.

ViewSensorValues

When the NXT is on Black, SensorValue(S3) will return an integer of 29 and when the NXT is on white, it will return an integer of 65. We can now evaluate the current sensor reading of the light sensor to determine if the NXT is on a black region or white region.

If Statements

Printer-friendly version

 

The IF statement is the most basic logic command that all other logical structure is based on. This gives our NXT the ability to make choices based off of data. In a basic IF statement, a conditional statement determines if a block of code is executed or not. If the statement is TRUE, the block is executed, if the statement is FALSE, the block is skipped. Let's look at an example:

IF-Blockv2

In this case, the logical test of the light sensor determines if NXT is on white or not. If the NXT is on white, the NXT will invert the motorspeed value so it is negative. This will cause the NXT to move backwards when the motor values are set.