class Dennis: def __init__(self, host, port, username, password): self.host = host self.port = port self.username = username self.password = password self.network = NetworkManager(host, port, username, password) def login(self): self.network.login() def goTo(self, x, y, z): old = self.network.dispatch.bot.location distance = Location() distance.x = x - old.x distance.z = z - old.z speed = 3 / 20 vx = speed * math.sin(math.tanh(distance.z / distance.x)) vz = speed * math.cos(math.tanh(distance.z / distance.x)) self.network.X = vx self.network.Z = vz if x > old.x: while self.network.dispatch.bot.location.x < x: sleep(0.05) elif x < old.x: while self.network.dispatch.bot.location.x > x: sleep(0.05) self.network.X = 0 self.network.Z = 0 def disconnect(self): self.network.s.close() exit()
class QueryResolver: def __init__(self, protocol, support_resolving=True): self.response_handler = ResponseHandler() self.request_handler = RequestHandler() self.network_handler = NetworkManager(protocol) self.support_resolving = support_resolving def get_next_address(self, address, dns_server_ip, req_type=PackageType.A, port=53): request = self.request_handler.create_request(address, self.support_resolving, req_type=req_type) resp = self.network_handler.send(request, dns_server_ip, port) self.response_handler.parse_response(resp) if self.response_handler.answers: yield self.response_handler.answers[0] if self.response_handler.additional: yield self.response_handler.additional[0] if self.response_handler.authority: yield self.response_handler.authority[0] def get_ip(self, address, root_ip, req_type=PackageType.A, port=53): if address is None: raise ValueError for answer in self.get_next_address(address, root_ip, req_type, port): if answer.internal_type == 'answer': return answer.address if answer.internal_type == 'additional': if answer.type == PackageType.A.value or answer.type == PackageType.AAAA.value: return self.get_ip(address, answer.address, req_type=req_type) return self.get_ip(address, self.get_ip(answer.address, root_ip, port=port), req_type=req_type) if answer.internal_type == 'authority': return self.get_ip(address, self.get_ip(answer.address, root_ip, port=port), req_type=req_type)
def __init__(self, host, port, username, password): self.host = host self.port = port self.username = username self.password = password self.network = NetworkManager(host, port, username, password)
def __init__(self, protocol, support_resolving=True): self.response_handler = ResponseHandler() self.request_handler = RequestHandler() self.network_handler = NetworkManager(protocol) self.support_resolving = support_resolving
def main(): # start the logger logger = logging.getLogger(__name__) handler = RotatingFileHandler('robot_log.log', "a", maxBytes=960000, backupCount=5) logger.addHandler(handler) # check for a default config file if os.path.isfile( "settings.default.json") and not os.path.isfile("settings.json"): open("settings.json", "a").close() copyfile("settings.default.json", "settings.json") # read the file with open("settings.json", "r") as f: values = jsonpickle.loads(f.read()) # get the results and save them global robot_type, m_settings, d_settings, state robot_type = values["type"] m_settings = values[robot_type] d_settings = values["drive"] # setup the state object if is_gripper(): state = GripperState(m_settings["lift_min"], m_settings["grip_min"]) # initalize i2c and piconzero piconzero.init() # Open the socket and start the listener thread netwk_mgr = NetworkManager(logger) netwk_mgr.start() # Make robot stuff robot_disabled = True watchdog = Watchdog(logger) if is_elevator(): piconzero.set_output_config(m_settings["motor_channel"], 1) # set channel 0 to PWM mode if is_gripper(): piconzero.set_output_config(m_settings["lift_servo"], 2) piconzero.set_output_config(m_settings["grip_servo"], 2) # set channel 0 and 1 to Servo mode # Initialization should be done now, start accepting packets while True: try: raw_pack = netwk_mgr.get_next_packet() if raw_pack is not None: try: pack = jsonpickle.decode( raw_pack ) # recieve packets, decode them, then de-json them except JSONDecodeError as e: print(e) logger.warning(str(e)) continue watchdog.reset() # Type-check the data if type(pack) is not Packet: print("pack is not a Packet", file=sys.stderr) continue # Process the packet if pack.type == PacketType.STATUS: # Check the contents of the packet if type(pack.data) is RobotStateData: if pack.data == RobotStateData.ENABLE: robot_disabled = False # Reinitialize the picon zero piconzero.init() if is_elevator(): piconzero.set_output_config( m_settings["motor_channel"], 1) # set channel 0 to PWM mode if is_gripper(): piconzero.set_output_config( m_settings["lift_servo"], 2) piconzero.set_output_config( m_settings["grip_servo"], 2) # set channel 0 and 1 to Servo mode continue elif pack.data == RobotStateData.DISABLE: robot_disabled = True piconzero.cleanup() continue elif pack.data == RobotStateData.E_STOP: piconzero.cleanup() break elif pack.type == PacketType.REQUEST: # Check for the request type if pack.data == RequestData.STATUS: # Send a response packet = Packet( PacketType. RESPONSE, # generate a packet saying if the robot is enabled or disabled RobotStateData.DISABLE if robot_disabled else RobotStateData.ENABLE) netwk_mgr.send_packet(jsonpickle.encode(packet)) elif pack.type == PacketType.RESPONSE: # do more stuff continue elif pack.type == PacketType.DATA: # See if the robot is disabled if robot_disabled: continue # Check and see if a list of packets was sent if type(pack.data) is list: for item in pack.data: process_data(item) else: process_data(pack) except Exception as e: logger.error(e, exc_info=True) pass # Emergency Stopped loop while True: # Disable all outputs piconzero.cleanup() # Accept a packet raw_pack = netwk_mgr.get_next_packet() if raw_pack is not None: pack = jsonpickle.decode( raw_pack) # receive packets, decode them, then de-json them # Check for a request if pack.type == PacketType.REQUEST: # Send a response, no matter the request type packet = Packet( PacketType.RESPONSE, RobotStateData.E_STOP ) # generate a packet saying that this robot is e-stopped netwk_mgr.send_packet(packet) time.sleep( .250 ) # delay for 250ms, don't want to spam the picon zero with cleanup requests pass
# Copyright 2014 Darcy Ryan <*****@*****.**> # # Dennis is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. # from networkManager import NetworkManager import socket import thread import types import zlib import DataTypes from pprint import pprint network = NetworkManager("localhost", 25565, "Thebot", "password") network.login() while True: network.handleNewPackets(network) network.sendWaitingPackets()