Exemplo n.º 1
0
 def test_logic_execution(self):
     stepper = Stepper()
     logic = AsteriskLogic(stepper)
     logic.execute = MagicMock()
     thread = Thread(target=logic.run)
     thread.start()
     time.sleep(0.01)
     logic.execute.assert_called()
     stepper.interrupt()
Exemplo n.º 2
0
 def test_rotate_by_angle(self):
     stepper = Stepper(base_time=0.00001)
     # rotate 180 degrees
     steps = stepper.rotate_by_angle(1.0)
     assert steps == 800
     assert stepper.current_step == 800
     # rotate -270 degrees
     steps = stepper.rotate_by_angle(-1.5)
     assert steps == -1200
     assert stepper.current_step == 1200
Exemplo n.º 3
0
 def test_rotate_by_steps(self):
     stepper = Stepper(base_time=0.0001)
     # rotate 100 steps
     steps = stepper.rotate_by_steps(100, 10)
     assert steps == 100
     assert stepper.current_step == 100
     # rotate -50 steps
     steps = stepper.rotate_by_steps(-50, 10)
     assert steps == -50
     assert stepper.current_step == 50
Exemplo n.º 4
0
 def test_set_angle(self):
     stepper = Stepper(base_time=0.00001)
     # 180 degrees
     steps = stepper.set_angle(1.0)
     assert steps == 800
     assert stepper.current_step == 800
     # 45 degrees
     steps = stepper.set_angle(0.25)
     assert steps == -600
     assert stepper.current_step == 200
     # -90 degrees
     steps = stepper.set_angle(-0.5)
     assert steps == -600
     assert stepper.current_step == 1200
Exemplo n.º 5
0
    def __init__(self):
        # configurations
        config = AsteriskConfig()
        self.stop_event = Event()
        self.main_thread = None
        self.timer_thread = None
        self.stepper = Stepper([config.get('System.step_pin', int), config.get('System.direction_pin', int), config.get('System.enable_pin', int)])
        self.transition = config.get('System.transition', int)
        self.logics = []
        self.logic_index = -1

        # load and append logics
        module = import_module('asterisk_mirror.logics')
        for logic_str in config.get('System.logics').split(','):
            logic_cls = getattr(module, logic_str.strip())
            logic = logic_cls(self.stepper)
            self.logics.append(logic)

        print("AsteriskMirror [", "transition:", self.transition, "]")
Exemplo n.º 6
0
 def test_reset(self):
     stepper = Stepper(base_time=0.00001)
     # rotate 10steps -> reset -10steps
     steps = stepper.rotate_by_steps(10)
     assert stepper.current_step == 10
     steps = stepper.reset()
     assert stepper.current_step == 0
     assert steps == -10
     # rotate 1200steps -> reset 400steps
     test_steps = int(stepper.number_of_steps * 3 / 4)
     steps = stepper.rotate_by_steps(test_steps)
     assert stepper.current_step == test_steps
     steps = stepper.reset()
     assert stepper.current_step == 0
     assert steps == stepper.number_of_steps - test_steps
     # rotate -400steps -> reset 400steps
     test_steps = int(-stepper.number_of_steps / 4)
     steps = stepper.rotate_by_steps(test_steps)
     assert stepper.current_step == stepper.number_of_steps + test_steps
     steps = stepper.reset()
     #print("steps=", steps, ", current=", stepper.current_step)
     assert stepper.current_step == 0
     assert steps == -test_steps
Exemplo n.º 7
0
    def test_interrupt(self):
        stepper = Stepper()

        def interrupt():
            # interrupt stepper thread after 50ms
            time.sleep(0.005)
            stepper.interrupt()

        # 1st test
        thread = Thread(target=interrupt)
        thread.start()
        steps = stepper.rotate_by_steps(100)
        #print("steps=", steps, ", current=", stepper.current_step)
        assert steps in range(4, 7)
        assert stepper.current_step == steps
        # 2nd test
        stepper.reset()
        thread = Thread(target=interrupt)
        thread.start()
        steps = stepper.rotate_by_steps(-100)
        #print("steps=", steps, ", current=", stepper.current_step)
        assert steps in range(-6, -3)
        assert stepper.current_step == stepper.number_of_steps + steps
Exemplo n.º 8
0
class AsteriskMirror:
    def __init__(self):
        # configurations
        config = AsteriskConfig()
        self.stop_event = Event()
        self.main_thread = None
        self.timer_thread = None
        self.stepper = Stepper([config.get('System.step_pin', int), config.get('System.direction_pin', int), config.get('System.enable_pin', int)])
        self.transition = config.get('System.transition', int)
        self.logics = []
        self.logic_index = -1

        # load and append logics
        module = import_module('asterisk_mirror.logics')
        for logic_str in config.get('System.logics').split(','):
            logic_cls = getattr(module, logic_str.strip())
            logic = logic_cls(self.stepper)
            self.logics.append(logic)

        print("AsteriskMirror [", "transition:", self.transition, "]")
        
    def start(self):
        if self.main_thread is not None:
            print("AsteriskMirror: already started.")
            return
        print ("AsteriskMirror: starting...")
        # renew threads
        if self.timer_thread is not None:
            self.stop_event.set()
        self.timer_thread = Thread(target=self.timer_run)
        self.stop_event.clear()
        self.main_thread = Thread(target=self.run)

        # start threads
        self.main_thread.start()
        self.timer_thread.start()

    def stop(self):
        print("AsteriskMirror: stopping...")
        self.stop_event.set()
        self.stepper.exit()
        self.timer_thread = None
        self.main_thread = None

    def timer_run(self):
        while not self.stop_event.is_set():
            # set a new index of logics
            self.logic_index = (self.logic_index+1)%len(self.logics)
            print("AsteriskMirror: changes logic:", self.logics[self.logic_index])
            # interrupt stepper thread and main thread
            self.stepper.interrupt()
            self.stop_event.wait(self.transition)

    def run(self):
        #print("AsteriskMirror.run starting...")
        while not self.stop_event.is_set():
            if self.logic_index >= 0 and len(self.logics) > 0:
                logic = self.logics[self.logic_index]
                logic.run()
            else:
                # wait until a right logic-index will be set
                self.stop_event.wait(1)
Exemplo n.º 9
0
 def test_init(self):
     stepper = Stepper([1, 2, 3])
     assert stepper.step_pin == 1
     assert stepper.direction_pin == 2
     assert stepper.enable_pin == 3
Exemplo n.º 10
0
 def test_encode_morse(self):
     for message, morse in self.test_morses.items():
         logic = MorseLogic(Stepper())
         logic.set_message(message)
         assert logic.morse == morse