engineering for kids basic programming structure …...engineering for kids 3322 rt. 22 west, suite...
TRANSCRIPT
Engineering For Kids
3322 Rt. 22 West, Suite 402, Branchburg, NJ 08876 (908)595-1010 www.stormingrobots.com 1 | P a g e
Basic Programming Structure using RobolabTM
Contents Basic Trouble shooting tips: ....................................................................................... 2
Flowchart ............................................................................................................. 2
Basics in Programming with Robolab ............................................................................. 3
-1- Go Forward/Backward ................................................................................ 3
-2- Modifiers ................................................................................................ 4
-3- Loop Structure .......................................................................................... 5
-4- Infinite Loop Structure ................................................................................ 6
-5- Wait for Push/Release (touch sensor) .............................................................. 7
-6- Wait for Darker/Brighter (light sensor) ............................................................. 8
-7- Wait for Specific Dark/Bright value (light sensor) ................................................ 9
-8- Basic "if" Structure / Robolab fork ................................................................. 10
-9- Basic sensor "loop" Structure ........................................................................ 11
-10- Basics Datalogging Sample ........................................................................... 13
Engineering For Kids
3322 Rt. 22 West, Suite 402, Branchburg, NJ 08876 (908)595-1010 www.stormingrobots.com 2 | P a g e
Basic Trouble shooting tips:
Best Practice:
- Always start with a flow chart - Test the logic of the flow chart by just following the logic without running the robot. e.d.
pretend your pencil is the robot. You will then move your pencil following your program step by step.BEWARE! Follow WHAT THE PROGRAM SAID TO DO, NOT WHAT YOU WANT IT TO DO.
- Write your program following your flowchart. - Check your program against the flowchart.
Sensors did not react?
- id you connect the sensors to the correct port? - did your program have all the sensors connected to the proper port numbers? - did you use the correct sensor icon? Double click to get the help screen to verify. - use the view to check the sensor value. - did you forget the threshold value (if you use light sensor)
Robot does not stop?
- did you forget the “stop” icon? - did you check the logic step by step without the robot actually running.
Flowchart
Start/End
Process/Action
Conditions/Decisions
Flow Line
Merge
Wait
Can be
called the
terminator
Represent:
a single step, or
a sub-process
Used in 2 structures:
1. "if-then-else" (do not repeat)
2. While loop (repeat)
Connect all
symbols and
indicate the
flow
direction.
To merge
decision
paths
Wait
until;
Wait for
Engineering For Kids
3322 Rt. 22 West, Suite 402, Branchburg, NJ 08876 (908)595-1010 www.stormingrobots.com 3 | P a g e
Basics in Programming with Robolab
-1- Go Forward/Backward
Sample 1: Go forward for 4 seconds and then stop.
Sample 2: Go forward for 4 seconds, then go in reverse for 4 seconds.
Sample 3: Make a point turn for a second. For some robots, it may mean 180°.
Exercises: Draw a flow chart for the following behaviors. Then, write the program and test run your
robot.
1) Robot go forward for 2 seconds and then stop. 2) Robot go forward for 2 seconds, back up for 2 seconds, and then stop. 3) Robot does point turn to left for 2 seconds, and then stop. 4) Robot does point turn to right for 2 seconds, and then stop. 5) Robot does a point turn for 180 degrees. 6) How about a drag turn for 180 degrees?
Engineering For Kids
3322 Rt. 22 West, Suite 402, Branchburg, NJ 08876 (908)595-1010 www.stormingrobots.com 4 | P a g e
-2- Modifiers
Modifiers are used to modifiable values used for many “functions”, such as motors, timers, sensors,
and many others. Here are a couple basic ones:
1- Numerical constants
2- Power levels
Sample 1: Your robot goes forward for 4 seconds, stop for 0.5 seconds, then make a left turn, stop
for 2 seconds, and then go forward for 5 seconds.
Sample 2: Decelerate your robot.
Exercises: Draw a flow chart for the following behaviors. Then, write the program and test run your
robot.
1) Robot go forward for 2 seconds and then stop. 2) Robot go forward for 2 seconds, back up for 2 seconds, and then stop. 3) Robot does point turn to left for 2 seconds, and then stop. 4) Robot does point turn to right for 2 seconds, and then stop. 5) Robot does a point turn for 180 degrees. 6) How about a drag turn for 180 degrees?
Engineering For Kids
3322 Rt. 22 West, Suite 402, Branchburg, NJ 08876 (908)595-1010 www.stormingrobots.com 5 | P a g e
-3- Loop Structure
This is for repeating a sequence of behaviors.
Sample 1: Repeat motor A and C go forward for 4 seconds. Repeat these steps for “20” times.
This is the a flowchart representing the logic flow:
Sample 2: Forward for 4 seconds, then turn for a second. Repeat this 20 times.
Exercises:
1) Draw a flowchart for the behaviors in Sample 2. Then, write the program and test your robot. 2) Robot goes forward for 2 seconds, back up for 2 seconds, and then stop. Repeat this 4 times. 3) Robot does point turn to left for 2 seconds, then stop and play a sound. Then, point turn to
right for 2 seconds, then stop and play a sound. Repeat this sequence 5 X.
4) Create your wiggle moment!
Beginning of a loop End of a loop
Being repeated
20x
Engineering For Kids
3322 Rt. 22 West, Suite 402, Branchburg, NJ 08876 (908)595-1010 www.stormingrobots.com 6 | P a g e
-4- Infinite Loop Structure
Sample 1: Repeat these steps infinitely. Notice the “land” and “jump”. They are equivalent to
“beginning of an infinite loop” and “end of an infinite loop”.
Exercises
1) Robot goes forward for 2 seconds, backs up for 2 seconds, and then stops. Repeat this 4 times.
2) Robot points turn to left for 2 seconds, then stops and plays a sound. Then, points turn to right for 2 seconds, then stops and plays a sound. Repeat this sequence 5 X.
3) Create your wiggle movementst!
Land/Beginning of
the loop
Jump/ End of
the loop
Random # of
seconds
Engineering For Kids
3322 Rt. 22 West, Suite 402, Branchburg, NJ 08876 (908)595-1010 www.stormingrobots.com 7 | P a g e
-5- Wait for Push/Release (touch sensor)
Wait for push:
Wait for release:
Sample1: Reset the touch sensor. This will allow you to view the sensor value when
presses. Download the program, and press “Run” at least once.
Sample 2: Robot moves forward until the front bumper (touch sensor) is pressed, back up for 1
second, turn for a random amount of time, and then stop.
Exercises:
1. Write a flow chart for the Sample2. 2. Program Sample 2 and test it. 3. Change the touch sensor to port 3, and it should stops after it turns a full 360 degree turn.
Modify your program and test it.
Engineering For Kids
3322 Rt. 22 West, Suite 402, Branchburg, NJ 08876 (908)595-1010 www.stormingrobots.com 8 | P a g e
-6- Wait for Darker/Brighter (light sensor)
Wait for Darker (5% by default):
Waiter Brighter (5% by default):
LED – Light
Emitting Diode
Photo-transistor
Sample1: Reset the light sensor. This will allow you to view the sensor value. Download the
program, and press “Run” at least once.
Value on the screen indicates the reflection value == light value.
Sample 2: Program your robot to stop at the edge of the table. (Assuming your table is not very
dark).
Motor A and C simply stop when the light sensor on port 1 detects light sensor reading drops
by 5 %.
Engineering For Kids
3322 Rt. 22 West, Suite 402, Branchburg, NJ 08876 (908)595-1010 www.stormingrobots.com 9 | P a g e
-7- Wait for Specific Dark/Bright value (light sensor)
Wait for light
value < 50%
Wait for light
value < 45%
Wait for light value
>50% (default)
Wait for light value
>45%
Sample 2: Keep going forward until the light sensor reads less than 40%.
Notice that the program initializes the light sensor for 10 milliseconds for warmup time.
Exercises:
1) Program for your robot to go straight and wait for dark. Then, it turns 180o left. 2) Program for your robot to keep turning until it sees very bright light.
darkest -------------------------------- brightest
0% 100%
To calculate threshold: (black + white) / 2
e.g. black = 30
white = 50
threshold = 40
Engineering For Kids
3322 Rt. 22 West, Suite 402, Branchburg, NJ 08876 (908)595-1010 www.stormingrobots.com 10 | P a g e
-8- Basic "if" Structure / Robolab fork
Touch sensor "if" structure
Light sensor “if” structure
Both need to end with a
“merge”
symbol:
Sample1: The robot will follow a line. Now, the number “50” depends on the threshold
calculation. What you need to do is to take reading for your test field as following:
step 1) place your light sensor above black, write this down: black = ______
step 2) place your light sensor above white, write this down: white = ______
step 3) threshold = (black + white)/2 . Replace the numerical value to what you calculated.
Sample 2: Robot goes forward until it hits an obstacle. When it hits an obstacle, it turns around for 1
second. Note that this is only a sample for general situation. You must conduct your own trouble
shooting your robot somehow does work as expected.
Exercises:
Complete all samples and test. You must attach proper sensors to your robot before you can
proceed.
Engineering For Kids
3322 Rt. 22 West, Suite 402, Branchburg, NJ 08876 (908)595-1010 www.stormingrobots.com 11 | P a g e
-9- Basic sensor "loop" Structure
Touch sensor loop
Light sensor loop
Sample 1: Robot traces the line using light sensor on port 2, until the touch sensor or port 1hits an
obstacle.
Sample 2: Robot goes forward. Once the touch sensor on port 1 hits an obstacle, it turns around. It
repeats this sequence of behaviors while the light sensor on port 2 sees lower than 45.
Engineering For Kids
3322 Rt. 22 West, Suite 402, Branchburg, NJ 08876 (908)595-1010 www.stormingrobots.com 12 | P a g e
Sample 3: Lined tracing with obstacle course. Robot traces a line with the light sensor on port 3.
When its touch sensor on port 2 hits an obstacle, it will turn around to get back on tracing line and
return to base.
Exercises:
Complete all samples and test. Again, you must attach proper sensors to your robot before you can
proceed.
Engineering For Kids
3322 Rt. 22 West, Suite 402, Branchburg, NJ 08876 (908)595-1010 www.stormingrobots.com 13 | P a g e
-10- Basics Datalogging Sample
Basic icons:
Reset: Start data logging: Stop data logging:
Initializes the NXT Sensors to take data and configures data logging settings. By default, each of each icon is connected to Port 1 and data is recorded in the Red Data Set. Note: This command must appear before any data logging of the sensor can begin.
Initializes the Rotation Sensor to take data and configures data logging settings. Port C data will be recorded in the Blue Set. Total Buffer Size is 1000. That means This number can range from 0-2000 and is dependent on the amount of memory used by the program. Note: This command must appear before any data logging of the sensor can begin.
Clears data logging buffer and resets the datalog timer. This frees up the space for longer programs.
Start the data logging and data will be saved in the Blue Data Set and data is captured every 1/4 second, i.e data is logged 4 times in a second.