SPIKE x Scikit Learn LabVIEW Interface

User Manual

This site is intended to guide users to use the LabVIEW interface "Sklearn_SPIKE_SVM.vi", which aims to demonstrate possible ways to introduce younger students to Machine Learning concepts using LEGO SPIKE Prime as the medium.

Setting up the Environment

Installing LabVIEW and UI Package

  • Install LabVIEW 2020 on your computer from NI's official website.

  • For mac users, please download the 64-bit version. For Windows users, both the 32-bit and the 64-bit version are accepted, but the latter is preferred. Meanwhile, you will need to make sure that the bit version of LabVIEW matches that of Python (more on this later).

  • Download the latest interface from here. Unzip the folder locally on your computer.

Installing Python

Download Python from the official website (Mac or PC). Make sure that you:

  • Are Downloading Python 3.7.7

  • For PC user, please select the installer with the same bit version as LabVIEW. ("x86-64 for 64-bit and "x86" for 32-bit). For Mac users, just download the 64-bit installation package.

  • Note for PC users: When downloading, please remember to select the option "Add Python to PATH" (refer to the screenshot to the right). This ensures that LabVIEW is able to find and launch its Python integration. To check if you have successfully done so, launch a command prompt and type "python" to see if python opens up. If not, you can always add it to PATH manually following this tutorial.

Installing Python Libraries

The easiest way is to use pip. Refer to here if you do not know how to use it. The libraries you need to install are:

Using the Interface

  1. Launching the UI

Once you unzip the LabVIEW folder, open it and open the program names "Sklearn_SPIKE_SVM" and start the LabVIEW program by clicking the "Run" button (small white arrow) on the top-left side of the front panel. You should see that most buttons are grayed out.

Now you have successfully launched the program, your available options include "Exit", "Help" (which goes to this site), and "Connect".

2. Preparing your SPIKE

You need to run the code template locally on SPIKE in order to set up a successful connection between SPIKE and the interface. To do that, you'll need to download the latest official SPIKE App to your computer.

Once you have it downloaded, please go to the LabVIEW folder you unzipped, and navigate to "SPIKE_Sklearn_SVM\python_code\spike", and open the file "usb_sklearn_baton" in SPIKE's official app. Using the app, upload the code locally to your SPIKE hub. See SPIKE's website for details on how to use the SPIKE APP.

Before you proceed, please make sure that you have followed the placemat instruction to build your own conducting baton! Link here.

3. Connect up your SPIKE!

Power up your SPIKE Hub and connect your hub to your computer via USB. Use the buttons to navigate to the script you just uploaded, then click the muddle button to run the program. Wait a few seconds for the program to start running.

From the drop-down menu at the top-left under "Initialization", select the port that your SPIKE shows up as. (one quick way to find out the port is by unplugging and plugging in your SPIKE to the computer, while clicking the "refresh button').

Click on "Connect". If you see a "success" window popping up, then you SPIKE is successfully connected!

4. Record Training Data

The next step is to train your smart baton by recording data of different gestures with corresponding labels.

To record a new training instance, click "New Instance" and wait for a dialog box to pop up. Input your training label (any string, but preferably single letters) and hit "start recording." You will hear a quick beep sound from SPIKE indicating that it is ready to record data.

5. Performing a Gesture

To perform a gesture, press and hold the force sensor connected to port A and swing. The hub will record the roll, pitch, and yaw angles across the half-second interval right after you start pressing the sensor. After you finish your gesture, release your force sensor, and you should see the training data in the indicator box below the "New Instance" button.

You can now continue to record new training instances. If you don't like the previous data you recorded, simply press the "Delete Prev" button.

6. Visualize Training Data

The "Training Data" tab presents the recorded data in its raw format (a 1D array of roll, pitch, and yaw angles of the hub). If you switch to the "Graphs" tab, you will see three separate graphs which corresponds to each Euler angles , which will better help you visualize the gesture.

7. Train Your Model

Once you are happy with you training data, you can now hit the magic button, "Train Model", and a dialog box will pop up for you to setup your training parameters. If you are not sure what to change, simply press the "Train" button, and the program will train the model for you under the default parameters (in our application, this is well enough 90% of the time!).

8. Model Trained

The algorithm used here is called Support Vector Machines, or SVM (more detail on scikit-learn's website). The parameters of the model can be found under the "Model Info" tab.

If you see this window pops up, it means that the amount of data taken is not enough to generate earning curves This wouldn't affect the performance of your model, so you shouldn't be worried.

SVM automatically attempts to break down your training data into several buckets, and not having enough data would result in buckets that only contains one type of label, which will cause the program to throw an error.

9. View Learning Curve

If you would like to see the learning curve, simply press "New Instance" and record more data. after you have at least 5 o 6 data for each distinct label, you should be able to train the model and see the learning curves.

The general trend for the curves are that with more training data being considered, the more accurate the model is going to become.

10. Deploy Your Model

When you are happy with your model, now let's go ahead and deploy the trained model back to SPIKE. Hit the "Deploy Model" button and wait for the green LED next to it to light up (you might have to hit it twice).

You should see the letter "d" showing up on your SPIKE screen, indicating that it has entered deploy mode. If you press the force sensor and perform a gesture now, it will try to predict the gesture based on the model and display the category both on the light matrix and in the LabVIEW UI.

11. Save Model & Load Existing Model

Finally, you can also hit the "Save Model" and "Load Model" buttons to save your model locally or load an existing model.

All models will be saved as a ".joblib" file. The default path is "SPIKE_Sklearn_SVM\python_code\models" .

Common Problems & Solutions

Q: I can't find SPIKE in the "Port" drop-down list. What should I do?

A: This could be because you do not have NI-VISA installed. Download and install the plug-in from here. If the problem still exist, you can also input it manually. Specifically:

  • Windows: Go to "Device Manager", and see what port your SPIKE shows up as under the "Ports" crop-down, and copy the name (usually called "COMx", where "x" is a number) into the "Port" field in the LabVIEW UI.

  • Mac: open up a terminal and run: ioreg -p IOUSB -l -w 0


(More FAQs are on the way...)