Using your Robot 

Making sure IDLE can find your files

Note:  This is for each time you start IDLE. This is not a one time fix for IDLE locating your files.

     For Windows:

     Note:  if you do not follow these steps (selecting "Edit with IDLE") then when you try to run a python file or program a python file
               onto your robot, IDLE will not be able to find the folder that contains the python files you are looking for.

     1)  open the folder with the python files you wish to run or program on the robot
     2)  right click on a python file that you wish to open and select "Edit with IDLE" --> this will open the file and an IDLE window
          (this window will say "==== No Subprocess ====" above the >>> prompt, but the window will still work properly)
     3)  now IDLE will be able to find the files in that folder to run and program on your robot

     For Mac:
 
     1)  open a terminal
     2)  type cd and the entire path of where your python files are. Example:  cd /Users/James/engi128/hw1 with your files in the
          hw1 folder
     3)  type idle &
     4)  now IDLE will be able to find the files in that folder to run and program on your robot

 
      Remember to type import robot at the >>> prompt when you first open IDLE before trying to connect to or program your robot


Helpful hints/tips

1)  in several of these step by step instructions, you need to type import robot. Each time you open IDLE, this step only needs to be
     completed once. For example, you do not have to import robot after connecting to your robot if you then decide to program your robot.
2)  in IDLE (at the >>> prompt and the robot> prompt), to cycle through past entered commands:
     Note: these keys can be changed --> see "How to change the keys to cycle through past entered commands" (in Setting up IDLE)
     a)  default keys for Windows hit alt + P for up and alt + N for down
     b)  default keys for Mac hit ctrl + P for up and ctrl + N for down
3)  in IDLE, when typing strings there is no difference between single quotes and double quotes as long as you remain consistent.
4)  when saving a file, be sure to type ".py" at the end of your file name. (example: your file should be named simon.py not simon) 
     If you don't, you'll see your code be all black instead of having syntax highlighting and the file will not be recognized as a python file.
5)  when you have an error in a file, the error message will give you a line number to look at in your code. In IDLE, the line number is
     stated in the bottom righthand corner of the window (it displays the line number your flashing text cursor is on).
6)  if you want to see what your program is doing, keep in mind that printing out your variables and watching how they change is a good
     way to understand what is happening in a given function.
7)  comments are your best friend. Commenting out a piece of code instead of deleting it, is often a good idea. Also, having comments
     explaining what a variable is or what you are trying to do, is good for when you leave your code and come back to it later. A comment
     in IDLE just means you have at the beginning of the comment. For a section of code, there is a shortcut in the menu to comment out a
     highlighted portion.



Connecting to your robot

0)  plug in your robot (with the robot turned on)
1)  type import robot at the >>> prompt in the IDLE window
2)  type robot.connect("serial port") at the >>> prompt 
     a)  for Windows users example: robot.connect("com5")
     b)  for Mac users example: robot.connect("/dev/tty.usbserial-A700eJSe")
3)  blue text will appear and the prompt will be robot>
4)  type python commands to use the robot

Note: to close your connection to the interactive prompt on the robot and return to the IDLE >>> prompt, hit ctrl d once. If you hit ctrl d
         again, IDLE will close.


Running the GUI tests on your robot (once connected to your robot)

0)  connect to your robot (see above)
1)  at the robot> prompt type import rone
2)  to access functions in the rone module type rone.name_of_function(arguments) --> for example: 
     a)  to run the right motor, you'd type rone.motor_set_pwm("r",80) with the first argument.
          being the left ("l") or right ("r") motor, and the second, the pwm between -100 and 100.
     b)  to access the light sensors type rone.light_sensor_get_value(position) with position being
          front right ("fr"), front left ("fl"), or rear ("r")
     c)  more functions from the rone module (link for other rone functions)


Creating a new Python file

     For Windows:

     1)  you will see a menu bar at the top of the IDLE window
     2)  in the File drop down menu, select New Window or hit ctrl + N which will open a very similiar window
          that says Untitled at the top where Python Shell was on the last window.
     3)  save this file either by ctrl + S or by clicking save in the File drop down menu.  Be sure to type ".py" at the end of your file name.
          If you don't, you'll see your code be all black instead of having syntax highlighting and the file will not be recognized as a python file.
     4)  write your python code and save it. Now you can use this file on your robot.

     For Mac:

     1)  at the top left of your screen you will see a menu bar for the IDLE window
     2)  in the File drop down menu, select New Window or hit + N which will open a very similiar window
          that says Untitled at the top where Python Shell was on the last window.
     3)  save this file either by + S or by clicking save in the File drop down menu. Be sure to type ".py" at the end of your file name.
          If you don't, you'll see your code be all black instead of having syntax highlighting and the file will not be recognized as a python file.
     4)  write your python code and save it. Now you can use this file on your robot.

Running a file on your robot

Note: running a file is a good way to debug. You can make a change in the file, and then simply rerun the file without having to
         reset the robot or reconnect to your robot.

0)  connect to your robot
1)  at the robot> prompt type run pythonfile.py --> example: run simon.py (note that unlike running a programmed file, the .py ending is included)
2)  to access a function from pythonfile.py, type function(arguments) at the robot> prompt --> example: foo()

Note:  if a function call is at the bottom of the file, when running the file, the function will run.



Programming your robot with a single file


0)  plug in your robot (with the robot turned on)
1)  type import robot at the >>> prompt in IDLE
2)  push the reset button on your robot
3)  type robot.program("serial port","pythonfile.py") at the >>> prompt 
     a)  for Windows users example: robot.program("com5","simon.py")
     b)  for Mac users example: robot.program("/dev/tty.usbserial-A700eJSe","simon.py")

Note: if you want this to run by itself you need to have a function call at the bottom of the file.


Programming your robot with multiple files

0)  plug in your robot (with the robot turned on)
1)  type import robot at the >>> prompt in IDLE
2)  push the reset button on the robot
3)  type robot.program("serial port","file1.py","file2.py",etc.) at the >>> prompt 
     a)  for Windows users example: robot.program("com5","drive.py","controller.py") 
     b)  for Mac users example: robot.program("/dev/tty.usbserial-A700eJSe","drive.py","controller.py") 

Note: "file1.py" will be the file run when you press the mode button (red LED button) on the robot. If you want this to run by itself
          you need to have a function call at the bottom of file1.py.


 
Running the previously programmed file(s) on your robot  

     First way:
     
     0)  program the robot with the file(s)
     1)  push the reset button on the robot
     2)  connect to your robot
     3)  once connected --> type import pythonfile at robot> prompt --> example: import follow (note that it's follow not follow.py)
     4)  to access a function from pythonfile, type function(arguments) at the robot> prompt --> example: follow1(0.5)

     Note:  if a function call is at the bottom of the programmed file, when importing the file, the function will run.

     Second way: 

     0)  program the robot with the file(s)
     1)  push the reset button on the robot
     2)  push the mode button (red LED button)

     Note: your programmed file will only run if there is a function call at the bottom of it before being programmed onto your robot.