Logic Tables and Flowcharts Dr. Jianna J. Zhang © copyright 2014 Department of Computer Science, Western Washington University Logic Tables Logic Tables are graphic representation that show the relationship between motors, sensors, and actions. Action Motor A (left wheel) Motor B (arms) Motor C (right wheel) Motor A (left wheel) Motor B (right wheel) Touch Sensor (objects) Action Example 1: Logic Table 1. 2. The main goal: 1) Make a robot to dance on a stage The sub-goals: 1) Walk to the stage 2) Turn towards the audiences 3) Walk left and right 4) Walk back and forth 5) Turn 180 and raise arms up and down 6) Turn 180 again 7) Repeat step 2) to 6) for 3 times 8) Leave the stage What do we need? 1. NXT controller 2. Two motors for two wheels 3. One motor for both arms Action Motor A (left wheel) Motor B (arms) Motor C (right wheel) Forward Forward Off Forward Backward Reverse Off Reverse Left Turn Off Off Forward Right Turn Forward Off Off Raise Arms Off Forward Off Lower Arms Off Reverse Off Example 2: Logic Table 1. 2. The main goal: 1) Following a black line on a flat white surface The sub-goals: 1) Go forward on the black line 2) Turn left if the robot is on the right side of the black line 3) Turn right if the robot is on the left side of the black line What do we need? 1. NXT controller 2. Two motors for two wheels 3. A light sensor The Logic between the light sensor and motors: 1. (black+white)/2=grey 2. If the light sensor is less than grey, the robot is close to white surface. It needs to turn towards black surface. 3. If the light sensor is greater than grey, the robot is on the black surface. It needs to turn towards the white surface. 4. The Logic Table Action Motor A (left wheel) Motor B (right wheel) Light Sensor (black line) Forward Forward Forward = grey Left Turn Forward Reverse > grey Right Turn Reverse Forward < grey Basic Types of Flowcharts Used in this Class 1. Process Flowchart • show higher level steps with plain English 2. Program Flowchart • show detailed steps using abstract variables Elements of Flowcharts Terminal On-page Off-page Example: start, end, or branch Conectors Example: internal/external input or initializations Input/output Process Action Pre-defined Process Example: “Turn 25” or “go forward” Example: data file, knowledge base A File Files Decision Entity Processing Direction Example: if touch the wall, go back else, go forward Example: sensors, RCX, or motor usually used in data flowcharts Example: after turn left, go forward Why bother? Start 1. Make plan clear Walk Forward • Visual representation • Good organizing tool 2. Correct logic Turn 90 • Efficient plan • Clearly display the logic B Walk Right 3. Improve programming • Quickly pinpoint problems • Identify bottle necks Walk Left 4. Example: make a robot to dance: Walk Backward 1) Walk to the stage 2) Turn towards the audiences Walk Forward 3) Walk left and right 4) Walk back and forth 5) Turn 180 and raise arms up and down A 6) Turn 180 again 7) Repeat step 3) to 6) for 3 times 8) Leave the stage A Turn 180 Arms up Arms down Turn 180 No 3 times? Yes Walk Off Stage End B Off Page Flowchart A Start Turn 180 B Come in from different page(s) Come in from different page(s) Flowchart for a Predefined Function Walk Off Stage Walk Forward Arms up Start Arms down Raise Arms Up Turn 90 Walk Right Turn 180 Walk Left Goes to a different page. No 3 times? B Lower Arms Turn right 90 Walk Backward Walk Forward Goes to a different page. A Yes Walk Off Stage Walk Forward End End Page 1 Page 2 Page 3 Examples to Avoid Mistakes ≠ Wrong place to connect arrows Arrows must be connected with symbols Loops must go clock wise direction Start Is the touch sensor pressed? Yes Stop motors End No Run both motors forward Can we do better? Start Is the touch sensor pressed? Yes Stop motors End Start No Run both motors forward Is the touch sensor pressed? No Run both motors forward Yes Stop motors End An Example of Process Flowchart Start A Read Light Sensor Display Command Is value<40? Yes Turn Left End Read Touch Sensor No Is Value=40? Yes A Sing and Turn 360 No Turn Right Yes Is value=1? No B B An Example of Program Flowchart Variables: L1 = light sensor on port 1 Functions: 1. TurnLeft( ) makes left turns 2. TurnRight( ) makes right turns 3. Display( ) shows message on the LED panel 4. Dance(degree) make several “degree” circles Start A L1=? Display( ) L1<40 Yes TurnLeft( ) T1=? No L1=40 N o TurnRight( ) End Yes A Dance(360) Yes T1=1 No B B
© Copyright 2024