Concept-based Lesson: Waddle Bot Unit 1 Lesson 2

Printer-friendly version
Objectives: 

Learn basic motor movement and program structure. The following skills should be learned:

  • Set the power level of a motor.
  • Understand what order commands are executed.
  • Create variables that can be used to quickly modify the parameters of a program.

 

Lesson: 

In this training, we use motor control to explore how an instruction affects the actions performed by the NXT. We begin with the most basic movement of moving forward and stopping:


  1. #pragma config(Motor,motorA,Left,tmotorNormal, PIDControl, encoder)
  2. #pragma config(Motor,motorC,Right,tmotorNormal, PIDControl, encoder)
  3. //*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
  4.  
  5. // Chapter 1 - Unit 2 - Basic Movement
  6. task main
  7. {
  8.   int timer1 = 1000 //Set Variable for later use
  9.  
  10.   eraseDisplay();
  11.   nxtDisplayString(1,"Move Forward");
  12.  
  13.   //Move forward
  14.   motor[motorA] = 100;
  15.   motor[motorC] = 100;
  16.   wait1Msec(timer1); //Use Variable
  17.  
  18.   //Stop
  19.   motor[Left] = 0;
  20.   motor[Right] = 0;
  21.  
  22. }

If you copy this to your robot, it should mimic the robot in this video:

WaddleBot C1-U1-P1 from Devin Hunter on Vimeo.

#pragma

Printer-friendly version

 

Before you use a motor, you must configure the NXT to access it. This brings us to our next rule:

RobotC Rule 4: A #pragma command is required for each connected device you wish to use.

To make writing programs easier, RobotC provides a tool to generate the configuration commands. To access this tool, go to the following menu location:

Robot > Motors and Sensors Setup

The only thing we need to do in that screen is label our motors. This tool will not generate #pragma commands for unlabeled devices. We don't need to modify any other settings. On our robot, the left motor is in Port A which is motorA in this tool. The Right motor is in Port C which is motorC. Here a screenshot of what the Motor tab looks like for the sample program.

MotorSetup

Once you Press "OK" in this menu, you should get the following instructions at the top of your program:

MotorSetup-Result

These instructions let the NXT know that there are peripherals attached. If you make changes to your NXT, you can come back to the Motor and Sensor Setup tool to update the configuration.

Variables

Printer-friendly version

 

In programming, a variable is a labeled section of memory that holds a specific data value. Once we create this variable, the data contained can be referenced multiple times. When you use a variable as a parameter of a function, the value of that variable is passed to the function. In our example, we create the "timer1" variable and set it to 1000. Later, when we set the parameter of our wait1Msec command as "timer1" the effective result is that the wait timer is set to 1000.

SimpleVariable

A variable declaration has four main elements:

VariableDeclaration

  1. Type: The type of data that is being stored.
    • Integers use "int"
    • Strings use "string"
  2. Label: This allows us to reference the data later. Make sure to use a name that is not already in use elsewhere.
  3. Equals sign: This separates the label from the value.
  4. Value: The data that is being stored in the variable.

Once you have created the variable, you can change the value stored with the same command you used to create the variable. When modifying a variable, RobotC requires that you use the same data type. This behavior means that you don't need to specify type when modifying a variable.

RobotC Rule 5: The type of data stored in a variable is only defined when you create a variable.

While variables are useful, you should only use them when appropriate. Every time you create a variable, a section of system memory is allocated to that variable. The NXT has a limited amount of memory and you should get into the habit of writing the most efficient programs possible. A good rule of thumb is that you should only use variables in the following situations:

  1. You need to use the same data value more than once.
  2. You need to store something for use later.
  3. You want to quickly adjust a parameters.

We'll talk more about variables as we use them.

motor [ sLabel ]

Printer-friendly version

 

The NXT brick can't move without the assistance of external motors. The only thing the NXT can do is send electric power to an attached motor. The more power the NXT provides, the faster the motor will rotate. RobotC controls this power by using special variables. It sets up a numerical scale from +100 to -100 where 0 is no power and 100 is full power. This scale is easier for humans to understand and the NXT uses that number to determine how much power to send to the motor. The direction in which the motor rotates is based on if the integer is positive or negative. On the waddle bot, positive number move the robot forward, negative numbers move the robot backwards.

Let's take a look at the motor control commands. Both of them control the same motor.

MotorSyntaxV2

When you configure the motors, the motor variables are automatically created. These special variables all begin with "motor" followed by the motor name encapsulated in square brackets. Each motor is given two names, one static and one dynamic. The static name is based off of the port that the motor is plugged into. Port A is called "motorA", Port B is "motorB", and Port C is "motorC". While you can technically use these names, it is generally better to use the dynamic name that you put in the "name" field in the motor and sensor setup. In the example, we use the "Left" and "Right" for our motors. By using dynamic names, we can change ports and only need to reconfigure the NXT instead of renaming every motor command.

RobotC Rule 6: When possible, name your attached devices and use the dynamic name.

Concepts: 

Phases of a program

Printer-friendly version

 

With all programming languages, there is an inherent order that certain instructions should be performed in. This behavior dictates were certain types of instructions are placed in the program. By using a template, we can streamline the creation process and build a uniform framework that all of our programs follow. The first step is to identify generic regions based on the actions taken by the NXT. Let's break apart our sample code and see how it is organized:

Phases

In RobotC, the first general region boundaries revolve around "task main {}". The boundaries are placed at the start (line 6) and the end (line 22) of "task main {}". Anything that happens before "task main{}" starts is considered to be in the "Pre-Initialization" Phase. This phase is where we configure everything that will be used in the program. At the other end of "task main{}" is the shutdown phase. This where the attached devices are turned off and the program closes. RobotC handles all of this for us once we reach the closing curly brace (line 22) of "task main{}".

Everything inside of "task main{}" is considered to be a part of the "Main Task Phase". Inside of this phase, there are two sub regions that occur at the start and end of the task. At the start of the task is the Initialization phase, not to be confused with Pre-Initialization phase that occurs outside of "task main {}". This phase is where we setup the initial state of the NXT. Most of our Variables will be created here.

At the end of the Main Task Phase is the Park Phase. This is where we stop any work being done by the NXT and report any relevant data. Not all programs require a park phase. The shutdown phase will turn off motors if they are currently running. We mainly use the park phase to report results of the work done.

When writing programs using this template, you should remember that it is only a guideline. Depending on the desired behavior, you may come up with a different template. For now, use the framework provided, but don't let it constrain your creativity. The great thing about Programming is that every problem has multiple solutions.