-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompass.py
132 lines (112 loc) · 2.97 KB
/
compass.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
import math
from i2clibraries import i2c_hmc5883l
import RPi.GPIO as GPIO
import time
Motor2F = 18
Motor2P = 16
Motor2B = 12
Motor1F = 15
Motor1P = 13
Motor1B = 11
leftMotor = 0
rightMotor = 0
timeofRot = 2.425
def turnToAngle(desiredTheta,currentTheta):
error = desiredTheta - currentTheta
print (desiredTheta,currentTheta)
print(error)
if(abs(error)<5.0):
stopBot()
if(error>5.0):
turnZeroRight()
elif(error< -5.0):
turnZeroLeft()
def turnToAngleL(theta):
turnZeroLeft()
time.sleep(timeofRot/360.0*theta)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(Motor1F,GPIO.OUT)
GPIO.setup(Motor1B,GPIO.OUT)
GPIO.setup(Motor1P,GPIO.OUT)
GPIO.setup(Motor2F,GPIO.OUT)
GPIO.setup(Motor2B,GPIO.OUT)
GPIO.setup(Motor2P,GPIO.OUT)
hmc5883l = i2c_hmc5883l.i2c_hmc5883l(1)
hmc5883l.setContinuousMode()
hmc5883l.setDeclination(0,6)
def moveForward():
GPIO.output(Motor1F,True)
GPIO.output(Motor2F,True)
GPIO.output(Motor1B,False)
GPIO.output(Motor2B,False)
GPIO.output(Motor1P,True)
GPIO.output(Motor2P,True)
def moveBackward():
GPIO.output(Motor1F,False)
GPIO.output(Motor2F,False)
GPIO.output(Motor1B,False)
GPIO.output(Motor2B,False)
GPIO.output(Motor1P,True)
GPIO.output(Motor2P,True)
def turnZeroLeft():
GPIO.output(Motor1F,True)
GPIO.output(Motor2F,False)
GPIO.output(Motor1B,False)
GPIO.output(Motor2B,False)
GPIO.output(Motor1P,True)
GPIO.output(Motor2P,True)
def turnZeroRight():
GPIO.output(Motor1F,False)
GPIO.output(Motor2F,True)
GPIO.output(Motor1B,False)
GPIO.output(Motor2B,False)
GPIO.output(Motor1P,True)
GPIO.output(Motor2P,True)
def turnLeft():
GPIO.output(Motor1F,True)
GPIO.output(Motor2F,False)
GPIO.output(Motor1B,False)
GPIO.output(Motor2B,True)
GPIO.output(Motor1P,True)
GPIO.output(Motor2P,False)
def turnRight():
GPIO.output(Motor1F,False)
GPIO.output(Motor2F,True)
GPIO.output(Motor1B,True)
GPIO.output(Motor2B,False)
GPIO.output(Motor1P,False)
GPIO.output(Motor2P,True)
def stopBot():
GPIO.output(Motor1F,False)
GPIO.output(Motor2F,True)
GPIO.output(Motor1B,True)
GPIO.output(Motor2B,True)
GPIO.output(Motor1P,False)
GPIO.output(Motor2P,False)
def testWSAD():
moveForward()
time.sleep(1)
stopBot()
time.sleep(3)
moveBackward()
time.sleep(1)
stopBot()
time.sleep(3)
turnZeroLeft()
time.sleep(1)
stopBot()
time.sleep(3)
turnZeroRight()
time.sleep(1)
stopBot()
time.sleep(3)
turnLeft()
time.sleep(1)
stopBot()
time.sleep(3)
turnRight()
time.sleep(1)
stopBot()
GPIO.cleanup()
while(1):
turnToAngle(90.0,hmc5883l.getHeading())