====== Software Setup ====== - Open the Visual Studio Code from the Applications directory on the flash drive - Click on the small green square in the bottom left corner of the screen\\ \\ {{ :pibot:challenge1:softwaremac:s1.png |VSCode Main Screen}} - Select the "Connect Current Window to Host..." option\\ \\ {{ :pibot:challenge1:softwaremac:s2.png |VSCode Remote-SSH}} - Type "pi@pibot##.local" and press enter\\ \\ Replace the text "pibot##" with the label on the pibot The password to enter when prompted is "DogsAndCatsAreNice2." without the quotes. The '.' at the end is important! It may take a couple of minutes while it installs some files on the PiBot {{ :pibot:challenge1:softwaremac:s3.png |VSCode Remote Connection}} - Click the "Open Folder" button\\ \\ {{ :pibot:challenge1:softwaremac:s4.png |VSCode Open Folder}} - Select the "pibot" folder and click the "OK" button\\ \\ {{ :pibot:challenge1:softwaremac:s5.png |VSCode Browse}} - Select the picar-line-tracking.py file in the list on the left\\ \\ {{ :pibot:challenge1:softwaremac:s6.png |VSCode picar-line-tracking.py}} - Select File->Save As... and save the file as picar-line-maze.py\\ \\ {{ :pibot:challenge1:softwaremac:s7.png |VSCode picar-line-maze.py}} - Use the information below to change the code so that the PiBot will solve the maze - After the changes to the code are complete, save it with File->Save ===== Software Explanation ===== * Read through the presentation found [[https://www.pololu.com/file/0J195/line-maze-algorithm.pdf|here]] to learn how to make a robot solve a maze\\ \\ Pay special attention to the section on Intersection and Turn Handling starting on slide 26 * Make the changes to handle all of the decisions for the intersections in the while loop at the end of the file * Create new functions for any code that you need to use multiple times and to keep the code in the while loop readable * Use a list to save the decisions so that the PiBot can complete the maze in the shortest path the second time through\\ \\ To make a variable a list, assign a value to it inside of square brackets listvar = [] To add a new value to a list use the append method listvar.append("L") To remove items from the list use the pop method listvar.pop() # Remove the last element listvar.pop(0) # Remove the first element listvar.pop(1) # Remove the second element listvar.pop(-1) # Remove the last element listvar.pop(-2) # Remove the second from last element To access the value of a particular item in the list place the index of the item in square brackets. The index of the first item is '0'. Negative numbers start at the end of the list. myval = listvar[3] # Retrieve the 4th element myval = listvar[1] # Retrieve the 2nd element lastval = listvar[-1] # Retrieve the last element almostlastval = listvar[-2] # Retrieve the 2nd to last element A variable can also be used as the index. In that case the value of the variable must be a number and will be used to determine the index for the value that is returned from the list.ivar = 3 ivar2 = ivar + 3 myval = listvar[ivar] # Retrieve the 4th element myval2 = listvar[ivar2] # Retrieve the 7th element * The logic to drive the PiBot along the line need to be made as fast as possible, but the decisions that determine if it is in an intersection need to be done more slowly * Use a loop counter in the while loop and only make decisions on finding the intersections every 'n' loops * There may be instances where the PiBot will "see" an intersection in the middle of the line where there isn't one as it is driving along * Print out information as the PiBot makes decisions to help debug if things don't go the way you expect * The PiBot wheels will be placed on the starting line, so to the sensors it will look just like a straight line ahead * The PiBot must stop when it gets to the box at the end of the maze * The program can pause when it reaches the end of the maze and wait for a keypress to tell it to start again when it's back at the start * Use the "input" function to wait for an "enter" keypress * A more advanced way to handle the restart is to save the path to a file and then quit when it reaches the end. That way it can read the file at the beginning if it exists and run the same maze even after it is turned off. See [[https://www.geeksforgeeks.org/reading-and-writing-lists-to-a-file-in-python/|this link]] for information on how to read and write lists to files * If you want to get really fancy, have the program get the file name from a parameter entered when it is run so that multiple mazes can be saved and re-run whenever desired\\ \\ The sys module will need to be imported to get parameters from the command line. The argparse module is helpful to make defining and getting parameters easier. Google is your friend to figure out how to implement this * For an extra challenge write the program so that the PiBot can go back through the maze in reverse, end to finish, instead of carrying it all the way back to the start for its 2nd run.\\ \\ Not only will the decisions be in reverse order, the turns will also be backwards. * Bonus points if you make the PiBot do a "Happy Dance" when it reaches the end of the maze :-)\\ \\ Great embarassment will be yours if it performs its "Happy Dance" before reaching the end of the maze :-( [[:pibot:challenge1#software|Return to Challenge 1]]