def test_socket_nonblock(): handler1 = PyserialSerialHandler("/dev/ttyUSB0", baudrate=19200) handler2 = PyserialSerialHandler("/dev/ttyUSB1", baudrate=19200) im920_1 = Im920(handler1, name="im920_1") im920_2 = Im920(handler2, name="im920_2") im920_1.clear_buf() im920_2.clear_buf() address_1 = ("2785", ) address_2 = ("1CD2", ) transceiver_1 = SocketTransceiver(im920_1, period=0.1, certain=True) transceiver_2 = SocketTransceiver(im920_2, period=0.1, certain=True) socket_1 = transceiver_1.create_socket(address_2) socket_2 = transceiver_2.create_socket(address_1) socket_1.send(im920_1.encode("Hello World " * 10), blocking=False) print("blocking...") sleep(5) count = 0 while True: data = socket_2.recv(256) if len(data): print("data: {} , len: {}".format(im920_1.decode(data), len(data))) else: count += 1 if count > 5: break
def test_socket(): handler1 = PyserialSerialHandler("/dev/ttyUSB0", baudrate=19200, read_timeout=0.1) handler2 = PyserialSerialHandler("/dev/ttyUSB1", baudrate=19200, read_timeout=0.1) im920_1 = Im920(handler1, name="im920_1") im920_2 = Im920(handler2, name="im920_2") im920_1.clear_buf() im920_2.clear_buf() address_1 = ("2785", ) address_2 = ("1CD2", ) transceiver_1 = SocketTransceiver(im920_1) transceiver_2 = SocketTransceiver(im920_2) socket_1 = transceiver_1.create_socket(address_2) socket_2 = transceiver_2.create_socket(address_1) socket_1.send(im920_1.encode("Hello World " * 100)) transceiver_1.flush(period=0.1, certain=True) count = 0 while True: transceiver_2.load() data = socket_2.recv(256) if len(data): print("data: {} , len: {}".format(im920_1.decode(data), len(data))) else: count += 1 if count > 5: break
def main(): handler = PyserialSerialHandler(control_setting.SERIAL_PORT, control_setting.BAUDRATE_IM920) im920 = Im920(handler, name=control_setting.NAME_IM920) print(f"Listen Start from ID: {im920.id}", end="\n\n") try: while True: if len(im920.buf): raw = im920.recv_raw() print(raw) if len(raw) == 2: addr, data = raw data = data.decode() device = "" if addr == whole_setting.ADDR_IM920_CHILD: device = "CHILD" if addr == whole_setting.ADDR_IM920_PARENT: device = "PARENT" if device: print(f"from {device}: {data}") except KeyboardInterrupt: handler.close()
def test_non_socket(): handler1 = PyserialSerialHandler("/dev/ttyUSB0", baudrate=19200) handler2 = PyserialSerialHandler("/dev/ttyUSB1", baudrate=19200) im920_1 = Im920(handler1, name="im920_1") im920_2 = Im920(handler2, name="im920_2") im920_1.clear_buf() im920_2.clear_buf() def main_1(): for _ in range(10): im920_1.send_raw(None, im920_1.encode("Hello World ")) sleep(0.2) def main_2(): que = deque() for _ in range(10): raw = im920_2.recv_raw() sleep(0.2) if not len(raw): continue que.append(raw[1]) stream = CommBytesStream() for d in que: stream.add(d) while True: data = stream.pop(32) if len(data): print(im920_1.decode(data)) else: break thread_1 = Thread(target=main_1) thread_2 = Thread(target=main_2) thread_1.start() thread_2.start()
def run_child(): pi = pigpio.pi() # handlers handler_bme280 = PigpioI2CHandler(pi, I2C_ADDRESS_BME280) handler_opt3002 = PigpioI2CHandler(pi, I2C_ADDRESS_OPT3002) handler_gps = PyserialSerialHandler(SERIAL_PORT_GPS, BAUDRATE_GPS) handler_im920 = PyserialSerialHandler(SERIAL_PORT_IM920, BAUDRATE_IM920) handler_led = PigpioDigitalOutputHandler(pi, GPIO_LED, name=NAME_LED) # sensors bme280 = Bme280(handler_bme280, name=NAME_BME280) opt3002 = Opt3002(handler_opt3002, name=NAME_OPT3002) gps = SamM8Q(handler_gps, name=NAME_GPS) # transceiver im920 = Im920(handler_im920, name=NAME_IM920) socket_transceiver = SocketTransceiver(im920, certain=True, name=NAME_SOCKET_TRANSCEIVER) # logger que = LogQueue(ChildLoggingModel, maxlen=5000, name=NAME_LOGQUEUE) dlogger = DataLogger(que, bme280, opt3002, gps, name=NAME_DATA_LOGGER) slogger = SystemLogger(name=NAME_SYSTEM_LOGGER) slogger.setFileHandler() manager = ComponentManager(handler_led, im920, socket_transceiver, dlogger, recursive=True, name=NAME_MANAGER) context = Context( { MissionStandbyNode: { True: ChildServerNode, False: MissionStandbyNode }, ChildServerNode: { True: None, False: ChildServerNode } }, start=MissionStandbyNode) cansat = CanSat(context, manager, dlogger=dlogger, slogger=slogger) cansat.run()
def test_socket_sender(): handler = PyserialSerialHandler("/dev/ttyUSB0", baudrate=19200) im920 = Im920(handler, name="im920") im920.clear_buf() address = ("1CD2", ) transceiver = SocketTransceiver(im920, period=0.1) socket = transceiver.create_socket(address) for _ in range(1): socket.send_later(im920.encode("Hello Taiki " * 10)) socket.flush()
def main(): device = sys.argv[1] if device not in (NAME_CHILD, NAME_PARENT): raise ValueError("The argument must be 'child' or 'parent'.") handler = PyserialSerialHandler(control_setting.SERIAL_PORT, control_setting.BAUDRATE_IM920) im920 = Im920(handler, name=control_setting.NAME_IM920) transceiver = SocketTransceiver(im920, certain=True) if device == NAME_CHILD: socket = transceiver.create_socket(whole_setting.ADDR_IM920_CHILD) else: socket = transceiver.create_socket(whole_setting.ADDR_IM920_PARENT) send(socket) recv(transceiver)
def test_socket_recver(): handler = PyserialSerialHandler("/dev/ttyUSB0", baudrate=19200) im920 = Im920(handler, name="im920") im920.clear_buf() address = ("37B6", ) transceiver = SocketTransceiver(im920, period=0.1) socket = transceiver.create_socket(address) sleep(10) count = 0 while True: data = socket.recv(256) if len(data): print("data: {} , len: {}".format(im920.decode(data), len(data))) else: count += 1 if count > 5: break
def exit(self) -> None: sock2control = self.transceiver.create_socket( whole_setting.ADDR_IM920_CONTROL) sock2control.send(Im920.encode("Child: Exit MissionStandbyNode."))
def exit(self) -> None: sock = self.transceiver.create_socket(whole_setting.ADDR_IM920_CONTROL) sock.send(Im920.encode("Child: Exit ChildServerNode."))
def run_parent(): # device setting pi = pigpio.pi() handler_bno055 = PigpioI2CHandler(pi, I2C_ADDRESS_BNO055) handler_bme280 = PigpioI2CHandler(pi, I2C_ADDRESS_BME280) handler_gps = PyserialSerialHandler(SERIAL_PORT_GPS, baudrate=BAUDRATE_GPS) handler_im920 = PyserialSerialHandler(SERIAL_PORT_IM920, baudrate=BAUDRATE_IM920) handler_motor_L_fin = PigpioPWMHandler(pi, GPIO_MOTOR_L_FIN, freq=MOTOR_PWM_FREQ) handler_motor_L_rin = PigpioPWMHandler(pi, GPIO_MOTOR_L_RIN, freq=MOTOR_PWM_FREQ) handler_motor_R_fin = PigpioPWMHandler(pi, GPIO_MOTOR_R_FIN, freq=MOTOR_PWM_FREQ) handler_motor_R_rin = PigpioPWMHandler(pi, GPIO_MOTOR_R_RIN, freq=MOTOR_PWM_FREQ) handler_mosfet_para = PigpioDigitalOutputHandler(pi, GPIO_MOSFET_PARA, name=NAME_MOSFET_PARA) handler_mosfet_child = PigpioDigitalOutputHandler(pi, GPIO_MOSFET_CHILD, name=NAME_MOSFET_CHILD) handler_sonic_trig = PigpioDigitalOutputHandler(pi, GPIO_SONIC_TRIG) handler_sonic_echo = PigpioDigitalInputHandler(pi, GPIO_SONIC_ECHO) handler_led = PigpioDigitalOutputHandler(pi, GPIO_LED, name=NAME_LED) # actuator motor_L = BD62xx(handler_motor_L_fin, handler_motor_L_rin, name=NAME_MOTOR_L) motor_R = BD62xx(handler_motor_R_fin, handler_motor_R_rin, name=NAME_MOTOR_R) wheels = TwoWheels(motor_L, motor_R, name=NAME_WHEELS) # transceiver im920 = Im920(handler_im920, name=NAME_IM920) im920.clear_buf() socket_transceiver = SocketTransceiver(im920, certain=True, name=NAME_SOCKET_TRANSCEIVER) # sensor bno055 = Bno055(handler_bno055, name=NAME_BNO055) bno055.change_operation_mode(Bno055.OperationMode.NDOF) bno055.remap_axis(Bno055.Axis.Y, Bno055.Axis.X, Bno055.Axis.Z) bno055.remap_sign(y=Bno055.AxisSign.NEGATIVE) bme280 = Bme280(handler_bme280, name=NAME_BME280) gps = SamM8Q(handler_gps, name=NAME_GPS) sonic = HcSr04(handler_sonic_echo, handler_sonic_trig, name=NAME_SUPERSONIC) que = LogQueue(LoggingModel, maxlen=10000, name=NAME_LOGQUEUE) dlogger = DataLogger(que, bno055, bme280, gps, sonic, name=NAME_DATA_LOGGER) slogger = SystemLogger(name=NAME_SYSTEM_LOGGER) slogger.setFileHandler() # register callable components in Nodes manager = ComponentManager(motor_L, motor_R, wheels, im920, socket_transceiver, handler_mosfet_para, handler_mosfet_child, handler_led, dlogger, slogger, recursive=True, name=NAME_MANAGER) # context setting context = Context( { MissionStandbyNode: { True: FallingNode, False: MissionStandbyNode }, FallingNode: { True: ParaSeparateNode, False: FallingNode }, ParaSeparateNode: { True: FirstRunningNode, False: ParaSeparateNode }, FirstRunningNode: { True: ChildSeparateNode, False: FirstRunningNode }, ChildSeparateNode: { True: SencondRunningNode, False: ChildSeparateNode }, SencondRunningNode: { True: GoalSearchNode, False: SencondRunningNode }, GoalSearchNode: { True: GoalDetectNode, False: GoalSearchNode }, GoalDetectNode: { True: None, False: GoalDetectNode } }, start=MissionStandbyNode) # build a cansat cansat = CanSat(context, manager, dlogger=dlogger, slogger=slogger) cansat.run()