ImportError: No module named BrickPi

We are having a ”building a robot” project in my technology class, and I need some help with some errors that I have met. I have decided to build a plant-irrigation robot. I have upgraded the Raspbian software to the latest version from Sourgeforge. And since I am going to use OpenCV in my project I have followed this tutorial to install it: http://www.pyimagesearch.com/2015/02/23/install-opencv-and-python-on-your-raspberry-pi-2-and-b/. I have followed the tutorial and installed OpenCV in a virtual environment, does this create any complications? It seems to work fine with Harris detection which is OK in my case. My main problem is that I get this error ImportError: No module named BrickPi. I have tried to copy the BrickPi.py file from the BrickPi-Python folder to my project folder and then I get another error instead “ImportError: No module named serial”. What am I doing wrong? I removed the file and only keep a picture (png) file and the python file in the folder.

Also, I have not yet figured out the first part where it will identify a logo (on a plant pot) and run towards it, and thereafter power the crane and check the plant pots moisture. So any help with this as well are appreciated. I have attached both a screenshot of the terminal and python file.


-- coding:utf-8 --

#!/usr/bin/env python
from BrickPi import*
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import numpy as np
import cv2
import RPi.GPIO as GPIO

#BRICKPI CODE TO WATER PLANTS

##########################CODE########################

BrickPi.SensorType[PORT_1] = TYPE_SENSOR_EV3_US_M0 #Set the type of sensor at PORT_1. S1 - ultrasonic sensor
BrickPi.SensorType[PORT_2] = TYPE_SENSOR_EV3_COLOR_M2 #Set the type of sensor at PORT_2. S2 - color sensor.

BrickPi.MotorEnable[PORT_A] = 1 #Set the type of sensor at PORT_A. Left wheel
BrickPi.MotorEnable[PORT_B] = 1 #Set the type of sensor at PORT_B. Right wheel
BrickPi.MotorEnable[PORT_C] = 1 #Set the type of sensor at PORT_C. Crane
BrickPi.MotorEnable[PORT_D] = 1 #Set the type of sensor at PORT_D. Small motor

BrickPiSetupSensors() #Send the properties of sensors to BrickPi. Set up the BrickPi.
BrickPiUpdateValues() # Ask BrickPi to update values for sensors/motors

running = True

#Variables
color_sensor = BrickPi.Sensor[PORT_1]
us_sensor = BrickPi.Sensor[PORT_2]

outPin = 7 ## Pump connected to pin 7
inPin = 5 ## Moisture sensor connected to pin 5
GPIO.setmode(GPIO.BOARD) ## Use BOARD pin numbering
GPIO.setup(outPin, GPIO.OUT) ## Set pin 7 to OUTPUT
GPIO.setup(inPin, GPIO.IN) ## Set pin 13 to INPUT

#def wait_inp(num):

ot = time.time()

def run(self):
while running:
##############CAMERA###############

    img = cv2.imread('potte.jpg')
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

    gray = np.float32(gray)
    # uses Harris algorithm to detect corners
    dst = cv2.cornerHarris(gray,2,3,0.04)

    #result is dilated for marking the corners, not important
    dst = cv2.dilate(dst,None)

    # Threshold for an optimal value, it may vary depending on the image.
    img[dst>0.01*dst.max()]=[0,0,255]

    cv2.imshow('result.png',img)
    if cv2.waitKey(0) & 0xff == 27:
        cv2.destroyAllWindows()


while True:
    humidity = GPIO.input(inPin)  ## Read input from humidity sensor
    BrickPi.MotorSpeed[PORT_A] = 50
    BrickPi.MotorSpeed[PORT_B] = 50

if color_sensor == 7 and us_sensor == 90:
    #time.sleep(.01)     # sleep for 100 ms
    BrickPi.MotorSpeed[PORT_C] = 50
    time.sleep(.06)     # sleep for 100 ms
elif color_sensor == 1:
    BrickPi.MotorSpeed[PORT_C] = 0
    time.sleep(.002)  # sleep for 2000 ms

if humidity == 1:
    GPIO.output(outPin, 1)
    time.sleep(.002) # sleep for 2000 ms
    GPIO.output(outPin, 0)
    BrickPi.MotorSpeed[PORT_C] = -50
    time.sleep(.001) # sleep for 1000 ms
    BrickPi.MotorSpeed[PORT_C] = 0
    BrickPi.MotorSpeed[PORT_A] = -50
    BrickPi.MotorSpeed[PORT_B] = -50
    time.sleep(.0025)  # sleep for 2500 ms

