-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcaroussel.py
187 lines (167 loc) · 7.45 KB
/
caroussel.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
from picamera import PiCamera
import RPi.GPIO as GPIO
import time
class Caroussel():
"""Here is saved every method related to the Raspberry and the GPIO. Therefor the methods for running the camera, the motor etc.
The schedule is saved in the experiment-class"""
def __init__(self):
#Set pins as global_parameter
self.pin_whitelight = 27
self.pin_redlight = 22
self.pin_motor1 = 24
self.pin_motor2 = 25
self.pin_motor1_dir = 18
self.pin_motor2_dir = 23
self.pin_clunch1 = 3
self.pin_clunch2 = 4
#camera
self.camera = self.set_camera()
#Light
self.redlight = False
self.whitelight = False
#TODO: GPIO outside the class?
# Use board pin numberingc
GPIO.setmode(GPIO.BCM)
#You need to set up every channel you are using as an input or an output.
#To configure a channel as an input
GPIO.setup(self.pin_whitelight, GPIO.OUT) ## Setup redLED --> GPIO.output(27,False) = White LED on
GPIO.setup(self.pin_redlight, GPIO.OUT) ## Setup whiteLED --> GPIO.output(22,False) = Red LED on
GPIO.setup(self.pin_motor1, GPIO.OUT) ## Setup Motor1 Speed --> GPIO.output(24,True) = motor1 turns
GPIO.setup(self.pin_motor2, GPIO.OUT) ## Setup Motor2 Speed --> GPIO.output(25,True) = motor2 turns
GPIO.setup(self.pin_motor1_dir, GPIO.OUT) ## Setup Motor1 Direction --> GPIO.output(18,True) = ccw
GPIO.setup(self.pin_motor2_dir, GPIO.OUT) ## Setup Motor2 Direction --> GPIO.output(23,True) = ccw
GPIO.setup(self.pin_clunch1, GPIO.OUT) ## Setup Magnet1 --> GPIO.output(3,True) = if(motor1): disc1 turns
GPIO.setup(self.pin_clunch2, GPIO.OUT) ## Setup Magnet2 --> GPIO.output(4,True) = if(motor2): disc2 turns
#motor
self.motor1dir = True #True = cw, False = ccw
def set_camera(self):
#Data comes from the old script
camera = PiCamera()
x,y = (1296, 972)
camera.resolution = (x, y)
px = 768
xb = (x/2.0 - px/2) / x
yb = (y/2.0 - px/2) / y
camera.zoom = (xb,yb,px/float(x),px/float(y))
camera.hflip = True
camera.vflip = True
camera.exposure_mode = 'auto'
return camera
def start_motor(self):
#part of the cronjob started in main -> experiment.cron()
GPIO.output([self.pin_clunch1,self.pin_clunch2],False) #Set the magnets,so that the caroussels don't move
GPIO.output([self.pin_motor1,self.pin_motor2],True) #starts channels 24,25 (motors)
def stop_motors(self):
GPIO.output([self.pin_motor1,self.pin_motor2],False)
GPIO.output([self.pin_clunch1,self.pin_clunch2],False)
def set_nightlight(self):
#part of the cronjob started in main -> experiment.cron()
if(self.redlight == False):
GPIO.output(self.pin_whitelight,True) #white off
GPIO.output(self.pin_redlight,False) #red on
self.whitelight = False
self.redlight = True
def set_daylight(self):
#part of the cronjob started in main -> experiment.cron()
if(self.whitelight == False):
GPIO.output(self.pin_whitelight,False) #white on
GPIO.output(self.pin_redlight,True) #red off
self.redlight = False
self.whitelight = True
def shut_light(self):
GPIO.output([self.pin_redlight,self.pin_whitelight],True) #white off
def stop_turning_motor2(self):
GPIO.output(self.pin_motor2_dir, False) #Motor2 out?
GPIO.output(self.pin_clunch2,False)
def stop_turning_motor1(self):
GPIO.output(self.pin_motor1_dir, False) #Motor1 out?
GPIO.output(self.pin_clunch1,False)
def turn_motor1(self,direction1):
self.stop_turning_motor2()
GPIO.output(self.pin_clunch1,True)
if(direction1 == 'cw'):
GPIO.output(self.pin_motor1_dir, False) #Motor1 clockwise?
self.motor1dir = True
elif(direction1 == 'ccw'):
GPIO.output(self.pin_motor1_dir, True) #Motor1 counterclockwise?
self.motor1dir = False
def turn_motor2(self,direction2):
self.stop_turning_motor1()
GPIO.output(self.pin_clunch2,True)
if(direction2 == 'same' and self.motor1dir):
GPIO.output(self.pin_motor2_dir, False) #Motor2 clockwise?
elif(direction2 == 'same' and not self.motor1dir):
GPIO.output(self.pin_motor2_dir, True) #Motor2 counterclockwise?
elif(direction2 == 'different' and self.motor1dir):
GPIO.output(self.pin_motor2_dir, True) #Motor2 counterclockwise?
elif(direction2 == 'different' and not self.motor1dir):
GPIO.output(self.pin_motor2_dir, False) #Motor2 clockwise?
def test_functions(self):
print("#### Welcome to the Functionality-test-regime ####")
print('Start the Camera')
self.camera.start_preview(fullscreen=False, window = (100, 20, 640, 480))
time.sleep(2)
print("### Testing the Light ###")
print('White Light')
self.set_daylight()
time.sleep(2)
print('Red Light')
self.set_nightlight()
time.sleep(5)
self.set_daylight()
print("### Testing the Motors ###")
self.start_motor()
print('Motor 1 - cw')
self.turn_motor1("cw")
time.sleep(5)
print('Motor 1 - ccw')
self.turn_motor1("ccw")
time.sleep(5)
print('Motor 2 - cw')
self.turn_motor2("different")
time.sleep(5)
print('Motor 2 - ccw')
self.turn_motor2("same")
time.sleep(5)
print('Motor 1 and 2 - cw/same')
for i in range(2):
self.turn_motor1("cw")
time.sleep(2)
self.turn_motor2("same")
time.sleep(2)
print('Motor 1 and 2 - cw/different')
for i in range(2):
self.turn_motor1("cw")
time.sleep(2)
self.turn_motor2("different")
time.sleep(2)
print('Motor 1 and 2 - ccw/same')
for i in range(2):
self.turn_motor1("ccw")
time.sleep(2)
self.turn_motor2("same")
time.sleep(2)
print('Motor 1 and 2 - ccw/different')
for i in range(2):
self.turn_motor1("ccw")
time.sleep(2)
self.turn_motor2("different")
time.sleep(2)
print('### Stop Motors ###')
self.stop_motors()
print("Shut the Light")
GPIO.output(self.pin_redlight, True)
time.sleep(2)
print("Stop the Camera")
self.camera.stop_preview()
self.camera.close()
print("### End of the Test ###")
self.cleanup()
def cleanup(self):
GPIO.output(self.pin_whitelight, True) # redLED off
GPIO.output(self.pin_redlight, True) # whiteLED off
GPIO.output(self.pin_motor1, False) # Motor1 off
GPIO.output(self.pin_motor2, False) # Motor2 off
GPIO.output(self.pin_clunch1, False) # Magnet 1 off
GPIO.output(self.pin_clunch2, False) # Magnet 2 off
GPIO.cleanup()