Skip to content

Instantly share code, notes, and snippets.

@JarrettR
Created November 3, 2015 07:29
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 JarrettR/bfdcca081d165f65908e to your computer and use it in GitHub Desktop.
Save JarrettR/bfdcca081d165f65908e to your computer and use it in GitHub Desktop.
Turn a CSV file into servo movements with a RasPi
#Setup here:
#http://cihatkeser.com/servo-control-with-raspberry-pi-in-5-minutes-or-less/
import os
import time
os.system('./servod')
def servoPosition(servoId, servoPos):
os.system("echo " + str(servoId) + "=" + str(servoPos) + "% > /dev/servoblaster")
def servoPlot(servoId1, servoPos1, servoId2, servoPos2, delay):
servoPosition(servoId1, servoPos1)
servoPosition(servoId2, servoPos2)
print(str(servoPos1) + ", " + str(servoPos2))
time.sleep(delay)
#thx wikipedia
def line(x0, y0, x1, y1, delay):
deltax = x1 - x0
deltay = y1 - y0
error = 0.0
deltaerr = abs(float(deltay) / float(deltax))
y = y0
xStep = 1
if x0 > x1:
xStep = -1
for x in range(x0, x1, xStep):
servoPlot(0, x, 1, y, delay)
error = error + deltaerr
while error >= 0.5:
if y1 > y0:
y = y + 1
else:
y = y - 1
error = error - 1.0
servoPlot(0, x, 1, y, delay)
f = open('servoinput.csv', 'r')
startLine = f.readline()
for nextLine in f.readlines():
start = startLine.split(',')
next = nextLine.split(',')
#print("Move from " + start[0] + ", " + start[1])
print("Move to " + next[0] + ", " + next[1])
line(int(start[0]), int(start[1]), int(next[0]), int(next[1]), float(next[2]))
startLine = nextLine
0 0 0
5 10 0.5
20 10 0.2
30 15 0.7
80 10 0.5
20 80 0.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment