-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpi-app.py
executable file
·284 lines (223 loc) · 8.45 KB
/
pi-app.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/usr/bin/python2
import sys
import time
from flask import Flask
from flask import request, Response,redirect, jsonify, url_for, render_template, abort
import json
from flask_api import FlaskAPI
import RPi.GPIO as GPIO
import smbus,math,serial,re,datetime
import pynmea2
print(sys.version)
app = Flask(__name__)
app.debug = True
class NEO6MGPS:
def __init__(self, serialport, baudratespeed):
self.gpsdevice = serial.Serial(port=serialport, baudrate=baudratespeed, timeout=5)
self.gpsdevice.flush()
self.init()
def flush(self):
self.gpsdevice.flush()
def init(self):
if self.isOpen():
return True
return False
def open(self):
self.gpsdevice.open()
def close(self):
self.gpsdevice.close()
def isOpen(self):
return self.gpsdevice.isOpen()
def readBuffer(self):
try:
data = self.gpsdevice.read(1)
n = self.gpsdevice.inWaiting()
if n:
data = data + self.gpsdevice.read(n)
return data
except Exception, e:
print "Big time read error, what happened: ", e
sys.exit(1)
class hmc5883l:
__scales = {
0.88: [0, 0.73],
1.30: [1, 0.92],
1.90: [2, 1.22],
2.50: [3, 1.52],
4.00: [4, 2.27],
4.70: [5, 2.56],
5.60: [6, 3.03],
8.10: [7, 4.35],
}
def __init__(self, port=1, address=0x1E, gauss=1.3, declination=(0,0)):
self.bus = smbus.SMBus(port)
self.address = address
(degrees, minutes) = declination
self.__declDegrees = degrees
self.__declMinutes = minutes
self.__declination = (degrees + minutes / 60) * math.pi / 180
(reg, self.__scale) = self.__scales[gauss]
self.bus.write_byte_data(self.address, 0x00, 0x70) # 8 Average, 15 Hz, normal measurement
self.bus.write_byte_data(self.address, 0x01, reg << 5) # Scale
self.bus.write_byte_data(self.address, 0x02, 0x00) # Continuous measurement
def declination(self):
return (self.__declDegrees, self.__declMinutes)
def twos_complement(self, val, len):
if (val & (1 << len - 1)):
val = val - (1<<len)
return val
def __convert(self, data, offset):
val = self.twos_complement(data[offset] << 8 | data[offset+1], 16)
if val == -4096: return None
return round(val * self.__scale, 4)
def axes(self):
data = self.bus.read_i2c_block_data(self.address, 0x00)
x = self.__convert(data, 3)
y = self.__convert(data, 7)
z = self.__convert(data, 5)
return (x,y,z)
def heading(self):
(x, y, z) = self.axes()
headingRad = math.atan2(y, x)
headingRad += self.__declination
if headingRad < 0:
headingRad += 2 * math.pi
elif headingRad > 2 * math.pi:
headingRad -= 2 * math.pi
headingDeg = headingRad * 180 / math.pi
return headingDeg
def degrees_minutes(self, headingDeg):
degrees = math.floor(headingDeg)
minutes = round((headingDeg - degrees) * 60)
return (degrees, minutes)
def degrees(self, headingDeg):
degrees = math.floor(headingDeg)
return (degrees)
def __str__(self):
(x, y, z) = self.axes()
return "Axis X: " + str(x) + "\n" \
"Axis Y: " + str(y) + "\n" \
"Axis Z: " + str(z) + "\n" \
"Declination: " + self.degrees(self.declination()) + "\n" \
"Heading: " + self.degrees(self.heading()) + "\n"
@app.route("/")
def indexpage():
return render_template("index.html")
@app.route("/gpsdata")
def gpsdata():
return render_template("gpsdata.html")
@app.route("/compass")
def compass():
return render_template("compass.html")
@app.route("/api/v1.0/compass/get")
def getheading():
try:
compass = hmc5883l(gauss = 2.5, declination = (13,0))
heading = compass.degrees(compass.heading())
response = jsonify(error="",heading = heading,status="SUCCESS")
response.status_code = 200
return response
except Exception,e:
print e
error = str(e)
error = error.replace("'","")
response = jsonify(error=error,heading="",status="FAILED")
response.status_code = 404
return response
@app.route("/api/v1.0/gps/get")
def gps():
try:
device = NEO6MGPS("/dev/ttyAMA0", 9600)
except Exception,e:
print e
response = jsonify({})
response.status_code = 404
return response
newdata = ""
line = ""
gga = False
while not gga:
if newdata:
line = newdata
newdata = ""
line = line + device.readBuffer()
if re.search("\r\n", line):
data, newdata = line.split("\r\n")
try:
pNMEA = pynmea2.parse(data)
if isinstance(pNMEA, pynmea2.types.talker.GGA):
gga = True
print "\r\nLAT:" + pNMEA.lat + pNMEA.lat_dir
print "LONG:" + pNMEA.lon + pNMEA.lon_dir
print "SAT:" + str(pNMEA.num_sats)
print "GPS Sig:" + str(pNMEA.gps_qual)
if (pNMEA.lon) and (pNMEA.lat):
long_degWhole = float(int(float(pNMEA.lon)/100))
long_degDec = (float(pNMEA.lon) - long_degWhole*100)/60
long_deg = long_degWhole + long_degDec
if pNMEA.lon_dir == 'W':
long_deg = (-1) * long_deg
lat_degWhole = float(int(float(pNMEA.lat)/100))
lat_degDec = (float(pNMEA.lat) - lat_degWhole*100)/60
lat_deg = lat_degWhole + lat_degDec
if pNMEA.lat_dir == 'S':
lat_deg = (-1) * lat_deg
else:
lat_deg=""
long_deg=""
response = jsonify(timestamp=str(pNMEA.timestamp),latitude=pNMEA.lat + pNMEA.lat_dir,longitude=pNMEA.lon + pNMEA.lon_dir,num_sats = pNMEA.num_sats,signal=pNMEA.gps_qual,altitude=pNMEA.altitude,altitude_units=pNMEA.altitude_units,geosep=pNMEA.geo_sep,age_gps_data=pNMEA.age_gps_data,ref_station_id=pNMEA.ref_station_id,latitude_deg=str(lat_deg),longitude_deg=str(long_deg))
response.status_code = 200
device.close()
return response
# p = (lat_deg,long_deg)
# print p
# NorthPole = (47.53643693,-110.91472199)
# bearing = int(getbearing(p,NorthPole))
# print "Moving to: " + str(bearing)
#m.move_to(bearing)
device.flush()
except Exception,e:
print e
pass
device.close()
@app.route("/api/v1.0/move")
def MoveRotor():
steps = 4096
direction = request.args.get('direction')
delayms = 2
arg_steps = int(steps)
arg_direction = int(direction)
arg_delayms = int(delayms)
GPIO.setmode(GPIO.BCM)
StepPins = [17,18,23,22]
for pin in StepPins:
GPIO.setup(pin,GPIO.OUT)
GPIO.output(pin, False)
Seq = [[1,0,0,0],
[0,1,0,0],
[0,0,1,0],
[0,0,0,1]]
StepCount = len(Seq)
StepDir = arg_direction
# Set to 1 or 2 for clockwise
# Set to -1 or -2 for anti-clockwise
WaitTime = arg_delayms/float(1000)
StepCounter = 0
TotalSteps = 0
while (TotalSteps != arg_steps):
for pin in range(0, 4):
TotalSteps += 1
xpin = StepPins[pin]
if Seq[StepCounter][pin]!=0:
GPIO.output(xpin, True)
else:
GPIO.output(xpin, False)
StepCounter += StepDir
if (StepCounter>=StepCount):
StepCounter = 0
if (StepCounter<0):
StepCounter = StepCount+StepDir
time.sleep(WaitTime)
return ''
if __name__ == "__main__":
app.run(port=80,debug=True,host="0.0.0.0")