Passenger Pick Up Challenge ICE Workshop: September 10th, 2011

Passenger Pick Up Challenge
ICE Weekend Workshop
September 10th, 2011

Pre Workshop Survey
Post Workshop Survey

Objective:
Program a your Passenger Robot to follow the driving course.  Robot must stop and pick up Passengers at the 3 stops on the course.  There may be a traffic obstacle.  If your robot encounters the obstacle, it must drive around the obstacle and continue the course.  Robot must start and stop in the "Parking Lot"

Sequence of Skills:
These resources will help you complete the challenge.  You will need to decide what sequence and controls you need to make these resources work.

Resources:
Passenger Robot Port Assignments

Types of Line Following:

Basic Line Follow
"Just Right Line Follow"
Proportional Line Follow

Dark Stop (Using Wait Block)

Line Following with LoopStop (Exiting a Loop)

Drive a Rectangle

Passenger Robot Course: Passenger Robot Basic Design and Port Assignments:
(TrafficBot Building Instructions) Line following works by using the Light Sensor to read the changes in reflected light level along the edge of a dark and light surface.   Then the Switch Block directs the motors to vary the speeds depending on the Light Sensor Value.

In a Basic Line Follow the Light Sensor reads the light value.  If the value reads darker than a set value the left wheel spins and the right wheel stops.  If the value reads lighter than the set value the right wheel spins and the left wheel stops.

 Loop(forever)     if LightValue < 40         LeftMotor Power = 40         RightMotor Power = 0     if LightValue > 40         RightMotor Power = 40         LeftMotor Power = 0

 Robot Project 1b:  Basic Line Follow Objectives: Use Conditional Switch Block to evaluate Input Values of Light Sensor Use Loop Block to Repeat Switch Block Project: Write an NXT-G program that will have your robot follow a line using a simple Algorithm. Process: 1.  Open the Mindstorms NXT-G software.  (Double click the orange square icon.) 2.  Name your program "LastnameBasicLineFollow" and Click "GO." 3.  Drag a Switch Block to the Programming Area.     a. Set the Sensor to "Light Sensor"     b. Set the Compare to < 40.  4.  Drag a Motor Block to top inside of the Switch Block 5.  Set the Motor Block Parameters to:     a. Motor Selection = C     b. Power = 40     c. Duration = Unlimited 6.  Drag another Motor Block and put it on the top inside of the Switch Block. 7.  Set the Motor Block Parameters to:     a. Motor Selection = B     b. Direction = Stop 8.  Drag another Motor Block and put it on the bottom inside of the Switch Block. 9.  Set the Motor Block Parameters to:     a. Motor Selection = B     b. Power = 40     c. Duration = Unlimited 10.  Drag another Motor Block and put it on the bottom inside of the Switch Block. 11.  Set the Motor Block Parameters to:     a. Motor Selection = C     b. Direction = Stop 12.  Drag a Loop Block into the programming area place it to the left of the Switch Block. 13.  Drag the Switch Block inside the Loop Block. 14.  Save Program.  (File -> Save) 15.  Download and test with the Robot. Notes: A.  You will need to work with the Light Value Numbers and Motor Speeds to "tune" the line follow to track the line depending on light conditions and path of line.  A lower motor speed will allow the robot to track a "more curvy" line. B.  This program will track the Right Hand side of the line.  How can you change the program to track the Left Hand side of the line? C.  The robot makes a wiggly track to follow the line.  How can you modify the program to allow for more efficient motion with the Robot.

Just Right Line Follow: The Proportional Line Follow uses a linear equation to adjust the Power in the C and B motors in proportion to the dark/light value in the Light Sensor.  The darker the reading, the faster the rate of turn.  This makes for a very smooth Line Follow.

 Pseudo Code: int Kp = 250; int offset = 45; int Tp = 40; int error; int Turn; int CPower; int BPower; while(true)  // infinite Loop {   error = LightSensor(3) - offset;   Turn = (error*Kp) / 100   CPower = Tp - Turn   BPower = Tp + Turn   if (CPower < 0)   {     CPower = 0;   }   if (CPower > 100)   {      CPower = 100;   }   if (BPower < 0)   {       BPower = 0;    }   if (BPower > 100)   {       BPower = 100;    }   motor[C] = CPower;   motor[B] = BPower; }

