Automated Fish Refuge

From Mech
Revision as of 19:33, 15 March 2009 by James Cooper (Talk | contribs)
Jump to: navigation, search
Entire Fish Refuge


Team Members


James Cooper - Junior in Mechanical Engineering
Chris Ryan - Masters Student in Biomedical Engineering


The weakly electric knifefish prefers to hide in tight spaces rather than to swim in the open. The fish will attempt to stay within an enclosure if the enclosure is moved. This provides a unique opportunity to study its behavior and the control of its unique ribbon fin locomotion. The goal of this project was to build a device that moves a box under computer control for Professor MacIver's lab. During operation, the box can oscillate back and forth in one degree of freedom. Unlike most fish, the ribbon fin allows the knifefish to move forward, backward, up, and down. Our device is manually adjustable (for reliability) so that the angle of oscillation and the angle of the enclosure can be set independently, each by steps of 10 degrees. Check out a video!

Parts List



  • 1/4 inch Acrylic
  • 1 Unipolar Stepper Motor
  • 1 Belt Part#:6484K237(Order at = $6.01)
  • 2 Pulleys Part#:6495K723 (Order at = $21.14 each)
  • 12" Drawer Slider
  • 4"x6" Sheet Metal
  • 3 Plastic Zip Ties
  • 2 Plastic 1/4" bolts and Wing Nuts
  • 2 Metal #8 Wing Nuts
  • 8 Metal #8 Bolts, Washers, and Nuts
  • 4 Metal #8 Spring Washers
  • 1 Metal 1/4" Bolt, Washer, and Stop Nut


How To Adjust Setup

We designed this project to easily configure to the changing needs of the user. On the mechanical side, the base is made from 80/20 aluminum square (1"x1") framing that has easily adjustable connections with just an allen wrench. This allows the user to raise or lower the height of the arm and also adjust the outside legs to the length of the tank. The sliding quarter circles allow the user to adjust the location and angle of the arm(and the oscillating motion) to any direction. The arm attaches to the quarter circle by two long bolts and wing nuts that keep them in place. Similarly, the end of the arm has another quarter circle which allows for the fish enclosure to independently adjust to any angle and has similar plastic bolts and wing nuts.

The design of the program also allows for a variety of configurations for specific needs. The PIC's programming acts as simply a transfer from an array of delay times to a signal for the L297 chip. The conversion from user input to an array of delay times is done in Matlab which is a common program that is easily configurable to the user's need. As noted below, there are two main types of programs that run the device: the functions that convert user input to delay times array function and the function that sends the array to the PIC. This modularity adds another dimension of adjustability where the user can put in any function or even create their own simple Matlab function to send a specific array to the second program.

How To Control

Once you have the setup orientated in the desired position, all you need to do to start having your refuge movie is send the PIC the control commands from MATLAB. In this section, we will go over how to create the array that contains the control parameters, and then how to send it to the PIC. We separated this into two function calls to allow the user to have complete control over the original path selected, and then allow the sending function to check to make sure the path selected is valid.

Generating Path

The user has 5 different way to generate the path. We created a MATLAB function to generate a square wave, a triangle wave, and a sin wave. We also created a function that allows the user to put in a function handle, and then get out the path from that. The final option is for the user to create their own without the assistance of any of the function we wrote.

The path is an array of delay times in between steps sent to the stepper motor. The way the stepper motor work is you send a pulse to the motor every time you want it to take a step. Each step on the stepper motor is 1.8 degrees rotation. The diameter of the pulley attached to the shaft of the motor is 45.2882 mm, which means the distance per step is 0.7114 mm. The array of delay times is the amount of time in between each step in microseconds (10^-6 seconds). The sign of the delay time states which direction it is going in. Positive for forward, and negative for backward.

Square Wave

