Running Thymio Wireless with Python on Ubuntu Virtual Machine

I have written a post on running Thymio using Raspberry Pi. Somehow the wireless connection was not working for me even after I plug the usb wifi. So I decided to try using Ubuntu Virtual Machine on Windows host. And it works.

Setup:

  • Host OS: Windows 10
  • VirtualBox
  • Virtual Machine OS: Ubuntu 16.04 LTS

After finish installing Ubuntu on the VirtualBox. These are the steps:

  1. Install aseba following the steps here. The official documentation provides steps for Ubuntu.
  2. Install dbus: sudo apt-get install python-dbus
  3. Install gobject: sudo apt-get install python-gobject

Now you can follow the steps to run the python code:

  1. run asebamedulla: sudo asebamedulla “ser:name=Thymio-II”
  2. run python code on a different terminal: sudo python pithymio.py
Advertisements

Running Python on Thymio

I found a link that describes the process here:

http://www.adambowes-portfolio.com/blog/2014/11/3/thymio-ii-control-with-python

The setup that I use is Raspberry Pi 2 Model B+ running Jessie. First we need to setup the hardware connection:

  1. Connect USB from Pi to Thymio
  2. Connect power from socket to Pi
  3. Start Pi

Now, we need to install some software. There is a bug in the software provided by Thymio websites, so I found a forum where they provided the fix for Pi. On your pi, open terminal, and type the following:

sudo apt-get install apt-transport-https

This is to enable apt-get to get source from https.

 

Now, we add the source by editing /etc/apt/sources.list:

sudo sh -c 'echo "deb https://dl.bintray.com/msprunck/Aseba jessie main" | sudo tee -a /etc/apt/sources.list'
sudo apt-get update
sudo apt-get install libenki libdashel aseba

After it is installed, we can test if asebamedulla is running. To do this, open a new terminal, and type:

sudo asebamedulla "ser:name=Thymio-II"

If it works, it will output something like the following:

Found Thymio-II on port /dev/ttyACM0

Now, download the sample code. Open a new terminal or a new tab, and run the sample code as follows:

sudo python pithymio.py

It will output:

dbus.Array([dbus.STring(u'thymio-II')], signature=dbus.Signature('s'))
0 0 0 0 0

At this point, it waits for you to enter the command.

  • Type “w” and press Enter to move forward for 1 second.
  • Type “s” and press Enter to move backward for 1 second.
  • Type “a” to rotate to left
  • Type “d” to rotate to right

Type CTRL-C to exit.

 

Installing zenburn theme on Aquamacs

I followed the steps in https://github.com/bbatsov/zenburn-emacs to install zenburn theme using MELPA. But I had difficulty making it stick the next time it loads. These are the steps that works with me:

  1. Run Aquamacs
  2. Type: M-x package-install zenburn-theme
  3. I also added (load-theme ‘zenburn t) to “~/Library/Preferences/Aquamacs Emacs/Preferences.el”

But when I close and run Aquamacs again it is not exactly zenburn as the lower bar text are too dark. So I need to load the theme manually:

M-x load-theme zenburn

To make it stick, I follow the steps in: https://www.emacswiki.org/emacs/AquamacsFAQ#toc21

  1. With zenburn theme loaded, I go to: Menu -> Options → Appearance → Auto Faces → Use Current Face as Default
  2. And I do the next step: Menu → Options → Appearance → Adopt Face and Frame Parameters as Default
  3. Answer “Yes” to the dialog.
  4. Exit Aquamacs and “Y” to save the settings.

Multiple LDAP authentication in Django

Django allows multiple LDAP configuration. The example of the configuration is given here: https://pythonhosted.org/django-auth-ldap/multiconfig.html

To do this we need to create a Python module under site-packages. For example we can create a module called ‘mypackage’.

$ ls /home/administrator/websenv/lib/python2.7/site-packages/mypackage
__init__.py ldap.py

We can leave __init__.py empty, and write the following entry into ldap.py:

#ldap.py
from django_auth_ldap.backend import LDAPBackend

class LDAPBackend1(LDAPBackend):
 settings_prefix="AUTH_LDAP_1_"

class LDAPBackend2(LDAPBackend):
 settings_prefix="AUTH_LDAP_2_"

class LDAPBackend3(LDAPBackend):
 settings_prefix="AUTH_LDAP_3_"

Now, we can configure the ldap in Django settings.py. This is an example of a configuration in Django.

import ldap
from django_auth_ldap.config import LDAPSearch


AUTHENTICATION_BACKENDS = (
    'mypackage.ldap.LDAPBackend1',
    'mypackage.ldap.LDAPBackend2',
    'mypackage.ldap.LDAPBackend3',
    'django.contrib.auth.backends.ModelBackend',
)

AUTH_LDAP_2_SERVER_URI = "ldap://student.myschool.edu.sg"
AUTH_LDAP_3_SERVER_URI = "ldap://stafff.myschool.edu.sg"
AUTH_LDAP_1_SERVER_URI = "ldap://192.168.3.16"

