├── mitosomat.service └── run.py /mitosomat.service: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=Mitosomat Service 3 | After=brickman.service 4 | Requisite=brickman.service 5 | 6 | [Service] 7 | Type=simple 8 | WorkingDirectory=/home/robot 9 | User=robot 10 | ExecStart=/usr/bin/micropython /home/robot/run.py 11 | 12 | [Install] 13 | WantedBy=default.target 14 | -------------------------------------------------------------------------------- /run.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/micropython 2 | # Mitosomat v1.0 3 | # Copyright (C) 2021 KaratekHD 4 | # 5 | # This program is free software: you can redistribute it and/or modify 6 | # it under the terms of the GNU General Public License as published by 7 | # the Free Software Foundation, either version 3 of the License, or 8 | # (at your option) any later version. 9 | # 10 | # This program is distributed in the hope that it will be useful, 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | # GNU General Public License for more details. 14 | # 15 | # You should have received a copy of the GNU General Public License 16 | # along with this program. If not, see . 17 | 18 | import os 19 | import sys 20 | from time import sleep 21 | 22 | from ev3dev2.motor import OUTPUT_A, OUTPUT_D, MoveTank, LargeMotor 23 | from ev3dev2.sensor import INPUT_1 24 | from ev3dev2.sensor.lego import InfraredSensor 25 | from ev3dev2.sound import Sound 26 | 27 | 28 | class State: 29 | speed = 5 30 | unspeed = speed * -1 31 | time = 4 32 | 33 | def __init__(self, id, name, tank): 34 | self.id = id 35 | self.name = name 36 | self.tank = tank 37 | 38 | def run(self): 39 | raise Exception("Override this function!") 40 | 41 | def requires(self): 42 | return range(0, self.id) 43 | 44 | 45 | class ProState(State): 46 | ''' 47 | Spindelfasern bilden sich 48 | ''' 49 | 50 | def __init__(self, tank): 51 | self.id = 0 52 | self.name = "Prophase" 53 | self.tank = tank 54 | 55 | def run(self): 56 | self.tank.on_for_seconds( 57 | right_speed=self.speed, 58 | left_speed=self.unspeed, 59 | seconds=self.time) 60 | 61 | 62 | class MetaState(State): 63 | ''' 64 | Spindelfasern docken an 65 | ''' 66 | 67 | def __init__(self, tank): 68 | self.id = 1 69 | self.name = "Metaphase" 70 | self.tank = tank 71 | 72 | def run(self): 73 | sleep(2.5) 74 | 75 | 76 | class AnaState(State): 77 | ''' 78 | Chromosom wird auseinander gezogen 79 | ''' 80 | 81 | def __init__(self, tank): 82 | self.id = 2 83 | self.name = "Anaphase" 84 | self.tank = tank 85 | 86 | def run(self): 87 | self.tank.on_for_seconds( 88 | right_speed=self.unspeed, 89 | left_speed=self.speed, 90 | seconds=self.time) 91 | 92 | 93 | class StateMachine: 94 | current_state = -1 95 | 96 | def __init__(self, tank): 97 | self.tank = tank 98 | self.states = [ 99 | ProState( 100 | self.tank), MetaState( 101 | self.tank), AnaState( 102 | self.tank)] 103 | 104 | def goto(self, state_id): 105 | if not state_id < len(self.states): 106 | raise Exception("Invalid state!") 107 | if self.current_state is not state_id - 1: 108 | for i in self.states[state_id].requires(): 109 | self.states[i].run() 110 | self.current_state = state_id 111 | self.states[state_id].run() 112 | 113 | def full_simulation(self): 114 | for i in range(0, len(self.states)): 115 | self.states[i].run() 116 | 117 | 118 | def main(): 119 | # Schriftgröße auf 14 setzen 120 | os.system('setfont Lat15-TerminusBold14') 121 | tank = MoveTank(OUTPUT_A, OUTPUT_D) 122 | a = LargeMotor(OUTPUT_A) 123 | d = LargeMotor(OUTPUT_D) 124 | sm = StateMachine(tank) 125 | dummy = State(id=-9, name="Dummy", tank=None) 126 | speed = dummy.speed 127 | unspeed = dummy.unspeed 128 | print("Drücken Sie eine der Tasten auf der linken Seite der Fernbedienung, um die Simulation zu starten.") 129 | sound = Sound() 130 | sound.play_file('/home/robot/.sounds/ansage.wav') 131 | ir = InfraredSensor(INPUT_1) 132 | while True: 133 | try: 134 | if ir.top_left(channel=1) or ir.bottom_left(channel=1): 135 | sm.full_simulation() 136 | elif ir.top_left(channel=2): 137 | # D vorwärts 138 | d.on_for_seconds(seconds=0.5, speed=speed) 139 | elif ir.bottom_left(channel=2): 140 | # D rückwärts 141 | d.on_for_seconds(seconds=0.5, speed=unspeed) 142 | elif ir.top_right(channel=2): 143 | a.on_for_seconds(seconds=0.5, speed=unspeed) 144 | elif ir.bottom_right(channel=2): 145 | a.on_for_seconds(seconds=0.5, speed=speed) 146 | elif ir.bottom_left(channel=3) or ir.top_left(channel=3): 147 | sound.beep() 148 | sys.exit(0) 149 | elif ir.bottom_right(channel=3) or ir.top_right(channel=3): 150 | sound.beep() 151 | os.system("sudo shutdown now") 152 | except KeyboardInterrupt: 153 | break 154 | 155 | 156 | main() 157 | --------------------------------------------------------------------------------