Jump to content
vicvelcro

Critter Catcher - Live Trap with a Brain

Recommended Posts

Currently written using Energia. If I can ever figure out Code Composer Studio, I'll probably redo it with that.

 

Photos (the attachments beginning with "SAM_xxxx"), hand written build notes (in photos), and hand drawn pseudo-schematic (in photo) are in the archive files attached to this post. Photos that didn't appear in the sequence were too blurry to include and didn't contain anything not already in one of the other photos. I added some comments to the code, in the body of this post - the attached code has fewer comments.

 

So, this is an 'intelligent' live trap. A creature must be far enough inside. If not far enough, then door stays open. Door will only close if creature would be unable to flee via the exit before the exit would be able to close.

 

For now, I am using an IR LED as an emitter and another IR LED as a receiver. This is because I don't want visible light to affect the system. Later, I intend to use a laser which is obviously a fixed wavelength and I will then choose a sensor very narrowly tuned to that wavelength. Hey, why not?!?

 

The code is certainly messy, but that's my current level of skill. It will improve as I improve. Suggestions, advice, general comments, and critiques are welcome and appreciated. If something belongs in a function, hit me with it. If you know a better way to implement "if" statements that the way I have done it, hit me with it.

 

* I would like to use a low power mode, to extend the battery life. I have mentioned this in another thread. Anyone willing to walk me through an implementation of low power mode, I'm open for input. I do not want arbitrary code, though. I won't 'just paste' anything I can't understand or decipher. Commented code with clear explanation would be much appreciated.

 

Yes, this thing does work. And it works quite well for its intended purpose. I think I will scale it up to 'varmint' size in the near future.

 

post-3-0-08555500-1362925994_thumb.jpgpost-3-0-00685500-1362926003_thumb.jpgpost-3-0-07670500-1362926010_thumb.jpgpost-3-0-52066700-1362926014_thumb.jpgpost-3-0-94846200-1362926019_thumb.jpgpost-3-0-94846200-1362926019_thumb.jpgpost-3-0-03291100-1362926025_thumb.jpgpost-3-0-63636600-1362926030_thumb.jpg

 

 

THE CODE

 

 

 

// The pin assignments are applicable when a 2553 MCU is used.
// If you will use any other MCU, check the pin layout to see if changes are necessary.

#include <Servo.h>
 
Servo myservo;  // create servo object to control a servo
 
int posm = 5;   // variable to store the servo position
int posz = 5;  // starting position of servo
boolean moveServo = 0;
int serswpdeg = 135; // define the limits of the sweep range for the servo
int swait = 5; // sets global wait delay value
int servport = P2_4; // sets the port number for servo signal line

const boolean powind = P2_1; //power indicator port number
//const byte tripwire = P2_5; //the beam generator pin assignment - currently not in use, it is wired to power supply to be continuously on
const byte tripdet = A4; //the port used as tripwire detector
const byte trapdoor = P2_2; //the actuator or relay or solenoid to be triggered - which pin
const byte trippedled = P2_0; //the LED port to indicate tripped
const byte occuled = P1_5; //the LED port to indicate hopper is occupied
const byte occusen = P2_3; //the sensor port to detect when hopper is occupied
const byte reswitch = P1_3; //count reset switch port - serial data for debugging - can be removed
const byte sens = 20; //how much sensor must decrease to activate trip
const unsigned int wait = 1; //sets the wait time during major loops
const unsigned int caloop = 1000; //the number of normal runs before recalibrating detector
const unsigned int calcount = 1000; //number of times to sample for calibration of sensor
const unsigned int detcount = 250; //number of times to sample for confirmation of detection

int basis = 0; //the averaged number we compare against to determine if trigger occurs
long light = 0; //sets the level of light before beginning
boolean tripstate = 0; //sets whether the trip is currently triggered
unsigned long tripcount = 0; //how many times trigger has been activated in total
boolean reswitchval = 0; // stores value of reset switch
boolean occupied = 0; // when a sensor is implemented, this will hold the state of the sensor
unsigned int loopx = 0; //counts loop repetitions for calibration
unsigned int loopy = 0; //counts number of total passes for detection
unsigned long totaal = 0; //keeps a running total of light readings to average out
unsigned long totaal1 = 0; //keeps a running total of light readings to average out

