Hey, thanks for the reply.
I decided to take a different approach and have a timer function called freq_callback that is called 100 times a second. I then use differences in time to flash the LEDs on and off at the rate that I desire. The issue that I face now is that I constantly get a USB error number 5. This issue occurs randomly and with no pattern. I may be polling the device too fast or trying to send too many things at once.
#!/usr/bin/env python
import time
import rospy
from blinkstick import blinkstick
from rgb_lights.msg import RGB_light
class RgbListener:
# Member variables # ---------------------------------
max_index = 10
light_to_index_mapping = {'rgb_lights/traction_control': 0,
'rgb_lights/Drive_motor_front_right_load': 1,
'rgb_lights/Drive_motor_front_left_load': 2,
'rgb_lights/Drive_motor_rear_right_load': 3,
'rgb_lights/Drive_motor_rear_left_load': 4,
}
# Find the first BlinkStick
bstick = blinkstick.find_first()
bstick.set_error_reporting(True)
def __init__(self):
self.current_topic = 'init'
self.current_freq = 0
self.current_rgb = [0, 0, 0] # np.zeros(3, dtype =np.uint8)
self.old_topics = [] # [0]*max_index
self.old_freqs = [] # [0]*max_index
self.old_rgbs = [] # array of RGB's
self.half_periods = []
self.old_times = [rospy.Time.now().to_sec()] * self.max_index
self.index_of_timer = -1
# Member functions # ---------------------------------
def listener(self):
rospy.loginfo("Started node " + self.bstick.get_description())
rospy.Timer(rospy.Duration(0.01), self.freq_callback)
for x in self.light_to_index_mapping:
rospy.Subscriber(x, RGB_light, self.data_in_callback)
rospy.spin()
def data_in_callback(self, data):
self.current_topic = data.RGB_id
self.current_freq = data.freq
self.current_rgb = list((int(ord(char)) for char in data.RGB))
if self.current_topic not in self.old_topics:
self.old_topics.append(self.current_topic)
self.old_freqs.append(self.current_freq)
self.old_rgbs.append(self.current_rgb)
self.half_periods.append(0.5*(1/float(self.current_freq)))
else:
self.index_of_timer = self.old_topics.index(self.current_topic)
self.old_freqs[self.index_of_timer] = self.current_freq
self.half_periods[self.index_of_timer] = 0.5 * \
(1/float(self.current_freq))
self.old_rgbs[self.index_of_timer] = self.current_rgb
def freq_callback(self, event):
for x in range(len(self.old_freqs)):
#time.sleep(0.00001)
temp = rospy.Time.now().to_sec()
t_check = (temp - self.old_times[x])
#print(self.old_topics[x], self.old_freqs[x], t_check, (self.half_periods[x]))
if (t_check > self.half_periods[x]):
self.bstick.set_color(
0, x, self.old_rgbs[x][0], self.old_rgbs[x][1], self.old_rgbs[x][2])
else:
self.bstick.set_color(0, x, 0, 0, 0)
#print (t_check.nsecs)
#print (2*self.half_periods[x])
if (t_check > (2 * self.half_periods[x])):
self.old_times[x] = temp
if __name__ == '__main__':
rospy.init_node('rgb_listener', anonymous=True)
rgb_obj = RgbListener()
rgb_obj.listener()
The error message I get is this: