def get(self, hub_id, hci=MY_BTCTRLR_HCI): print("Turn on MoveHub") movehub = MoveHub(hub_id, 'Auto', hci) movehub.start() print("MoveHub ready!") self.movehub = movehub return self.movehub
def init_movehub(): mh = MoveHub(MY_MOVEHUB_ADD, 'BlueZ', MY_BTCTRLR_HCI) mh.start() mh.subscribe_all() mh.listen_hubtilt(MODE_HUBTILT_BASIC) mh.listen_colordist_sensor(PORT_D) mh.listen_angle_sensor(PORT_C) if mh.is_connected(): print(('Is connected: ', mh.is_connected())) return mh
def init_lego(): retval = {} my_movehub = MoveHub(MY_MOVEHUB_ADD, MY_MOVEHUB_MODE, MY_BTCTRLR_HCI) retval['movehub'] = my_movehub try: my_movehub.start() except: retval['errStr'] = str(sys.exc_info()[0]) retval['movehub'] = None try_stop_movehub(my_movehub) return retval
class Boost: def __init__(self, mac_addr, mode='Auto', hci="hci0"): self.mac_addr = mac_addr self.mode = mode self.hci = hci def init_hub(self): retval = {} self.my_movehub = MoveHub(self.mac_addr, self.mode, self.hci) try: self.my_movehub.start() except: err_msg = sys.exc_info()[0] logger.error("Could not INIT movehub({},{},{}). Error:{}".format( self.mac_addr, self.mode, self.hci, err_msg)) retval['errStr'] = str(err_msg) self.try_stop_movehub() return retval def try_stop_movehub(self): if (self.my_movehub is not None): try: logger.error("Stopping movehub({},{},{})".format( self.mac_addr, self.mode, self.hci)) self.my_movehub.stop() except: err_msg = sys.exc_info()[0] logger.error( "Could not STOP movehub({},{},{}). Error:{}".format( self.mac_addr, self.mode, self.hci, err_msg)) def try_move_hub(self, motor, time_ms, dutycycle_pct): retval = {} try: self.my_movehub.run_motor_for_time(motor, time_ms, dutycycle_pct) except: err_msg = sys.exc_info()[0] logger.error("Could not MOVE movehub({},{},{}). Error:{}".format( self.mac_addr, self.mode, self.hci, err_msg)) #-- if(say_error): #-- say("Error while trying to move lego. {}".format( #-- sys.exc_info()[0])) retval['errStr'] = str(err_msg) return retval def go_forward_command(self): self.try_move_hub(MOTOR_AB, 2000, 100) def go_back_command(self): self.try_move_hub(MOTOR_AB, 2000, -100)
#!/usr/bin/python3 from utopia2output import * from time import sleep from pyb00st.movehub import MoveHub from pyb00st.constants import * mymovehub = MoveHub('00:16:53:A5:03:67', 'Auto', 'hci0') mymovehub.start() while True: try: mymovehub.connect() except Exception as e: print(e) print('Cannot connect to Boost, trying again..') else: break # define our action functions def head(objID): print('move head') try: mymovehub.run_motor_for_angle(MOTOR_D, 70, 20) sleep(2) mymovehub.run_motor_for_angle(MOTOR_D, 140, -10) sleep(1) mymovehub.run_motor_for_angle(MOTOR_D, 70, 20) except: print('Cannot connect to Boost.')
elif status == CLOSED and self.get_status() == OPEN: logger.debug("closing thumb turn") self._set_working(True) self.motor_actor.close(lambda: (self._set_working(False), callback())) def get_status(self): """サムターンの状態を取得する""" color = self.hub_actor.get_color().get() return to_status_from_color(color) print('turn the power on of the movehub in 10 seconds') sleep(1) hub = MoveHub(address=None, backend='Auto') hub.start(timeout=1, retry=10) hub.subscribe_all() def sigint_handler(sig, stack): hub.stop() signal.signal(signal.SIGINT, sigint_handler) sleep(1) hub_actor_ref = HubActor.start(hub) hub_actor_proxy = hub_actor_ref.proxy() key_motor_actor_ref = KeyMotorActor.start(hub_actor_proxy) key_motor_actor_proxy = key_motor_actor_ref.proxy()