tview python console
The tview graphical monitoring application has been getting a lot of work lately. First was support for enumerating controllers by UUID, then monitoring faults and graphically highlighting them and then decoding textual values for fault codes. I’m excited to announce yet another improvement for tview, this one on the larger side: an interactive python REPL (read-evaluate-print-loop)! Available now in pypi moteus-gui v0.3.93.
The bottom console now has two tabs, the first and default remains the diagnostic console, where you can see what diagnostic messages the devices are emitting and send diagnostic channel commands directly to connected devices. The new, second tab, is labeled “Python” and provides access to a fully functional Python evaluation environment with full access to the moteus python library, including asynchronous operations.
Read on to see how to use it.
Basic Operation
Basic operation can be pretty simple. For example:
await controller.query()
Will run a query operation on the first/default controller. Like the CPython REPL, if you type an expression, the result will be displayed at the console.
If multiple controllers are connected to tview, you can access them in two ways. The most consistent is to use get_controller(id), like:
c4 = get_controller(4)
await c4.query()
Which will communicate with the controller with CAN ID 4. The argument to get_controller can be a CAN-ID, a UUID, a moteus.DeviceAddress, or any string that would be usable as a prefix in the diagnostic console.
The full list of controllers is also available as the variable controllers and the current transport is transport, so you can also do things like:
await transport.cycle([c.make_query() for c in controllers])
Functions and multi-line support
As with the CPython REPL, you can define functions and multi-line blocks like while and for loops. For instance, the following is fine:
while True:
print(await controller.query())
await asyncio.sleep(1)
Or you can define synchronous and asychronous functions:
def foo(a, b):
return a + b
foo(1, 2)
async def show():
print(await controller.query())
await show()
Commands
Any python library function can be used, not just queries. You can send commands all while plotting items in tview, or logging all or a subset of channels.
Interrupting
With the ability to create loops, or asynchronous operations that may take a long time to complete, you may start a python evaluation that takes too long to complete. Don’t worry, CTRL-L will break out of the current operation, bringing you back to a prompt the same as in the CPython REPL.
Upgrading
To upgrade your tview to get python support, just use pip:
python -m pip install --upgrade moteus moteus-gui
and that’s all it takes!