-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera_sensors.py
83 lines (65 loc) · 2.4 KB
/
camera_sensors.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
from urllib2 import urlopen
import json
import numpy as np
import math
class AngleFilter:
def __init__(self, freq):
self.freq = freq
self.last_sine = 0
self.last_cos = 0
self.angle = 0
def update(self, new_angle):
self.last_sine = self.freq * self.last_sine + (1 - self.freq) * math.sin(new_angle)
self.last_cos = self.freq * self.last_cos + (1 - self.freq) * math.cos(new_angle)
self.angle = math.atan2(self.last_sine, self.last_cos)
return self.angle
def get_angle(self, degrees=True):
if degrees:
return self.angle * (180.0 / math.pi)
else:
return self.angle
class Sensor:
def __init__(self, name):
self.name = name
self.value = None
@staticmethod
def get_last_reading(data):
return data[len(data) - 1][1]
def update(self, data):
self.value = np.array(self.get_last_reading(data))
def set(self, value):
self.value = value
def get_jsonparsed_data(url):
response = urlopen(url)
data = str(response.read())
return json.loads(data)
class CameraSensors:
def __init__(self, url):
self.url = url # type: str
self.sensors = {} # type: dict[str, Sensor]
def update_sensor(self, name, data):
""" Updates a sensors value. If it doesn't exist, create it"""
if name not in self.sensors:
self.sensors[name] = Sensor(name)
self.sensors[name].update(data[name]["data"])
def set_sensor(self, name, value):
if name not in self.sensors:
self.sensors[name] = Sensor(name)
self.sensors[name].set(value)
def get_sensor(self, name):
""" Return the sensor with the give name """
if name in self.sensors:
return self.sensors[name]
else:
return False
def update(self):
sensors_data = get_jsonparsed_data(self.url + "sensors.json")
if "accel" in sensors_data:
self.update_sensor("accel", sensors_data)
ax, ay, az = self.get_sensor("accel").value
yaw = math.atan2(ax, ay) * (180 / math.pi)
pitch = math.atan2(ay, math.sqrt(math.pow(ax, 2) + math.pow(az, 2))) * (180 / math.pi)
roll = math.atan2(ax, az) * (180 / math.pi)
self.set_sensor("rotation", yaw)
self.set_sensor("pitch", pitch)
self.set_sensor("roll", roll)