Well I got my Competition Kit, Wow!! A lot of the components in the kits are a little smaller than what I'm used to working with, but damn that is a lot of stuff and the packaging is great. The next few tutorials after this will be on projects with the competition kit.
But For now I will just wrap up the automated feeder Blog with what I have done so far on the vision based feedback. The arduino one will be the next blog.
-Built the feeders themselves
-measured the federate of the feeders
-Made a decent script for detecting the food with the Pi and Camera.
Work to do:
-Add more species to the Open-Loop controller [Tilapia only at the moment]
-Add a timing module to implement feed schedules based of fish age
-Better Blob detection for the Pi camera and have a see if I can get it to read out the size of the fish to fully automate the feed schedules.
Codes, Codes , Codes
The Codes are only proof of concepts at the moment, if you want to use them in your set-up as a minimum you will need to implement a means of knowing the time to space the feedings. I will be posting updates on these once I have got the parts to implement this.
What you will need:
>RaspberyPi 2b
>USB Camera
>Opto Isolated Relay
First things first you will need to install OpenCV in the Pi [ see Blog 6]
The Code: [Is there a better way to Post Code to these blogs?]
Python Code |
---|
#!/usr/bin/python
##*********Explain a Little About the Codes Purpose *****************## print("Vision Based Fish Feeder") print("This code is the first of its kind, addresing fish feeding with an active feedback system") print("Proof Of Concept Code,Under GNU, So Use it as you wish ") print("10th/8/2015") print("By Michael Ratcliffe") print("Mike@MichaelRatcliffe.com")
##**********************Import Some Librays**************************## import cv2 import numpy as np import RPi.GPIO as GPIO import time
print("Librarys Loaded")
##*** Creating Video Capture and Defining Pins and other variables **## cap = cv2.VideoCapture(0) butPin = 21 # Broadcom pin 21 (P1 pin xx) motorPin = 18 # Broadcom pin 18 (P1 pin 12) #dc = 20 # duty cycle (0-100) for PWM pin##not used unless pwm
GPIO.setmode(GPIO.BCM) # Broadcom pin-numbering scheme GPIO.setup(motorPin, GPIO.OUT) # PWM pin set as output #pwm = GPIO.PWM(pwmPin, 50) # Initialize PWM on pwmPin 100Hz frequency GPIO.setup(butPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Button pin set as input w/ pull-up #pwm.start(0)
print("Definitions Finished and Entering Main Loop")
##*********************** Main Loop **********************************## ## ## ## Runs Continuosly for testing purposes ## ## Eventualy It will Run every Hour 8am-8pm ## ## Feeding the Fish as Much as thet Can Eat in 5-10 minutes ## ##********************************************************************## while(1):
##**************** Capturing Image From Webcam ***********************##
_,frame = cap.read()
##***************** Bluring Image to unify color *********************##
frame = cv2.blur(frame,(3,3))
##**************** Converting To Gray Scale **************************## ## ## ##Simple Conversion Using RGB Works Empty Tank ## ##Convert from One Channel to Cancel Noise from Fish Color if Needed ## ##********************************************************************##
gray= cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) thresh = cv2.adaptiveThreshold( gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\ cv2.THRESH_BINARY,75,20)
thresh2 = thresh.copy()
##***************** Identify Pellets Of Food ************************##
contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
##************ Simple Logic to Feed Fish on Demand ******************## ## ## ## PWM PIN Used Later As Project Evloves ## ## Most Likley Proportional+Intergral Control ## ##*******************************************************************##
if (len(contours) > 7 or GPIO.input(butPin)): print("Waiting for Fish to Eat") GPIO.output(motorPin,False);
else: # button is pressed:
print("Adding Food") GPIO.output(motorPin,True) time.sleep(0.01) GPIO.output(motorPin,False); ##************ Print Some Useful Things to the Screen ***************##
print'Amount Of Food: ',len(contours)-1,' Pellets'
cv2.imshow('Normal',frame) cv2.imshow('Gray',gray) cv2.imshow('Pellets',thresh2)
time.sleep(0.1)
##**************** Checks If User Has Asked to Leave *****************##
if cv2.waitKey(33)== 27: break
##************** Clean up everything before leaving *****************##
GPIO.cleanup() # cleanup all GPIO cap.release() cv2.destroyAllWindows() |
To Run This you will need to:
-Wire up a button to earth and a opto isolated relay [see code for used pins]
-Download the file from [www.michaelratcliffe.com/projects]
-Unzip it into your home/pi folder.
-Locate the File "ProofofConcept.py" Right Click on the File Properties>Permissions and select everyone> OK
-Because we are using the GPIO pins. we must run it as root
-cd home/pi [or wherever your proofofconcept.py is located]
-sudo python ProofofConcept.py
Now if everything is working you will have three screen pop up and when you pres the button it will do its best to keep the set amout of food in the pond. As it stands this will not be enough to optimise your fish feeding, it still needs to be ran at the optimum times during the day and not left to run all day etc. But It should give you a little intro into how things are coming along.