Create a gist now

Instantly share code, notes, and snippets.

anonymous /TacoCopter simulation.py
Created Mar 25, 2012

What would you like to do?
A file to generate acceleration, velocity, and position vs. time data for a simulated TacoCopter
import math
import random
mass_object = 3.
drag_coefficient = 0.3
g = 9.8
dt = 0.1
total_T = 30 # final time in seconds
v_max = 10 #maximum speed of tacocopter
b = 0.1 #constant for tweaking the rate of acceleration of the tacocopter
c = 0.5
initial_v = 0.
initial_x = 0.
setpoint_x = 100
accel_max = 5.6
N = int(total_T*1/dt)
print N
t = [0]
accel = []
v = [initial_v]
x = [initial_x]
for i in range(N):
curr_accel = b*(setpoint_x - x[i])-c*v[i]+random.gauss(0,0.5)
if (abs(curr_accel)>accel_max):
if (curr_accel>0):
curr_accel = accel_max +random.gauss(0,0.1)
else:
curr_accel = -accel_max -random.gauss(0,0.5)
accel.append(curr_accel)
new_v = v[i] + curr_accel*dt
v.append(new_v)
new_x = x[i] + v[i]*dt + 0.5*curr_accel*dt**2
x.append(new_x)
new_t = (i+1)*dt
t.append(new_t)
outFile = open('TacoCopter.csv','w')
for i in range(N):
print '%.5f; %.5f; %.5f; %.5f' % (t[i], x[i], v[i], accel[i])
s = str(t[i]) + ',' + str(x[i]) + ',' + str(v[i]) + ',' + str(accel[i])
outFile.write(s)
outFile.write('\n')
outFile.close()
outFile = open('TacoCopter-short.csv','w') #this file is for generating a table of only integral values of time.
i = 0
while(i<N):
s = str(t[i]) + ',' + str(x[i]) + ',' + str(v[i]) + ',' + str(accel[i])
outFile.write(s)
outFile.write('\n')
i = i + 10
outFile.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment