FlexSEA Wiki

A WEARABLE ROBOTICS TOOLKIT

User Tools

Site Tools


pythonscripts2

Python Interface 2.0

To learn the basics and how to install Python please refer to the original article: Actuator Package: Python scripts.

The new python interface addresses some short comings of the previous version. The main features are:

  • Multiple device connections possible
  • Improved serial and communication management, all abstracted away from the user

The new interface is available from the dev branch of the git repo under the Python folder (Not sure how to clone a repo? Please refer to Source Code). The new access functions are documented in pyFlexsea.py. Take a look at the new python demo scripts to see examples of the interface usage, and look here for an overview of the API functions.

Quick Start

Ready to get started? Try running a demo script:

  1. Make sure your device has recent enough firmware (at least as recent as August 2018)
  2. Clone the repo (git clone -b dev –recursive https://github.com/DephyInc/Actuator-Package)
  3. Copy the appropriate lib into a build folder. You may have to create that folder.
    • All prebuilt libs are found under the release folder
    • For windows, copy release/win/libfx_plan_stack.dll to fx_plan_stack/build/win
    • Ensure the redistributable DLLs (libgcc_s_dw2.dll, libstdc++-6.dll, and libwinpthread-1.dll) are in the Python directory.
    • For raspi, copy release/raspi/libfx_plan_stack.so to fx_plan_stack/build/unix
    • For linux, copy release/linux/libfx_plan_stack.so to fx_plan_stack/build/unix – (sorry, not released yet!)
  4. In a text editor:
    1. Open flexseapython/com.txt and write down your port(s) name (ex.: COM5 or /dev/ttyACM0)
    2. Open fxMain.py and make sure that FLEXSEA_DEVICES = 1 for a 1 DOF test
  5. Plug in and turn on the device
  6. From cmd / terminal, run the fxMain.py script in python
    • If your device has never had poles configured, you should run “find poles” from the demo script before trying other demos

If you encounter problems with the DLLs, refer to the General Hints Page for helpful actions.

Converting from Interface 1.0

The main functionality change is that the new interface allows you to connect to multiple devices at a time. This means that interface needs some way for you to specify which device you intend to interact with at any given time. This handle is the device id. You can get a full list of available device ids by calling fxGetDeviceIds(). Thus for the most part, function calls will change in the following format:

foo(args…) → foo(devId, args…)

This is true of the following functions:

  • actPackFSM2
  • findPoles
  • setControlMode
  • setMotorVoltage
  • setMotorCurrent
  • setPosition
  • setZGains

There are however several changes to the interface that should simplify your code:

Initialization

The utility function loadAndGetDevice() should simplify your initialization to one line. It can be used in two ways

loadAndGetDevice( filename, numDevices)

  • filename contains the path to a txt file where each line is a com port
  • numDevices contains the number of devices we expect to be connected

loadAndGetDevice( comPortList )

  • comPortList contains a list of strings specifying ports to open
  • numDevices is implicitly set to len(comPortList)

The function opens all specified COM ports and waits until fxGetDeviceIds provides a list of ids that is at least numDevices long. On success, the function returns this list. Otherwise the function calls sys.exit().

If you'd like to write the initialization yourself, note the following changes in API functions:

  1. initPyFlexsea() → loadFlexsea()
  2. initControlVariables() → No longer needed
  3. setPyFlexSEASerialPort(s) → fxOpen(port, idx)
    • instead of creating a serial object and passing it to the interface, you simply give the port name and port index
    • the port index isn't really used after this, so as long as you use a different index for each port name you should be fine

There are however, several exceptions which should mostly simplify your code:

  1. initPyFlexsea() → loadFlexsea()
  2. initControlVariables() → No longer needed
  3. setPyFlexSEASerialPort(s) → fxOpen(port, idx)
    • instead of creating a serial object and passing it to the interface, you simply give the port name and port index
    • the port index isn't really used after this, so as long as you use a different index for each port name you should be fine

Streaming and Reading Data

Since the interface now abstracts communication protocols from the user this process is greatly simplified. To start streaming data from the device (and simultaneously stream control commands to the device) use the following

setSuccess = fxSetStreamVariables(devId, varsToStream) #varsToStream is a list of field ids taken from pyFlexsea_def.py
streamSuccess = fxStartStreaming(devId, frequencyInHz, shouldLogToFile, shouldUseAutoStreaming) 
#frequencyInHz must match a frequency in FX_VALID_FREQUENCIES from pyFlexsea_def.py

Note: The same function is used for different devices (rigid vs. pocket). You are expected to provide field ids that are valid for the device specified by devId.

The interface starts streaming data from the device, and populating an internal buffer with received data. The process for reading data is as follows:

dataValues = fxReadDevice(devId, fieldIds) #fieldIds is a list of field ids whose values are retrieved

dataValues is then populated with the values of the requested field ids, in the order requested. If an error occurred while fetching a field (fieldId invalid, or field not accessible because it's not being streamed) the value will be None.

Printing Data

Previous utility functions printRigid() and printPocket_s are replaced by printData(labels, values), where you should provide labels, a list of string which matches the length of values.

Sending Controller Commands

All controller commands simply take the device id handle, and are mentioned in the above section.

Building from Source

Before you start, install the following tools:

Ubuntu:

  • CMake
  • git
  • g++ (should come pre-installed)
  • python3 (32 / 64 bit should match your g++ version. default is 32 bit)

Windows:

  • CMake
  • git
  • MinGW_w64 (install the 32 bit version)
  • python3 (32 bit version)

Detailed instructions: Install Build Tools (Windows).

First, navigate to the directory you'd like to contain all the source code.

Clone the code repositories:

git clone https://github.com/DephyInc/serial.git
git clone --recursive https://github.com/DephyInc/FlexSEA-Stack-SharedLib.git
git clone --recursive https://github.com/DephyInc/Actuator-Package.git

Compile the serial library:

cd serial
git checkout dev
mkdir build && cd build

cmake .. #See note 1)
make #See note 2)

Compile FlexSEA-Stack-SharedLib (see note 3):

cd ../../FlexSEA-Stack-SharedLib
mkdir build && cd build
cmake ..
make

The libs are built inside a directory depending on your platform. 'win' for windows, 'unix' for unix. Copy the libs from the build directory to the folder where they'll be used. You can do that graphically, or via command line. What you want is to have libserialc.a and libFlexSEA-Stack-Plan.a in the fx_plan_stack\lib folder.

cp serial/build/unix/libserialc.a Actuator-Package/fx_plan_stack/lib/
cp FlexSEA-Stack-SharedLib/Release_static/libFlexSEA-Stack-Plan.a Actuator-Package/fx_plan_stack/lib/

Compile fx_plan_stack:

cd Actuator-Package/fx_plan_stack
mkdir build && cd build
cmake ..
make

Test the lib:

  1. First, make sure that Actuator-Package\Python\flexseapython\com.txt has the right COM port.
  2. Navigate to the Actuator-Package\Python folder.
python fxMain.py

Notes:

  1. For Windows, for all CMake steps, add -G “MinGW Makefiles”. Instead of simply calling
    cmake ..

    call

    cmake .. -G "MinGW Makefiles"
  2. If 'make' doesn't work try using 'mingw32-make.exe' instead.
  3. This repo includes an Eclipse .cproject file; you can use use that instead of CMake
pythonscripts2.txt · Last modified: 2018/10/16 17:40 by jfduval