For the square wave generator, all you need to do is enter the forward velocity (mm/s), the backward velocity (mm/s), and how far it travels (mm). The square wave generator will first have the refuge go forward, before it goes backward.

   %time_array is the outputted array of delay times (us)
   %vel1 is forward velocity (mm/s)
   %vel2 is backward velocity (mm/s)
   %length is length traveled (mm)

Triangle Wave

For the triangle wave generator, all you need to do is enter the peak forward velocity (mm/s), the peak backward velocity (mm/s), and how far it travels (mm). The triangle wave generator will first have the refuge go forward, before it goes backward.

   %time_array is the outputted array of delay times (us)
   %vmax1 is forward peak velocity (mm/s)
   %vmax2 is backward peak velocity (mm/s)
   %length is length traveled (mm)

Sine Wave

For the sine wave generator, all you need to do is enter the magnitude of travel (mm), and the frequency (rad/s). The sine wave generator will first have the refuge go forward, before it goes backward. The sine wave generator takes some time to generate the array of delay times, since it uses the function handle converter function to create the sin wave function, as will be discussed.

   %time_array is the outputted array of delay times (us)
   %mag is magnitude of distance traveled (mm)
   %wn is frequency (rad/s)

Function Handle

For the function handle converter, all you need to do is enter a function handle that is the function you want the displacement profile to follow. This function works by checking every millisecond to find when the step distance has occurred, and then taking that time difference and setting it to the list of the delay times. Then if sets that delay time as positive or negative depending on whether or not the displacement is positive or negative. This process repeater until the the net displacement goes back to 0. If this never happens, then the function will fun forever. The function displays the current displacement

   %time_array is the outputted array of delay times (us)
   %fun is function handle of displacement as a function of time.  Displacement should be in mm, and time should be in seconds.

Create Your Own

The last option to create an the array of delay times is to create it yourself. With this method, you can create they array any way you want to. Things to remember while doing this is that you are inputting the the time in microseconds it takes to travel 0.7114 mm. Also, you need to give the time you are inputting a sign (positive or negative) depending on which direction you want to go. When creating the array of delay times, make sure to have an equal number of positive and negative delay times, since if you don't then the communication function will not send you array of delay times, since then your path will not go in both directions equal amounts.

Sending Path

For the serial communication function, all you need to do is input the array of delay times, com port, number of cycles, and how you would like it to handle step times that are too large and too small. Com port should be inputted as a string of the location where you RS232 cable is inputted into your computer. If you don't know what that is, try using 'COM4' at first, and then if this is incorrect, you will get an error message with what com ports are open so you know what to try next. Cycles is the number of time you want the program to run though your array of delay times. Too fast and too slow inputs tell the program how you want it to handle delay times that are either too fast or too slow. The too fast value in the program is for values that are too fast for the setup in ideal conditions, and your setup might need values slower then this value. The too slow value is determined by the largest delay time the PIC can handle (2^16-1), since the delay times are held in variables that 16 bit integers. Input 0 if you want the program to stop if a delay time is out of range. Input 1 if you want the program to correct that improver delay time with the maximum or minimum possible value.

   %time_array is the inputted array of delay times (us)
   %com_port is the port your RS232 cable is connected to (eg. 'COM4')
   %cycles is number of time you want the array of delay times to be run
   %too_fast is how you want the program to handle velocities that are too fast
   %too_slow is how you want the program to handle velocities that are too slow

The serial communication function checks to make sure inputted delay times are within acceptable velocities, makes sure array of delay time is proper length (can be up to 500 numbers long), and makes sure the inputted path goes the same distance in both directions. If you stop the function after it starts transmitting to the PIC, but before it closes the transmission, then it is recommended you restart MATLAB to clear communication bugs. In that event, you should also click the reset button on the PIC. After plugging in and before transmission, it is recommended that you restart the PIC so you know it is at the proper place in its program to accept the arrays that will be transmitted to it.

PIC Code

There are two main parts to the PIC C code. First, it reads the information from the PIC. Then it runs the motor as specified from MATLAB.