AUTH_LDAP_2_USER_DN_TEMPLATE = "%(user)s@student.myschool.edu.sg"
AUTH_LDAP_3_USER_DN_TEMPLATE = "%(user)s@staff.myschool.edu.sg"
AUTH_LDAP_1_USER_DN_TEMPLATE = "%(user)s@myschool.edu.sg"

AUTH_LDAP_2_BIND_DN = "dc=STUDENT,dc=MYSCHOOL,dc=EDU,dc=SG"
AUTH_LDAP_3_BIND_DN = "dc=STAFF,dc=MYSCHOOL,dc=EDU,dc=SG"
AUTH_LDAP_1_BIND_DN = "dc=MYSCHOOL,dc=EDU,dc=SG"

In the example above, we created three backends LDAP1, 2, and 3. We first import the backends to Django and set the URI, DN_TEMPLATE, and bind the domain.

We may need to restart Django and the server:

$python manage.py syncdb
$sudo apachectl restart

Erasing External Harddisk that cannot be opened

I got problem with my external hard disk. After some issues of umounting the disk, I couldn’t open the hard disk anymore. I tried to use Disk Util to repair, but it said it cannot be repaired and must be erased. But I can’t even erased it! It says disk can’t be opened. So I finally managed to erase it.

First, go to Terminal and type:

$ diskutil list

This will show a list of disk and partition. It happens that I can’t seem to erase the volume. But somehow I can erase the disk. So this what I do.

$ diskutil unmountDisk force /dev/disk1

and after it unmount, I type:

$ diskutil eraseDisk JHFS+ Backup /dev/disk1
Started erase on disk1
Unmounting disk
Creating the partition map
Waiting for the disks to reappear
Formatting disk1s2 as Mac OS Extended (Journaled) with name Backup
Initialized /dev/rdisk1s2 as a 931 GB case-insensitive HFS Plus volume with a 81920k journal
Mounting disk
Finished erase on disk1

Introducing scikit-learn for Machine Learning in Python

In this tutorial, we will go through a simple steps to classify whether a voltage is a digital 0 or 1. We will make things pretty straight forward with large margin so that we can easily visualize. The idea is just to introduce some of the functions in scikit-learn that we can use for linear classification.

The simple problem we want to do is given a set of voltages and its digital logic, we want to be able to determine what would be the digital logic given a new set of voltages. In this example, we use supervised learning, where we label the data and have the training set. Then given a new data, we want to determine which class it belongs to. We will use linear classifier from Support Vector Machine.

First given a training data sets of voltages, e.g.

>>> import numpy as np
>>> data=[0.5,0.1,0.2,0.15,0,0.3,0.4,0.5,0.35,0.45]
>>> data=np.array(data).reshape(len(data),1)

The first line of the above code import numpy module so that we can use its array data stucture. The second line create a list of voltages as a Python list. The third line, convert the Python list to Numpy array and change from a row vector to a column vector.

We can then train the data set by labeling the voltages. For simplicity, let’s label any voltages below 0.2 to be digital logic 0, and anything above 0.3 to be logic 1. To do this we create a target list according to our data set.

>>> target=np.array([0,0,0,0,0,1,1,1,1,1])

Now we can create our linear classifier using SVM.

>>> from sklearn import svm
>>> clf = svm.SVC(kernel='linear')

The first line import the module svm, and the second line creates a linear classifier. Next, we have to fit the data with the target.

>>> clf.fit(data,target)

And now we can predict new data. Let’s say we want to determine what the digital logic is for voltage 0.21V, we can type:

>>> clf.predict(0.21)
array([0])

Or we can also give an array:

>>> newdata=[0,0.5,0.22, 0.1,0.44]
>>> newdata=np.array(newdata).reshape(len(newdata),1)
>>> clf.predict(newdata)
array([0, 1, 0, 0, 1])

As you can see that voltages around 0.2 and below is classified as logic 0 while those around 0.3 and above is classified as logic 1.

Using SublimeText to write JSim file

50.002 Computation Structure course in SUTD requires students to write a kind of SPICE-like code using JSim, an MIT software to simulate circuit design. The editor, however, is pretty basic, and I have noticed that some students prefer to use SublimeText to write the JSim file. Here is the instruction to setup SublimeText to use with JSim.

  1. Download SublimeText.
  2. In Mac OS X, run SublimeText, press “CMD-SHIFT-P” to open the Command Palette. Otherwise, go to Menu -> Tools -> Command Palette.
  3. Type “browse packages” this will open a new Finder window with the location to install the packages.
  4. Create a new Folder called “JSim”.
  5. Download JSim syntax highlighter. Copy jsim.tmlanguage to the newly created folder “JSim”.
  6. Associate jsim extention to open with SublimeText. To do this (in Mac OS X):
    1. Find a JSim file, e.g. lab3.jsim. Right click and choose “Get Info”.
    2. Under “Open With”, choose “Sublime Text”.
    3. Click “Change All”.

UPDATE: There is some issue when using SublimeText in Ubuntu. A solution was to rename jsim.tmlanguage to jsim.tmLanguage (Note the capital “L”).