= Intermediate Control Systems Training With the Zebra Zero (Z0) Platform = == Introduction == In a class on programming, you'll learn about how to write code, run it, and all the tricks on how to use the language to its full potential. In Robotics, you'll learn how to make programs you write interact with the outside world--input and output, sensors and motors. If you put all of this together with a mechanism, wiring, and power, you get a robot. To help you understand this concept, not only will you be writing code, you'll be interfacing circuits to the computer, and controlling things like lights. Once you master the basics, you can write fancier code and more advanced circuits to do whatever you want. === Getting Ready: WHAT'S IN THE BOX!?! === [[Image(whatisinthebox.gif,left,100px,margin=15)]] ''' BEHOLD! ''' The box before you contains a Zebra Zero (Z0) development environment. Please make sure the box contains the following items: * A Zebra Zero board, with a Raspberry Pi Zero W, and a breadboard. Note the label on the bottom of the Zebra Zero. * In the Zebra Zero board there is a microsd card. Don't touch it. * A USB to microsd cable. * A bag with 3 LEDs and 3 resistors. * Some jumper cables. * A microsd-to-sd card adapter === What is a Raspberry Pi Zero W? === A [https://www.raspberrypi.org/products/raspberry-pi-zero-w/ Raspberry Pi Zero W] is a tiny $10 fully functioning computer with Wifi and Bluetooth that uses a microsd card as a hard drive. Its suitable for embedded applications with its 40-pin GPIO (General Purpose Input and Output Connector). Power is provided by a microusb port, and it can provide hdmi video. Typically it runs Raspbian Linux, a free Debian based operating system. It's big brother is the Raspberry Pi 3. [[Image(dh9kiPi.jpg,30%,align=center)]] === What is a Breadboard? === A [https://learn.sparkfun.com/tutorials/how-to-use-a-breadboard breadboard] is an easy way to build a circuit without soldering or other hard connections. [[Image(64-00.jpg,30%,align=center)]] == Your First Software Control System Activity: Output == === Getting Started === In this activity, you'll be writing software to control LED lights. First, some setup... 1. If it's not done so already, install [https://support.apple.com/kb/DL999?viewlocale=en_US&locale=en_US Bonjour Print Services] and [https://www.bitvise.com/ssh-client-download Bitvise SSH client] on your notebook, then reboot your notebook. The first one lets you contact the Pi Zero by a simple name, and the second one is a client you use to communicate with the pi. When you use the SSH client to talk to the Pi, you are actually '''on''' the Pi. If you run code in the terminal window, it runs on the Pi. 1. If you haven't done so already, Plug the usb end of the cord into your computer and the microusb cord into the microusb cord on the zebra zero. Note that there are two ports on the Pi Zero W, put it in the one near the middle of the board (the one marked USB, *not* Power). Wait a moment until all the drivers install. 1. Run Bitvise SSH client that you installed in step 1. Now, look at the bottom of your pi. BEHOLD! You'll see the connection information for this particular Zebra Zero unit, for example: * `pi@spam.local/spam1` * This means use `spam.local` for the host in Bitvise, `pi` for the username, and `spam1` for the password. Each Zebra Zero (Z0) unit has different connection information. * Click the Login button. 1. It may take a minute to connect to the Pi. Be patient. If it asks you to Accept and Save a key, do it. 1. I SAID BE PATIENT! 1. A text window will open up with a prompt that looks like `pi@hostname`. That's a Linux Prompt. Now you're good to go. 1. From not on we call this process '''logging into the z0''' 1. When you're done with the Z0, run ```sudo shutdown now``` before unplugging the power. === So, What's Linux?=== Linux is an operating system like Microsoft Windows that often runs on computers in professional software environments. It is command line based which means you often just type the commands you want to do. We can't teach you everything about Linux here, but here's a few commands to get you started. All work on a Linux system is done under a user, in this case pi. There can be many users on a machine in Linux. Files and executable organized into directories and you may not have the ability to run or see everything. The Z0 platform is preconfigured so you start out in the dev directory with everything you need. See a mentor or advanced student if you run into issues. * `cp ` -- Rename a file * `rm ` -- Delete a file. Obviously, be careful with this one. * `cd ` -- Change your current directory (leave blank to go to your home directory) * `ls ` -- List all files in a directory (leave blank for the current directory) * `nano ` -- Edit a text file. `Ctrl-O` saves. `Ctrl-X` exits. * `compile .java` -- Compiles a java class file (we made this command special for you all) * `run ` -- Runs a java class file. No extension (we made this command special for you all) When you first log in, you start out in the dev directory for the pi user. There's one file in that directory, `TestDriver.java`. == Exercise 1: Make an LED blink in software with the Z0 == Your first exercise is to enter a program to turn a LED on and off with a circuit you assemble on the Z0's breadboard. === Assemble the circuit === To make the Pi Zero W on the Z0 board control an LED, you need to connect an LED to the GPIO connector of the PI. Using a resistor, led, and jumper wires, assemble the circuit shown in the diagram below. [[Image(1led_bb.png,500px),align=center]] === Write the program === 1. '''Log in to the Z0''' (see above). 1. Edit the `TestDriver.java` program by executing `nano TestDriver.java` 1. Modify the file to the following (replace the section marked "PUT CODE IN HERE" with the corresponding section below). Do a `Ctrl-O` save, and `Ctrl-X` exit when done. {{{ #!Lineno #!java import com.pi4j.io.gpio.*; /** * Test Driver for Pi Programming--Team 2537 */ public class TestDriver { public static GpioController gpio; public TestDriver() { System.out.println("Staring Pi Test Driver"); // Set the pins to match the numbering on the pi zero w GpioFactory.setDefaultProvider(new RaspiGpioProvider(RaspiPinNumberingScheme.BROADCOM_PIN_NUMBERING)); // Create gpio controlelr gpio = GpioFactory.getInstance(); } public void runTest() throws InterruptedException { //=========================================== // PUT CODE IN HERE //=========================================== // provision gpio pin #12 as an output pin and turn on final GpioPinDigitalOutput pin = gpio.provisionDigitalOutputPin( RaspiBcmPin.GPIO_12, "MyLED", PinState.HIGH); // set shutdown state for this pin pin.setShutdownOptions(true, PinState.LOW); // Blink away! while(true) { System.out.println("LED on"); // Turn pin on pin.high(); // Wait a second Thread.sleep(1000); System.out.println("LED off"); // Turn pin off pin.low(); // Wait a second Thread.sleep(1000); } //========================================== // PUT CODE IN HERE //========================================== } public static void main(String[] args) throws InterruptedException { try { TestDriver d = new TestDriver(); d.runTest(); } finally { gpio.shutdown(); System.out.println("Exiting Test Driver"); } } } }}} ==== So, what does all code that mean? ==== {{{ 1. Includes the Pi4J library so we can talk to the GPIO connector. 6. The Java class name. The file must be named the same as the class.java. 7. The variable that represents the connection to the GPIO connector. 9. The constructor for '''TestDriver''' 12. Tells the Pi4J library to use the numbering scheme printed on the Z0 board. 13. Gets a connection to the GPIO library and stores it in the variable in 7. 18. This method runs the test. It does all the real work to blink the light. 24-25. Sets up GPIO Pin 12 as a digital output, defining that setting the pin is "HIGH" or "ON". 28. When the program stops, turn off all the pins. 31-43. Infinitely loop, turning the pin on (high) waiting a second (1000ms) then off (low), waiting, then repeating. 50-50. This is what is actually executes when you run the '''TestDriver''' class. It creates the '''TestDriver''' class, calls "runTest", then exits gracefully when done. }}} 4. Compile the program by running `compile TestDriver.java`. Wait a bit for it to complete, and if it reports errors, be sure to fix them. 5. Run the program by executing `run TestDriver`. In a few moments you should be rewarded by a blinking led. Truly an amazing sight! 6. All done. We'll be using this circuit and code as a basis for future exercises. === Exercise 2: Your Turn--Make Three LEDs blink in sequence. === Now that you have made one LED blink, now make three LEDs blink one at a time in a sequence. Start with the circuit and code for one LED above, and add to it. Start with improving the circuit then the code ('''HINT:''' GPIO Pins on the Raspberry Pi Zero W that are labeled with numbers are fair game to use) === Advanced Exercise 3: Make an 8-Digit LED Display Count Down === Feeling fancy? Now that you have improved the program, you're ready to try something more advanced. You'll be doing research and working on some serious code for this one. Try to make the program as small as you can. Make the program count down from 9 to 0 once per half second on the 8-segment LED display (as a mentor for one). When you hit 0 blink the dot on the display 5 times. To use the 8-segment display, be sure to find the right spec sheet online for it. Work in groups for this exercise, and be sure to ask the mentors for extra resistors and jumpers as needed. == MORE EXERCISES == Coming soon!