1.  Select "Edit -> Define Variables" and create the Variable "Kp".  Make sure the Datatype is "number." 2. Continue to add the following Variables (all Datatype "number"):
offset
Tp
error
Turn
CPower
BPower 3. Drag a Variable Block into the program area and Select "Kp"  (The Kp is the constant that determines how reactive the robot is to Dark and Light. (The slope of the Linear Equation.)) 4. Set the Kp variable parameters to "write" and "250" for Value: 5. Drag a Variable Block into the program and Select "offset."  (The "offset" is the target value for the Light Sensor) 6.  Set the offset parameters to "write" and "45" for value. 7.  Drag a Variable Block into the program and select "Tp".  (The "Tp" block represents the initial straight forward speed for the robot.)  Set the Value of "Tp" to 40. 8.  Drag a Loop Block to the program. 9.  We will now create the equation: "error = LightValue(3) - offset."  Put an "offset" variable and a Light Sensor Block into the Loop. 10.  Put a Math Block in the Loop: 11. Set Math Block to "Subtraction." 12.  Put a Variable Block after the Subtraction Block, set it to "error." 13.  Set the error parameters to "Write." 14.  Run a Data wire (Yellow Line) from the LightSensor Block to the A port of the Math Block 15. Run a Data wire from offset to "B" in the Math Block. 16. Run a Data wire from the Result of the Math Block to the error Block. 17.  We will now write this equation:  "Turn = (error*Kp) / 100."  Put two more variable blocks in the program.  Set to "error" and "Kp" 18.  Put a Math Block in the program and set it to "Multiply."  Wire the error and Kp blocks to the Multiply Math Block 19.  Put another Math Block in and set it to "Divide." 20.  Set the Divide block parameters to B = 100. ***Note:  Why divide by 100?  In NXT-G 1.0 (The version this program is written in) We do not have Floating Point Numbers.  (Decimals).  Thus, we do calculations multiplied by 100 (Or Kp = 250 instead of Kp = 2.5) and then Divide by 100 right before we use the Variable.)***

21.  Wire Divide Block 22. Put a "Turn" Variable in the Program.  Set Turn to "write" and wire result from Divide into Turn. 23.  Build the equation for the CPower:  "CPower = Tp - Turn." 24.  Build the equation for the BPower:  "BPower = Tp + Turn." 25. We now need to correct of CPower if it is Less than 0:  Put a CPower Variable and a Compare Block in the program.  Set the Compare to "Less Than"  ( < ).   Wire the CPower block to the "Less Than" block. 26.  Put a Switch Block on the program.  Uncheck the "Flat View" Checkbox. 27.  Set the Switch Block parameter Control to "Value" and the Type to "Logic." 28.  Put a CPower variable block in the "True" side of the Switch. 29.  Set the CPower in the Switch to "Write" and the value to "0" 30.  Wire Logic Block to Switch.  Command now reads:

if (CPower < 0)
CPower = 0; 31.  Do the same for the following command:

if (CPower > 100)
CPower = 100; 32  Set commands for the BPower blocks: 33.  Put a CPower Variable block and a C Motor Action Block in the program 34.  Wire the CPower block to the Power input of the C Motor Block: 35.  Repeat process for BPower Block and B Motor Block: 36.  You are done!  The entire program will look like this: 37.  Save and test your program on the line.  Make adjustments as needed.  To use Proportional Line Follow - you should turn it into a "MyBlock."

Dark Stop (Using the Wait Block): Creating MyBlocks:

MyBlocks: A MyBlock is a user defined segment of code that resides within one programming block. The user can take multiple blocks and "group" them together into a block that can be reused in different programs. This is similar to defining methods or functions in other programming languages.

