CS 1020 - Winter 2013 - Class 5
Quiz 2
Administrative
- Eight-ball competition on Friday
- Have your robots ready to start at 9pm
- work out all of the bugs, etc. the night before
- come in a few minutes early and make sure everything is still working :)
- make sure to plug in your boards AND turn them off the night before (they charge much better when they're off)
- We'll run the competition with the lights off and the blinds down
- this will make it easier on your sensors
- test your robot in these conditions!
- Time limit?
- Visit Quarry Hill School on Friday at 1pm
- ~30 minutes at the school (should be back by 2, 2:30 at the latest)
- we'll need to put together a few robots to take along
- extra credit
- logistics
- who want to come?
- looking for 7-10 people...
- who can drive?
- may have another visit for another school, but I'll keep you posted...
look at the bad-motor procedure in
concurrency.txt code
- what will it do when I run it?
- the *hope* is that it will change the direction when the switch is press
- It doesn't though. What will it actually do?
- when you hold down the switch it will constantly reverse direction until you let go
- what is the problem?
- the key problem is that "switch 7" becomes true the when the switch is activated
- this is called an "edge" triggered event
- draw diagram
- in some situations this is great
- in others, not so great :(
- solutions?
- one solution is to put in a "wait" command after the "a, rd"
- how long do we wait for?
- what we really want is for the direction to change, but then not continue in the loop (i.e. check switch 7 again) until the switch is released
- how could we write that?
- look at good-motor procedure
toggle
concurrency
- what does it mean for two things to happen "concurrently"?
- at the same time
- what does concurrency mean for computers?
- concurrency is crucial for modern computers/computer science
- rarely is any one single thing running on a computer at a time
- you can actually see all the different things running at once
- we can talk some other time about how that actually happens on the hardware...
what does concurrency mean for a computer program?
- the program can be doing multiple things simultaneously
- normally when we think about a program we think about it executed a sequential set of steps
- with concurrency a program can have 2 or more sequential set of steps occurring at the same time
- in the simplest case, think of running two procedures **at the same time**
processes
- a "process" is a single executing unit that executes its code sequentially (this is not quite technically ture with multi-threading, but we'll ignore that complication for now)
- concurrency involves running multiple processes at the "same time"
Handy Logo concurrency
- run motor-demo from
concurrency.txt code
- Notice that while the song is playing I can still interact with the motor via the switch
- with what we've learned so far, could you do this?
- No! (or definitely not very easily)
- what is happening?
- we have two separate things running at the same time:
- our play-song procedure
- our good-motor procedure
launch
launch [some-code]
- the launch command executes some code concurrent (i.e. in parallel) to the current running code
- specifically it creates a new process and starts executing the code
- the main process then continues to run
- the two processes run concurrently
- given the launch command, how could I get the behavior from motor-demo?
- call launch with either play-song OR good-motor
- then, call the other one after it normally
forever
forever [some-code]
- a similar command to launch is forever, which execute the code in the square brackets over and over again as if it were in a loop **as a separate process**!
- run motor-demo2 from
concurrency.txt code
- how could we do this with forever?
- want to beep and wait in a loop continuously
- put that in a forever statement
when
when [some-question] [some-code]
- the when statement allows us to execute some-code whenever some-question is true
- similar to an if statement
- however, this checking and executing happens in a different process
- the current code does NOT stop and continues executing
- think of it like telling Logo to listen in the background, but to continue doing what it was doing
- could we implement the same beeping and motor changing behavior with a when statement?
- look at motor-demo3 in
concurrency.txt code
every
every <time> [some-code]
- executes some-code in another process (i.e. concurrently or in the background) every <time> tenths of a second
- similar to when
- when: runs only when a particular
- every: runs at some timed interval
- could we implement the same beeping and motor changing behavior with an every statement?
- look at motor-demo4 in
concurrency.txt code
stoprules
- eventually, you may want to stop having your concurrent processes running in the background
- the "stoprules" command stops all processes that were spawned by the current process EXCEPT those that were started with launch
- look at motor-demo5 in
concurrency.txt code
- what will happen when I run the code?
- what will happen when I activate sensor 7?
- what will happen when I active sensor 8?
- will anything happen if I now activate sensor 7 again?
look at fancy-ping-pong in
more_concurrency.txt code
- what does the procedure do?
- similar to the ping-pong procedure you wrote
- but makes beeping noises when it backs up
- why do we need the stoprules?
- it kills the parallel process that was beeping up
- notice that this is one of the advantages of not using launch
look at too-much-concurrency in
more_concurrency.txt code
- what does the procedure do?
- causes trouble :)
- loops and concurrency often don't mix
- we'll freeze up the Handy board
- basically we spawn a bunch of processes from the loop
- generally, just don't include concurrency in loops
look at eight-ball-challenge in
more_concurrency.txt code
- here's a hypothetical example of how concurrency might be useful
- in theory we might be able to do all of these things in one giant loop
- can make things much cleaner (and easier to debug) by making them separate procedures run concurrently
timing
- we've seen time come up with a few commands
- wait
- every
- sometimes wait isn't enough
- Handy Logo has a built in timer that counts by tenths up to ~64s (I wouldn't go past 30)
- two associate commands
- resett: resets the timer
- timer: gets the current value of the timer
- look at procedures in
timer.txt code