void setup()
  {
  Serial.begin(9600);
//  pinMode(tripwire, OUTPUT); // it is hard wired to the power supply to be continuously on
  pinMode(trapdoor, OUTPUT);
  pinMode(tripdet, INPUT);
  pinMode(trippedled, OUTPUT);
  pinMode(occuled, OUTPUT);
  pinMode(occusen, INPUT);
  pinMode(powind, OUTPUT);
  pinMode(reswitch, INPUT); // if using external switch
  pinMode(servport, OUTPUT);
// pinMode(reswitch, INPUT_PULLUP); // if using onboard switch - not sure if this line is even correct syntax (probably not)
  myservo.attach(servport);  // attaches the defined servo pin to the servo object

//  digitalWrite(tripwire, HIGH);  // turning on tripwire
  myservo.write(5); // twitches the servo so that it won't jitter later
  myservo.write(0); // twitches the servo so that it won't jitter later
  myservo.write(5); // twitches the servo so that it won't jitter later
  digitalWrite(trapdoor, LOW);   // turning off trip
  digitalWrite(trippedled, LOW); //turning off tripped indicator
  digitalWrite(occuled, LOW); //turning off occupied indicator
  digitalWrite(powind, HIGH); // turning on the power indicator
  digitalWrite(servport,LOW); // turns off the servo data line for now
  }

void loop()
{
//readings to set the sensors based on averaged readings
  if (tripstate != 1)
  {
  totaal=0;
  totaal1 =0;
  light=0;
  for(loopx =0; loopx < calcount; loopx++)
    {
  light = analogRead(tripdet);
  totaal = totaal1 + light;
  totaal1 = totaal;
  delay(wait);
    }
  light = totaal/loopx;      // divide the total readings by number of reads
  Serial.print("Sensor Calibrating to ");
  Serial.println(totaal/loopx);
  digitalWrite(powind, LOW);
  delay(100);
  digitalWrite(powind, HIGH);
  totaal = 0;
  totaal1 = 0;
  }

basis = light - sens;  // setting trip sensitivity

// after every (caloop) the detector will recalibrate
    for(loopy = 0;loopy < caloop; loopy++)
    {
       for(loopx =0; loopx < detcount; loopx++)      //readings to see if the sensor is dark
       {
       light = analogRead(tripdet);
       totaal = totaal1 + light;
       totaal1 = totaal;
       delay(wait);
       }
    light = totaal/loopx;
    totaal=0;
    totaal1=0;

reswitchval = digitalRead(reswitch);
  if (reswitchval == HIGH)
  {
    tripcount=0;
  }
 
  if (tripcount != 0)
  {
    digitalWrite (trippedled, HIGH);
  }
else
  {
    digitalWrite (trippedled, LOW);
  }

occupied=digitalRead(occusen);
if (occupied == HIGH)
  {
    digitalWrite(occuled,HIGH);
  }
else
  {
    digitalWrite(occuled,LOW);
  }
 
       if ((light < basis) && (tripstate != 1))          // testing if the sensor was in the dark
         {
         tripcount++;
         digitalWrite(trapdoor, HIGH);  // turning the trip in port on if the sensor is dark
         (tripstate = 1);
         (moveServo = 1);
         }
       else
       {
         if ((light > basis) && (tripstate != 0))
         {       digitalWrite(trapdoor, LOW); // turning it off if not
       (tripstate = 0);
       totaal = 0;  
       totaal1 = 0;  
         }
       }

    if (moveServo == 1)
    {
    Serial.println(" ");
    Serial.print("Moving the Servo");
    Serial.println(" ");

    for(posm = 5; posm < serswpdeg; posm ++)  // goes from 0 degrees to 180 degrees
        {                                  // in steps of 1 degree
               myservo.write(posm);              // tell servo to go to position in variable 'pos'
               delay(swait);                       // waits 15ms for the servo to reach the position
        }
            
    if(posm>0)
      {
    for(posz = serswpdeg; posz >= 6; posz --) // returns servo to 0 degrees
        {
          myservo.write(posz);
          delay(swait);
        }
      }
      (moveServo = 0);
    }

       Serial.print("Reading ");
       Serial.print(light);
       Serial.print("   ");
       Serial.print("Trip on ");
       Serial.print(basis);
       Serial.print("   ");
       Serial.print(light-basis);
       Serial.print("   ");
       Serial.print("Trip State ");
       Serial.print(tripstate);
       Serial.print("   ");
       Serial.print("Time to next Calibrate ");
       Serial.print(caloop-loopy);
       Serial.print("   ");
       Serial.print("Occupied ");
       Serial.print(occupied);
       Serial.print("   ");
       Serial.print("Times Tripped ");
       Serial.print(tripcount);
       Serial.println("");

 //  delay(wait);
 }
}

 

 

 

