-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrotation.py
executable file
·78 lines (59 loc) · 1.99 KB
/
rotation.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
from mpu6050 import mpu6050
import time
from utils import printProgressBar
class Rotation:
# first u need to calibrate the readings before use
# mpu6050's i2c address
_mpu = mpu6050(0x68)
def __init__(self):
self._stop = False # for stop the program
self._ratecalibrationYaw = 0
self._angleYaw = 0
# self.calibration()
pass
def calibration(self, num_of_points = 1000):
for num in range(num_of_points):
progress = (num + 1) / num_of_points * 100
printProgressBar(progress)
# braking logic
if self._stop:
break
# take data from the sensor
gyro_data = self._mpu.get_gyro_data()
self._ratecalibrationYaw += round(gyro_data['z'])
# calculate the average value
self._ratecalibrationYaw /= num_of_points
print(f'The Calibration value of yaw is : {self._ratecalibrationYaw}')
self.calibrated = True
def calculate_rotation(self, average = 5):
while True:
# breaking logic
if self._stop:
break
# inital values
rateYaw = 0
start = time.time()
for _ in range(average):
gyro_data = self._mpu.get_gyro_data()
rateYaw += round(gyro_data['z'])
# calculation
dt = time.time() - start
rateYaw /= average
rateYaw -= self._ratecalibrationYaw
self._angleYaw += rateYaw * dt
# print(f'algle is : {round(self._angleYaw)}')
pass
def reset(self):
self._angleYaw = 0
print("rotation reseted...")
def stop_game(self):
self._stop = True
if __name__ == '__main__':
rot= Rotation()
rot.calibration()
try:
while True:
rot.calculate_rotation()
except:
print('rotation calculation stopped...')
pass