State Machine Exercises

From Tekkotsu Wiki

Revision as of 02:06, 1 February 2016 by Touretzky (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Combination Lock

Write a state machine to make the robot act as a five-digit combination lock, in binary. Assume the secret combination is 10110. Use the Play button for 0 and the Advance button for 1. If the user enters the correct button sequence, your behavior should say "Unlocked". If they enter an incorrect sequence, then after the fifth button press your behavior should play a growling dog sound (use "growl.wav") and allow the user to try again.

  • Why would it be a bad idea to let the user know immediately if they got one of the digits wrong, instead of waiting for them to enter all five digits first?
  • Use the Storyboard tool to create a graphical layout of your state machine. What is the best way to arrange the nodes?

Parallel Actions

Read about fork/join in Tutorial: Parallelism and Multi-Way Transitions. Write a state machine to cycle repeatedly through three sound and light actions. The first action should light the green power LED and play the "ping.wav" sound. use LedActivate(PowerGreenLEDMask) to light the LED. The second action should light the Play LED (use PlayLEDMask) and play the "barkmed.wav" sound. The third action should light the Advance LED (use AdvanceLEDMask) and play the "camera.wav" sound. Each action should light the LED and play the sound simultaneously, and the action should terminate when the sound finishes playing.

  • What other LEDs are available on the Create/ASUS robot? Look in Tekkotsu/Shared/CreateInfo.h to find out.
  • Modify your program so that each time the user presses a button, the sequence reverses direction. This should work no matter where the robot is in the sequence when the button is pressed.
  • Create a layout and execution trace using the Storyboard tool.

Note: if you are using a simulated robot (Tekkotsu currently does not simulate LEDs), or you're using a robot that doesn't have any LEDs, you can simulate the LED by defining a node that prints a text message like "Red LED is on".

Concurrently Active States

If you haven't already done so, read about fork/join in Tutorial: Parallelism and Multi-Way Transitions. Write a state machine the runs two sequences in parallel, asynchronously. The first sequence should light the green power LED for 3 seconds, then the red power LED for 1 second, and repeat. The second sequence should play a these three sound files in order: barklow.wav, barkmed.wav, and barkhigh.wav, and repeat.

  • Use the Storyboard tool to produce an execution trace of your state machine.
  • Modify your state machine to respond to a button press by reversing the direction of the sound sequence and swapping the times of the LED sequence.