Laser Trip Trap v10 - Energia Code.rarSAM_0055.rarSAM_0056.rarSAM_0057.rarSAM_0059.rarSAM_0060.rarSAM_0061.rarSAM_0062.rarSAM_0063.rarSAM_0064.rar

post-3-0-66451700-1362925998_thumb.jpg

Edited by bluehash
Added code tags, uploaded pics.

Share this post


Link to post
Share on other sites

I normally would have. In this instance, there are many and they were humongous. Even reduced 50% on each axis, they were large. I suspect that if I had made them visible in the post, a moderator would have had me flogged and then keel-hauled.

 

The pictures are only important if you are interested in the project. You don't seem to be, so it's no loss to you. If you were interested, you probably would want them sufficiently enough to make the effort to download.

Share this post


Link to post
Share on other sites

I normally would have. In this instance, there are many and they were humongous. Even reduced 50% on each axis, they were large. I suspect that if I had made them visible in the post, a moderator would have had me flogged and then keel-hauled.

 

The pictures are only important if you are interested in the project. You don't seem to be, so it's no loss to you. If you were interested, you probably would want them sufficiently enough to make the effort to download.

 Fixed. Also good project!

 

you should really upload your build images as images instead of .rar, personally i won't make the effort to download and unzip them individually.

Fixed.

Share this post


Link to post
Share on other sites

I appreciate the help you gave with cleaning up that post and making the images more conveniently available for others.

 

Thanks, BlueHash.

Share this post


Link to post
Share on other sites

I appreciate the help you gave with cleaning up that post and making the images more conveniently available for others.

 

Thanks, BlueHash.

Yeah, that's how bluehash cares for his community ;)

 

Wasn't my intention to offend you, just a quick tip :)

My experience is that moderators of this forum are very generous in terms of file sizes, they like content on this forum to be easy to access.

Usually the images you attach to your posts are 'thumbnailed' automatically for you, this way your post does not get bloated with giant images and people who are interested in them can view them fullsize..

Uploading them directly saves you the hassle of zipping them and we are able to view them in every browser, on any platform, including mobile, just as JPEGs are meant to. They are compressed anyway.

 

 

The pictures are only important if you are interested in the project. You don't seem to be, so it's no loss to you.

You're right, it is no loss to me. But you might lose some feedback from members who are not that much interested in your project as a whole, but in some particular details of it, only visible in the pictures.

Your idea of mounting the servo with ice-sticks is very neat, for example. I would not have noticed it if bluehash didn't edit your post ;)

Share this post


Link to post
Share on other sites

I can't tell from the photos... are you using a laser in this?  Regardless, I doubt the MSP430 is a significant power drain in comparison to the other parts.  LPM may help a little, but I think your other components are probably at fault for the short battery life.

Share this post


Link to post
Share on other sites

