Skip to content

Instantly share code, notes, and snippets.

@wattnotions
Created May 25, 2014 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wattnotions/8151c36fc1542acdf9f1 to your computer and use it in GitHub Desktop.
Save wattnotions/8151c36fc1542acdf9f1 to your computer and use it in GitHub Desktop.
This script generates 2d plots using a stepper motor and a webcam
## updated on 26th January 2014
## scans surrounding area and plots x-y points forming a 2d map
import cv2
from numpy import *
import math
import time
import serial
import matplotlib.pyplot as plt
ser = serial.Serial('/dev/ttyUSB1', 9600)
ser.close()
ser.open()
#variables
loop = 1
dot_dist = 0
count = 0
i = 0
step_num = []
obj_dist = []
x_points = []
y_points = []
angles= []
#cv2.namedWindow("preview")
vc = cv2.VideoCapture(2)
if vc.isOpened(): # try to get the first frame
rval, frame = vc.read()
else:
rval = False
#print "failed to open webcam"
input("type something and press enter to continue...") ## wait for user input to begin
ser.write('g') ## this tells the pic to start rotating the stepper
while loop == 1:
############Get distance value from camera###############
#cv2.imshow("preview", frame)
rval, frame = vc.read()
key = cv2.waitKey(20)
if key == 27: # exit on ESC
loop = 0
num = (frame[...,...,2] > 245)
xy_val = num.nonzero()
y_val = median(xy_val[0])
x_val = median(xy_val[1])
#dist = ((x_val - 320)**2 + (y_val - 240)**2 )**0.5 # distance of dot from center pixel
dist = abs(x_val - 320) # distance of dot from center x_axis only
#print " dist from center pixel is " + str(dist)
theta =0.0011450*dist + 0.0154
tan_theta = math.tan(theta)
if tan_theta > 0: # bit of error checking
obj_dist.append(float(5.33 / tan_theta))## add the latest distance value to obj_dist lsit
###### Get stepper value from PIC ####################
try:
step_num.append(int(ser.readline())) ## add the latest step value to the ste_num list
except ValueError:
pass
if (int(ser.readline()) > 2000): ## if a full revolution has occured, stop grabbing frames and process data
loop = 0
ser.flushInput() ## prevent the serial receive buffer from being overloaded
print "\033[12;0H" + "the dot is " + str(obj_dist) + "cm away"
ser.write('h') ## sent a 'h' over serial to return the stepper to original position
######## Get x-y points from angle and distance information ################
for i in range(len(step_num)):
angles.append(step_num[i]*0.176) ## converting steps into angles in degrees
i = 0
print "len angles : " + str(len(angles)), "count = " +str(count), "length obj_dist = " + str(len(obj_dist))
for i in range(len(angles)): ## getting x-y points from distance and angle information
x_points.append(obj_dist[i]*(math.cos(math.radians(angles[i]))))
y_points.append(obj_dist[i]*(math.sin(math.radians(angles[i]))))
plt.plot(x_points,y_points,'ro') ## plot the x-y points
plt.show() ## show the plot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment