VEXcode V5 Python REPL (console)

VEXcode V5 preview 1 was released earlier this week with Python support. The way Python is implemented is a little (well, a lot really) different from C++.

A program written in C has to be converted from the text source into a machine executable, this is done by compiling the source code and then linking with libraries that allow the program to operate on the V5.

A VEXcode Python program is sent to the V5 brain as text, a special program that is installed on the V5 brain then uses that file directly to execute the program (although we actually compile the Python source into byte code on the brain before executing). The program installed by VEXcode on the brain is called the Python VM (virtual machine), it can provide some capabilities that you do not have with programs written in C++, one of those is a console which is typically called the REPL (Read-Eval-Print Loop)

To use the Python REPL you need another program that can communicate with the V5 on the serial port we have dedicated to user programs. On the Mac this is easy, start up the terminal and use the screen command. On windows it’s a little more difficult as software has to be installed, one option is PuTTY but there are others.

To use PuTTY you need to know which serial COM port the V5 is using (all this assumes direct USB connection, it does not work wirelessly via the controller although BLE support is possible), open up device manager and check the ports category.

py1
The V5 is using COM3 and COM4, the one we are interested in is COM4, the “VEX V5 User Port”.

Launch PuTTY, select the serial radio button, enter the COM port and set baud rate to 115200 (although that probably doesn’t matter).

py2

Write a small Python program in VEXcode, download and run, you will see the Python banner and anything you send to the console using the print command.

py3

You can now interact with Python just as if you were writing a program in VEXcode, however, these are temporary commands and any devices or variables you create will be lost when the V5 brain runs another program or is turned off.

Hitting tab will show a list of currently know commands and objects.

py4

There’s quite a lot as we import everything from the vex module for convenience.

now you could create a motor instance.

m1=Motor(Ports.PORT1)

ask the motor to spin

m1.spin(FORWARD, 10, RPM)

if at any point during entry you forget what the command is you can hit tab, it will show current options.
perhaps ask for motor position

m1.position()

and then send stop

m1.stop()

Here are what those commands look like in PuTTY.

py5

This is a great way to experiment with commands and learn what’s possible, if you have a Python program on the V5 there’s even no need to fire up VEXcode to just play with the REPL, just run the program with PuTTY connected.

note. VEXcode V5 Pro also has a terminal program integrated, it’s not as powerful as PuTTY and does not support the VT100 command set that is really needed for the REPL. Make sure VEXcode V5 Pro is closed to use PuTTY or disable the terminal in its preferences.

16 Likes

There are, however, some gotcha’s. A program that blocks will not allow the REPL to run, this is because we run the REPL after the code has finished being interpreted. code that has a blocking loop like this will not show the REPL.

# Library imports
from vex import *

# Begin project code
brain.screen.print("Hello on the brain")

my_var=0
while True:
    brain.screen.print("running... ", my_var)
    brain.screen.next_row()
    my_var=my_var+1
    wait(1, SECONDS)

The code runs the while loop forever, REPL does not run. The solution is to run the blocking while loop in another thread, you could create a second thread to run that code as follows.

# Library imports
from vex import *

# Begin project code
brain.screen.print("Hello on the brain")

def my_function():
    my_var=0
    while True:
        brain.screen.print("running... ", my_var)
        brain.screen.next_row()
        my_var=my_var+1
        wait(1, SECONDS)

t1=Thread(my_function)

place the blocking loop inside a function and execute as a second thread using the Thread class (there are more examples of using threads and events in VEXcode V5)

4 Likes

Thanks for the writeup, this looks super useful!

Does this mean that a python program on a brain could be copied to a computer and viewed intact? Or is there some processing that happens during the download process that makes this infeasible?


Any particular reason the serial data can’t be sent over VEXnet? Is support for doing this over bluetooth planned?


Are global variables declared in the running program accessible from the REPL? For example, if I write and run a python program that just declares a bunch of motors, and then access the REPL in putty/screen, can I drive those motors without needing to re-declare them in the REPL?

Lastly, are there any plans to add python support to VEXcode V5 pro?

2 Likes

yes, VEXcode will add some small boiler plate code at the beginning, but basically the code you see is the code that is executed.

The only issue is that you would need a special terminal program like the one we have built into VEXcode V5 Pro that understands how to demultiplex the output from the user code when sent over vexnet. The controller has a single USB serial port that is shared between admin (ie. download etc.) messages and stdio from user programs. Direct USB uses separate serial ports as does the BLE connection.

yes, anything declared in the user program is available, variables, functions, any other object.

yes, for example. in the program below I declared a motor, a thread and have a function.

py6

when I hit tab in the REPL, you can see those objects. I asked for the value of “motor1” and it shows some basic information (class and port)

Tim said so here.

5 Likes

so this is the exact code sent to the brain for that small program above, we just declare an instance of the brain, add one more imported module that’s needed by blocks and any devices created in the graphical configuration.

code
from vex import *
import urandom

# Brain should be defined by default
brain=Brain()

# Robot configuration code


# ------------------------------------------
# 
# 	Project:      VEXcode Project
#	Author:       VEX
#	Created:
#	Description:  VEXcode V5 Python Project
# 
# ------------------------------------------

# Library imports
from vex import *

# Begin project code
brain.screen.print("Hello on the brain")

motor1=Motor(Ports.PORT4)

def my_function():
    my_var=0
    while True:
        brain.screen.print("running... ", my_var)
        brain.screen.next_row()
        my_var=my_var+1
        wait(1, SECONDS)

t1=Thread(my_function)
7 Likes