Blog Post

PiStorms Python Programming Tutorial

PiStorms Python Programming Tutorial

4535

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:

  

import os,sys,inspect,time
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
sys.path.insert(0,parentdir) 
from PiStorms import PiStorms 
psm = PiStorms() 
psm.screen.termPrintln("EV3 touch sensor readout (BBS1):")
psm.screen.termPrintln(" ") 
psm.BBS1.resetTouchesEV3()
exit = False 
while(not exit):
    touch = psm.BBS1.isTouchedEV3()
    numTouch = psm.BBS1.numTouchesEV3()
    psm.screen.termReplaceLastLine(str(touch) + " " + str(numTouch))
    if(psm.screen.checkButton(0,0,320,320)):
        psm.screen.termPrintln("")
        psm.screen.termPrintln("Exiting to menu")
        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.

Comments (4)

Matha G 04.17.2016
How does "currentframe" get initialized? (as shown in the PiStorms Python Programming Tutorial) I understand the advantages in placing the code in the /PiStorms/programs library but surely this mode of operational is applicable only for the introductory exercises?
Michael G 04.18.2016
currentframe is initialized in the inspect module. It will return the frame object for the caller's stack frame, which in this case is inspect.getfile(). Basically, it allows for the PiStorms Browser to function smoothly when entering and exiting the programs accessible from the PiStorms Browser.Any .py file in the PiStorms/programs folder with two digits at the beginning of the name will show in the PiStorms Browser upon startup of the Raspberry Pi. This allows users to open any program in the field, or anytime where ssh or any other type of remote access is not available. Feel free to write any programs you like and place them anywhere in the Raspberry Pi file system. You will have to open them using the traditions python command from the terminal.
Matha G 04.18.2016
Hello Michael, thanks for the clarification on "currentframe." Is there some way to use the PiStorms screen along the lines of the EV3Shield screen? I understand the advantages of the PiStorms browser for the reasons you have stated above. My use case to launch the applicable program remotely and then use the screen for further interaction - especially graphics displays by the running program.For example, with EV3Shield I provide visual feedback from 3rd party sensors along very elementary lines. I am thinking of reproducing these exercises with PiStorms unless you and other experienced users feel that my approach is wrong.
Michael G 04.18.2016
The libraries for the EVShield screen are not exactly the same but you can use the PiStorms screen in the same way. Please use the reference for the mindsensorsUI library ( http://mindsensors.com/reference/PiStorms/html/classmindsensors_u_i_1_1mindsensors_u_i.html ) and view our example programs in the PiStorms/programs folder to see how we are providing feedback on the screen.We encourage creativity and created the PiStorms with the touchscreen so that users can develop programs anyway they see fit. We would love for you to share any projects with us so that we can post them in blog format on our website. Contact us for the template if you are interested.
Please Login to add your comments

Featured Posts

Learn programming with Blockly visual programming environment for PiStorms

Read more
image-recognition-robot-with-pistorms-and-pi-camera

image-recognition-robot-with-pistorms-and-pi-camera

Want to make a PiStorms robot that uses a PiCamera to recognize icons placed in front of it and reacts...

Read more

Poll

  • What do you use to program your FRC Robot?
    Total:
  • What is your preferred programming language for PiStorms?
    Total: