-
Notifications
You must be signed in to change notification settings - Fork 2
/
Motor_Switch_Control.py
93 lines (84 loc) · 2.74 KB
/
Motor_Switch_Control.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
from Motor_Movement import zplus,zminus,rightstep,leftstep,upwardstep,downwardstep
import RPi.GPIO as GPIO
import time
from os import path
import os.path
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(15,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(19,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(21,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(23,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(33,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(29,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
if(path.exists("position_movements.txt")==False):
f=open("position_movements.txt","w")
f1=open("present_position.txt","w")
f.write("0 0\n")
f1.write("0 0\n")
f.close();f1.close()
file=open("present_position.txt","r")
curr=file.read()
if(curr==''):
curr='0 0'
print("tes")
print(curr)
split=curr.split(' ');file.close()
x=float(split[0]);y=float(split[1])
try:
f=open("position_movements.txt","a")
cu=open("present_position.txt","w")
while True:
if GPIO.input(15):
zplus()
time.sleep(0.1)
if GPIO.input(19):
zminus()
time.sleep(0.1)
if GPIO.input(21):
## if(x-1<0):
## print("moving out of bound on left")
## else:
x=x-1
print("x="+str(x)+"y="+str(y))
f.write(str(x)+" "+str(y)+"\n");cu.seek(0)
cu.write(str(x)+" "+str(y))
leftstep()
time.sleep(0.1)
if GPIO.input(23):
## if(y-0.126<0):
## print("moving out of bound on down")
## else:
y=y-1
print("x="+str(x)+"y="+str(y))
f.write(str(x)+" "+str(y)+"\n");cu.seek(0)
cu.write(str(x)+" "+str(y))
downwardstep()
time.sleep(0.1)
if GPIO.input(33):
## if(y+0.126>52):
## print("moving out of bound on up")
## else:
y=y+1
print("x="+str(x)+"y="+str(y))
f.write(str(x)+" "+str(y)+"\n");cu.seek(0)
cu.write(str(x)+" "+str(y))
upwardstep()
time.sleep(0.1)
if GPIO.input(29):
## if(x+0.126>84):
## print("moving out of bound on right")
## else:
x=x+1
print("x="+str(x)+"y="+str(y))
f.write(str(x)+" "+str(y)+"\n");cu.seek(0)
cu.write(str(x)+" "+str(y))
rightstep()
time.sleep(0.1)
except KeyboardInterrupt:
print("KeyBoard has been Interrupted")
except Exception as exc:
print(exc,"has occured")
finally:
cu.close()
f.close()