-
Notifications
You must be signed in to change notification settings - Fork 2
/
Drop_Test.py~
139 lines (129 loc) · 4.56 KB
/
Drop_Test.py~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import smbus
import mraa
import time
import math
import numpy as np
import scipy.signal as signal
import IMU
#########################################################
# initializing Variables
global A_motor_velocity,B_motor_velocity,C_motor_velocity,D_motor_velocity
A_motor_velocity=B_motor_velocity=C_motor_velocity=D_motor_velocity=0
A=B=C=D=E=F=G=H=I=0
P = 0.3 # proportional control value
timer = 1
output = [A,B,C,D,E,F,G,H,I]
Pin = [14,20,0,21,36,48,47,33,46] #PWM for GP13,GP12,GP182,GP183 Gpio for GP14,GP15,GP49,GP48,GP47
##########################################################
# enabling outputs
for x in xrange(0,4):
output[x] = mraa.Pwm(Pin[x])
output[x].period_us(700)
output[x].enable(True)
#print x, output[x]
for x in xrange(4,9):
output[x] = mraa.Gpio(Pin[x])
output[x].dir(mraa.DIR_OUT)
#print x, output[x]
[Apwm,Bpwm,Cpwm,Dpwm,Adir,Bdir,Cdir,Ddir,mode] = output
output[8].write(1) #Set mode pin to high for pwm/direction
###########################################################
def A_motor_dir():
global A_motor_speed
if A_motor_velocity >= 0:
A_motor_dir = 1
A_motor_speed = abs(A_motor_velocity)/100
if A_motor_speed > 1:
A_motor_speed = 1
elif A_motor_velocity < 0:
A_motor_dir = 0
A_motor_speed = abs(A_motor_velocity)/100
if A_motor_speed > 1:
A_motor_speed = 1
Adir.write(A_motor_dir)
def B_motor_dir():
global B_motor_speed
if B_motor_velocity >= 0:
B_motor_dir = 1
B_motor_speed = abs(B_motor_velocity)/100
if B_motor_speed > 1:
B_motor_speed = 1
elif B_motor_velocity < 0:
B_motor_dir = 0
B_motor_speed = abs(B_motor_velocity)/100
if B_motor_speed > 1:
B_motor_speed = 1
Bdir.write(B_motor_dir)
def C_motor_dir():
global C_motor_speed
if C_motor_velocity >= 0:
C_motor_dir = 1
C_motor_speed = abs(C_motor_velocity)/100
if C_motor_speed > 1:
C_motor_speed = 1
elif C_motor_velocity < 0:
C_motor_dir = 0
C_motor_speed = abs(C_motor_velocity)/100
if C_motor_speed > 1:
C_motor_speed = 1
Cdir.write(C_motor_dir)
def D_motor_dir():
global D_motor_speed
if D_motor_velocity >= 0:
D_motor_dir = 1
D_motor_speed = abs(D_motor_velocity)/100
if D_motor_speed > 1:
D_motor_speed = 1
elif D_motor_velocity < 0:
D_motor_dir = 0
D_motor_speed = abs(D_motor_velocity)/100
if D_motor_speed > 1:
D_motor_speed = 1
Ddir.write(D_motor_dir)
def roll_control():
global A_motor_velocity,B_motor_velocity,C_motor_velocity,D_motor_velocity
t_a=time.time()
[ACCx,ACCy,ACCz,GYRx,GYRy,GYRz,MAGx,MAGy,MAGz] = IMU.read()
A_motor_velocity = P*GYRz + A_motor_velocity
B_motor_velocity = P*GYRz + B_motor_velocity
C_motor_velocity = P*GYRz + C_motor_velocity
D_motor_velocity = P*GYRz + D_motor_velocity
A_motor_dir()
B_motor_dir()
C_motor_dir()
D_motor_dir()
Apwm.write(A_motor_speed)
Bpwm.write(B_motor_speed)
Cpwm.write(C_motor_speed)
Dpwm.write(D_motor_speed)
t=time.time()
Data.write('%5.3f,%5.3f,%5.3f,%5.3f,%5.3f,%5.3f,%5.3f,%5.3f,%5.3f,%5.3f\n') % (t,ACCx,ACCy,ACCz,GYRx,GYRy,GYRz,MAGx,MAGy,MAGz)
filename = time.strftime("%Y-%m-%d %H:%M:%S")
Data = open(filename.__add__('CubeSat_Drop.txt'), 'a')
Data.write('Time,ACCx,ACCy,ACCz,GYRx,GYRy,GYRz,MAGx,MAGy,MAGz\n')
timestart = time.time()
while True:
[ACCx,ACCy,ACCz,GYRx,GYRy,GYRz,MAGx,MAGy,MAGz] = IMU.read()
while ACCz < 8:
while timer < 0.1:
Adir.write(0)
Bdir.write(0)
Cdir.write(0)
Ddir.write(0)
Apwm.write(25)
Bpwm.write(25)
Cpwm.write(25)
Dpwm.write(25)
[ACCx,ACCy,ACCz,GYRx,GYRy,GYRz,MAGx,MAGy,MAGz] = IMU.read()
t=time.time()
Data.write('%5.3f,%5.3f,%5.3f,%5.3f,%5.3f,%5.3f,%5.3f,%5.3f,%5.3f,%5.3f\n') % (t,ACCx,ACCy,ACCz,GYRx,GYRy,GYRz,MAGx,MAGy,MAGz)
timer = time.time() - timestart
roll_control()
Adir.write(0)
Bdir.write(0)
Cdir.write(0)
Ddir.write(0)
Apwm.write(0)
Bpwm.write(0)
Cpwm.write(0)
Dpwm.write(0)