legoEv3/save/home/robot/motor1.py

from ev3dev2.motor import *
from ev3dev2.sensor import INPUT_1
from ev3dev2.sensor.lego import TouchSensor
from ev3dev2.led import Leds
from ev3dev.core import Sound
from datetime import datetime
import time
import threading

t0=datetime.now()
print("start t1.py ... ")
print("sys.version", sys.version, "time=" + str(t0))

leds = Leds()
mA = MediumMotor(OUTPUT_A)   # medium motor on output a
lck = threading.Lock();


class MoPr(threading.Thread):
    """ print the motor state, whenever it changes """
    stop = False;
    def __init__(self, mo):
        threading.Thread.__init__(self)
        self.mot = mo

    def run(self):
        cL = 0;
        cP = 0;
                            # attention: without locking, very soon main thread will dy, because of inconsistent data in device!
        lck.acquire()
        print(str(datetime.now()) + " motor " + self.mot.address + "begin count_per_rot=" + 
            str(self.mot.count_per_rot) +  " commands=" + repr(self.mot.commands))
        lck.release()
        lPos = "undef"
        while not MoPr.stop:
            cL = cL + 1
            
            time.sleep(0.001)
            lck.acquire()
            if lPos != self.mot.position:
                lPos = self.mot.position  
                cP = cP + 1
                print(str(datetime.now()) + " motor " + self.mot.address + " state=" + repr(self.mot.state) +   
                   "position="+str(self.mot.position) + " speed="+str(self.mot.speed) + " cnt="+str(cP)+'/'+str(cL))
            lck.release()
        print(str(datetime.now()) + " motor " + self.mot.address + ' end')        

MoPr(mA).start()

for x in range(0, 3):
    Sound.play_song((("A5", 'e'),("C4", 'e'), ('E5', 'h')))     # play postouto
    leds.set_color("LEFT", "GREEN")                             # LED colors     
    leds.set_color("RIGHT", "RED")
    lck.acquire()
    print(str(datetime.now()) + " rotate plus")
    mA.on_for_rotations(SpeedPercent(25), 0.5, block=False)     # half rotation on gripper
    lck.release()
    time.sleep(1)
    lck.acquire()
    print(str(datetime.now()) + " rotate stop")
    mA.stop()
    lck.release()
 
    Sound.play_song((("A5", 'e'),("C4", 'e'), ('E5', 'h')))
    leds.set_color("LEFT", "RED")
    leds.set_color("RIGHT", "GREEN")
    lck.acquire()
    print(str(datetime.now()) + " rotate minus")
    mA.on_for_rotations(SpeedPercent(-25), 0.5, block=False)    # half rotation backward on gripper
    lck.release()
    time.sleep(1)
    print(str(datetime.now()) + " rotate stop")
    mA.stop()

Sound.speak('what is going on in motor1.py')   # speak a little text
leds.set_color("LEFT", "YELLOW")
leds.set_color("RIGHT", "RED")
MoPr.stop = True

leds.set_color("LEFT", "BLACK")
leds.set_color("RIGHT", "BLACK")