Generally, if you are using a sequence of blocks more than twice in a program - you should turn it into a MyBlock. (Examples include Dark Stops and Line Following).

 Directions for making a MyBlock. 1. Select the Blocks you wish to group together. (In this example, we are going to group a "Dark Stop" routine. 2. Click the MyBlock icon. 3. Name your MyBlock - use a name that describes the MyBlock action. Example: "DarkStop" 4. Click Next. 5. Drag icons to "decorate" your block. This will give you a visual cue as to the purpose of the block. In this example, we will use a Stop Sign and a Light for "Dark Stop". 6. Click "Finish" 7. The computer saves the MyBlock and puts it back into the original program. 8. You can access the MyBlock from the MyBlocks menu. Line Following and Exiting a Loop:

We can exit a Loop with a Sensor control.  In this project, we need the robot to stop Line Following when the 2nd Light Sensor reaches the black mark at the passenger stations.  The psuedo code for this would look like:

 Loop Until (LightSensor2 < 40) {    LineFollow(); } StopMotors(BC);

 These directions detail how to exit a Loop with the second Light Sensor.  We will use a MyBlock for the Line Following Code. 1.  Insert a Loop Block into the Program: 2. Put the desired code into the Loop (I used a Line Follow MyBlock): 3. Select the Loop and Change the Control Parameter to "Sensor."  4. Change the Sensor Parameter to "Light Sensor."  Change the Port to 2.  Set the Until Parameter to  "< 40"  5.  Add a "Stop Motor" Block after the Loop.  This stops the motors once the loop is finished. Drive a Rectangle:

 Robot Project 1a: Drive in a Rectangle Objectives: Select Movement Block parameters for motors selection, direction, distance, power, and turning. Use Swing turns to change direction. Use loop blocks to repeat commands. Project: Write an NXT-G program that will drive your robot in a rectangle.  Use rotations to measure distance. Use a loop block to keep the program short. Download and run your program to the robot. Process: 1.  Open the Mindstorms NXT-G software.  (Double click the orange square icon.) 2.  Name your program "RectangleLastname" and Click "GO." 3.  Drag a Motor Block to the Program Area. 4.  Set the Motor Block Parameters to:     a. Steering Straight Ahead     b. Power = 50     c. Duration = 4 Rotations 5.  Drag a 2nd Motor Block to the Program Area. 6.  Set 2nd Motor Block Parameters to:     a. Steering All the Way Left     b. Power = 50     c. Duration = .55 Rotations 7. Drag a 3rd Motor Block to the Program Area 8.  Set 3rd Motor Block Parameters to:     a. Steering Straight Ahead     b. Power = 50     c. Duration = 2.5 Rotations 9.  Drag a 4th Motor Block to the Program Area 10.  Set 4th Motor Block Parameters to:     a. Steering all the Way Left     b. Power = 50     c. Duration = .55 Rotations 11.  Drag a Loop Block before the 4 Motor Blocks. 12.  Put the 4 Motor Blocks inside the Loop Block 13.  Set Loop Count to "2," 14.  Plug Robot into USB port in computer.  15.  Click the Download Button in the Lower Right Corner 16.  After Robot beeps twice, unplug from computer and try on rectangle course. 17.  Select "Software" on the Robot. 18.  Scroll across until you see your program "RectangleLastname." 19.  Put the robot on the table and press the Orange button to start and turn your program.  Press the Grey Rectangle to stop the robot. 20.  Make adjustments to your program as needed to make a square.

Suggested Strategy:

1. Develop a Good Line Following Program to the Robot can get all the way around the course.

2. Add the Dark Stop and Turns to get the robot from the parking lot to the Track and then around the course.

3. Make a MyBlock out of the Line Follow.

4. Add the Exit Loop codes so the robot stops at the Passenger Stations.

5. Add the Code so the Robot can Load the Passengers at Each Station.

6. Develop additional Exit Loop codes so the robot will stop for traffic if needed (using Ultrasonic Sensor).

Email Mr.Michaud