elif humidity == 0:
    BrickPi.MotorSpeed[PORT_C] = -50
    time.sleep(.001)  # sleep for 1000 ms
    BrickPi.MotorSpeed[PORT_C] = 0
    BrickPi.MotorSpeed[PORT_A] = -50
    BrickPi.MotorSpeed[PORT_B] = -50
    time.sleep(.0025)  # sleep for 2500 ms

else:
    # Move Motors
    ot = time.time()
    power = -150
    BrickPi.MotorSpeed[PORT_A] = power
    BrickPi.MotorSpeed[PORT_B] = power
    result = BrickPiUpdateValues()
if int(us_sensor)<200 and time.time() - ot > 5:
    ot = time.time()
    power1=[-150,-150]
    deg=[110,-110]
    port=[PORT_A,PORT_B]
    motorRotateDegree(power1,deg,port,.1)
    time.sleep(.1)     # sleep for 10 ms
    #time.sleep(.01)     # sleep for 100 ms
    #! The color sensor will go to sleep and not return proper values if it is left for longer than 100 ms.  You must be sure to poll the color sensor every 100 ms!

I’m (somewhat) familiar with that tutorial for OpenCV as I am also following it.

When you create a virtual environment, it isolates all the files and setups to within that virtualenv. Therefore it no longer knows anything about BrickPi as that is set outside of its virtualenv.

In this particular case, it would be best not to run OpenCV from within its virtualenv but to install it in the main environment. Or, do the opposite and re-install BrickPi within the CV environment, although I’m not 100% sure everything will go smoothly on the first try.

So on this tutorial: http://www.pyimagesearch.com/2015/02/23/install-opencv-and-python-on-your-raspberry-pi-2-and-b/
Skip step 7 and continue as if you’ve done that step.
If, as it seems is the case, you’re already all set up, then remove the following lines from your .profile file

# virtualenv and virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

run
source ~/.profile

and redo step 8 till the end. Make sure you never have (cv) in your prompt.

Virtual environments are (usually) not necessary for BrickPi, as most people use a dedicated SD card for it.

Hope this helps
(and btw, what a great project idea! I totally dig it!)

Thanks a lot! It works, but I get another error message instead: ImportError:Attempting to use a port that is not open.

Missed BrickPiSetup() in my code. After adding that line I get another Permission Denied ‘/dev/ttyAMA0’ error. Do you have any idea how I can solve this? I have attached the error message.

My suggestion is to not use virtual env and just use it directly.

Unless you need to run different versions of libraries that are conflicting, using virtual env isn’t going to make life easier or better. There’s already a script for Raspbian Jessie that will install OpenCV using apt-get that I wrote in github.

Thanks for your answer! Btw, I started over again with a new clean install of Raspbian Jessie and installed OpenCV globally. But I get this “Permission denied” error, do you know why? :slight_smile: I can not copy files to the BrickPi_Python folder either, any idea why this does not work? :slight_smile:

did you run with sudo?

usually permission issues are caused by running commands without sudo. for example, if you run

apt-get install opencv*

that usually fails, but if you run with sudo it works

sudo apt-get install opencv*

Are you using Raspbian for Robots (latest image is Jessie), or the standard Raspbian from the Foundation?
If I were to do this, I’d start with Raspbian for Robots (april 25th version) and then follow the PyImageSearch tutorial, skipping the virtual env steps.

instead of manually running those commands, there’s already a script in the dexter github for BrickPi_Python

https://github.com/DexterInd/BrickPi_Python/tree/master/Project_Examples/openCV

just run “sudo ./install.sh”

and it should do all the necessary steps for you

Thanks for all answers! I forgot to mention but I went on with a full reinstall of Raspbian for Robots and upgrade from Wheezy to Jessie. And I also followed CleoQc answer to install OpenCV, and now it works globally without any problems. Unfortunately, I came by another permission denied error when I run my script, and that is what I am now figuring out right now. I think it is related to permission errors with the BrickPi.py file. Any suggestions? :slight_smile:

It says "raise serialexception(could not open port s s (self._port msg))"
Permission Denied ‘/dev/ttyAMA0’

I have attached the error message.

Hello Autonomous,

would you get the same answer if you use sudo?
sudo python PlantIrrigt.py

Permission Denied ‘/dev/ttyAMA0’

What version of the Raspberry Pi are you using?

“install BrickPi within the CV environment, although I’m not 100% sure everything will go smoothly on the first try.”

Why would not this work? Surely brickpi is just a module like any other? But how do you do it?
Being in the virtualenv (e.g. cv) and running your install script does not work.

Respectfully, I think your advice not to use virtual envs with Python and openCV is just wrong; this is standard practice.

Rob