-
Notifications
You must be signed in to change notification settings - Fork 0
/
motor_distance_sensor.py
31 lines (27 loc) · 10.3 KB
/
motor_distance_sensor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# pybricks blocks file:{"blocks":{"languageVersion":0,"blocks":[{"type":"blockGlobalSetup","id":"bjK,wS1MYO7aiYkFSwd{","x":-38,"y":5,"deletable":false,"next":{"block":{"type":"variables_set_city_hub","id":"ZRpv)bQ_A|8C:_oAgehH","extraState":{"optionLevel":0},"fields":{"VAR":{"id":"6Nhb-)Cm[(%u0kno-=rf"}},"next":{"block":{"type":"variables_set_motor","id":"fP~tX6LzSUa@/?$y^:2}","fields":{"VAR":{"id":"%Fn3xc:Qr~map/`Uql@l"}},"inputs":{"PORT":{"shadow":{"type":"blockParametersPort","id":"+Lj6G]n8#K.4.w9OT1e?","fields":{"NAME":"A"}}},"POSITIVE_DIRECTION":{"shadow":{"type":"blockParametersDirection","id":"{J!r%$@)W+a_w.xph/Fh","fields":{"SELECTION":"Direction.CLOCKWISE"}}}},"next":{"block":{"type":"variables_set_ultrasonic_sensor","id":"ry`TCO9E;OJ.G5E#u:^a","fields":{"VAR":{"id":"Ek@DC/?baMHmC-^1*h/7"}},"inputs":{"PORT":{"shadow":{"type":"blockParametersPort","id":"l5L=s[v.~Sg~?9Bk(V0c","fields":{"NAME":"B"}}}},"next":{"block":{"type":"variables_setup_any","id":"O3=Ryl5]}bPW%#wM6h,B","fields":{"VAR":{"id":"i3}Q4Jf+SrRazfxiT@LA"}},"inputs":{"VALUE0":{"shadow":{"type":"blockMathNumber","id":"7O)A7AuL0cg(1.x6YgKD","fields":{"NUM":0}}}}}}}}}}}}},{"type":"blockGlobalStart","id":"3tJe|AWl0baN(wH9a$@.","x":-18,"y":259,"deletable":false,"next":{"block":{"type":"blockHubStopButton","id":")~7`)Xu@HbinOwhTe@-q","inputs":{"VAR":{"shadow":{"type":"variables_get_button_hub","id":"{z@bWDiu|D7ELv?f-zK3","fields":{"VAR":{"id":"6Nhb-)Cm[(%u0kno-=rf","name":"city hub","type":"CityHub"}}}},"VALUE0":{"shadow":{"type":"blockParametersButton","id":")C#R)}P,mxuM5Y7/o(QT","fields":{"VALUE":"CENTER"}},"block":{"type":"blockButtonIsPressed","id":"y/`:b1%/;8II[f8}dzQt","inputs":{"VAR":{"shadow":{"type":"variables_get_keypad","id":"4V4$VVRZ::d9i0%x#-vc","fields":{"VAR":{"id":"6Nhb-)Cm[(%u0kno-=rf","name":"city hub","type":"CityHub"}}}},"VALUE0":{"shadow":{"type":"blockParametersButton","id":"B^F+#6,Pos9IT0(n9uqh","fields":{"VALUE":"CENTER"}}}}}}},"next":{"block":{"type":"blockLightOnColor","id":"@(oqE,_lL2v=^3_Z~P%z","extraState":{"optionLevel":1},"fields":{"METHOD":"LIGHT_ON"},"inputs":{"VAR":{"shadow":{"type":"variables_get_color_light_device","id":"q)od5rPM=gzkWU6^yPJZ","fields":{"VAR":{"id":"6Nhb-)Cm[(%u0kno-=rf","name":"city hub","type":"CityHub"}}}},"COLOR":{"shadow":{"type":"variables_get_color","id":"AoabZ*wYC`U|BuQ!XP~z","fields":{"COLOUR":"#0000ff","VAR":{"id":"#l{_6R2Kh{iYARGD~[l.","name":"blue","type":"ColorDef"}}}}},"next":{"block":{"type":"blockFlowWhile","id":"[{7SQ{AC^*w!Iea~-0mw","fields":{"MODE":"WHILE"},"inputs":{"BOOL":{"shadow":{"type":"blockLogicTrue","id":"+_Y#`@01+5dy[L_IH4Y8"}},"DO":{"block":{"type":"blockVariableSetValue","id":"L[lt/wM([;rJ5ONW.X4H","inputs":{"VAR":{"shadow":{"type":"variables_get_any","id":"#zr{UA^i$vDbago]3WS@","fields":{"VAR":{"id":"i3}Q4Jf+SrRazfxiT@LA","name":"speed","type":"Any"}}}},"VALUE0":{"shadow":{"type":"blockMathNumber","id":"d+f7Dw7ehar6[EdjugLj","fields":{"NUM":0}},"block":{"type":"blockDistance","id":"+{bA5lL}cj[6S}sO=fy1","inputs":{"VAR":{"shadow":{"type":"variables_get_distance_device","id":"Z6b)WTWC.$aE+cK~5{A7","fields":{"VAR":{"id":"Ek@DC/?baMHmC-^1*h/7","name":"distance sensor","type":"UltrasonicSensor"}}}}}}}},"next":{"block":{"type":"blockIfElse","id":"7J7ab.v3v)/,A_[F/FC(","extraState":{"optionLevel":1},"inputs":{"IF0":{"shadow":{"type":"blockLogicTrue","id":"C*E]5h[s.C4$4S$R!gK."},"block":{"type":"blockLogicCompare","id":"=xT`dS_!]_gC{nRU@VS?","fields":{"OP1":"LTE"},"inputs":{"A":{"shadow":{"type":"blockMathNumber","id":"bpEiFs@#I-s=,(6_VG]3","fields":{"NUM":3}},"block":{"type":"blockVariableGetValue","id":"k2icH*@%f)8+~brZ$B@k","inputs":{"VAR":{"shadow":{"type":"variables_get_any","id":")d%[2Mr|Y+(2{_39XJ?7","fields":{"VAR":{"id":"i3}Q4Jf+SrRazfxiT@LA","name":"speed","type":"Any"}}}}}}},"B":{"shadow":{"type":"blockMathNumber","id":"6kekrQW24wf+uaX+#U4f","fields":{"NUM":40}}}}}},"DO0":{"block":{"type":"blockVariableSetValue","id":"6hg;[PMQb2S|wFn;~jF/","inputs":{"VAR":{"shadow":{"type":"variables_get_any","id":"*WoOV]d_ak3]z,DA,pFT","fields":{"VAR":{"id":"i3}Q4Jf+SrRazfxiT@LA","name":"speed","type":"Any"}}}},"VALUE0":{"shadow":{"type":"blockMathNumber","id":"d+f7Dw7ehar6[EdjugLj","fields":{"NUM":0}}}}}},"ELSE":{"block":{"type":"blockIfElse","id":"E+I.Fo5xqJ6%D{`Y?GW5","extraState":{"optionLevel":1},"inputs":{"IF0":{"shadow":{"type":"blockLogicTrue","id":"C*E]5h[s.C4$4S$R!gK."},"block":{"type":"blockLogicCompare","id":"(32x(Z9{V]4%yZABtN^u","fields":{"OP1":"GT"},"inputs":{"A":{"shadow":{"type":"blockMathNumber","id":"bpEiFs@#I-s=,(6_VG]3","fields":{"NUM":3}},"block":{"type":"blockVariableGetValue","id":"kXxGmSE?nbd+V6Xm82,R","inputs":{"VAR":{"shadow":{"type":"variables_get_any","id":"C~_:#c2@T}Vj4:!~H?Vw","fields":{"VAR":{"id":"i3}Q4Jf+SrRazfxiT@LA","name":"speed","type":"Any"}}}}}}},"B":{"shadow":{"type":"blockMathNumber","id":"2A4aY:,6mT|(|n4gtuPM","fields":{"NUM":140}}}}}},"DO0":{"block":{"type":"blockVariableSetValue","id":"G/xC(,+VSGWKI-I:b[8*","inputs":{"VAR":{"shadow":{"type":"variables_get_any","id":"!^G,S[!yS8J|%1[c`MG:","fields":{"VAR":{"id":"i3}Q4Jf+SrRazfxiT@LA","name":"speed","type":"Any"}}}},"VALUE0":{"shadow":{"type":"blockMathNumber","id":"%A17QxEt#W@NRgy]KBPT","fields":{"NUM":100}}}}}},"ELSE":{"block":{"type":"blockVariableSetValue","id":"es:q*_cNA-1FXvsbvDmx","inputs":{"VAR":{"shadow":{"type":"variables_get_any","id":"]NVwu=x?_X5BXBbU]J-[","fields":{"VAR":{"id":"i3}Q4Jf+SrRazfxiT@LA","name":"speed","type":"Any"}}}},"VALUE0":{"shadow":{"type":"blockMathNumber","id":"_3?pyAZ=Ox(UDi4w%)Y!","fields":{"NUM":100}},"block":{"type":"blockMathArithmetic","id":"TPCCLVZvPbqICB`2jli=","fields":{"OP":"MULTIPLY"},"inputs":{"A":{"shadow":{"type":"blockMathNumber","id":"PwIJWHTgB8`1p=I{tZDt","fields":{"NUM":1}},"block":{"type":"blockMathArithmetic","id":";r2`LebAPTDtHThBvU=2","fields":{"OP":"MINUS"},"inputs":{"A":{"shadow":{"type":"blockMathNumber","id":"%NW$H4/XIZOucNv3XLC_","fields":{"NUM":1}},"block":{"type":"blockVariableGetValue","id":"/qkcc}@IO:Vi|`$lI?hE","inputs":{"VAR":{"shadow":{"type":"variables_get_any","id":"}T_9nuDCBw{FVE{,F/jN","fields":{"VAR":{"id":"i3}Q4Jf+SrRazfxiT@LA","name":"speed","type":"Any"}}}}}}},"B":{"shadow":{"type":"blockMathNumber","id":"OQV|Cnu8awf0tAeRwM(=","fields":{"NUM":40}}}}}},"B":{"shadow":{"type":"blockMathNumber","id":"Tq^%P;A}mEuWgp2bVCD1","fields":{"NUM":1}}}}}}}}}}}}},"next":{"block":{"type":"blockVariableSetValue","id":"4*poUgGwbnL9XhYe2hzw","inputs":{"VAR":{"shadow":{"type":"variables_get_any","id":"[;}L~.G.8yStC;UF`vE{","fields":{"VAR":{"id":"i3}Q4Jf+SrRazfxiT@LA","name":"speed","type":"Any"}}}},"VALUE0":{"shadow":{"type":"blockMathNumber","id":"N@!!A(maD%`}-p/HvHVb","fields":{"NUM":0}},"block":{"type":"blockMathArithmetic","id":"(d8Ep0?%NR)I#|,ys#*i","fields":{"OP":"MINUS"},"inputs":{"A":{"shadow":{"type":"blockMathNumber","id":"4eB+:R7w~=|3#E+oS.qH","fields":{"NUM":100}}},"B":{"shadow":{"type":"blockMathNumber","id":"qg~r;VF-+wwEA{@YDrf+","fields":{"NUM":1}},"block":{"type":"blockVariableGetValue","id":"[zazVEZ?y*?SM=lfSDp^","inputs":{"VAR":{"shadow":{"type":"variables_get_any","id":"QfGA?7hPxB/8~o.j5V}N","fields":{"VAR":{"id":"i3}Q4Jf+SrRazfxiT@LA","name":"speed","type":"Any"}}}}}}}}}}},"next":{"block":{"type":"blockMotorDuty","id":"]|?8TSXn|a9E[tU)CQ_l","inputs":{"VAR":{"shadow":{"type":"variables_get_simple_motor_device","id":"|[_w6RIGbZ%(#1buaqzC","fields":{"VAR":{"id":"%Fn3xc:Qr~map/`Uql@l","name":"motor","type":"Motor"}}}},"VALUE0":{"shadow":{"type":"unit_percent","id":"2r0AET3qkgw`Lb88lVhK","fields":{"VALUE0":50}},"block":{"type":"blockVariableGetValue","id":"[K*B-qxt3|MvzF9)~eZU","inputs":{"VAR":{"shadow":{"type":"variables_get_any","id":".*c9iv%Fc+DX@sPnK^9n","fields":{"VAR":{"id":"i3}Q4Jf+SrRazfxiT@LA","name":"speed","type":"Any"}}}}}}}},"next":{"block":{"type":"blockPrint","id":"v;s,xxpqClq-_PpHLfJ[","extraState":{"optionLevel":1},"fields":{"SEP":"SEP_COMMA_SPACE"},"inputs":{"TEXT0":{"shadow":{"type":"text","id":",]~`|h:Wx|wH8m}B%~-R","fields":{"TEXT":"abc"}},"block":{"type":"blockDistance","id":"D]EQRu{xNYM%[34bHG%1","inputs":{"VAR":{"shadow":{"type":"variables_get_distance_device","id":"H:LHR~Dns*nnH]We-`mR","fields":{"VAR":{"id":"Ek@DC/?baMHmC-^1*h/7","name":"distance sensor","type":"UltrasonicSensor"}}}}}}},"TEXT1":{"shadow":{"type":"text","id":"C%LQ=dmAkm{]LGDwy$Em","fields":{"TEXT":"abc"}},"block":{"type":"blockVariableGetValue","id":"z/Gi*^vf0y|fH]#,=I?l","inputs":{"VAR":{"shadow":{"type":"variables_get_any","id":"9BC9$-y^S0M=8XN7;L=T","fields":{"VAR":{"id":"i3}Q4Jf+SrRazfxiT@LA","name":"speed","type":"Any"}}}}}}}},"next":{"block":{"type":"blockWaitTime","id":":xxk|ksEPKfFJA@4BA0H","inputs":{"VALUE0":{"shadow":{"type":"unit_time","id":"0+r1n(AQf]Ct|E~Z-6$;","fields":{"VALUE0":10}}}}}}}}}}}}}}}}}}}}}}}}]},"variables":[{"name":"red","id":"0;r[$oFicKg(;4YU/uwh","type":"ColorDef"},{"name":"orange","id":"3$`N[?|TgM$Y0)Q6?^gj","type":"ColorDef"},{"name":"yellow","id":"ZBD._/JZ=:hbjxZ^l@Gk","type":"ColorDef"},{"name":"green","id":"*cjn*QOy-QBWY#f#ja/I","type":"ColorDef"},{"name":"cyan","id":":+/(,*T{Y!O9skO`19WO","type":"ColorDef"},{"name":"blue","id":"#l{_6R2Kh{iYARGD~[l.","type":"ColorDef"},{"name":"violet","id":"dFp6pN%KZ$.#CR8f{rhR","type":"ColorDef"},{"name":"magenta","id":"NOWH-E)vulOUj0Nv{kc8","type":"ColorDef"},{"name":"white","id":"{;zbm0otTqs$-L#EFw^0","type":"ColorDef"},{"name":"none","id":"VR#Y;PPJC+/HRKw#fqP~","type":"ColorDef"},{"name":"city hub","id":"6Nhb-)Cm[(%u0kno-=rf","type":"CityHub"},{"name":"motor","id":"%Fn3xc:Qr~map/`Uql@l","type":"Motor"},{"name":"distance sensor","id":"Ek@DC/?baMHmC-^1*h/7","type":"UltrasonicSensor"},{"name":"speed","id":"i3}Q4Jf+SrRazfxiT@LA","type":"Any"},{"name":"data","id":"P(K*F-[k%0z4i,]!y2vl","type":"Any"}],"info":{"type":"pybricks","version":"1.2.3"}}
from pybricks.hubs import CityHub
from pybricks.parameters import Button, Color, Direction, Port
from pybricks.pupdevices import Motor, UltrasonicSensor
from pybricks.tools import wait
# Set up all devices.
city_hub = CityHub()
distance_sensor = UltrasonicSensor(Port.B)
motor = Motor(Port.A, Direction.CLOCKWISE)
# Initialize variables.
speed = 0
# The main program starts here.
city_hub.system.set_stop_button(Button.CENTER in city_hub.buttons.pressed())
city_hub.light.on(Color.BLUE)
while True:
speed = distance_sensor.distance()
if speed <= 40:
speed = 0
else:
if speed > 140:
speed = 100
else:
speed = (speed - 40) * 1
speed = 100 - speed
motor.dc(speed)
print(distance_sensor.distance(), speed, sep=", ")
wait(10)