def run(self): try: log.info("Started HTTP server (content) on port %d" % self.port_number) self.content_server = HTTPServer(('', self.port_number), self.handler_class) self.content_server.serve_forever() # Exit cleanly, stop both web servers and all motors except (KeyboardInterrupt, Exception) as e: log.exception(e) if self.content_server: self.content_server.socket.close() self.content_server = None for motor in list_motors(): motor.stop()
#!/usr/bin/env micropython """ Stop all motors """ from ev3dev2.motor import list_motors for motor in list_motors(): motor.stop(stop_action='brake')
def get_info(old_sensor_addresses, old_motor_addresses, all_info=False): info = {"disconnected_devices": []} if all_info: for group_name, leds in LEDS.led_groups.items(): info["led:" + group_name] = { led.desc.split("_")[0]: led.brightness_pct for led in leds } sensor_addresses = set() for sensor in list_sensors("*"): try: address = sensor.address if address.count(":") > 1: # addresses for i2c sensors end with ':i2c*', remove this address = address[:address.index(":", address.index(":") + 1)] if address in old_sensor_addresses: old_sensor_addresses.remove(address) info[address] = { "values": " ".join( str(sensor.value(i)) for i in range(sensor.num_values)) } else: info[address] = { "driver_name": sensor.driver_name, "mode": { "values": sensor.modes, "selected": sensor.mode }, "command": sensor.commands, "values": " ".join( str(sensor.value(i)) for i in range(sensor.num_values)), #"decimals": sensor.decimals, } sensor_addresses.add(address) except Exception: traceback.print_exc() info["disconnected_devices"].extend(old_sensor_addresses) motor_addresses = set() for motor in list_motors("*"): try: address = motor.address if address in old_motor_addresses: old_motor_addresses.remove(address) info[address] = {"position": motor.position} else: info[address] = { "driver_name": motor.driver_name, "duty_cycle_sp": motor.duty_cycle_sp, "polarity": motor.polarity, "position": motor.position, "position_sp": motor.position_sp, "speed_sp": motor.speed_sp, "ramp_up_sp": motor.ramp_up_sp, "ramp_down_sp": motor.ramp_down_sp, "stop_action": { "values": motor.stop_actions, "selected": motor.stop_action }, "time_sp": motor.time_sp, "command": motor.commands } motor_addresses.add(address) except Exception: traceback.print_exc() info["disconnected_devices"].extend(old_motor_addresses) content = json.dumps(info).encode("utf-8") return content, sensor_addresses, motor_addresses
def stop_all_motors(): for motor in list_motors(): motor.stop(stop_action='brake')