-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpepper_heartbot_lights.py
More file actions
186 lines (152 loc) · 7.72 KB
/
pepper_heartbot_lights.py
File metadata and controls
186 lines (152 loc) · 7.72 KB
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
import math
import numpy as np
import os
import time
import sys
from datetime import datetime
from naoqi import ALProxy
ROBOT_IP = '192.168.1.193'
#ROBOT_IP = '192.168.1.163'
#ROBOT_IP = '192.168.1.162'
class PepperHandler(object):
__instance = None
@staticmethod
def getInstance(hr_reader=None, asynchMode=False, logger=None):
""" Static access method. To create singleton handler """
if PepperHandler.__instance == None:
# Create new
PepperHandler(hr_reader, asynchMode, logger)
return PepperHandler.__instance
def __init__(self, hr_reader, asynchMode, logger):
if PepperHandler.__instance != None:
raise Exception("This class is a singleton!")
else:
PepperHandler.__instance = self
super(PepperHandler, self).__init__()
self.motion_handler = ALProxy("ALMotion", ROBOT_IP, 9559)
self.motion_handler.wakeUp()
self.basic_awareness = ALProxy("ALBasicAwareness", ROBOT_IP, 9559)
self.basic_awareness.stopAwareness()
self.motion_handler.setBreathConfig([['Bpm', 2.0], ['Amplitude', 1.0]])
self.motion_handler.setBreathEnabled ('Body', False)
self.motion_handler.setBreathEnabled ('Arms', True)
self.headLockPitch = None
self.headLockYaw = None
self.heart_rate = 60.0/60.0 # 60 beats per second
self.hr_reader = hr_reader
self.asynchMode = asynchMode
if self.asynchMode:
self.heart_rate = self.heart_rate * 0.8
self.logger = logger
# The Leds we want to use for heart beat display
# Create a new group
heart_led_group = [# Ear Led
"Ears/Led/Right/0Deg/Actuator/Value",
"Ears/Led/Left/0Deg/Actuator/Value",
"Ears/Led/Right/36Deg/Actuator/Value",
"Ears/Led/Left/36Deg/Actuator/Value",
"Ears/Led/Right/72Deg/Actuator/Value",
"Ears/Led/Left/72Deg/Actuator/Value",
"Ears/Led/Right/108Deg/Actuator/Value",
"Ears/Led/Left/108Deg/Actuator/Value",
"Ears/Led/Right/144Deg/Actuator/Value",
"Ears/Led/Left/144Deg/Actuator/Value",
"Ears/Led/Right/180Deg/Actuator/Value",
"Ears/Led/Left/180Deg/Actuator/Value",
"Ears/Led/Right/216Deg/Actuator/Value",
"Ears/Led/Left/216Deg/Actuator/Value",
"Ears/Led/Right/252Deg/Actuator/Value",
"Ears/Led/Left/252Deg/Actuator/Value",
"Ears/Led/Right/288Deg/Actuator/Value",
"Ears/Led/Left/288Deg/Actuator/Value",
"Ears/Led/Right/324Deg/Actuator/Value",
"Ears/Led/Left/324Deg/Actuator/Value",
#Shoulder Leds
"ChestBoard/Led/Blue/Actuator/Value",
#"ChestBoard/Led/Green/Actuator/Value",
#"ChestBoard/Led/Red/Actuator/Value"
]
shoulder_led_group = [#Shoulder Leds
"ChestBoard/Led/Blue/Actuator/Value",
"ChestBoard/Led/Green/Actuator/Value",
"ChestBoard/Led/Red/Actuator/Value"
]
self.leds = ALProxy("ALLeds",ROBOT_IP, 9559)
self.leds.createGroup("HeartLeds",heart_led_group)
self.leds.createGroup("ShoulderLeds",shoulder_led_group)
# Switch the new group on
self.leds.off("ShoulderLeds")
self.leds.on("HeartLeds")
def update_heart_rate(self):
if self.hr_reader and not self.asynchMode:
rate = self.hr_reader.heartRate
updated_rate = float(rate)/60.0
#
if updated_rate > 0.01 and self.heart_rate != updated_rate:
print("Updating robot rate to %f bps" % updated_rate)
self.heart_rate = updated_rate
if self.logger:
self.logger.info("Pepper heart_rate updated to : %f beats per second" % self.heart_rate)
elif self.hr_reader and self.asynchMode:
# a quarter rate for asynch
rate = self.hr_reader.heartRate
updated_rate = (float(rate) * 0.8)/60.0
if updated_rate > 0.01 and self.heart_rate != updated_rate:
print("Updating robot asynchronous rate to %f bps" % updated_rate)
self.heart_rate = updated_rate
if self.logger:
self.logger.info("Pepper asynchronous heart_rate updated to : %f beats per second" % self.heart_rate)
else:
# Read from file
hr_file = "./heartRate.txt"
if os.path.isfile(hr_file):
if self.logger:
self.logger.info("Updating heart_rate from file")
f = hr_file.open()
rate = int(f.readline())
updated_rate = float(rate)/60.0
if updated_rate > 0.01 and self.heart_rate != updated_rate:
print("Updating robot rate to %f bps" % updated_rate)
self.heart_rate = updated_rate
if self.logger:
self.logger.info("Pepper heart_rate updated to : %f beats per second" % self.heart_rate)
def synch_hr(self):
# wait for connection
# "waiting for subscribers..."
time.sleep(1)
self.set_active = True
# params
T = .05 # update time
phase = 0.0
phase_time = T
last_time = None
try:
# loop
while self.set_active:
# Get an update of heart rate from the reader
self.update_heart_rate()
# update pulse rate for the robot
if self.logger:
self.logger.info("Pepper current heart_rate: %f beats per seond" % self.heart_rate)
f_pulse = self.heart_rate/2.0
# calculate intesity phase
this_time = datetime.now()
if last_time:
phase_time = (this_time - last_time).total_seconds()
# increment pulse phase by current rate
phase += phase_time * f_pulse * 2 * np.pi
last_time = this_time
# magnitude
mag = np.cos(phase) * 0.5 + 0.5
if self.logger:
self.logger.info("Phase: %f \t Brightness: %f" %(np.degrees(phase), mag))
# fix up the brightness
self.leds.setIntensity("HeartLeds", mag)
# sleep
time.sleep(T)
finally:
# Switch off the lights
self.leds.off("HeartLeds")
if __name__ =='__main__':
main_robot = PepperHandler.getInstance(hr_reader=None, logger=None)
main_robot.synch_hr()