def create_motor(char): index = motor_chars.index(char) motor = silverpak.Silverpak() motor.baudRate = settings['MOTOR_%s_BAUD_RATE' % char] motor.driverAddress = settings['MOTOR_%s_DRIVER_ADDRESS' % char] motor.enableLimitSwitches = settings['MOTOR_%s_ENABLE_LIMIT_SWITCHES' % char] motor.enablePositionCorrection = settings['MOTOR_%s_ENABLE_POSITION_CORRECTION' % char] motor.velocity = settings['MOTOR_%s_VELOCITY' % char] motor.acceleration = settings['MOTOR_%s_ACCELERATION' % char] motor.maxPosition = admin.motor_bounds()[index][1] if not settings['MOTOR_%s_SET_HOLDING_CURRENT' % char]: motor.holdingCurrent = None if settings['MOTOR_%s_FAKE' % char]: motor.setFake() motor.name = char motor.stoppedMovingHandlers.append(motorStoppedMovingHandler) return motor
def handle_ConnectionRequest(msg): global user, server, motor_chars debug("Got connection request message") debug("protocol supported: {0}".format(str(msg.newest_protocol_supported))) this_protocol_version = 0 if msg.newest_protocol_supported < this_protocol_version: server.send_message(ConnectionResult(ConnectionResult.UnsupportedProtocol)) server.close() return try: user = admin.login(msg.username, msg.password) except (admin.UserDoesNotExist, admin.BadPassword): warning("Invalid login: "******", Password: <hidden>") server.send_message(ConnectionResult(ConnectionResult.InvalidLogin)) server.close() return # if they request hardware to turn on, make sure they have privileges if msg.hardware_flag and not user.has_privilege(Privilege.OperateHardware): warning(msg.username + " requested hardware access but doesn't have permission") server.send_message(ConnectionResult(ConnectionResult.InsufficientPrivileges, user.privileges())) server.close() return elif not msg.hardware_flag and not user.has_privilege(Privilege.ManageUsers): warning(msg.username + " tried to manage users but doesn't have permission") server.send_message(ConnectionResult(ConnectionResult.InsufficientPrivileges, user.privileges())) server.close() return debug("Successful login for user " + msg.username) motor_bounds = admin.motor_bounds() server.send_message(ConnectionResult(ConnectionResult.Success, this_protocol_version, user.privileges())) if msg.hardware_flag: server.send_message(InitializationInformation(motor_bounds, admin.static_bookmarks(), user.bookmarks())) if msg.hardware_flag: initialize_hardware()
def on_connection_close(): debug("connection closing") # send motors home if motors is not None: # leave A alone # send B to its min so that it's upright motor_b = motors['B'] motor_b_home = admin.motor_bounds()[1][0] debug("sending motor " + repr(motor_b.name) + " home to " + repr(motor_b_home)) # send X, Y, Z to 0 for char in "XYZ": motor = motors[char] motor.stoppedMovingHandlers.remove(motorStoppedMovingHandler) debug("sending motor " + repr(motor.name) + " home to 0") move_motor(motor, 0) # clean up global finished finished = True if live_view_thread is not None: debug("waiting for live view thread to join") live_view_thread.join() if motors_thread is not None: debug("waiting for motors thread to join") motor_movement_queue.put({}) motors_thread.join() if monitor_thread is not None: debug("waiting for monitor thread to join") monitor_thread.join() if message_thread is not None: debug("waiting for message thread to join") message_queue.put(DummyCloseConnection()) message_thread.join() if motors is not None: debug("shutting down motors") motor_disposer_threads = [] def shutdown_motor(motor): debug("waiting for motor " + repr(motor.name) + " to stop moving") for _ in range(settings['MOTOR_MOVEMENT_TIMEOUT']): if motor.isReady(): break time.sleep(1) else: error("timed out waiting for motor " + repr(motor.name) + " to stop moving") debug("disposing motor " + repr(motor.name)) motor.dispose() for motor in motors.values(): thread = make_thread(shutdown_motor, "motor " + repr(motor.name) + " disposer", args=[motor]) thread.start() motor_disposer_threads.append(thread) for thread in motor_disposer_threads: thread.join() set_power_switch(on=False) debug("done with life. comitting seppuku") os._exit(0)