-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
102 lines (82 loc) · 2.59 KB
/
main.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
from machine import Pin, I2C
from math import sqrt
from time import sleep_ms
import functions, acc, mag
#========== functions =============
# Twos complement
# MQTT send/receive
# Connect to wifi
#========== acc ===============
# accelerometer API
#========== mag ===============
# magnetometer api
# Main function to detect change in angle within 300ms
def start (client):
n = 0
while n < 3:
x, y, z = mag.readXYZ(i2c)
xy = mag.angle(x,y)
xz = mag.angle(x,z)
yz = mag.angle(y,z)
if n == 0:
print ('Calculating...')
inxy = xy
inxz = xz
inyz = yz
if n == 2:
print ('Result:')
yzd = mag.difference(yz,inyz)
xyd = mag.difference(xy,inxy)
xzd = mag.difference(xz,inxz)
if yzd >=50 and xyd <=50 and xzd <= 50:
functions.mqttSend('s',1,client)
print ('flat swing')
elif yzd >=29 and xyd >= 56 and xzd >= 13:
functions.mqttSend('s',2,client)
print('top spin')
else:
#Miss! send data to server for processing
functions.mqttSend3(xyd,yzd,xzd,client)
#functions.mqttSend('s',0,client)
print ('Angle achieved = ' + str(yzd))
print ('No swing detected')
return 0
n += 1
sleep_ms(150)
#=================================Start program ============================
# Initialize i2c
i2c = I2C(scl=Pin(5), sda=Pin(4), freq=100000)
# Connect to wifi
functions.do_connect()
# Initializecontinuous measurement
mag.setup_cont(i2c)
# Initialize accelerometer
acc.setup(i2c)
# Calibration (currently not needed for angle measurement)
#xOffset , yOffset = 0,0
#functions.calibrate(i2c)
#Setup mqtt client, ensure you are connected to EE Rover
client = functions.mqttConnect()
input("PLEASE PRESS ENTER TO START")
print('ready')
pressed = True;
while True :
#button pressed will swap pressed value
#pressed = 1 - we are measuring swings
#pressed = 0 - we are using the compass mode
first = Pin(12, Pin.IN, Pin.PULL_UP).value()
if first:
sleep_ms(50)
second = Pin(12, Pin.IN, Pin.PULL_UP).value()
print('second = ' + str(second))
if first and not second:
pressed = not pressed
# Swing mode
if pressed == True:
if acc.magnitude(i2c) > 150:
start(client)
# Compass mode
if pressed == False:
x, y, z = mag.readXYZ(i2c)
functions.mqttSend('c',mag.angle(x,y),client)
sleep_ms(1)