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!