Close

First Simulation

A project log for Earbot

A robot with 'ears' to hear and turn to the source of a transient sound.

jangojungleJangoJungle 05/04/2015 at 19:510 Comments

I've made a simple python program to simulate the earbot, testing out just the very basic function of taking in a difference of time and outputting an angle.

So here's the code:

__author__ = 'JangoJungle'
"""Earbot test program

This program is the basic model of an earbot. Enter a small difference in time
and it will output what angle the sound came from, how far it has to turn and
in which direction, and it's new position.
"""

from numpy import *

# initialize variables

current_angle = 0
length = .1524  # this is approximately 6 inches in meters
time_constant = length/340.29  # length of time for sound to travel the full .1524 meters distance

# get the difference in time

while True:
    delta_T = input('Please input the difference of time, a number between -0.00044785 and 0.00044785: \n(use negative sign to denote direction)\n')

    # test for validity: the absolute value of delta_T should never be greater than time_constant

    if abs(delta_T) > time_constant:
        print ('Invalid time difference!')
        continue

    # calculate where to go

    x = delta_T/time_constant
    angle = arcsin(x)*180/pi

    print ("The angle the sound came from is %s." % angle)

    # go there/update position

    current_angle += angle
    if current_angle > 180:
        current_angle -= 180
    elif current_angle < 0:
        current_angle += 180
    print ("My new angle is %s." % current_angle)

So just a couple notes:

  1. this program does not distinguish between a sound from the front and a sound from behind. For simplicity's sake, I'm leaving it that way until I know the robot is built.
  2. In hindsight, it would have been simpler to make the time constant an integer, but this still shows the function well enough.

Discussions