-
Notifications
You must be signed in to change notification settings - Fork 4
/
EMAR.py
199 lines (160 loc) · 6.09 KB
/
EMAR.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
############################################################################################
#
# Project: Peter Moss COVID-19 AI Research Project
# Repository: EMAR Mini, Emergency Assistance Robot
#
# Author: Adam Milton-Barker (AdamMiltonBarker.com)
# Contributors:
# Title: EMAR Mini Emergency Assistance Robot Class
# Description: The EMAR Mini Emergency Assistance Robot Class is the the core
# for the EMAR Mini software.
# License: MIT License
# Last Modified: 2020-07-12
#
############################################################################################
import geocoder, json, psutil, sys, threading, time
import RPi.GPIO as GPIO
from threading import Thread
from Classes.Helpers import Helpers
from Classes.iotJumpWay import Device as iotJumpWay
from Classes.RealsenseRead import RealsenseRead
from Classes.RealsenseStream import RealsenseStream
class EMAR():
""" EMAR Mini Emergency Assistance Robot Class
The EMAR Mini Emergency Assistance Robot Class is the the core wrapper class
for the EMAR Mini software.
"""
def __init__(self):
""" Initializes the class. """
self.Helpers = Helpers("EMAR")
# Starts the iotJumpWay
self.iotJumpWay = iotJumpWay()
self.iotJumpWay.connect()
# Subscribes to the EMAR Mini commands topic
self.iotJumpWay.channelSub("Commands")
# Sets the EMAR Mini commands callback function
self.iotJumpWay.commandsCallback = self.commands
self.Helpers.logger.info("EMAR Mini awaiting commands.")
self.Helpers.logger.info("EMAR Mini Emergency Assistance Robot Class initialization complete.")
def hardware(self):
""" Loads the EMAR Mini hardware modules. """
# Head Servo 1
h1Pin = 18
GPIO.setmode(GPIO.BCM)
GPIO.setup(h1Pin, GPIO.OUT)
self.h1 = GPIO.PWM(h1Pin, 50)
self.h1.start(7)
time.sleep(0.5)
self.h1.ChangeDutyCycle(0)
# Arm Servo 1
a1Pin = 12
GPIO.setmode(GPIO.BCM)
GPIO.setup(a1Pin, GPIO.OUT)
self.a1 = GPIO.PWM(a1Pin, 50)
self.a1.start(7)
time.sleep(0.5)
self.a1.ChangeDutyCycle(0)
# Arm Servo 2
a2Pin = 13
GPIO.setmode(GPIO.BCM)
GPIO.setup(a2Pin, GPIO.OUT)
self.a2 = GPIO.PWM(a2Pin, 50)
self.a2.start(7)
time.sleep(0.5)
self.a2.ChangeDutyCycle(0)
self.Helpers.logger.info("EMAR Mini hardware modules loaded.")
def commands(self, topic, payload):
"""
iotJumpWay Commands Callback
The callback function that is triggerend in the event of a
command communication from the iotJumpWay.
"""
self.Helpers.logger.info("Recieved iotJumpWay Command Data : " + payload.decode())
command = json.loads(payload.decode("utf-8"))
cycle = 0
servo = None
if(command["Type"]=="Head"):
if(command["Value"]=="RIGHT"):
cycle = 2.0
servo = self.h1
if(command["Value"]=="LEFT"):
cycle = 12.0
servo = self.h1
if(command["Value"]=="CENTER"):
cycle = 7.0
servo = self.h1
if(command["Type"]=="Arm"):
if(command["Value"]=="2UP"):
cycle = 7.0
servo = self.a1
if(command["Value"]=="2DOWN"):
cycle = 12.0
servo = self.a1
if(command["Value"] == "UP"):
cycle = 7.0
servo = self.a2
if(command["Value"]=="DOWN"):
cycle = 12.0
servo = self.a2
servo.ChangeDutyCycle(cycle)
time.sleep(0.5)
servo.ChangeDutyCycle(0)
def life(self):
""" Sends vital statistics to HIAS """
# Gets vitals
cpu = psutil.cpu_percent()
mem = psutil.virtual_memory()[2]
hdd = psutil.disk_usage('/').percent
tmp = psutil.sensors_temperatures()['cpu-thermal'][0].current
g = geocoder.ip('me')
self.Helpers.logger.info("EMAR Mini Life (TEMPERATURE): " + str(tmp) + "\u00b0")
self.Helpers.logger.info("EMAR Mini Life (CPU): " + str(cpu) + "%")
self.Helpers.logger.info("EMAR Mini Life (Memory): " + str(mem) + "%")
self.Helpers.logger.info("EMAR Mini Life (HDD): " + str(hdd) + "%")
self.Helpers.logger.info("EMAR Mini Life (LAT): " + str(g.latlng[0]))
self.Helpers.logger.info("EMAR Mini Life (LNG): " + str(g.latlng[1]))
# Send iotJumpWay notification
self.iotJumpWay.channelPub("Life", {
"CPU": cpu,
"Memory": mem,
"Diskspace": hdd,
"Temperature": tmp,
"Latitude": g.latlng[0],
"Longitude": g.latlng[1]
})
# Life thread
threading.Timer(60.0, self.life).start()
def threading(self):
""" Starts the EMAR Mini software threads. """
# Life thread
threading.Timer(60.0, self.life).start()
# Realsense threads
Thread(target=RealsenseRead().run).start()
Thread(target=RealsenseStream().run).start()
def shutdown(self):
""" Shuts down the EMAR Mini software. """
# Shutdown servos
self.h1.stop()
self.a1.stop()
self.a2.stop()
GPIO.cleanup()
# Disconnect from iotJumpWay
self.iotJumpWay.disconnect()
self.Helpers.logger.info("EMAR Mini Exiting")
sys.exit()
EMAR = EMAR()
def main():
# Starts threading
try:
EMAR.hardware()
EMAR.threading()
#Continous loop to keep the program running
while True:
continue
# Exits the program
EMAR.shutdown()
except KeyboardInterrupt:
# Cathces CTRL + C and exits the program
EMAR.shutdown()
if __name__ == "__main__":
main()