-
Notifications
You must be signed in to change notification settings - Fork 2
/
CubSAT_RCS.py
111 lines (99 loc) · 3.69 KB
/
CubSAT_RCS.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
import smbus
import mraa
import time
import math
import numpy as np
import scipy.signal as signal
import IMU
##################################################
# initializing Variables
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():
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)
return [GYRz,A_motor_velocity,B_motor_velocity,C_motor_velocity,D_motor_velocity]
while True:
[GYRz,A_motor_velocity,B_motor_velocity,C_motor_velocity,D_motor_velocity]=roll_control()
print "GYRz: %3.2f, A: %3.2f, B: %3.2f, C: %3.2f, D: %3.2f" %(GYRz,A_motor_velocity,B_motor_velocity,C_motor_velocity,D_motor_velocity)