Skip to content

oliverc1623/Mice-Detector

Repository files navigation

Mice Detector

Table of Contents

  1. Description
  2. Prerequisites
  3. Installing
  4. Connecting Python to Arduino
  5. Connecting arduino to python
  6. Mice Class
  7. Instantiating the Mice
  8. Defining functions
  9. The main loop
  10. Usage

Description

Welcome to Mice-Detector. The goal of this project is to allow mice to enter and exit testing chambers without human help. The mice will travel through a tube with 2 RFid sensors on each end. The mice will also have a RFid chip implemented in their head so they can be read. In this README is will outline and explain major chunks of code in the main script.

Prerequisites

Hardware: 2 arduino unos, 2 Tower Pro SG-5010 servos, 2 ONETAK 125kHZ RFid Readers, linux machine, PVC pipe

Libraries: pyUSB, pyTransitions, keyboard_alike, machine can run python 2.7/3.0

Installing

To install simply clone/downloads the repository.

Instructions for installing the libraries can be found in the links.

pyUSB: https://github.com/walac/pyusb#installing-pyusb-on-gnulinux-systems

keyboard_alike: https://github.com/riklaunim/pyusb-keyboard-alike

pyTransitions: https://github.com/pytransitions/transitions#installation

Arduino: https://playground.arduino.cc/Linux/Ubuntu

Connecting Python to Arduino

On the python side initialize 2 arduino objects

Depending on which port you plug the arduino into, the device location may differ.

arduino1 = serial.Serial('/dev/ttyACM1', 9600)
arduino2 = serial.Serial('/dev/ttyACM0', 9600)

Connecting Arduino to Python

All of this is taking place in the arduino IDE

Create servo object

Servo myservo;

In setup, set the buad to 96000 and connect the servo to the port.

Serial.begin(9600); 
myservo.attach(9);

In loop we're checking to see if something came across serial. Then we parse it into an int and use that value to write to our servo.

if (Serial.available() > 0) {   // something came across serial
            int integerValue = Serial.parseInt();
            if (integerValue != 0)
              {
              Serial.println(integerValue);
              myservo.write(integerValue);
              }
        }

Connecting RFid sensors with Python

#initialize list of RFidReaders connected to computer
device = list(usb.core.find(find_all=True, idVendor=0x16c0, idProduct = 0x27db))
reader1 = reader.Reader(0x16c0, 0x27db, 36, 3, should_reset=False)
reader2 = reader.Reader(0x16c0, 0x27db, 36, 3, should_reset=False)

Mice Class

In the Mice Class there are 4 main attributes: code, isAllowed, States, and Transitions.

The code in Mice Class is an int value which we will use to compare to the RFid key.

isAllowed is a boolean value used to determine if the mice is allowed to enter the tube.

States is an array holding the state names we will use (PyTransitions might be obsolete).

states=['left', 'centerOut', 'right', 'centerIn']

transitions is an array holding arrays containing the tigger, source, and destination (PyTransitions might be obsolete).

transitions = [
        {'trigger': 'leftToCenter', 'source': 'left', 'dest': 'center'},
        {'trigger': 'centerToRight', 'source': 'center', 'dest': 'right'},
        {'trigger': 'rightToCenter', 'source': 'right', 'dest': 'center'},
        {'trigger': 'centerToLeft', 'source': 'center', 'dest': 'left'}
]

Instantiating the Mice

To instantiate the Mice modify this chunk of code

#instantiate mice
miceID = [None] * 3
miceID[0] = Mice(6164996, True)
miceID[1] = Mice(8657565, True)
miceID[2] = Mice(12919161, True)

Defining functions

The function readReader1() is used to interpret the data from the RFid Key. It stores and returns the key into an str value called code1. readReader2() is synonomous except we're using the second RFis reader plug into the machine and storing and saving the key value into code2.

def readReader1():
    reader1.initialize(device[0])
    global code1
    code1 = reader1.read()
    if code1 != '': 
        code1 = int(code1)
    reader1.disconnect()
    return code1

The function gate1() writes to the arduino telling it to turn the servo to 160 degrees - rest 2 seconds - and turn the servo to 70 degrees. gate2() is essentially the same except we are writing to arduino2.

def gate1():
    arduino1.write(str(openGate))
    time.sleep(2)
    arduino1.write(str(closeGate))

The main loop

The main loop in the program is an infinite while loop. In our case we have 4 states we will be transitioning to and from. The variable globalState is used to asign and determine which state we're in.

Usage

Open the terminal and cd to folder with all the files.

sudo python MiceDetector.py

About

No description or website provided.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published