Reading Information

We read is using RS 232 serial communication at a baud rate of 19200. We send everything in digits to protect our information because we head there have been some issues previously using RS232 with some high bit numbers before.
First, read in length of arrays to be transmitted.

     for(i=0; i<5; i++){
        readin[i] = fgetc();
     //combine digits sent into length
     length =(int16)(readin[0]-48)*10000+(int16)(readin[1]-48)*1000+(int16)(readin[2]-48)*100+(int16)(readin[3]-48)*10+(int16)(readin[4]-48);

Second, read in time delay array.

     for (i=0; i<length; i++){
        for (j=0; j<5; j++){
           readin[j] = fgetc();
        //combine digits sent into delay time
        delay_time[i] =(int16)(readin[0]-48)*10000+(int16)(readin[1]-48)*1000+(int16)(readin[2]-48)*100+(int16)(readin[3]-48)*10+(int16)(readin[4]-48);

Third, read in direction array.

     for (i=0; i<length; i++){
        readin[0] = fgetc();
        direction[i] =(readin[0]-48);

Fourth, read in number of cycles.

     for(i=0; i<5; i++){
        readin[i] = fgetc();
     //combine digits sent into number of cycles
     cycles =(int16)(readin[0]-48)*10000+(int16)(readin[1]-48)*1000+(int16)(readin[2]-48)*100+(int16)(readin[3]-48)*10+(int16)(readin[4]-48);

Running Motor

This part of the code runs though the delay time array cycles number of times. The stepper motor control chip likes to receive long periods of high, and then a short low to signal that it is time to step the motor.

     for (j=0; j<cycles; j++){ //run for set number of cycles
        for (i=0; i<length; i++){ //run thought each entry of delay time array
           if(direction[i]==0){ //set forward direction
           }else{ //set backward direction
           delay_us(delay_time[i]); //wait delay time - 1ms
           delay_ms(1); // last 1ms of delay time

Electrical Design

The circuit design and building is minimal for this project. The only necessary circuitry is to connect the unipolar stepper motor with the pic, and connect the RS232 cable to the pic. Please see the pages for the Unipolar Stepper Motor Driver Circuit and the PIC RS232 cable for more information on how they work and how to set them up.

Unipolar stepper circuit schematic.png

PIC to L297 Connections

  • Pin D0 is connected to Clock
  • Pin D1 is connected to DIR

RS232 Cable Connections

  • Orange Wire to RC6
  • Ground to Ground

Overall Circuit

Automated fish refuge circuit.jpg
Automated fish refuge closecircuit.jpg

Mechanical Design


The main philosophy for the mechanical design is to build something that is reliable and adjustable. For the frame, we used Aluminum Fractional T-Slotted Framing System in order to make it strong an adjustable. The 1" by 1" beams are overkill the for the loads that will be applied, and the weight of the frame will make sure it does not move. Using the T-Slotted Framing System also allows you to undo a bolt, and easily be able to adjust the dimensions of the frame, which allows one to easy change the size of the frame if one were to get a new fish tank and allows you to move the location of the quarter circle holding the arm up to a desired location. The quarter circles allow you to adjust the angle of the arm and box in 10 degree increments. This allows great flexibility in the angles of the setup, while when you put in a nut and lock it with a wing nut, you get a system that is sold and will only move along the desired axis. The arm, box, quarter circle, and back plane for the motor are made out of 1/4 in acrylic because it is a durable plastic material that will not effect the electric field emitted by the fish.

We used the laser in the shop to cut out the parts made out of acrylic. Here are the CAD files for these parts in case you would like to edit or remake them:


Further Reading

Ghost Knifefish Wiki

MacIver Lab Home Page

Prey Capture Paper by Mark E. Nelson and Malcolm A. MacIver, Journal of Experimental Biology 202(10): 1195-1203

Personal tools