-
Notifications
You must be signed in to change notification settings - Fork 0
/
CompassCorrection.py
165 lines (137 loc) · 3.59 KB
/
CompassCorrection.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
import time
import smbus
import RPi.GPIO as IO
import sys
import Tkinter as tk
import os
import smbus
import time
import math
import thread
IO.setwarnings(False)
IO.setmode (IO.BOARD)
IO.setup(31,IO.OUT)
IO.setup(33,IO.OUT)
IO.setup(35,IO.OUT)
IO.setup(37,IO.OUT)
IO.setup(38,IO.OUT)
IO.setup(40,IO.OUT)
left = IO.PWM(38,100)
right = IO.PWM(40,100)
left.start(0)
right.start(0)
DONE=""
RANGE=5
class Compass:
bus = smbus.SMBus(1)
address = 0x1e
def __init__(self, address):
self.address = address
def read_byte(self, adr):
return self.bus.read_byte_data(self.address, adr)
def read_word(self, adr):
high = self.bus.read_byte_data(self.address, adr)
low = self.bus.read_byte_data(self.address, adr+1)
val = (high << 8) + low
return val
def read_word_2c(self, adr):
val = self.read_word(adr)
if (val >= 0x8000):
return -((65535 - val) + 1)
else:
return val
def write_byte(self, adr, value):
self.bus.write_byte_data(self.address, adr, value)
def getOrientation(self):
self.write_byte(0, 0b01110000) # Set to 8 samples @ 15Hz
self.write_byte(1, 0b00100000) # 1.3 gain LSb / Gauss 1090 (default)
self.write_byte(2, 0b00000000) # Continuous sampling
## time.sleep(3)
scale = 0.92
x_offset = 46
y_offset = -117
x_out = (self.read_word_2c(3) - x_offset) * scale
y_out = (self.read_word_2c(7) - y_offset) * scale
z_out = (self.read_word_2c(5)) * scale
bearing = math.atan2(y_out, x_out)
if (bearing < 0):
bearing += 2 * math.pi
return math.degrees(bearing)
class Motion:
global tickDirection
tickDirection=[]
def forward(self,actualDir):
print "working forward........................................."
IO.output(31,IO.HIGH)
IO.output(33,IO.LOW)
IO.output(35,IO.LOW)
IO.output(37,IO.HIGH)
left.ChangeDutyCycle(60)
right.ChangeDutyCycle(60)
time.sleep(1)
left.ChangeDutyCycle(0)
right.ChangeDutyCycle(0)
tickDirection.append(actualDir)
print "------------------------------------------------------"
print tickDirection
print "Forward"
def right(self):
print "working right........................................."
IO.output(31,IO.HIGH)
IO.output(33,IO.LOW)
IO.output(35,IO.HIGH)
IO.output(37,IO.LOW)
left.ChangeDutyCycle(30)
right.ChangeDutyCycle(30)
time.sleep(1)
left.ChangeDutyCycle(0)
right.ChangeDutyCycle(0)
print "Right"
def left(self):
print "working left........................................."
IO.output(31,IO.LOW)
IO.output(33,IO.HIGH)
IO.output(35,IO.LOW)
IO.output(37,IO.HIGH)
left.ChangeDutyCycle(30)
right.ChangeDutyCycle(30)
time.sleep(1)
left.ChangeDutyCycle(0)
right.ChangeDutyCycle(0)
print "Left"
def calDir(target, current, compassRange=RANGE):
motion=Motion()
target=target%360
current=current%360
delta=(target-current)%360
print("Target=%f Current=%f Delta=%f"%(target,current,delta))
if delta <= compassRange:
status=DONE
else:
if delta>180:
status=motion.left()
else:
status=motion.right()
return status
def main():
motion=Motion()
compass = Compass(0x1e)
while(True):
ANGLE=287
try:
angleTarget=float(ANGLE)
status=motion.left()
while (status!=DONE):
currentAngle=compass.getOrientation()
status=calDir(angleTarget,currentAngle)
time.sleep(1)
print("Angle Reached!")
break
except ValueError:
print("Enter valid angle!")
pass
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print ("Finish")