Latest posts

PiStorms Python Programming Tutorial

PiStorms is a functional little device that allows users to make games and build Lego Robots with the Raspberry Pi. mindsensors.com supports python libraries for the PiStorms. You may find it useful to access the Raspberry Pi linux command line. You can do this by following the instructions here.

 

Write the Python Program

 

This tutorial uses the EV3 Touch Sensor example. The first few lines of code should be used in all PiStorms programs to ensure proper import of the PiStorms class and functionality of the browser.

 

There are a few libraries you must import at the beginning of each porgram for the code to run succesfully:   

import os, sys, inspect, time  

These three lines need to be at the beginning of each program for the browser and any pictures to function as desired:  

currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
sys.path.insert(0,parentdir)   

To use the PiStorms functions, import the class from the PiStorms library and create an instance of the class as a variable: 

from PiStorms import PiStorms
psm = PiStorms() 

Now you can start writing your PiStorms code! 

Print a message on the screen at the next available line: 

psm.screen.termPrintln("EV3 touch sensor readout (BBS1):")
psm.screen.termPrintln(" ")

Reset the EV3 Touch Sensor touch count:

psm. BBS1.resetTouchesEV3() 

It is best to have a way to exit the program. In this program we just use while(not exit). Set the exit variable to False before entering the loop. 

exit = False
while(not exit): 

Call the data from the EV3 Touch Sensor and store it as variables:  

    touch = psm.BBS1.isTouchedEV3()
    numTouch = psm.BBS1.numTouchesEV3() 

Print the data on the PiStorms screen: 

    psm.screen.termReplaceLastLine(str(touch) + "  " + str(numTouch)) 

The exit code for this example is very simple. A section of (in this the whole) screen is tested. If it is touched, an exit message appears and the exit variable is changed to True. This will exit the while loop and close the program to the browser. 

    if(psm.screen.checkButton(0,0,320,320)):
        psm.screen.termPrintln("")
        psm.screen.termPrintln("Exiting to menu")
        exit = True

   

See the full 06-touch_sensor.py example code below:

  

  1. import os,sys,inspect,time
  2. currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
  3. parentdir = os.path.dirname(currentdir)
  4. sys.path.insert(0,parentdir) 
  5. from PiStorms import PiStorms 
  6. psm = PiStorms() 
  7. psm.screen.termPrintln("EV3 touch sensor readout (BBS1):")
  8. psm.screen.termPrintln(" ") 
  9. psm.BBS1.resetTouchesEV3()
  10. exit = False 
  11. while(not exit):
  12.  touch = psm.BBS1.isTouchedEV3()
  13.  numTouch = psm.BBS1.numTouchesEV3() [code language="cpp"] *** PASTE YOUR CODE IN HERE *** [/code]
  14.  psm.screen.termReplaceLastLine(str(touch) + " " + str(numTouch))
  15.  if(psm.screen.checkButton(0,0,320,320)):
  16.  psm.screen.termPrintln("")
  17.  psm.screen.termPrintln("Exiting to menu")
  18.  exit = True

 

 Open Your Program from the PiStorms Browser      

 

The PiStorms Browser allows you to open your python programs directly from the PiStorms touch screen. 

 

Make sure you program begins with two numeric digits and a dash:

     eg:    55-PiStormsProgram

 

You program must be located in the /home/pi/PiStorms/programs directory.

 

The next time you refresh your PiStorms screen or reboot your Raspberry Pi, you should see your program.

For your reference a program for this tutorial is provided in the sample programs: touch_sensor_tutorial.py

Feel free to compare your program with this one.

 

Posted in: How to, PiStorms

Leave a comment