Created
September 17, 2015 07:51
-
-
Save taylor224/3e5b49014c8b5e9c4c64 to your computer and use it in GitHub Desktop.
Vehicle Sound System
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
import time | |
import fcntl, os | |
#p = subprocess.Popen(['mplayer', '-slave', '-quiet', 'motor_start.wav'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
#while(True): | |
# speedstr = raw_input('rpm: ') | |
# print '\r\n' | |
# speednum = int(speedstr) | |
# p.stdin.write('speed_set ' + str(playspeed) + '\n') | |
# break | |
f = open('datastore/data3.json', 'r') | |
data = [] | |
for line in f: | |
sp = line.replace('\n', '').split('=') | |
sec = float(sp[0]) | |
speed = int(sp[1]) | |
rpm = int(float(sp[2])) | |
data.append([sec, speed, rpm]) | |
starttime = time.time() | |
prevlist = [] | |
index = 0 | |
while(True): | |
nowtime = time.time() | |
counter = nowtime - starttime | |
if index >= len(data): | |
break | |
if counter > data[index][0]: | |
sec = data[index][0] | |
speed = data[index][1] | |
rpm = data[index][2] | |
speedacceleration = 0.0 | |
if len(prevlist) == 0: | |
prevlist.append([sec, speed, rpm]) | |
continue | |
print speed | |
difftime = sec - prevlist[-1][0] | |
diffspeed = speed - prevlist[-1][1] | |
# Increasing Speed | |
# Sampling 7 data | |
if not diffspeed < 0: | |
if len(prevlist) <= 8: | |
# Cannot get acceleration | |
pass | |
else: | |
print 'difffff ' + str(prevlist[-1][1] - prevlist[-2][1]) | |
if len(prevlist) > 8: | |
sumspeed = 0.0 | |
sumtime = 0.0 | |
for vindex in range(len(prevlist)): | |
if vindex == 0: | |
continue | |
sumtime += prevlist[vindex][0] - prevlist[vindex-1][0] | |
sumspeed += prevlist[vindex][1] - prevlist[vindex-1][1] | |
speedacceleration = (sumspeed / (len(prevlist) - 1)) * (1 / (sumtime / (len(prevlist) - 1))) | |
prevlist.pop(0) | |
else: | |
pass | |
prevlist.append([sec, speed, rpm]) | |
pass | |
print 'acceleration : ' + str(speedacceleration) | |
#p.stdin.write('speed_set ' + str(speedacceleration) + '\n') | |
prevtime = sec | |
prevspeed = speed | |
index += 1 | |
#for i in range(8000): | |
# playspeed = i/2000.0 | |
# p.stdin.write('speed_set ' + str(playspeed) + '\n') | |
# print i | |
# time.sleep(0.005) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment