Code: Select all
import os
import math
from Adafruit_I2C import Adafruit_I2C
from Adafruit_L3GD20 import *
from Adafruit_LSM303DLHC import *
from Adafruit_PWM_Servo_Driver import PWM
from servo_actuator import ServoActuator
import scipy
import numpy as np
from numpy import *
import time
from datetime import datetime, date, timedelta
#import gpsdata as gps
#accelerometer
lsm = LSM303DLHC(0x19, 0x1E, False)
lsm.setTempEnabled(True)
acc = lsm.readAccelerationsG()
heading = lsm.readMagneticHeading()
#gyroscope
l3g = L3GD20(0x6b, False)
gyro = l3g.readDps()
#servo motors
pwm = PWM(0x40, debug=True)
pwm.setPWMFreq(60)
def sub(a,b):
return b-a
vecsub = vectorize(sub)
def AccelInput(accel,t):
tsq = t**2
G = 9.8
return (G*accel*tsq)/2
def Velocity(a,t,v):
G = 9.8
return (G*a*t)+v
def prevelocity(p1,p2,t):
if t < 2:
return 0.0
else:
return (p2-p1)/t
def AngleRads(t,gyr):
convert = t*math.pi/180
return gyr*convert
def TerminalV(t,a,v):
G = 9.8
return t*(a*G)+v
count = 0
DeltaT = 1
class InitialCondition:
#From GPS, find initial position (Start/Launch). GPS output type = decimal coordinates.
"""pos1 = gps.lat
pos2 = gps.long
pos3 = 0.0"""
x=y=z = 0.0
print('DESTINATION:')
print("x'=10.00, y'=7.50, z'=16.09")
pos4=10.00
pos5=7.50
pos6=16.09
print('START (T=0):')
print('x=0, y=0, z=0')
boat = array([x, y, z])
destination = array([pos4, pos5, pos6])
Roll = gyro.x
Pitch = gyro.y
Yaw = gyro.z
print'Launch orientation is:', (Roll, Pitch, Yaw)
print'Desired impact orientation: (5, 6, 7)'
launch = array([Roll, Pitch, Yaw])
impact = array([5, 6, 7])
print'Vector from Launch to Destination is:', vecsub(boat, destination)
print'Rotation from Launch to Impact is:', vecsub(launch, impact)
print("Ready to launch.")
print'Heading:', heading
count = count +1
IC = InitialCondition
timelapsed = float(DeltaT * count)
class CurrentCondition:
if timelapsed == DeltaT:
previousx = IC.x
previousy = IC.y
previousz = IC.z
currentx = previousx + AccelInput(acc.x, timelapsed)
currenty = previousy + AccelInput(acc.y, timelapsed)
elevation = previousz + AccelInput(acc.z, timelapsed)
previousx = currentx
previousy = currenty
previousz = elevation
position = array([currentx, currenty, elevation])
else:
currentx = IC.x + (previousx - IC.x) + AccelInput(acc.x, timelapsed)
currenty = IC.y + (previousy - IC.y) + AccelInput(acc.y, timelapsed)
elevation = IC.z + (previousz - IC.z) + AccelInput(acc.z, timelapsed)
previousx = currentx
previousy = currenty
previousz = elevation
position = array([currentx, currenty, elevation])
count = count + 1
CC = CurrentConditionCode: Select all
import os
import math
from Adafruit_I2C import Adafruit_I2C
from Adafruit_L3GD20 import *
from Adafruit_LSM303DLHC import *
from Adafruit_PWM_Servo_Driver import PWM
from servo_actuator import ServoActuator
import scipy
import numpy as np
from numpy import *
import time
from datetime import datetime, date, timedelta
#import gpsdata as gps
#servo motors
pwm = PWM(0x40, debug=True)
pwm.setPWMFreq(60)
def sub(a,b):
return b-a
vecsub = vectorize(sub)
def AccelInput(accel,t):
tsq = t**2
G = 9.8
return (G*accel*tsq)/2
def Velocity(a,t,v):
G = 9.8
return (G*a*t)+v
def prevelocity(p1,p2,t):
if t < 2:
return 0.0
else:
return (p2-p1)/t
def AngleRads(t,gyr):
convert = t*math.pi/180
return gyr*convert
def TerminalV(t,a,v):
G = 9.8
return t*(a*G)+v
count = 0
DeltaT = 1
class InitialCondition:
#accelerometer
lsm = LSM303DLHC(0x19, 0x1E, False)
lsm.setTempEnabled(True)
acc = lsm.readAccelerationsG()
heading = lsm.readMagneticHeading()
#gyroscope
l3g = L3GD20(0x6b, False)
gyro = l3g.readDps()
#From GPS, find initial position (Start/Launch). GPS output type = decimal coordinates.
"""pos1 = gps.lat
pos2 = gps.long
pos3 = 0.0"""
x=y=z = 0.0
print('DESTINATION:')
print("x'=10.00, y'=7.50, z'=16.09")
pos4=10.00
pos5=7.50
pos6=16.09
print('START (T=0):')
print('x=0, y=0, z=0')
boat = array([x, y, z])
destination = array([pos4, pos5, pos6])
Roll = gyro.x
Pitch = gyro.y
Yaw = gyro.z
print'Launch orientation is:', (Roll, Pitch, Yaw)
print'Desired impact orientation: (5, 6, 7)'
launch = array([Roll, Pitch, Yaw])
impact = array([5, 6, 7])
print'Vector from Launch to Destination is:', vecsub(boat, destination)
print'Rotation from Launch to Impact is:', vecsub(launch, impact)
print("Ready to launch.")
print'Heading:', heading
count = count +1
IC = InitialCondition
timelapsed = float(DeltaT * count)
class CurrentCondition:
#accelerometer
lsm = LSM303DLHC(0x19, 0x1E, False)
lsm.setTempEnabled(True)
acc = lsm.readAccelerationsG()
heading = lsm.readMagneticHeading()
#gyroscope
l3g = L3GD20(0x6b, False)
gyro = l3g.readDps()
if timelapsed == DeltaT:
previousx = IC.x
previousy = IC.y
previousz = IC.z
currentx = previousx + AccelInput(acc.x, timelapsed)
currenty = previousy + AccelInput(acc.y, timelapsed)
elevation = previousz + AccelInput(acc.z, timelapsed)
previousx = currentx
previousy = currenty
previousz = elevation
position = array([currentx, currenty, elevation])
count = count + 1
else:
currentx = IC.x + (previousx - IC.x) + AccelInput(acc.x, timelapsed)
currenty = IC.y + (previousy - IC.y) + AccelInput(acc.y, timelapsed)
elevation = IC.z + (previousz - IC.z) + AccelInput(acc.z, timelapsed)
previousx = currentx
previousy = currenty
previousz = elevation
position = array([currentx, currenty, elevation])
count = count + 1
CC = CurrentCondition
class TransitAnalysis:
print("Launch.")
#gyroscope
l3g = L3GD20(0x6b, False)
gyro = l3g.readDps()
while (True):
time.sleep(1)
if CC.acc.x < 0.1:
CC.acc.x = 0.000
if CC.acc.y < 0.1:
CC.acc.y = 0.000
"""if gyro.x - IC.Roll > 0:
VX = math.cos(AngleRads(DeltaT, gyro.y)) * (Velocity(acc.x,DeltaT,vx))
if gyro.y - IC.Pitch > 0:
VY = math.cos(AngleRads(DeltaT, gyro.y)) * (Velocity(acc.y,DeltaT,vy))
if gyro.z - IC.Yaw > 0:
FallVel = math.cos(AngleRads(DeltaT, gyro.z)) *(Vprev(z, previousz, timelapsed))
currentx = AccPos(acc.x, DeltaT, VX, previousx)
currenty = AccPos(acc.y, DeltaT, VY, previousy)
elevation = Vprev(DeltaT, previousz, pos3) + previousz"""
CurrentRoll = AngleRads(DeltaT, CC.gyro.x)
CurrentPitch = AngleRads(DeltaT, CC.gyro.y)
CurrentYaw = AngleRads(DeltaT, CC.gyro.z)
CurrentO = array([CC.gyro.x, CC.gyro.y, CC.gyro.z])
print'Position:', CC.position
print'Vector from Position to Destination is:', vecsub(CC.position, IC.destination)
print'Current orientation, Gyro X: %8.3f dps Y: %8.3f dps Z: %8.3f dps' % (CC.gyro.x, CC.gyro.y, CC.gyro.z)
print'Rotation from Current to Impact is:', vecsub(CurrentO, IC.impact)
print'Heading:', CC.heading
"""if TerminalV(DeltaT, CC.acc.z, FallVel) > 19.5:
print "That's all she's got Captain." """