def classical_gear(module, large_teeth, small_teeth): xdr = Range1d(start=-300, end=150) ydr = Range1d(start=-100, end=100) plot = Plot(x_range=xdr, y_range=ydr, plot_width=800, plot_height=800) plot.add_tools(PanTool(), WheelZoomTool(), BoxZoomTool(), UndoTool(), RedoTool(), ResetTool()) radius = pitch_radius(module, large_teeth) angle = 0 glyph = Gear(x=-radius, y=0, module=module, teeth=large_teeth, angle=angle, fill_color=fill_color[0], line_color=line_color) plot.add_glyph(glyph) radius = pitch_radius(module, small_teeth) angle = half_tooth(small_teeth) glyph = Gear(x=radius, y=0, module=module, teeth=small_teeth, angle=angle, fill_color=fill_color[1], line_color=line_color) plot.add_glyph(glyph) return plot
def create_discrete_mechanism(all_outputs, gear_radii, pos_thresh, output_min): """creates mechanism using RNN output based on discrete gear radius options :param all_outputs: the list of outputs created by RNN :param gear_radii: the list of gear radius options """ r = gear_radii[all_outputs[0][0]] mechanism = [Gear(r, (r, r, 0), 0)] for index, curr in enumerate(all_outputs[1:]): prev_gear = mechanism[-1] # get position for the next gear new_pos = get_gear_pos_linear(prev_gear.pos, curr[1], prev_gear.radius, gear_radii[curr[0]], \ pos_thresh, output_min) mechanism.append(Gear(gear_radii[curr[0]], new_pos, len(mechanism) - 1)) prev_gear.next_gears.append(index + 1) # find ratio of the current gear new_ratio = prev_gear.ratio * (prev_gear.radius / mechanism[-1].radius) if (-pos_thresh <= curr[1] <= pos_thresh): mechanism[-1].ratio = new_ratio else: mechanism[-1].ratio = prev_gear.ratio return mechanism
def classical_gear(module, large_teeth, small_teeth): plot = figure( x_range=(-300, 150), y_range=(-100, 100), x_axis_type=None, y_axis_type=None, width=800, height=800, tools=tools, ) radius = pitch_radius(module, large_teeth) angle = 0 glyph = Gear(x=-radius, y=0, module=module, teeth=large_teeth, angle=angle, fill_color=fill_color[0], line_color=line_color) plot.add_glyph(glyph) radius = pitch_radius(module, small_teeth) angle = half_tooth(small_teeth) glyph = Gear(x=radius, y=0, module=module, teeth=small_teeth, angle=angle, fill_color=fill_color[1], line_color=line_color) plot.add_glyph(glyph) return plot
def setUpPlayer(self): newGear = Gear(500) hunter = Guardian(1, newGear) newGear2 = Gear(500) warlock = Guardian(2, newGear2) newGear3 = Gear(500) titan = Guardian(3, newGear3) return Player(hunter, warlock, titan)
def epicyclic_gear(module, sun_teeth, planet_teeth): plot = figure( x_range=(-150, 150), y_range=(-150, 150), x_axis_type=None, y_axis_type=None, width=800, height=800, tools=tools, ) annulus_teeth = sun_teeth + 2 * planet_teeth glyph = Gear(x=0, y=0, module=module, teeth=annulus_teeth, angle=0, fill_color=fill_color[0], line_color=line_color, internal=True) plot.add_glyph(glyph) glyph = Gear(x=0, y=0, module=module, teeth=sun_teeth, angle=0, fill_color=fill_color[2], line_color=line_color) plot.add_glyph(glyph) sun_radius = pitch_radius(module, sun_teeth) planet_radius = pitch_radius(module, planet_teeth) radius = sun_radius + planet_radius angle = half_tooth(planet_teeth) for i, j in [(+1, 0), (0, +1), (-1, 0), (0, -1)]: glyph = Gear(x=radius * i, y=radius * j, module=module, teeth=planet_teeth, angle=angle, fill_color=fill_color[1], line_color=line_color) plot.add_glyph(glyph) return plot
def epicyclic_gear(module, sun_teeth, planet_teeth): xdr = Range1d(start=-150, end=150) ydr = Range1d(start=-150, end=150) plot = Plot(x_range=xdr, y_range=ydr, plot_width=800, plot_height=800) plot.add_tools(PanTool(), WheelZoomTool(), BoxZoomTool(), UndoTool(), RedoTool(), ResetTool()) annulus_teeth = sun_teeth + 2 * planet_teeth glyph = Gear(x=0, y=0, module=module, teeth=annulus_teeth, angle=0, fill_color=fill_color[0], line_color=line_color, internal=True) plot.add_glyph(glyph) glyph = Gear(x=0, y=0, module=module, teeth=sun_teeth, angle=0, fill_color=fill_color[2], line_color=line_color) plot.add_glyph(glyph) sun_radius = pitch_radius(module, sun_teeth) planet_radius = pitch_radius(module, planet_teeth) radius = sun_radius + planet_radius angle = half_tooth(planet_teeth) for i, j in [(+1, 0), (0, +1), (-1, 0), (0, -1)]: glyph = Gear(x=radius * i, y=radius * j, module=module, teeth=planet_teeth, angle=angle, fill_color=fill_color[1], line_color=line_color) plot.add_glyph(glyph) return plot
def mechanism_from_GA(ind): """create mechanism representation from GA genome""" r = ind[0][0] mechanism = [Gear(r, (r, r, 0), 0)] end_ind = ind[-1] for index, curr in enumerate(ind[1:end_ind]): prev_gear = mechanism[-1] # get position for the next gear new_pos = (prev_gear.pos[0] + prev_gear.radius + curr[0], prev_gear.pos[1], prev_gear.pos[2]) if (curr[1] == 2): new_pos = (prev_gear.pos[0], prev_gear.pos[1], prev_gear.pos[2] + 1) elif (curr[1] == 0): new_pos = (prev_gear.pos[0], prev_gear.pos[1], prev_gear.pos[2] - 1) mechanism.append(Gear(curr[0], new_pos, len(mechanism) - 1)) prev_gear.next_gears.append(index + 1) return mechanism
def create_mechanism_representation(all_outputs, pos_thresh, output_min): """uses a list of gear RNN outputs to create a more understandable representation for the mechanism, each gear is represented with (radius, position, previous gear, next gears, ratio) so that all information for each gear in the mechanism is readily available this method converts the raw RNN output into the above form - stored as a Gear object """ # populate mechanism with the first gear # ratio set to 1.0 by default when gear is instantiated r = all_outputs[0][0] init_x = np.cos(np.pi / 4) * r init_y = np.sin(np.pi / 4) * r mechanism = [Gear(r, (init_x, init_y, 0), 0)] # go through all outputs and create a gear object for each one for index, curr in enumerate(all_outputs[1:]): prev_gear = mechanism[ -1] # previous gear is always last outputted mechanism # must set padding to true if space should be inserted between gears for 3D printing new_pos = get_gear_pos_linear(prev_gear.pos, curr[1], prev_gear.radius, curr[0], pos_thresh, output_min) mechanism.append(Gear(curr[0], new_pos, len(mechanism) - 1)) # add index of current into list of nxt gears for gear it attaches to prev_gear.next_gears.append(index + 1) # find ratio of current gear - only changes if not attached to front/back new_ratio = prev_gear.ratio * (prev_gear.radius / mechanism[-1].radius) if (-pos_thresh <= curr[1] <= pos_thresh): mechanism[-1].ratio = new_ratio else: mechanism[-1].ratio = prev_gear.ratio return mechanism
def setUp(self): #logging.basicConfig(level=logging.INFO) ws = self.ws = WS2812(1, 8) lights = self.lights = Lights(ws) # Fill the lattice with a recognizable pattern for i, p in enumerate(tg(len(lights), 0)): lat = lights.lattice[i] for j, c in enumerate(p): lat[j] = c # The leds are all clear self.assertEqual(sum(sum(c) for c in ws), 0) self.gear = Gear(lights=lights)
def sample_gear(): xdr = Range1d(start=-30, end=30) ydr = Range1d(start=-30, end=30) plot = Plot(x_range=xdr, y_range=ydr, plot_width=800, plot_height=800) plot.add_tools(PanTool(), WheelZoomTool(), BoxZoomTool(), UndoTool(), RedoTool(), ResetTool()) glyph = Gear(x=0, y=0, module=5, teeth=8, angle=0, shaft_size=0.2, fill_color=fill_color[2], line_color=line_color) plot.add_glyph(glyph) return plot
def individual_gear(): plot = figure( x_range=(-30, 30), y_range=(-30, 30), x_axis_type=None, y_axis_type=None, width=800, height=800, tools=tools, ) glyph = Gear(x=0, y=0, module=5, teeth=8, angle=0, shaft_size=0.2, fill_color=fill_color[2], line_color=line_color) plot.add_glyph(glyph) return plot
def gear(self, storage, interface, **kwargs): return Gear(storage, self.router, interface, **kwargs)
class control(): gear = Gear() def __init__(self, **kwargs): # ctrl = control( Gear=gear ) got_gear = kwargs.get('Gear', None) if got_gear: self.gear = copy.copy(got_gear) def setfrequency(self, freq): # frequency in MHz cmd = "af set tx1freq %s; af set rxfreq %s" % (freq, freq) self.gear.master.t.write_wait(cmd) self.gear.slave.t.write_wait(cmd) def setpower(self, power): cmd = "af set powerout %s" % power self.gear.master.t.write_wait(cmd) self.gear.slave.t.write_wait(cmd) def setbw(self, bw): cmd = "af set channelbandwidth %s" % bw self.gear.master.t.write_wait(cmd) self.gear.slave.t.write_wait(cmd) def wait(self, mod_rate, delay=25): sleep(2) self.gear.slave.t.radio_ready(ready_state=[('slave', 'operational')], timeout=40) self.gear.master.t.radio_ready(ready_state=[('master', 'operational')], timeout=40) print "wait_mod_rate(%s, %d)" % (mod_rate, delay) return self.wait_mod_rate(mod_rate, timeout=delay) # sleep(3) def get_mod_rate(self): s_mod_rate = self.gear.slave.w.get_page_field("labefana", "Local Mod Rate") m_mod_rate = self.gear.master.w.get_page_field("labefana", "Local Mod Rate") return (s_mod_rate, m_mod_rate) # return True if timeout def wait_mod_rate(self, mod_rate, timeout=25): delay = timeout mod = self.get_mod_rate() while delay: if mod[0] == mod_rate and mod[1] == mod_rate: return 0 mod = self.get_mod_rate() delay -= 1 sleep(1) return 1 def settemp(self, temp): if self.gear.therm: self.gear.therm.set_temp_chamber(temp) print "did set_temp" self.gear.therm.waitTempChamber(soak_time=10) print "did wait" def close(self): self.gear.master.close() self.gear.slave.close()
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.indexed_range = self.indexed_range[:7] self.center = self.lattice[self.indexed_range[0]] self.gear = Gear(lights=self, indexed_range=self.indexed_range[1:7])
exc_type, exc_value, exc_traceback = sys.exc_info() plist = traceback.format_exception(exc_type, exc_value, exc_traceback) print plist ctrl.close() return True if __name__ == '__main__': m = uut(ipaddr='10.8.8.100', opmode='master') # 113 s = uut(ipaddr='10.8.8.108', opmode='slave') # 138 m.telnet() s.telnet() m.web() s.web() t = thermotron("10.8.9.20") gear = Gear(master=m, slave=s, therm=t) # Logname = "fpa031215" # Temp = (1,2, 1) # BW = ( 10,51,10) # Power = ( 10,11,1) # Freq = (5150,5926,50) # Temp = (-40,70, 5) # BW = ( 50,51,1) # Power = ( 10,26,1) # Freq = (5150,5926,1) Logname = "5XBigData" Temp = (0, 71, 5) BW = (50, 51, 1) Power = (10, 26, 1)
def __init__(self, cfg_file: str = None, **kwargs): self._cfg = Cfg(cfg_file, **kwargs) self._discord = commands.Bot(self._cfg.command_prefix) self._inferkit = InferKit(self._cfg.inferkit_api_key) self._channels = {} self._member_mention = {} self._mention_regex = None self._discord.add_cog(Gear(self)) cog = self._discord.get_cog('Gear') self._cmd_regex = cog.compile_regex() @self._discord.event async def on_ready(): # Get member list sorted in reverse order by lower-case # display name. This particular sorting choice ensures # that a regex matching any member name will find the # longest possible match in case of common prefixes. def display_name_lc(m): return m.display_name.lower() members = list(self._discord.get_all_members()) members.sort(key=display_name_lc, reverse=True) arr = [] for member in members: name = member.display_name arr.append(re.escape(name)) # TODO: handle name collisions self._member_mention['@' + name.lower()] = member.mention self._mention_regex = re.compile('@(' + '|'.join(arr) + ')', flags=re.I) user = self._discord.user self._name_regex = re.compile(r'(' + re.escape(user.display_name) + r')\]\s*', flags=re.I) log.info('Logged in as ' + log.green(str(user), 1)) @self._discord.event async def on_message(msg): await self._discord.process_commands(msg) is_cmd = self._is_cmd(msg) msg_log = self.chan_msg_log(msg.channel.id) if len(msg_log) == 0: await self.chan_get_history(msg_log, msg.channel) else: self.chan_append_msg(msg_log, msg, is_cmd, TwerkBoi.list_tail(msg_log)) user = self._discord.user if msg.author == user: return if is_cmd: return user_id = user.id mentioned = False for member in msg.mentions: if member.id == user_id: mentioned = True if not mentioned: return await msg.channel.trigger_typing() prompt = self.gen_prompt(msg_log, user.display_name) log.debug( log.white('<prompt>', 1) + log.yellow(prompt) + log.white('</prompt>', 1)) reply = self._inferkit.generate(prompt) if reply == None: reply = 'Unable to generate a reply' else: sanitized = [] for line in reply.splitlines(): if (len(line) > 0) and not line.isspace(): sanitized.append(line.strip()) log.debug( log.white('<generated>', 1) + log.blue('\n'.join(sanitized), 1) + log.white('</generated>', 1)) arr = [] for line in sanitized: if line[0] == '[': line = line[1:] m = self._name_regex.match(line) if m == None: break span = m.span()[1] if span >= len(line): continue line = line[span:] arr.append(line) if self._cfg.auto_mention: need_receiver_mention = True receiver_mention = msg.author.mention else: if len(arr) < 1: return need_receiver_mention = False receiver_mention = None tmp = '\n'.join(arr) pos = 0 reply = '' mentions_seen = {'@' + user.display_name.lower(): True} for m in self._mention_regex.finditer(tmp): span = m.span() reply += tmp[pos:span[0]] pos = span[1] cleaned_mention = m.group(0).lower() if cleaned_mention in mentions_seen: continue mentions_seen[cleaned_mention] = True mention = self._member_mention[cleaned_mention] reply += mention if need_receiver_mention and (mention == receiver_mention): need_receiver_mention = False reply += tmp[pos:len(tmp)] if need_receiver_mention: if len(reply) > 0: reply = receiver_mention + ' ' + reply else: reply = receiver_mention await msg.channel.send(reply)
def __init__(self, layer, no, p, button): self.layer = layer self.no = no # motor ID in [0,3] self.mask = 1<<no # motor bitmask self.polarity = p # polarity direction, +1 or -1 self.button = button # digital button on the controller to nudge this motor pygame.init() pygame.joystick.init() j = pygame.joystick.Joystick(0) j.init() # gear ratio for the directional motor, in the reverse order # so as to map the turrent angle to the tacho count g = Gear(Gear.TURRET, Gear.BIG) motors = [Motor(0,0,1,3), Motor(0,1,-1,2), Motor(0,2,1,1), Motor(0,3,-1,0)] thrusts = [Motor(1,0,1,3), Motor(1,1,-1,2), Motor(1,2,1,1), Motor(1,3,-1,0)] def reset(): c = Program() # reset tacho counts on all motors for m in motors: c.output.layer(m.layer).ports(m.mask) c.output.reset().clear_count() c.output.power(0).polarity(m.polarity).start() # activate thrust motors for m in thrusts: c.output.layer(m.layer).ports(m.mask) c.output.power(0).polarity(m.polarity).start()
# c.output.ports(0xF).speed(10).start() # c.output.stop(ports=0xF) # c.output.layer(0).ports(0xF).speed(10).start() # c.output.layer(1).ports(0xF).speed(10).start() # c.output.stop(ports=0xF).layer(1).stop(ports=0x0F) # c.sound.tone(volume=1, frequency=550, duration=100) # c.sound.ready() # c.sound.tone(volume=1, frequency=660, duration=100) # c.sound.ready() c.ui.led(color='orange', mode='off') g = Gear(Gear.BIG, Gear.TURRET) c.output.ports(1) # c.output.polarity(1) # move_by_X functions seem to internally reset tacho count # so read is not giving me meaningful tacho count c.output.move_by_step(g(60), g(240), g(60), speed=20) # c.output.move_by_time( 500, 1000, 500, speed=20) # c.output.ready() # c.output.move_by_step( g(60), g(240), g(60), speed= 20) c.send(h) for i in range(50):
def set_up_gears(self): self.gears = [] self.gears_group.remove_all() center_gear_location = (Window.width / 6 * 4.5, Window.height / 4 * 2.5) center_gear_size = min(Window.width / 10, Window.height / 10) self.center_gear = Gear(None, None, center_gear_size, 10, 'center', 0, center_gear_location, center_gear_location, 1, colors['dark_grey']) self.canvas.add(self.center_gear.color) self.gears_group.add(self.center_gear) self.center_gear_center = GearCenter(None, None, center_gear_location, center_gear_location, 'center', center_gear_size / 2, colors['dark_grey']) self.canvas.add(colors['dark_grey']) self.canvas.add(self.center_gear_center) self.play_center_gear = False self.music_gears = [] tempo_location = (center_gear_location[0], center_gear_location[1] + center_gear_size + center_gear_size / 5) instrument_location = (center_gear_location[0], center_gear_location[1] - center_gear_size - center_gear_size / 5) pitch_location = (center_gear_location[0] + center_gear_size + center_gear_size / 5, center_gear_location[1]) volume_location = (center_gear_location[0] - center_gear_size - center_gear_size / 5, center_gear_location[1]) self.music_box_gear_locations = [ tempo_location, instrument_location, pitch_location, volume_location ] counter = 0 label_font_size = min(Window.width // 80, Window.height // 80) for y in range(0, 4): for x in range(len(self.gear_values) // 4): gear_type = gear_type_map[y] size = min(Window.width / 10, Window.height / 10) music_pos = self.music_box_gear_locations[y] scaled_x, scaled_y = self.get_scaled_x_y( (Window.width, Window.height), x, y) gear = Gear(x, y, size, 8, gear_type, self.gear_values[counter], (scaled_x, scaled_y), music_pos, 0, colors['dark_grey']) self.gears.append(gear) self.canvas.add(gear.color) self.gears_group.add(gear) gear_center = GearCenter(x, y, (scaled_x, scaled_y), music_pos, gear_type, size / 2, colors['dark_grey']) self.gear_centers.append(gear_center) self.canvas.add(gear_center) ## white dots for storage purposes gear_loc = GearLocation((scaled_x, scaled_y), size / 2, x, y, gear_type) self.gear_storage_locations.append(gear_loc) self.canvas.add(gear_loc) text = str(self.gear_values[counter]) font_name = './fonts/PassionOne-Regular' if y == 3: # get volume as percent text = str(100 * self.gear_values[counter] // 127) + '%' if y == 1: # get icon for instrument font_name = './fonts/music-instruments' text = instruments[self.gear_values[counter]] label = Label(text=text, font_name=font_name, color=(0, 0, 0, 1), center_x=scaled_x, center_y=scaled_y, font_size=str(label_font_size) + 'sp') self.gear_labels.append(label) self.add_widget(label) counter += 1 for indx, loc in enumerate(self.music_box_gear_locations): gear_type = gear_type_map[indx % 4] gear_loc = GearLocation(loc, center_gear_size / 2, None, None, gear_type) self.gear_music_locations.append(gear_loc) self.canvas.add(gear_loc)