If going to use a laser, be sure the power is low enough and the frequency is such that it will not harm anything inadvertently caught in the trap.  

(e.g. damage their vision)  If you catch something other than vermin wouldn't want to harm them.

 

Might be more useful if could distinguish what in the trap.  We keep catching birds in a live trap set for rats.  

Share this post


Link to post
Share on other sites

Phillip,

 

I took no offense. I did consider your suggestion.

 

Rereading my previous reply to you, I suppose I should have chosen my words better, it does appear as though I were being a bit abrupt. For that, I apologize. The error was mine.

 

 

Wasn't my intention to offend you, just a quick tip :smile:
My experience is that moderators of this forum are very generous in terms of file sizes, they like content on this forum to be easy to access.
Usually the images you attach to your posts are 'thumbnailed' automatically for you, this way your post does not get bloated with giant images and people who are interested in them can view them fullsize..
Uploading them directly saves you the hassle of zipping them and we are able to view them in every browser, on any platform, including mobile, just as JPEGs are meant to. They are compressed anyway.
 


You're right, it is no loss to me. But you might lose some feedback from members who are not that much interested in your project as a whole, but in some particular details of it, only visible in the pictures.
Your idea of mounting the servo with ice-sticks is very neat, for example. I would not have noticed it if bluehash didn't edit your post ;-)

Share this post


Link to post
Share on other sites

Thanks for the interest and for the input. I didn't expect to get any feedback for a few days, yet.

While this is currently a working device, it is not my end goal. This will be scaled up and there will be some improvements.

If anyone wants to make one of these (or something similar), follow along and ask me about anything specific. I can redraw a better schematic instead of the quickie build-version, I will post any updated code, and I intend to make a video after I lose the plywood-popsicle stick housing.

This started off as a project just for practice and learning. I thought I'd throw in a servo, some IR LEDs, and then practice writing some code related to sweep and analog input. During my self-imposed-lesson, it occured to me that I could take it off a breadboard, add a lever, and voila! A trap was born.

Then came the obvious idea of using a laser. I mean, hey, who doesn't like lasers?

Really, when I started this, it was going to be an evening of "break the beam to sweep a servo" and then pull it all apart. That was back in October. I set it aside for a while, instead of disassembling it. I picked it back up just a few days ago and went forward.

----

CDE,

There will indeed be a next time. I frequently use popsicle sticks for quickie testing rigs. Cheap way to prototype, you see.

----

GeekDoc,

There is no laser in this unit at this time. Currently, I have an IR LED emitting at a current draw of 12mA and another IR LED picking up the emission from the other IR LED. It's a fast and cheap way of testing an idea, the IR LED acts very much like a Light Detecting Resistor (LDR). The laser will be the permanent component and will be installed when I improve the main box (hopefully soon).

Regarding the power consumption: yes, the MCU is not the biggest draw. I intend to make the beam a pulsed emission (via PWM) and also to have a relay that disconnects the power from the servo until just before it will be called into action.

My intent is to make a varmint catcher than can be left unattended for at least several days, with some type of alert to notify me if anything gets caught in the interrim. I don't want anything remaining in there long enough to starve. I want something humane. Therefore, I want to minimize power consumption all the way around, as much as possible.

----

tingo,

Thank you.

Regarding your question about critters reacting to IR: mice don't seem to be disturbed or frightened at all. I will have to research the visual range of varmints before deciding on the final wavelength, so I may discover that IR is feasible or I may find that I need to go farther into the IR.

----

SugarAddict,

Handy tip, there. I expect to be posting photos from time to time. Thanks.
 

----

 

igor,

 

Indeed, I will restrict the current to the laser to a very low level and further implement PWM. I agree, I don't want to stress the critters any more than can be avoided.

 

Regarding a way to distinguish between one animal and another: I wonder if that can be done by PIR to detect the body temperature of whatever enters and then only trigger on temperatures in a limited range. Hmmmm. Thanks for that suggestion. If I figure something out or if anyone suggests something viable, I'll let you know.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...