From eca382a9b3ce352b05b966f7f00b6a3fa7d323a0 Mon Sep 17 00:00:00 2001 From: jpunkt Date: Wed, 17 Nov 2021 19:20:12 +0100 Subject: [PATCH] debugging integration hell. --- micropizza/main.py | 104 ++++++++++++++++++++++++++++++--------------- micropizza/test.py | 29 ++++++++----- 2 files changed, 89 insertions(+), 44 deletions(-) diff --git a/micropizza/main.py b/micropizza/main.py index 3765273..cdf65b7 100644 --- a/micropizza/main.py +++ b/micropizza/main.py @@ -5,22 +5,22 @@ import time _DEBUG = const(1) _PWM_MAX = const(65535) -_PWM_FREQ = const(2000) +_PWM_FREQ = const(4000) _PIN_EN_MOTOR_VERT = const(14) _PIN_A_MOTOR_VERT = const(12) _PIN_B_MOTOR_VERT = const(13) _PIN_EN_MOTOR_HOR = const(15) -_PIN_A_MOTOR_HOR = const(10) -_PIN_B_MOTOR_HOR = const(11) +_PIN_A_MOTOR_HOR = const(11) +_PIN_B_MOTOR_HOR = const(10) -_PIN_EN_BACKLIGHT = const(17) -_PIN_A_BACKLIGHT = const(18) -_PIN_B_BACKLIGHT = const(19) -_PIN_EN_FRONTLIGHT = const(16) -_PIN_A_FRONTLIGHT = const(20) -_PIN_B_FRONTLIGHT = const(21) +_PIN_EN_BACKLIGHT = const(16) # 17 +_PIN_A_BACKLIGHT = const(20) # 18 +_PIN_B_BACKLIGHT = const(21) # 19 +_PIN_EN_FRONTLIGHT = const(17) # 16 +_PIN_A_FRONTLIGHT = const(18) # 20 +_PIN_B_FRONTLIGHT = const(19) # 21 _PIN_LED_RED_BTN = const(2) _PIN_LED_BLU_BTN = const(4) @@ -30,8 +30,8 @@ _PIN_BLU_BTN = const(5) _PIN_SENS_END_VERT = const(9) _PIN_SENS_CNT_VERT = const(7) -_PIN_SENS_END_HOR = const(6) -_PIN_SENS_CNT_HOR = const(8) +_PIN_SENS_END_HOR = const(8) +_PIN_SENS_CNT_HOR = const(6) CMD_MOT_V = 'V' # run vertical motor CMD_MOT_H = 'H' # run horizontal motor @@ -43,24 +43,31 @@ CMD_RW = 'R' # rewind LED = Pin(25, Pin.OUT) + class Scroll: def __init__(self, cnt_pin:Pin, end_pin:Pin, en_pin:PWM, a_pin:Pin, b_pin:Pin) -> None: self.c = 0 self._dir = 0 self._speed = 0 + self.stopped = False self.en_pin = en_pin self.a_pin = a_pin self.b_pin = b_pin self.c_pin = cnt_pin self.c_pin.irq(self._count, Pin.IRQ_RISING | Pin.IRQ_FALLING) self.e_pin = end_pin - self.e_pin.irq(self._stop, Pin.IRQ_RISING) + # self.e_pin.irq(self._stop, Pin.IRQ_RISING) def _count(self, pin): self.c += self._dir + # if _DEBUG: + # print(f'{self} count={self.c}') def _stop(self, pin): + if _DEBUG: + print(f'{self} stopped.') self.speed = 0 + self.stopped = True @property def dir(self): @@ -68,6 +75,10 @@ class Scroll: @dir.setter def dir(self, d): + if d == self._dir: + return + if _DEBUG: + print(f'{self} dir={d}') self.a_pin.value(1 if d > 0 else 0) self.b_pin.value(1 if d < 0 else 0) self._dir = d @@ -78,15 +89,19 @@ class Scroll: @speed.setter def speed(self, speed): - self.en_pin.duty_u16(speed * (_PWM_MAX // 100)) + # if _DEBUG: + # print(f'{self} speed={speed}') + self._speed = abs(speed) + self.en_pin.duty_u16(self._speed * (_PWM_MAX // 100)) self.dir = (1 if speed > 0 else (-1 if speed < 0 else 0)) - self._speed = speed + pwm_bl = PWM(Pin(_PIN_EN_BACKLIGHT)) pwm_bl.freq(_PWM_FREQ) pwm_fl = PWM(Pin(_PIN_EN_FRONTLIGHT)) pwm_fl.freq(_PWM_FREQ) + pwm_mv = PWM(Pin(_PIN_EN_MOTOR_VERT)) pwm_mv.freq(_PWM_FREQ) pwm_mh = PWM(Pin(_PIN_EN_MOTOR_HOR)) @@ -118,39 +133,49 @@ uart0 = UART(0, baudrate=9600, tx=Pin(0), rx=Pin(1)) scroll_h = Scroll(cnt_h, end_h, pwm_mh, pin_mh_a, pin_mh_b) scroll_v = Scroll(cnt_v, end_v, pwm_mv, pin_mv_a, pin_mv_b) + def motor_vert(*opts): _motorctrl(scroll_v, *opts) return 'OK' + def motor_hor(*opts): _motorctrl(scroll_h, *opts) return 'OK' -def _motorctrl(scroll: Scroll, steps: int, timeout_ms: int = 500, *opts): + +def _motorctrl(scroll: Scroll, steps: int, timeout_ms: int = 5000, *opts): end = scroll.c + steps - print(f'motorctrl, end={end}, scroll.c={scroll.c}') + if _DEBUG: + print(f'motorctrl, end={end}, scroll.c={scroll.c}') try: startime = time.ticks_ms() - while time.ticks_diff(startime, time.ticks_ms()) < timeout_ms: + while (not scroll.stopped) and (time.ticks_diff(time.ticks_ms(), startime) < timeout_ms): err = end - scroll.c - if abs(err) > 4: - speed = (err / (2 * steps)) + 0.5 - scroll.speed = int(100 * speed) + if abs(err) > 1: + speed = 100 if err > 0 else -100 # (err / (2 * abs(steps))) * 400 # + 0.5 + scroll.speed = int(speed) else: scroll.speed = 0 break time.sleep_us(50) finally: + if _DEBUG: + print(f'motorctrl, scroll.c={scroll.c}') scroll.speed = 0 + scroll.stopped = False + def backlight(*opts): _fade_led(pwm_bl, *opts) return 'OK' + def frontlight(*opts): _fade_led(pwm_fl, *opts) return 'OK' + def _fade_led(led_pin: PWM, intensity: int, fade_ms: int, steps: int): intensity = intensity * (_PWM_MAX // 100) if fade_ms == 0: @@ -170,6 +195,7 @@ def _fade_led(led_pin: PWM, intensity: int, fade_ms: int, steps: int): led_pin.duty_u16(intensity) + def user_interaction(timeout): def _toggle_leds(timer): led_blu.toggle() @@ -191,6 +217,7 @@ def user_interaction(timeout): led_red.value(0) return 'T' # Timeout + def recording(timeout): timeout *= 1000 startime = time.ticks_ms() @@ -205,6 +232,7 @@ def recording(timeout): time.sleep_ms(50) return 'OK' + def rewind(*opts): #scroll_h.speed = -100 #scroll_v.speed = -100 @@ -214,8 +242,10 @@ def rewind(*opts): #scroll_v.c = 0 return 'OK' + ############################################################################### + choice_map = { CMD_MOT_V: motor_vert, CMD_MOT_H: motor_hor, @@ -226,17 +256,22 @@ choice_map = { CMD_RW: rewind } -def parseCmd(cmd_str: str): - cmd, parm_str = cmd_str.split(':') + +def parseCmd(cmd_str: bytes): + cmd, parm_str = cmd_str.decode('utf-8').split(':') parms = [] if len(parm_str) != 0: parms = [int(x) for x in parm_str.split('+')] + resp = 'E' if _DEBUG: print(cmd, parms, sep=': ') - resp = 'OK' - else: - resp = choice_map[cmd](*parms) - uart0.write(f'{resp}\n'.encode('utf-8')) + # resp = 'OK' + # else: + resp = choice_map[cmd](*parms) + if _DEBUG: + print('response', resp, sep=': ') + return resp + def flash_led(times: int = 1, on_ms: int = 200, off_ms: int = 500): LED.off() @@ -249,16 +284,17 @@ def flash_led(times: int = 1, on_ms: int = 200, off_ms: int = 500): if __name__ == '__main__': - pin_bl_a.value(1) - pin_bl_b.value(0) - pin_fl_a.value(1) - pin_fl_b.value(0) - rxData = '' + pin_bl_a.value(0) + pin_bl_b.value(1) + pin_fl_a.value(0) + pin_fl_b.value(1) + rxData = b'' flash_led(2) while True: rxBuf = uart0.read(1) if rxBuf is not None: - rxData += rxBuf.decode('utf-8') + rxData += rxBuf # .decode('utf-8') if rxData.endswith('\n'): - parseCmd(rxData.strip()) - rxData = '' + resp = parseCmd(rxData.strip()) + uart0.write(f'{resp}\n'.encode('utf-8')) + rxData = b'' diff --git a/micropizza/test.py b/micropizza/test.py index a04561f..1286d8e 100644 --- a/micropizza/test.py +++ b/micropizza/test.py @@ -147,17 +147,26 @@ def recording(timeout): time.sleep_ms(50) return 'OK' +# def uart_comm(): +# uart0 = UART(0, baudrate=9600, tx=Pin(0), rx=Pin(1)) +# rxData = b'' +# while True: +# rxBuf = uart0.read(1) +# if rxBuf is not None: +# # print(rxBuf) +# rxData += rxBuf # .decode('utf-8') +# if rxData.endswith(b'\n'): +# print(rxData.strip()) +# uart0.write('OK\n'.encode('utf-8')) +# rxData = b'' + def uart_comm(): uart0 = UART(0, baudrate=9600, tx=Pin(0), rx=Pin(1)) - rxData = '' + # rxData = b'' while True: - rxBuf = uart0.read(1) - if rxBuf is not None: - rxData += rxBuf.decode('utf-8') - if rxData.endswith('\n'): - print(rxData.strip()) - uart0.write(b'OK\n') - rxData = '' + if uart0.any(): + print(uart0.read()) + def test_sensors(): for i in range(4): @@ -167,5 +176,5 @@ def test_sensors(): if __name__ == '__main__': #print(user_interaction(20)) #Print - #test_sensors() - uart_comm() + test_sensors() + #uart_comm()