Thursday, February 3, 2011

Digital range finder code

Here is some sample code for interacting with the digital range finder.



CODE BEGINS:
----------------------------------------------------------------------

import serial
import pygame
from pygame.locals import *
import time
import math
from visual import *
import threading

init = 1

try:
sonic = serial.Serial(7,baudrate=9600, bytesize=8,parity = 'N',stopbits = 1,timeout=None, xonxoff=0, rtscts=0)
sonic.open
except:
print "Sonic range finder not connected or bad COM port number"
quit()

def parsesonic():
hold = 'a'
while(hold != '\r'):
hold = sonic.read()
sonic.read()
distance = sonic.read(3)
return int(distance)
class object_avoidance_thread(threading.Thread):
def run(self):
while True:
if (parsesonic()< 24):
print "woah man you're too close\n"

while(1==1):
if init == 1:
object_avoidance_thread().start()
init = 0
-----------------------------------------------------------------
CODE ENDS:


The above code causes the robot to output a string whenever an object is closer than 24 inches to it.

No comments:

Post a Comment