In this project, we will show you how to control the LEGO® Technic Cat D11 Bulldozer (42131) with the Xbox Controller.

This is a fun way to drive the bulldozer around without having to look at your phone screen all time time. You’ll also use the controller to switch between the functions of the blade, ripper, and ladder.

Driving the LEGO® Technic Cat D11 Bulldozer (42131) with the Xbox Controller.
Driving the LEGO® Technic Cat D11 Bulldozer (42131) with the Xbox Controller.

Requirements

To follow this project, you will need the following:

See this overview for all compatible models.

Understanding the code

The program consists of two main tasks that run in parallel. The first task handles the driving and steering of the bulldozer, as well as powering the function motor.

The second task controls the position of the motor that selects which function is being operated. The details are included as comments in the program below.

When the program begins, it resets the function selector to the zero position, corresponding to the blade up and down movement. You can select any function by pressing the A/B/X/Y buttons. This will switch the function selector as follows:

  • X (blue): Moves the blade up and down.
  • B (red): Tilts the blade back and forth.
  • Y (yellow): Moves the ladder up and down.
  • A (green): Moves the ripper up and down.

Once the function is selected, use the bumper buttons to control the motor that powers the selected function.

Use the direction pad to control the tracks. You can change the drive acceleration in the main program to a lower value to make the vehicle start and stop driving more gradually. You can also change the speed values corresponding the left and right motor blocks.

This program lets you drive the LEGO® Technic Cat D11 Bulldozer (42131)
  with the Xbox Controller.
This program lets you drive the LEGO® Technic Cat D11 Bulldozer (42131) with the Xbox Controller.

Running the Pybricks program

This project uses Pybricks on your LEGO hub. Pybricks makes your creations come alive and helps you unlock the full potential of your LEGO Technic, City, MINDSTORMS, BOOST, or Spike sets.

If you haven’t already, install Pybricks on your hub as shown below, or check out our getting started guide for more details. You can go back to the LEGO firmware and apps at any time.

Install the Pybricks firmware.
settings
install

Now import the program you downloaded earlier, as shown below. Click to connect your hub and ▶ to start!

Import a Pybricks Code project.
files
import
open
connect
run

You can run imported block programs even if you’re not signed up. This is a great way to try out Pybricks and see how it works.

Pairing with the Xbox Controller

The very first time, you’ll need to pair the hub with your controller:

  1. Turn the controller on.
  2. Press and hold the pairing button on the back.
  3. Release after a few seconds. The controller light will start flashing more rapidly. This is pairing mode.
  4. Then start your program. Either using the Pybricks app, or using the green button on the hub if you’ve already loaded the program earlier.

The hub will start looking for your controller when the program starts. When the connection is successful, the controller light will stop flashing and stay on for as long as the program is running.

You can see this in the video below.

Connecting without pairing

The next time you use it, pairing is not required. Just turn the controller on. The hub will connect to it automatically when your program runs.

The Xbox controller remembers only one device. If you use the controller with another LEGO hub or your Xbox console, and then with this hub again, you’ll need to pair them again as above.

Updating the Xbox Controller

If you often use the Xbox Controller with your console, it is probably already up to date. If you have not used it for a while or if you bought one recently, you may need to update it.

To update the controller without a console, you can use the Xbox Accessories app on a Windows computer. Connect the controller via USB to the computer and follow the instructions in the app to click on “Update now”. If it is already at firmware version 5.17 or later, you don’t need to do anything.

Technic Hub limitations

Once you start your program, the Technic Hub will disconnect from your computer to free up the connection for the Xbox Controller. This means you can’t use the Pybricks app to stop the program or change the code while the Xbox Controller is connected.

To change your program, stop the program by pressing the green button on the hub. Then you can connect to the hub with the Pybricks app again, and change your program as needed.

Running it as a Python program

You can also run this project as a Python (MicroPython) program. The following code was generated from the block program above. To run it, create a new empty Python program in Pybricks and copy the code into it.

# pybricks blocks file:{"blocks":{"languageVersion":0,"blocks":[{"type":"blockGlobalSetup","id":"bjK,wS1MYO7aiYkFSwd{","x":-28,"y":360,"deletable":false,"next":{"block":{"type":"variables_set_motor","id":"g=O6sK{#.90KNfvnR{3l","fields":{"VAR":{"id":"HB$E(E|0GcK=3:b9OGWK"}},"inputs":{"PORT":{"shadow":{"type":"blockParametersPort","id":"D*][0)m_^se#;]?i?nE~","fields":{"NAME":"A"}}},"POSITIVE_DIRECTION":{"shadow":{"type":"blockParametersDirection","id":"ts6pK}D}12wK?-A7rdcH","fields":{"SELECTION":"Direction.COUNTERCLOCKWISE"}}}},"next":{"block":{"type":"variables_set_motor","id":"$=xhOYJ^ftcYuvA1[:R8","fields":{"VAR":{"id":"WfLWxs!;-+H11Ww!!x:_"}},"inputs":{"PORT":{"shadow":{"type":"blockParametersPort","id":"p!6p@JZi%jsz4X-{UbX)","fields":{"NAME":"B"}}},"POSITIVE_DIRECTION":{"shadow":{"type":"blockParametersDirection","id":"R$^/X.)X.[ug3MtZ.A3H","fields":{"SELECTION":"Direction.CLOCKWISE"}}}},"next":{"block":{"type":"variables_set_motor","id":"$gK`*.B=^D#Sb*GM}@o0","fields":{"VAR":{"id":"9]^QIL#QX=31WQ@/A7^z"}},"inputs":{"PORT":{"shadow":{"type":"blockParametersPort","id":"1=vO3e10f`.G2/~mNPRF","fields":{"NAME":"C"}}},"POSITIVE_DIRECTION":{"shadow":{"type":"blockParametersDirection","id":"Gzy?NnDeNd6;2}DJNrD(","fields":{"SELECTION":"Direction.CLOCKWISE"}}}},"next":{"block":{"type":"variables_set_motor","id":"w7GkS+reB%Y`.G$J%@.1","fields":{"VAR":{"id":"GtM_hctvAF2:Pdx~WV{="}},"inputs":{"PORT":{"shadow":{"type":"blockParametersPort","id":"=eKu%~uk,.EKZG.a;/b9","fields":{"NAME":"D"}}},"POSITIVE_DIRECTION":{"shadow":{"type":"blockParametersDirection","id":"_s)r!Z|Klq*1J=xg(A8S","fields":{"SELECTION":"Direction.CLOCKWISE"}}}},"next":{"block":{"type":"variables_set_xbox_controller","id":":R1kyfUqQqgrq=vV~h5G","fields":{"VAR":{"id":"8]m)Qa4HePdTq@zKBGC;"}},"next":{"block":{"type":"variables_setup_any","id":"=u+{+^9$B/o{(gX73LKB","fields":{"VAR":{"id":"|:C@ETh=EDU-gt}caTMY"}},"inputs":{"VALUE0":{"shadow":{"type":"blockMathNumber","id":"alqXBx7UGnD02B4+i:CV","fields":{"NUM":0}}}},"next":{"block":{"type":"variables_setup_any","id":"x)*dv{l$cZpN6w.H8+Ua","fields":{"VAR":{"id":"K!78++=(@jb1Q@?p`Xo?"}},"inputs":{"VALUE0":{"shadow":{"type":"blockMathNumber","id":"QUA)4]@v52[zMQoT.e#S","fields":{"NUM":0}}}},"next":{"block":{"type":"variables_setup_any","id":"}L7!g8iCRCuM*GbWadh2","fields":{"VAR":{"id":".zD)C%AekgQ,4G0#oX_4"}},"inputs":{"VALUE0":{"shadow":{"type":"blockMathNumber","id":"{S}j0^,I*j+ZPCmO?.lI","fields":{"NUM":1}}}}}}}}}}}}}}}}}}}}},{"type":"blockGlobalStart","id":"3tJe|AWl0baN(wH9a$@.","x":-20,"y":906,"deletable":false,"next":{"block":{"type":"blockComment","id":"V!#pkkro!N]8|i+=(q|]","fields":{"FIELDNAME":"This main task will handle driving and the motor\nthat powers (not switches) the function gearbox."},"next":{"block":{"type":"blockMotorConfigure","id":"o50O)HuKH^!kkl75d.j,","extraState":{"optionLevel":1},"fields":{"METHOD":"MOTOR_ACCELERATION"},"inputs":{"VAR":{"shadow":{"type":"variables_get_motor_device","id":"wg.FC*!eHWx@_(P-%!_!","fields":{"VAR":{"id":"HB$E(E|0GcK=3:b9OGWK","name":"left","type":"Motor"}}}},"ARG0":{"shadow":{"type":"unit_angularAcceleration","id":"lqZ6.[N9bi;1K9V!3-3!","fields":{"VALUE0":2500}}}},"next":{"block":{"type":"blockMotorConfigure","id":"B%R0SaiJaG_e,ia$?e]V","extraState":{"optionLevel":1},"fields":{"METHOD":"MOTOR_ACCELERATION"},"inputs":{"VAR":{"shadow":{"type":"variables_get_motor_device","id":"*Zh+cr_B?RHY={P:tuWR","fields":{"VAR":{"id":"WfLWxs!;-+H11Ww!!x:_","name":"right","type":"Motor"}}}},"ARG0":{"shadow":{"type":"unit_angularAcceleration","id":"=_=?hreur#Ke,]1Hm6[^","fields":{"VALUE0":2500}}}},"next":{"block":{"type":"blockFlowWhile","id":")XPU1ML5!B?wjvZX~iqH","fields":{"MODE":"WHILE"},"inputs":{"BOOL":{"shadow":{"type":"blockLogicTrue","id":"T+fy{AI.b7k3c!ID([DR"}},"DO":{"block":{"type":"blockIfElse","id":"*{FT(LnDLGFQI)|++xFN","extraState":{"optionLevel":1},"inputs":{"IF0":{"shadow":{"type":"blockLogicTrue","id":"?5=:(zTA=mV2WEb4tqk,"},"block":{"type":"blockVariableGetValue","id":"MNqE`t?%;z)^|OiTp[[p","inputs":{"VAR":{"shadow":{"type":"variables_get_any","id":"+R0hiKRkU3-5g,?@Xq|a","fields":{"VAR":{"id":".zD)C%AekgQ,4G0#oX_4","name":"busy switching","type":"Any"}}}}}}},"DO0":{"block":{"type":"blockComment","id":"1fW+qhE-2Gw%/7BSIu6-","fields":{"FIELDNAME":"If we are currently busy switching the function, we stop\ndriving and powering the function motor to be safe."},"next":{"block":{"type":"blockMotorStop","id":"3lAgK6zG1A61l}Jnr,Nr","inputs":{"VAR":{"shadow":{"type":"variables_get_simple_motor_device","id":"(69#%{tW!p~9qg?PC5:F","fields":{"VAR":{"id":"HB$E(E|0GcK=3:b9OGWK","name":"left","type":"Motor"}}}},"VALUE0":{"shadow":{"type":"parameters_stop_3","id":"RQ9DBN-G8=RAaR-]R(q$","fields":{"VALUE":"Stop.COAST"}}}},"next":{"block":{"type":"blockMotorStop","id":"R6A.gBv-$?n,BF2NUZM_","inputs":{"VAR":{"shadow":{"type":"variables_get_simple_motor_device","id":"jzh72]Uw9O=Fy~=2;3C!","fields":{"VAR":{"id":"WfLWxs!;-+H11Ww!!x:_","name":"right","type":"Motor"}}}},"VALUE0":{"shadow":{"type":"parameters_stop_3","id":"pO=WD~gP/Fet[9xjxoiH","fields":{"VALUE":"Stop.COAST"}}}},"next":{"block":{"type":"blockMotorStop","id":"-eQf1=QEExaH{NJ5=I5=","inputs":{"VAR":{"shadow":{"type":"variables_get_simple_motor_device","id":"vf9C{VfDWxhA;znkdrMV","fields":{"VAR":{"id":"9]^QIL#QX=31WQ@/A7^z","name":"function","type":"Motor"}}}},"VALUE0":{"shadow":{"type":"parameters_stop_3","id":"mzQ!7QYnXBh:tSVrcsQ4","fields":{"VALUE":"Stop.COAST"}}}}}}}}}}}},"ELSE":{"block":{"type":"blockComment","id":"X+5]`FAANoTE^^|)bdoV","fields":{"FIELDNAME":"Otherwise drive the motors based on the buttons that are pressed."},"next":{"block":{"type":"blockComment","id":"5Bh-a6qu?]*v2l`Wn4PA","fields":{"FIELDNAME":"Use the bumpers for the function motor."},"next":{"block":{"type":"blockIfElse","id":"m[7!gY%:0sA;wz;L`f[f","extraState":{"optionLevel":2},"inputs":{"IF0":{"shadow":{"type":"blockLogicTrue","id":"qPOUxH-_+tQqHLYIeiun"},"block":{"type":"blockButtonIsPressed","id":"x@xXHgbD(0dtC3xF129j","inputs":{"VAR":{"shadow":{"type":"variables_get_keypad","id":"^iw}wvDkvd{lz1towzgD","fields":{"VAR":{"id":"8]m)Qa4HePdTq@zKBGC;","name":"controller","type":"XboxController"}}}},"VALUE0":{"shadow":{"type":"blockParametersButton","id":"[f0CKh4zgq-GT]%][72J","fields":{"VALUE":"RB"}}}}}},"DO0":{"block":{"type":"blockMotorDuty","id":"c!Q.B:wSVsExH+9_yX*y","inputs":{"VAR":{"shadow":{"type":"variables_get_simple_motor_device","id":"drl2O=5U/S`rNET3fSnl","fields":{"VAR":{"id":"9]^QIL#QX=31WQ@/A7^z","name":"function","type":"Motor"}}}},"VALUE0":{"shadow":{"type":"unit_percent","id":"^[KNd7cw+ypX2JJ09ur0","fields":{"VALUE0":100}}}}}},"IF1":{"shadow":{"type":"blockLogicTrue","id":"KkiFi.!:PF,jzHmdfMf4"},"block":{"type":"blockButtonIsPressed","id":"dE5}jm,^cp@wb2_(_9`k","inputs":{"VAR":{"shadow":{"type":"variables_get_keypad","id":"=(hb$G2K}i?`nimF/eYV","fields":{"VAR":{"id":"8]m)Qa4HePdTq@zKBGC;","name":"controller","type":"XboxController"}}}},"VALUE0":{"shadow":{"type":"blockParametersButton","id":"AYi.q]%In#9B|a1QD7vj","fields":{"VALUE":"LB"}}}}}},"DO1":{"block":{"type":"blockMotorDuty","id":"Z.7z^w1G.4M;Rf`@.T@t","inputs":{"VAR":{"shadow":{"type":"variables_get_simple_motor_device","id":"ZlJeTOG0rEunqM5R-b%m","fields":{"VAR":{"id":"9]^QIL#QX=31WQ@/A7^z","name":"function","type":"Motor"}}}},"VALUE0":{"shadow":{"type":"unit_percent","id":"E[=ozMStgHRJNa?(6awG","fields":{"VALUE0":-100}}}}}},"ELSE":{"block":{"type":"blockMotorStop","id":"}%m%5g$ux7bK0drb=NK]","inputs":{"VAR":{"shadow":{"type":"variables_get_simple_motor_device","id":"4x)i@*nv{k.8hbuVHd+O","fields":{"VAR":{"id":"9]^QIL#QX=31WQ@/A7^z","name":"function","type":"Motor"}}}},"VALUE0":{"shadow":{"type":"parameters_stop_3","id":"q=S.ws:}$|ZgLT*A`mI7","fields":{"VALUE":"Stop.COAST"}}}}}}},"next":{"block":{"type":"blockComment","id":"6m$C^]{b24:Ud)52nNk4","fields":{"FIELDNAME":"Use the direction pad for driving."},"next":{"block":{"type":"blockIfElse","id":"2=s4}Pz_KxunlN{f9xtC","extraState":{"optionLevel":8},"inputs":{"IF0":{"shadow":{"type":"blockLogicTrue","id":"y/#h3]*N!XS?n-{hVzS{"},"block":{"type":"blockLogicCompare","id":"qE@9E?)gqZn-k_?EMR${","fields":{"OP1":"EQ"},"inputs":{"A":{"shadow":{"type":"blockMathNumber","id":"gB,WHMN!E4?#)ohkct7Z","fields":{"NUM":3}},"block":{"type":"blockJoystickValue","id":"BQ.[Z^idjd^ru*i}3f,c","fields":{"JOYSTICK":"XBOX_DPAD"},"inputs":{"VAR":{"shadow":{"type":"variables_get_gamepad","id":";!T#RZP4oj#VJ%,#}k_*","fields":{"VAR":{"id":"8]m)Qa4HePdTq@zKBGC;","name":"controller","type":"XboxController"}}}}}}},"B":{"shadow":{"type":"blockMathNumber","id":"[qGX|K+npB]MY=*X9UoA","fields":{"NUM":1}}}}}},"DO0":{"block":{"type":"blockComment","id":"3shJ.LQ_5t~+J_(B(kbz","fields":{"FIELDNAME":"Forward"},"next":{"block":{"type":"blockMotorRun","id":"o?h|jBpWa(+0%;-$p42y","extraState":{"optionLevel":0},"fields":{"METHOD":"MOTOR_RUN_FOREVER"},"inputs":{"VAR":{"shadow":{"type":"variables_get_motor_device","id":"(wesG#=HS=:aKk?!}sJ9","fields":{"VAR":{"id":"HB$E(E|0GcK=3:b9OGWK","name":"left","type":"Motor"}}}},"VALUE0":{"shadow":{"type":"unit_angularVelocity","id":"YLv^6}SMC(hGYh.*Lgqh","fields":{"VALUE0":1000}}}},"next":{"block":{"type":"blockMotorRun","id":"NviPF$.d`jW-_Y%E.tS/","extraState":{"optionLevel":0},"fields":{"METHOD":"MOTOR_RUN_FOREVER"},"inputs":{"VAR":{"shadow":{"type":"variables_get_motor_device","id":"Xq8T{?Rx}YAvUxR:E})P","fields":{"VAR":{"id":"WfLWxs!;-+H11Ww!!x:_","name":"right","type":"Motor"}}}},"VALUE0":{"shadow":{"type":"unit_angularVelocity","id":"kpwY|]f/+kJW:Xh0$,6u","fields":{"VALUE0":1000}}}}}}}}}},"IF1":{"shadow":{"type":"blockLogicTrue","id":"uhOlyOlEF{Q!O$wJD)ax"},"block":{"type":"blockLogicCompare","id":"IBzsF9wN0P~md)yfb]BB","fields":{"OP1":"EQ"},"inputs":{"A":{"shadow":{"type":"blockMathNumber","id":"gB,WHMN!E4?#)ohkct7Z","fields":{"NUM":3}},"block":{"type":"blockJoystickValue","id":"FNn|u9!czeiS,hT)N)Q[","fields":{"JOYSTICK":"XBOX_DPAD"},"inputs":{"VAR":{"shadow":{"type":"variables_get_gamepad","id":"v*Oib+.$U%!a+O-jLa4!","fields":{"VAR":{"id":"8]m)Qa4HePdTq@zKBGC;","name":"controller","type":"XboxController"}}}}}}},"B":{"shadow":{"type":"blockMathNumber","id":"|0!*Koi~,+uV_C=?2JB.","fields":{"NUM":2}}}}}},"DO1":{"block":{"type":"blockComment","id":"_BJs:PPr}^x{=Uq=@E-B","fields":{"FIELDNAME":"Forward / Right"},"next":{"block":{"type":"blockMotorStop","id":"D_(Y9qIvdZ2+LKXYv`,@","inputs":{"VAR":{"shadow":{"type":"variables_get_simple_motor_device","id":"X)XtD+M$:D2l/sldMWmf","fields":{"VAR":{"id":"WfLWxs!;-+H11Ww!!x:_","name":"right","type":"Motor"}}}},"VALUE0":{"shadow":{"type":"parameters_stop_3","id":"N)kmtKR5h+MI_EueUP#1","fields":{"VALUE":"Stop.COAST"}}}},"next":{"block":{"type":"blockMotorRun","id":"p5M^#^m(@Byi!+aQ(fiD","extraState":{"optionLevel":0},"fields":{"METHOD":"MOTOR_RUN_FOREVER"},"inputs":{"VAR":{"shadow":{"type":"variables_get_motor_device","id":"qH:`2VbQD@WPcUVXSyGo","fields":{"VAR":{"id":"HB$E(E|0GcK=3:b9OGWK","name":"left","type":"Motor"}}}},"VALUE0":{"shadow":{"type":"unit_angularVelocity","id":"bka+LksiLYX_d|x~o^F1","fields":{"VALUE0":1000}}}}}}}}}},"IF2":{"shadow":{"type":"blockLogicTrue","id":"4sr@p#Y)Xw_=SxAPX6K#"},"block":{"type":"blockLogicCompare","id":"NaI0):56t[XuX[AI`OC-","fields":{"OP1":"EQ"},"inputs":{"A":{"shadow":{"type":"blockMathNumber","id":"gB,WHMN!E4?#)ohkct7Z","fields":{"NUM":3}},"block":{"type":"blockJoystickValue","id":"1=|fm`aZN:C6$do5h6O@","fields":{"JOYSTICK":"XBOX_DPAD"},"inputs":{"VAR":{"shadow":{"type":"variables_get_gamepad","id":"Y_=H3{yYaUsiEH.VM#^F","fields":{"VAR":{"id":"8]m)Qa4HePdTq@zKBGC;","name":"controller","type":"XboxController"}}}}}}},"B":{"shadow":{"type":"blockMathNumber","id":"E+Rm`?-CiuNrwS).~OE3","fields":{"NUM":3}}}}}},"DO2":{"block":{"type":"blockComment","id":"OJB/RN!`{*aTVO:FgB1u","fields":{"FIELDNAME":"Right"},"next":{"block":{"type":"blockMotorRun","id":"!b{?Em:QKjTydmFMern-","extraState":{"optionLevel":0},"fields":{"METHOD":"MOTOR_RUN_FOREVER"},"inputs":{"VAR":{"shadow":{"type":"variables_get_motor_device","id":"hrN7$q+QWv@(m^;Z!gH%","fields":{"VAR":{"id":"HB$E(E|0GcK=3:b9OGWK","name":"left","type":"Motor"}}}},"VALUE0":{"shadow":{"type":"unit_angularVelocity","id":"x~[}O1KyscF.#;eUp~lJ","fields":{"VALUE0":1000}}}},"next":{"block":{"type":"blockMotorRun","id":"@|j!z9#z)$@fmy2GIgwB","extraState":{"optionLevel":0},"fields":{"METHOD":"MOTOR_RUN_FOREVER"},"inputs":{"VAR":{"shadow":{"type":"variables_get_motor_device","id":"yC+k;d3?,(/SDS`RB29v","fields":{"VAR":{"id":"WfLWxs!;-+H11Ww!!x:_","name":"right","type":"Motor"}}}},"VALUE0":{"shadow":{"type":"unit_angularVelocity","id":"%Z/2h(nyMihWMW9*2x.b","fields":{"VALUE0":-1000}}}}}}}}}},"IF3":{"shadow":{"type":"blockLogicTrue","id":"/4|75J;fU:{kyb3at5C`"},"block":{"type":"blockLogicCompare","id":"%$BC!.]3s#AQ0I(bhWG#","fields":{"OP1":"EQ"},"inputs":{"A":{"shadow":{"type":"blockMathNumber","id":"gB,WHMN!E4?#)ohkct7Z","fields":{"NUM":3}},"block":{"type":"blockJoystickValue","id":"?0HtL5gPgW-A#35~BK5#","fields":{"JOYSTICK":"XBOX_DPAD"},"inputs":{"VAR":{"shadow":{"type":"variables_get_gamepad","id":"@.t{o0X?Jb+v!pk/+m7h","fields":{"VAR":{"id":"8]m)Qa4HePdTq@zKBGC;","name":"controller","type":"XboxController"}}}}}}},"B":{"shadow":{"type":"blockMathNumber","id":"8y8ujCgtU[i+atVXVw,,","fields":{"NUM":4}}}}}},"DO3":{"block":{"type":"blockComment","id":"e#LB9Eo`qEo!6kPC:%3^","fields":{"FIELDNAME":"Reverse / Right"},"next":{"block":{"type":"blockMotorRun","id":"HmLBG:)KPKH?;v^f-q`(","extraState":{"optionLevel":0},"fields":{"METHOD":"MOTOR_RUN_FOREVER"},"inputs":{"VAR":{"shadow":{"type":"variables_get_motor_device","id":"X3`8+;HNvrVlF*TJf@ZX","fields":{"VAR":{"id":"WfLWxs!;-+H11Ww!!x:_","name":"right","type":"Motor"}}}},"VALUE0":{"shadow":{"type":"unit_angularVelocity","id":"M8;nyNQ4}-9=q@^QTXlk","fields":{"VALUE0":-1000}}}},"next":{"block":{"type":"blockMotorStop","id":"ZHcge4CC;AdNn[ZBd_n|","inputs":{"VAR":{"shadow":{"type":"variables_get_simple_motor_device","id":"7CebA~V^8Y.`l,djZehp","fields":{"VAR":{"id":"HB$E(E|0GcK=3:b9OGWK","name":"left","type":"Motor"}}}},"VALUE0":{"shadow":{"type":"parameters_stop_3","id":"Q@W;fx34AZr_FdVIAr{e","fields":{"VALUE":"Stop.COAST"}}}}}}}}}},"IF4":{"shadow":{"type":"blockLogicTrue","id":"Z|D^Z6[G%=yW1lcwfY8;"},"block":{"type":"blockLogicCompare","id":"m)#;%e*G4lg(P2Ah#4R5","fields":{"OP1":"EQ"},"inputs":{"A":{"shadow":{"type":"blockMathNumber","id":"gB,WHMN!E4?#)ohkct7Z","fields":{"NUM":3}},"block":{"type":"blockJoystickValue","id":"F(`T%ZF/1ZnkyZskOjL!","fields":{"JOYSTICK":"XBOX_DPAD"},"inputs":{"VAR":{"shadow":{"type":"variables_get_gamepad","id":"/nLZzux`.#H*|L;C#1g?","fields":{"VAR":{"id":"8]m)Qa4HePdTq@zKBGC;","name":"controller","type":"XboxController"}}}}}}},"B":{"shadow":{"type":"blockMathNumber","id":",/F[[ug$9K(9J*1~$bl.","fields":{"NUM":5}}}}}},"DO4":{"block":{"type":"blockComment","id":"md9OrXdY:)cgpE[H;RAz","fields":{"FIELDNAME":"Reverse"},"next":{"block":{"type":"blockMotorRun","id":"c?X{*Bz;[Bwy)R2UC5yp","extraState":{"optionLevel":0},"fields":{"METHOD":"MOTOR_RUN_FOREVER"},"inputs":{"VAR":{"shadow":{"type":"variables_get_motor_device","id":"D[!]Tt[eC$8-sd!A*.bR","fields":{"VAR":{"id":"HB$E(E|0GcK=3:b9OGWK","name":"left","type":"Motor"}}}},"VALUE0":{"shadow":{"type":"unit_angularVelocity","id":"^,5W@IB[tbu+D5Z#OY;.","fields":{"VALUE0":-1000}}}},"next":{"block":{"type":"blockMotorRun","id":"mf1[yg|G_.LPWa%YuwRU","extraState":{"optionLevel":0},"fields":{"METHOD":"MOTOR_RUN_FOREVER"},"inputs":{"VAR":{"shadow":{"type":"variables_get_motor_device","id":"TzCb`eX@t!t2CO7xAT7e","fields":{"VAR":{"id":"WfLWxs!;-+H11Ww!!x:_","name":"right","type":"Motor"}}}},"VALUE0":{"shadow":{"type":"unit_angularVelocity","id":"3N~]av!iF26SbavUIld`","fields":{"VALUE0":-1000}}}}}}}}}},"IF5":{"shadow":{"type":"blockLogicTrue","id":"9D:;o|brMvr*:{;KbGRx"},"block":{"type":"blockLogicCompare","id":"}LQ#4gB=e42sa_9{$i5s","fields":{"OP1":"EQ"},"inputs":{"A":{"shadow":{"type":"blockMathNumber","id":"gB,WHMN!E4?#)ohkct7Z","fields":{"NUM":3}},"block":{"type":"blockJoystickValue","id":"[~..N`3[B+f?R@g)kd;q","fields":{"JOYSTICK":"XBOX_DPAD"},"inputs":{"VAR":{"shadow":{"type":"variables_get_gamepad","id":"Q]W@nPa4)gUC{v4K0m0,","fields":{"VAR":{"id":"8]m)Qa4HePdTq@zKBGC;","name":"controller","type":"XboxController"}}}}}}},"B":{"shadow":{"type":"blockMathNumber","id":"x;:eHZCPMd}Hfjk4fiF5","fields":{"NUM":6}}}}}},"DO5":{"block":{"type":"blockComment","id":"(JUnKa0WLPCI#TJz?U,p","fields":{"FIELDNAME":"Reverse / Left"},"next":{"block":{"type":"blockMotorRun","id":"%g;Key[11ncE3D.LGtS#","extraState":{"optionLevel":0},"fields":{"METHOD":"MOTOR_RUN_FOREVER"},"inputs":{"VAR":{"shadow":{"type":"variables_get_motor_device","id":"0PQ@*NZnI{z}},"VALUE0":{"shadow":{"type":"unit_angularVelocity","id":"Q)51:CfdC$A]B%;0b/(p","fields":{"VALUE0":-1000}}}},"next":{"block":{"type":"blockMotorStop","id":"DO{)7ThdC6u@)hF*?Y*n","inputs":{"VAR":{"shadow":{"type":"variables_get_simple_motor_device","id":"uvteJ;]?T4:7E#]wE8jX","fields":{"VAR":{"id":"WfLWxs!;-+H11Ww!!x:_","name":"right","type":"Motor"}}}},"VALUE0":{"shadow":{"type":"parameters_stop_3","id":"@pRLG@o(_|{Ek()*XDC7","fields":{"VALUE":"Stop.COAST"}}}}}}}}}},"IF6":{"shadow":{"type":"blockLogicTrue","id":"q95g!6$~As/d^9H7=t5L"},"block":{"type":"blockLogicCompare","id":"!{4]QbE2RL4ctFdC/AC;","fields":{"OP1":"EQ"},"inputs":{"A":{"shadow":{"type":"blockMathNumber","id":"gB,WHMN!E4?#)ohkct7Z","fields":{"NUM":3}},"block":{"type":"blockJoystickValue","id":"n$]e$1[h)(P)mH}Jct76","fields":{"JOYSTICK":"XBOX_DPAD"},"inputs":{"VAR":{"shadow":{"type":"variables_get_gamepad","id":"|2:O|eaty-`3}i@$LF[N","fields":{"VAR":{"id":"8]m)Qa4HePdTq@zKBGC;","name":"controller","type":"XboxController"}}}}}}},"B":{"shadow":{"type":"blockMathNumber","id":"5$GUln7oSKeg9Fa;jwFD","fields":{"NUM":7}}}}}},"DO6":{"block":{"type":"blockComment","id":"t{k/x1.27WF(ts{C-:vE","fields":{"FIELDNAME":"Left"},"next":{"block":{"type":"blockMotorRun","id":"{cR=2JnwN:Gze4jAE$^.","extraState":{"optionLevel":0},"fields":{"METHOD":"MOTOR_RUN_FOREVER"},"inputs":{"VAR":{"shadow":{"type":"variables_get_motor_device","id":"F5t[EU!Q]7R7ke/g)an~","fields":{"VAR":{"id":"HB$E(E|0GcK=3:b9OGWK","name":"left","type":"Motor"}}}},"VALUE0":{"shadow":{"type":"unit_angularVelocity","id":"oGhh@ZJCkpXw]E:B1E0:","fields":{"VALUE0":-1000}}}},"next":{"block":{"type":"blockMotorRun","id":"OhL{DZ-3-=iDE0~7b:4G","extraState":{"optionLevel":0},"fields":{"METHOD":"MOTOR_RUN_FOREVER"},"inputs":{"VAR":{"shadow":{"type":"variables_get_motor_device","id":"79zn(}]H7`}0ISy-)H{C","fields":{"VAR":{"id":"WfLWxs!;-+H11Ww!!x:_","name":"right","type":"Motor"}}}},"VALUE0":{"shadow":{"type":"unit_angularVelocity","id":"Csp@B;2bD}saRw$D/Q+Q","fields":{"VALUE0":1000}}}}}}}}}},"IF7":{"shadow":{"type":"blockLogicTrue","id":"T.RZ_F+D-N53vC1KvTqh"},"block":{"type":"blockLogicCompare","id":"q~_(C!dAjD*xjht5]4uA","fields":{"OP1":"EQ"},"inputs":{"A":{"shadow":{"type":"blockMathNumber","id":"gB,WHMN!E4?#)ohkct7Z","fields":{"NUM":3}},"block":{"type":"blockJoystickValue","id":"%[IMLmtMKdMW1Moi[,HL","fields":{"JOYSTICK":"XBOX_DPAD"},"inputs":{"VAR":{"shadow":{"type":"variables_get_gamepad","id":"tEF;ukV*LoC#x?,.dn+E","fields":{"VAR":{"id":"8]m)Qa4HePdTq@zKBGC;","name":"controller","type":"XboxController"}}}}}}},"B":{"shadow":{"type":"blockMathNumber","id":"@gG7lBmI*D:Di^Sf^1@F","fields":{"NUM":8}}}}}},"DO7":{"block":{"type":"blockComment","id":"J4VtlK);J;5GN:r9!(Y$","fields":{"FIELDNAME":"Forward / Left"},"next":{"block":{"type":"blockMotorRun","id":"`7ca}uS;U-dzlWzG%T9{","extraState":{"optionLevel":0},"fields":{"METHOD":"MOTOR_RUN_FOREVER"},"inputs":{"VAR":{"shadow":{"type":"variables_get_motor_device","id":")gy~C4l~TB_,t0?-Z1|S","fields":{"VAR":{"id":"WfLWxs!;-+H11Ww!!x:_","name":"right","type":"Motor"}}}},"VALUE0":{"shadow":{"type":"unit_angularVelocity","id":",qB@$I{r0Pvdlo86NWtr","fields":{"VALUE0":1000}}}},"next":{"block":{"type":"blockMotorStop","id":"20b|[w^iG][04qa~R*8%","inputs":{"VAR":{"shadow":{"type":"variables_get_simple_motor_device","id":"*8/:[o3C5ol,MQkDUta9","fields":{"VAR":{"id":"HB$E(E|0GcK=3:b9OGWK","name":"left","type":"Motor"}}}},"VALUE0":{"shadow":{"type":"parameters_stop_3","id":"NOoQ]TZ2dSg4ox~xVDM@","fields":{"VALUE":"Stop.COAST"}}}}}}}}}},"ELSE":{"block":{"type":"blockComment","id":"E,pV{m[,ROPR,m*`8OwD","fields":{"FIELDNAME":"Nothing, so stop."},"next":{"block":{"type":"blockMotorStop","id":"V^HG^}~:2;7wx$KQP%AA","inputs":{"VAR":{"shadow":{"type":"variables_get_simple_motor_device","id":"(w.,9VA)A2I5@-wg{a!t","fields":{"VAR":{"id":"HB$E(E|0GcK=3:b9OGWK","name":"left","type":"Motor"}}}},"VALUE0":{"shadow":{"type":"parameters_stop_3","id":"1pqq}#RZ9717]J0D4AI~","fields":{"VALUE":"Stop.COAST"}}}},"next":{"block":{"type":"blockMotorStop","id":"kkD}:g6sRjPe!PXpNTYe","inputs":{"VAR":{"shadow":{"type":"variables_get_simple_motor_device","id":"j}H[+O8UlBZ6JmIBuyS`","fields":{"VAR":{"id":"WfLWxs!;-+H11Ww!!x:_","name":"right","type":"Motor"}}}},"VALUE0":{"shadow":{"type":"parameters_stop_3","id":"]SBUV9,.)y=`K:=+5yWi","fields":{"VALUE":"Stop.COAST"}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}},{"type":"blockGlobalStart","id":"oNK8Crph);pPl`9WLjn{","x":770,"y":899,"next":{"block":{"type":"blockComment","id":"|zg2NR+_I[B*`9}Z/+0G","fields":{"FIELDNAME":"This task will handle the motor that switches the function gearbox."},"next":{"block":{"type":"blockComment","id":",G:DEc1w_F8x;K3M_d_w","fields":{"FIELDNAME":"First it resets the gearbox by finding the start and end stops."},"next":{"block":{"type":"blockMotorRun","id":"t$dM_~8A`j%wi}:d(wdx","extraState":{"optionLevel":3},"fields":{"METHOD":"MOTOR_RUN_UNTIL_STALLED"},"inputs":{"VAR":{"shadow":{"type":"variables_get_motor_device","id":"zr0myoV/UC|3Ox?PhHTI","fields":{"VAR":{"id":"GtM_hctvAF2:Pdx~WV{=","name":"switch","type":"Motor"}}}},"VALUE0":{"shadow":{"type":"unit_angularVelocity","id":"Hc#2x,oNj9gSX(.LWcP4","fields":{"VALUE0":500}}},"LIMIT":{"shadow":{"type":"unit_percent","id":"xSFx/t}6AoN7cCi$.(uH","fields":{"VALUE0":50}}},"THEN":{"shadow":{"type":"parameters_stop_3","id":"!+E^__r9oM}?s|:~br}]","fields":{"VALUE":"Stop.COAST"}}}},"next":{"block":{"type":"blockVariableSetValue","id":"Jp(0X1iCMCe+;0-!]$zY","inputs":{"VAR":{"shadow":{"type":"variables_get_any","id":"Y/EPWY-2AM]]bq=xHe+;","fields":{"VAR":{"id":"K!78++=(@jb1Q@?p`Xo?","name":"right end","type":"Any"}}}},"VALUE0":{"shadow":{"type":"blockMathNumber","id":"SOO%OB9yxJwpUPK8=*ZW","fields":{"NUM":0}},"block":{"type":"blockMotorMeasure","id":"hB:x:x1k$@Tdiio`eX;h","extraState":{"optionLevel":1},"fields":{"METHOD":"MOTOR_ANGLE"},"inputs":{"VAR":{"shadow":{"type":"variables_get_motor_device","id":"X5J{KgIiblrTN:)E?{q1","fields":{"VAR":{"id":"GtM_hctvAF2:Pdx~WV{=","name":"switch","type":"Motor"}}}}}}}},"next":{"block":{"type":"blockMotorRun","id":"iCZ~|_Epo?z5Il(~=AzL","extraState":{"optionLevel":3},"fields":{"METHOD":"MOTOR_RUN_UNTIL_STALLED"},"inputs":{"VAR":{"shadow":{"type":"variables_get_motor_device","id":"s.UUkzq3]lG^T~-eZdOR","fields":{"VAR":{"id":"GtM_hctvAF2:Pdx~WV{=","name":"switch","type":"Motor"}}}},"VALUE0":{"shadow":{"type":"unit_angularVelocity","id":"[5nu6/Mw-3a:vPh?P8jh","fields":{"VALUE0":-500}}},"LIMIT":{"shadow":{"type":"unit_percent","id":"8F5:gU1++[~QQjpx;t}0","fields":{"VALUE0":50}}},"THEN":{"shadow":{"type":"parameters_stop_3","id":"|nfXP%Lx8#;vK]DGRb7j","fields":{"VALUE":"Stop.COAST"}}}},"next":{"block":{"type":"blockVariableSetValue","id":"s]_W*`}3*o[OXUg*-9t3","inputs":{"VAR":{"shadow":{"type":"variables_get_any","id":"vxp2J,QoWz[ZI$4{Fs_N","fields":{"VAR":{"id":"|:C@ETh=EDU-gt}caTMY","name":"left end","type":"Any"}}}},"VALUE0":{"shadow":{"type":"blockMathNumber","id":"SOO%OB9yxJwpUPK8=*ZW","fields":{"NUM":0}},"block":{"type":"blockMotorMeasure","id":"4P[S=$omwW]w8k).!FAO","extraState":{"optionLevel":1},"fields":{"METHOD":"MOTOR_ANGLE"},"inputs":{"VAR":{"shadow":{"type":"variables_get_motor_device","id":"|R6!~jPxY{?X.IM1!4CX","fields":{"VAR":{"id":"GtM_hctvAF2:Pdx~WV{=","name":"switch","type":"Motor"}}}}}}}},"next":{"block":{"type":"blockMotorResetAngle","id":"C.K[AnOtb_Tp/;1bdwK6","inputs":{"VAR":{"shadow":{"type":"variables_get_motor_device","id":"y4[%it5ul67jO+H}Ez0H","fields":{"VAR":{"id":"GtM_hctvAF2:Pdx~WV{=","name":"switch","type":"Motor"}}}},"VALUE0":{"shadow":{"type":"unit_angle","id":"N#T([W*,$e7`??QOWFBw","fields":{"VALUE0":0}},"block":{"type":"blockMathFormula","id":"+]dBXOn*_)KCG9u[H`8(","extraState":{"optionLevel":2},"fields":{"FORMULA":"(270 + a - b) / 2"},"inputs":{"a":{"shadow":{"type":"blockMathNumber","id":"XOlE}?1+]HF`B{g4Gk?n","fields":{"NUM":0}},"block":{"type":"blockVariableGetValue","id":"m7ue,%}[Sbv:*/UT$+BJ","inputs":{"VAR":{"shadow":{"type":"variables_get_any","id":"7/6ePKuf;hP;IfCs74@w","fields":{"VAR":{"id":"|:C@ETh=EDU-gt}caTMY","name":"left end","type":"Any"}}}}}}},"b":{"shadow":{"type":"blockMathNumber","id":"^6i|R#:xv21#F{;i.n$X","fields":{"NUM":0}},"block":{"type":"blockVariableGetValue","id":"C}j=SM#7kRSO}FV^qN?Z","inputs":{"VAR":{"shadow":{"type":"variables_get_any","id":"kqqgdu0PR^U0CD6c!-Ls","fields":{"VAR":{"id":"K!78++=(@jb1Q@?p`Xo?","name":"right end","type":"Any"}}}}}}}}}}},"next":{"block":{"type":"blockFunctionCallerStatement","id":"0uv5e#XBt6j9Z7$^;6Sb","extraState":{"optionLevel":1},"fields":{"ICON":"TASK","VAR":{"id":"SfQO0qSRH4KT9-W!daxW"}},"inputs":{"ARG0":{"shadow":{"type":"blockMathNumber","id":"!:R-:.llzJ]rRXVRKQ@C","fields":{"NUM":0}}}},"next":{"block":{"type":"blockComment","id":"~vYo#}-ALMsA6sJuDDOq","fields":{"FIELDNAME":"Now we run the main loop of operating the switch."},"next":{"block":{"type":"blockFlowWhile","id":"NZ4Lr./POLZ{z8HQo*qY","fields":{"MODE":"WHILE"},"inputs":{"BOOL":{"shadow":{"type":"blockLogicTrue","id":"+x%C2YgSnI,n|EbGy5_x"}},"DO":{"block":{"type":"blockComment","id":"IUfw=YrE}`{A@5PO57Jj","fields":{"FIELDNAME":"Wait for the center switch to be pressed."},"next":{"block":{"type":"blockWaitUntil","id":"F^iu^]QmQ7OKMJl{;cAH","inputs":{"BOOL":{"shadow":{"type":"blockLogicTrue","id":"@XiqFv|*D:/H$+s-#Uv`"},"block":{"type":"blockButtonIsPressed","id":"49Y};Qsb@`y/4V,EBjI8","inputs":{"VAR":{"shadow":{"type":"variables_get_keypad","id":"{!.^$H*ej-we.opIgJ8t","fields":{"VAR":{"id":"8]m)Qa4HePdTq@zKBGC;","name":"controller","type":"XboxController"}}}},"VALUE0":{"shadow":{"type":"blockParametersButton","id":"14#/*]lgP#K3|)BSOY-y","fields":{"VALUE":"BUTTON_ANY"}}}}}}},"next":{"block":{"type":"blockComment","id":"FT52M~86uc9v]0NsddtH","fields":{"FIELDNAME":"Then select the function based on which other button is pressed."},"next":{"block":{"type":"blockIfElse","id":".]7+Qn3cxwS/QkIu#-T]","extraState":{"optionLevel":4},"inputs":{"IF0":{"shadow":{"type":"blockLogicTrue","id":"{g9iEDm_bF;zVqny*GW6"},"block":{"type":"blockButtonIsPressed","id":"l=/)4{[uwZH2%A[w/+ZO","inputs":{"VAR":{"shadow":{"type":"variables_get_keypad","id":"mj$NmV]QsZ?oM=t$v_p=","fields":{"VAR":{"id":"8]m)Qa4HePdTq@zKBGC;","name":"controller","type":"XboxController"}}}},"VALUE0":{"shadow":{"type":"blockParametersButton","id":"~x{]+H`0f,{_J1Q8Ia)W","fields":{"VALUE":"X"}}}}}},"DO0":{"block":{"type":"blockComment","id":"sP){gavCsEKiW{qN|B?{","fields":{"FIELDNAME":"Blade up/down function."},"next":{"block":{"type":"blockFunctionCallerStatement","id":":AEVvt#Roy/n499^+QCF","extraState":{"optionLevel":1},"fields":{"ICON":"TASK","VAR":{"id":"SfQO0qSRH4KT9-W!daxW"}},"inputs":{"ARG0":{"shadow":{"type":"blockMathNumber","id":"3(pk!2@:wX0f~9ssS}wd","fields":{"NUM":0}}}},"next":{"block":{"type":"blockWaitUntil","id":"y`bBwpNR0=f?DKFU$8e}","inputs":{"BOOL":{"shadow":{"type":"blockLogicTrue","id":"K.~|8]y3r)hIvOKB:;pY"},"block":{"type":"blockLogicIsNone","id":"]Rv?K}%b+V#^qjm/1,e8","fields":{"OP":"NOT"},"inputs":{"BOOL":{"shadow":{"type":"blockLogicTrue","id":"Njl=W[giy{M)i:$U%yG/"},"block":{"type":"blockButtonIsPressed","id":"vqm-3}mTuo.jj,f!p/XD","inputs":{"VAR":{"shadow":{"type":"variables_get_keypad","id":"f%L9[S8D^`3T7c/^w6KL","fields":{"VAR":{"id":"8]m)Qa4HePdTq@zKBGC;","name":"controller","type":"XboxController"}}}},"VALUE0":{"shadow":{"type":"blockParametersButton","id":"z-3itP6{W|4u~MVJyBxj","fields":{"VALUE":"X"}}}}}}}}}}}}}}}},"IF1":{"shadow":{"type":"blockLogicTrue","id":"#~5:*UcEsuTC0D*c%dd6"},"block":{"type":"blockButtonIsPressed","id":"E9xdjAy[J}5R)C@}YX6Z","inputs":{"VAR":{"shadow":{"type":"variables_get_keypad","id":";0%{l?lh7@-WHzhPTC-_","fields":{"VAR":{"id":"8]m)Qa4HePdTq@zKBGC;","name":"controller","type":"XboxController"}}}},"VALUE0":{"shadow":{"type":"blockParametersButton","id":"FN_DpK|0IxtaIdn,Jg4T","fields":{"VALUE":"A"}}}}}},"DO1":{"block":{"type":"blockComment","id":"1b8qs]0r)/TGEn0L.+KK","fields":{"FIELDNAME":"Ripper function."},"next":{"block":{"type":"blockFunctionCallerStatement","id":"P15n]AnF[dnMA@3fMzoe","extraState":{"optionLevel":1},"fields":{"ICON":"TASK","VAR":{"id":"SfQO0qSRH4KT9-W!daxW"}},"inputs":{"ARG0":{"shadow":{"type":"blockMathNumber","id":"uyBATBdT0}8(Th,o.2:E","fields":{"NUM":1}}}},"next":{"block":{"type":"blockWaitUntil","id":"0}!{w[amC@0rWs}C9J_n","inputs":{"BOOL":{"shadow":{"type":"blockLogicTrue","id":"K.~|8]y3r)hIvOKB:;pY"},"block":{"type":"blockLogicIsNone","id":"]Ei?RZ~hk/fmi`??2hD]","fields":{"OP":"NOT"},"inputs":{"BOOL":{"shadow":{"type":"blockLogicTrue","id":"Njl=W[giy{M)i:$U%yG/"},"block":{"type":"blockButtonIsPressed","id":"lA@4AvyE7Noc/$U|n9t-","inputs":{"VAR":{"shadow":{"type":"variables_get_keypad","id":"0q=fmkO]?r7~Iza[AqD8","fields":{"VAR":{"id":"8]m)Qa4HePdTq@zKBGC;","name":"controller","type":"XboxController"}}}},"VALUE0":{"shadow":{"type":"blockParametersButton","id":"wy2($|V|8.c.PE,v4Ii=","fields":{"VALUE":"A"}}}}}}}}}}}}}}}},"IF2":{"shadow":{"type":"blockLogicTrue","id":":yqK;Ko)F,UB7{UZ=XX/"},"block":{"type":"blockButtonIsPressed","id":"w?,11X_~YWe2r1sEiJ`.","inputs":{"VAR":{"shadow":{"type":"variables_get_keypad","id":"}^8LY1h:6ojvo/I6O~4+","fields":{"VAR":{"id":"8]m)Qa4HePdTq@zKBGC;","name":"controller","type":"XboxController"}}}},"VALUE0":{"shadow":{"type":"blockParametersButton","id":".pZbEmK(jCqIp*y@yN9j","fields":{"VALUE":"Y"}}}}}},"DO2":{"block":{"type":"blockComment","id":";Ue1jhh-P$}5@;mk`SPG","fields":{"FIELDNAME":"Ladder function."},"next":{"block":{"type":"blockFunctionCallerStatement","id":"wRzY%Lhsc?wyQ6LMBu)L","extraState":{"optionLevel":1},"fields":{"ICON":"TASK","VAR":{"id":"SfQO0qSRH4KT9-W!daxW"}},"inputs":{"ARG0":{"shadow":{"type":"blockMathNumber","id":"@w.y1]!JGpTW=t6HT;aS","fields":{"NUM":2}}}},"next":{"block":{"type":"blockWaitUntil","id":"]dFA,`JtRRSfSs^QV8KE","inputs":{"BOOL":{"shadow":{"type":"blockLogicTrue","id":"K.~|8]y3r)hIvOKB:;pY"},"block":{"type":"blockLogicIsNone","id":"lt-sXuj_9d:d1BpbkK3I","fields":{"OP":"NOT"},"inputs":{"BOOL":{"shadow":{"type":"blockLogicTrue","id":"Njl=W[giy{M)i:$U%yG/"},"block":{"type":"blockButtonIsPressed","id":"$%V)a*!nO$qn(ocBxRyH","inputs":{"VAR":{"shadow":{"type":"variables_get_keypad","id":"7$lZiYB8,]{7|6RpMe.-","fields":{"VAR":{"id":"8]m)Qa4HePdTq@zKBGC;","name":"controller","type":"XboxController"}}}},"VALUE0":{"shadow":{"type":"blockParametersButton","id":"?IkzlNv;Sh3WwJ67IBfC","fields":{"VALUE":"Y"}}}}}}}}}}}}}}}},"IF3":{"shadow":{"type":"blockLogicTrue","id":"Vt7[46eTJh(]I*Zy72V["},"block":{"type":"blockButtonIsPressed","id":"_,VGxTEG1e%dtZA]WY;F","inputs":{"VAR":{"shadow":{"type":"variables_get_keypad","id":"x,4h$jIGsCY{s+LY%K~:","fields":{"VAR":{"id":"8]m)Qa4HePdTq@zKBGC;","name":"controller","type":"XboxController"}}}},"VALUE0":{"shadow":{"type":"blockParametersButton","id":"0!|tw,}p84nn;qnDs!_|","fields":{"VALUE":"B"}}}}}},"DO3":{"block":{"type":"blockComment","id":"?4zZ8Q{RL8z84cV{)kAn","fields":{"FIELDNAME":"Blade tilt function."},"next":{"block":{"type":"blockFunctionCallerStatement","id":"=Uo0kPJddoUg+s-@Nz74","extraState":{"optionLevel":1},"fields":{"ICON":"TASK","VAR":{"id":"SfQO0qSRH4KT9-W!daxW"}},"inputs":{"ARG0":{"shadow":{"type":"blockMathNumber","id":"osI4?/]Ar-0EBadn^#@z","fields":{"NUM":3}}}},"next":{"block":{"type":"blockWaitUntil","id":"1r9Nxw:Z|Kcf4JP5T~)(","inputs":{"BOOL":{"shadow":{"type":"blockLogicTrue","id":"K.~|8]y3r)hIvOKB:;pY"},"block":{"type":"blockLogicIsNone","id":":j@9aeJycFduVR2b]BP4","fields":{"OP":"NOT"},"inputs":{"BOOL":{"shadow":{"type":"blockLogicTrue","id":"Njl=W[giy{M)i:$U%yG/"},"block":{"type":"blockButtonIsPressed","id":"nO+](,p)Z%L=C8E{-DR{","inputs":{"VAR":{"shadow":{"type":"variables_get_keypad","id":"xswaMYpos9^GL{-rP5^Z","fields":{"VAR":{"id":"8]m)Qa4HePdTq@zKBGC;","name":"controller","type":"XboxController"}}}},"VALUE0":{"shadow":{"type":"blockParametersButton","id":"6.Kz(G,);;|[R=S`09(R","fields":{"VALUE":"B"}}}}}}}}}}}}}}}},"ELSE":{"block":{"type":"blockComment","id":"zRLw8Ih81]cHr+_]%SPN","fields":{"FIELDNAME":"No other button pressed, so don't switch."}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}},{"type":"variables_setup_function","id":"B~q]vZ!#[w@]AJW.r4NV","x":769,"y":2507,"extraState":{"optionLevel":1},"fields":{"ICON":"TASK","VAR":{"id":"SfQO0qSRH4KT9-W!daxW"}},"inputs":{"ARG0":{"shadow":{"type":"variables_setup_function_argument","id":"sfiHHH;M0p:-UCiIiNon","fields":{"VAR":{"id":"f`#%rdf$YNY~-JO!;rhT","name":"number","type":"FunctionArgument"}}}},"STACK":{"block":{"type":"blockComment","id":";56BuFmx;6T6T00aH;B[","fields":{"FIELDNAME":"This function takes care of moving the motor to positions 0, 1, 2, or 3:\n\n0: Blade up/down.\n1: Ripper.\n2: Ladder.\n3: Blade tilt.\n\nFirst, indicate that we are going to be busy switching."},"next":{"block":{"type":"blockVariableSetValue","id":"q`mZZ+O9aYd8f:H:S,?,","inputs":{"VAR":{"shadow":{"type":"variables_get_any","id":"2KT!Uy}1y6r3[HqSt4fg","fields":{"VAR":{"id":".zD)C%AekgQ,4G0#oX_4","name":"busy switching","type":"Any"}}}},"VALUE0":{"shadow":{"type":"blockMathNumber","id":"7~mvfs6[.i.%EIp;$)S#","fields":{"NUM":1}}}},"next":{"block":{"type":"blockFlowRepeat","id":"wX[D:H#oO.mT.JrZ)L^K","inputs":{"TIMES":{"shadow":{"type":"blockMathNumber","id":"44!U-](Dj+TB]*XYA5$a","fields":{"NUM":5}}},"DO":{"block":{"type":"blockComment","id":"iXqELwg5~3U-`[(FxUXN","fields":{"FIELDNAME":"Go to the target angle. But give up if it takes more\nthan 2 seconds, which means it's stuck for now."},"next":{"block":{"type":"blockMultiTask","id":"e_/WOVuH`o]gcRA6l03l","extraState":{"optionLevel":0},"fields":{"METHOD":"MULTITASK_RACE"},"inputs":{"TASK0":{"block":{"type":"blockMotorRun","id":"F[dVdZ*Z:*b^62wVCxF:","extraState":{"optionLevel":2},"fields":{"METHOD":"MOTOR_RUN_TO"},"inputs":{"VAR":{"shadow":{"type":"variables_get_motor_device","id":"oY~s0-@1^U95jm}aL6_r","fields":{"VAR":{"id":"GtM_hctvAF2:Pdx~WV{=","name":"switch","type":"Motor"}}}},"VALUE0":{"shadow":{"type":"unit_angularVelocity","id":"oXdA*_j^%Xhm9{X?C(x1","fields":{"VALUE0":750}}},"ANGLE":{"shadow":{"type":"unit_angle","id":"|0r@h9v)Gv^]^bv,OlG`","fields":{"VALUE0":360}},"block":{"type":"blockMathArithmetic","id":"@e!EdrEW@O-W.ZwMf-@3","fields":{"OP":"MULTIPLY"},"inputs":{"A":{"shadow":{"type":"blockMathNumber","id":"33+{-pU0iUFVZl$XCD`k","fields":{"NUM":1}},"block":{"type":"variables_setup_function_argumentConstant","id":"k:b*#0I]A],[@S5I[Q{G","fields":{"VAR":{"text":"number"}}}},"B":{"shadow":{"type":"blockMathNumber","id":"Ygz*BBt8HN{.JU@v7)Rc","fields":{"NUM":90}}}}}},"THEN":{"shadow":{"type":"parameters_stop_4","id":";)04i,KFHFY8yJ{h?6KA","fields":{"VALUE":"Stop.COAST"}}}}}},"TASK1":{"block":{"type":"blockWaitTime","id":".B1e^5,k;^AD8_E19#%T","inputs":{"VALUE0":{"shadow":{"type":"unit_time","id":"kZIKc%HB3dye=1tWNZ`P","fields":{"VALUE0":2000}}}}}}},"next":{"block":{"type":"blockComment","id":"I(EE(O!4(bXo1Hy2_amq","fields":{"FIELDNAME":"Let's check if we're on target."},"next":{"block":{"type":"blockIfElse","id":"hnD5,F8O0fr;IZ/{R3DT","extraState":{"optionLevel":0},"inputs":{"IF0":{"shadow":{"type":"blockLogicTrue","id":"j+9-+KY3m.VxUwWwg0Zf"},"block":{"type":"blockLogicCompare","id":"aJ$DL8L13]_Y%Axy%Lf%","fields":{"OP1":"LTE"},"inputs":{"A":{"shadow":{"type":"blockMathNumber","id":"l(@o!S%R100tDhI4NRHQ","fields":{"NUM":3}},"block":{"type":"blockMathOp","id":"mmRgPr!o7(]`7-?zjOSa","extraState":{"optionLevel":0},"fields":{"OP":"ABS"},"inputs":{"NUM":{"shadow":{"type":"blockMathNumber","id":"`,RjEsgzwC*ySYL||i*#","fields":{"NUM":0}},"block":{"type":"blockMathArithmetic","id":"TBNhoF;H~g.=W+7+l{7p","fields":{"OP":"MINUS"},"inputs":{"A":{"shadow":{"type":"blockMathNumber","id":"fkd~Cu00QFzjWJsXOhRq","fields":{"NUM":1}},"block":{"type":"blockMotorMeasure","id":":^=6g?K5o]U]CmBO%8*[","extraState":{"optionLevel":1},"fields":{"METHOD":"MOTOR_ANGLE"},"inputs":{"VAR":{"shadow":{"type":"variables_get_motor_device","id":"t@hitC^JD314FxmBRb,4","fields":{"VAR":{"id":"GtM_hctvAF2:Pdx~WV{=","name":"switch","type":"Motor"}}}}}}},"B":{"shadow":{"type":"blockMathNumber","id":"p61ZU9G0d,..=]}h[c9Z","fields":{"NUM":1}},"block":{"type":"blockMathArithmetic","id":"DRK^YSi.NmE^+Xpzo2?f","fields":{"OP":"MULTIPLY"},"inputs":{"A":{"shadow":{"type":"blockMathNumber","id":"33+{-pU0iUFVZl$XCD`k","fields":{"NUM":1}},"block":{"type":"variables_setup_function_argumentConstant","id":"dGKYXQ:1MgR:;niDx2@x","fields":{"VAR":{"text":"number"}}}},"B":{"shadow":{"type":"blockMathNumber","id":"f=C)0(tT#sMgu`kV1|]m","fields":{"NUM":90}}}}}}}}}}}},"B":{"shadow":{"type":"blockMathNumber","id":"u^sH;}i[^YyIGW#MymA/","fields":{"NUM":10}}}}}},"DO0":{"block":{"type":"blockComment","id":"jOovlHwhF.2{.|`=r2NX","fields":{"FIELDNAME":"We are close to the target angle.\nso we can exit this repeating loop."},"next":{"block":{"type":"blockFlowBreakContinue","id":"fN*grf18Y-_g/VKOr/N=","fields":{"FLOW":"BREAK"}}}}}},"next":{"block":{"type":"blockComment","id":"sB|uvk-sh_/c~Xlq_?]R","fields":{"FIELDNAME":"Otherwise, we're not done yet, so we must be stuck.\nLet's wiggle the motor around to try to get it unstuck."},"next":{"block":{"type":"blockMotorTrack","id":"~Pu+S*%,~Wz+J|wPx*;z","inputs":{"VAR":{"shadow":{"type":"variables_get_motor_device","id":"ST4WrEs#!n:!Ot|tqJBF","fields":{"VAR":{"id":"GtM_hctvAF2:Pdx~WV{=","name":"switch","type":"Motor"}}}},"VALUE0":{"shadow":{"type":"unit_angle","id":"aElZ4G6kN_?^A8h+Ys[$","fields":{"VALUE0":0}}}},"next":{"block":{"type":"blockWaitTime","id":":CR,0g^VJDCSHkYg{XOf","inputs":{"VALUE0":{"shadow":{"type":"unit_time","id":".{cJ7y;X9@HTDt[vZm~)","fields":{"VALUE0":1000}}}},"next":{"block":{"type":"blockMotorTrack","id":"N4dN$yQ.Yo*O1=g))#Qb","inputs":{"VAR":{"shadow":{"type":"variables_get_motor_device","id":"DOpfb3bd3IYz@j:*yIR7","fields":{"VAR":{"id":"GtM_hctvAF2:Pdx~WV{=","name":"switch","type":"Motor"}}}},"VALUE0":{"shadow":{"type":"unit_angle","id":"8:?!+z4!|d:YyRzELUot","fields":{"VALUE0":270}}}},"next":{"block":{"type":"blockWaitTime","id":"Y)Va`*IEd;.9m`Op[d?]","inputs":{"VALUE0":{"shadow":{"type":"unit_time","id":"Rk?(p;%eK7h{f5djTWZM","fields":{"VALUE0":1000}}}},"next":{"block":{"type":"blockMotorStop","id":"0n!t9OGkmSnTdJ]!tyg4","inputs":{"VAR":{"shadow":{"type":"variables_get_simple_motor_device","id":"hPg+0E-n5ZXo(LG:D|m,","fields":{"VAR":{"id":"GtM_hctvAF2:Pdx~WV{=","name":"switch","type":"Motor"}}}},"VALUE0":{"shadow":{"type":"parameters_stop_3","id":"pNnc:eu;J4^9g[:]~O4m","fields":{"VALUE":"Stop.COAST"}}}}}}}}}}}}}}}}}}}}}}}}},"next":{"block":{"type":"blockComment","id":"O$jB.^Tb6-QG96beX`^:","fields":{"FIELDNAME":"We're no longer busy switching, so we can drive again."},"next":{"block":{"type":"blockVariableSetValue","id":"hw7xPi,fkM#a-i(rk955","inputs":{"VAR":{"shadow":{"type":"variables_get_any","id":"@1}jvJ:a0a$}S*~Ny*Q-","fields":{"VAR":{"id":".zD)C%AekgQ,4G0#oX_4","name":"busy switching","type":"Any"}}}},"VALUE0":{"shadow":{"type":"blockMathNumber","id":"MVnO$QyL(DAe[[jzL!d8","fields":{"NUM":0}}}}}}}}}}}}}}}}]},"variables":[{"name":"red","id":"uO/h@3+4kh@Ac|Z_?Th8","type":"ColorDef"},{"name":"orange","id":"PbBhi3@n_tXs7EJrgedn","type":"ColorDef"},{"name":"yellow","id":"Xco^(#|Z|18MIT#u{biM","type":"ColorDef"},{"name":"green","id":"kA|}YYi=|SWR30=Z/tif","type":"ColorDef"},{"name":"cyan","id":"FFE|fWaoM:f.dtv#(VU2","type":"ColorDef"},{"name":"blue","id":"_s-rrV.@96Il~UP_hcog","type":"ColorDef"},{"name":"violet","id":"jppN#=b?sft-jQ3aO2~=","type":"ColorDef"},{"name":"magenta","id":"93H`nT:`gVY@Q!(`*c5g","type":"ColorDef"},{"name":"white","id":"XKUh(iy^MLb*+i*5PnYn","type":"ColorDef"},{"name":"none","id":"rz1ZYL.?ep#IDV^cyM!g","type":"ColorDef"},{"name":"left","id":"HB$E(E|0GcK=3:b9OGWK","type":"Motor"},{"name":"right","id":"WfLWxs!;-+H11Ww!!x:_","type":"Motor"},{"name":"function","id":"9]^QIL#QX=31WQ@/A7^z","type":"Motor"},{"name":"switch","id":"GtM_hctvAF2:Pdx~WV{=","type":"Motor"},{"name":"number","id":"f`#%rdf$YNY~-JO!;rhT","type":"FunctionArgument"},{"name":"controller","id":"8]m)Qa4HePdTq@zKBGC;","type":"XboxController"},{"name":"left end","id":"|:C@ETh=EDU-gt}caTMY","type":"Any"},{"name":"right end","id":"K!78++=(@jb1Q@?p`Xo?","type":"Any"},{"name":"busy switching","id":".zD)C%AekgQ,4G0#oX_4","type":"Any"},{"name":"switch function","id":"SfQO0qSRH4KT9-W!daxW","type":"Function"}],"info":{"type":"pybricks","version":"1.2.2"}}
from pybricks.iodevices import XboxController
from pybricks.parameters import Button, Direction, Port, Stop
from pybricks.pupdevices import Motor
from pybricks.tools import multitask, run_task, wait

# Set up all devices.
left = Motor(Port.A, Direction.COUNTERCLOCKWISE)
right = Motor(Port.B, Direction.CLOCKWISE)
function = Motor(Port.C, Direction.CLOCKWISE)
switch = Motor(Port.D, Direction.CLOCKWISE)
controller = XboxController()

# Initialize variables.
left_end = 0
right_end = 0
busy_switching = 1

async def switch_function(number):
    global busy_switching
    await wait(1)
    # This function takes care of moving the motor to positions 0, 1, 2, or 3:

    # 0: Blade up/down.
    # 1: Ripper.
    # 2: Ladder.
    # 3: Blade tilt.

    # First, indicate that we are going to be busy switching.
    busy_switching = 1
    for count in range(5):
        await wait(1)
        # Go to the target angle. But give up if it takes more
        # than 2 seconds, which means it's stuck for now.
        await multitask(switch.run_target(750, number * 90, Stop.COAST), wait(2000), race=True)
        # Let's check if we're on target.
        if abs(switch.angle() - number * 90) <= 10:
            # We are close to the target angle.
            # so we can exit this repeating loop.
            break
        # Otherwise, we're not done yet, so we must be stuck.
        # Let's wiggle the motor around to try to get it unstuck.
        switch.track_target(0)
        await wait(1000)
        switch.track_target(270)
        await wait(1000)
        switch.stop()
    # We're no longer busy switching, so we can drive again.
    busy_switching = 0

async def main1():
    # This main task will handle driving and the motor
    # that powers (not switches) the function gearbox.
    left.control.limits(acceleration=2500)
    right.control.limits(acceleration=2500)
    while True:
        await wait(1)
        if busy_switching:
            # If we are currently busy switching the function, we stop
            # driving and powering the function motor to be safe.
            left.stop()
            right.stop()
            function.stop()
        else:
            # Otherwise drive the motors based on the buttons that are pressed.
            # Use the bumpers for the function motor.
            if Button.RB in controller.buttons.pressed():
                function.dc(100)
            elif Button.LB in controller.buttons.pressed():
                function.dc(-100)
            else:
                function.stop()
            # Use the direction pad for driving.
            if controller.dpad() == 1:
                # Forward
                left.run(1000)
                right.run(1000)
            elif controller.dpad() == 2:
                # Forward / Right
                right.stop()
                left.run(1000)
            elif controller.dpad() == 3:
                # Right
                left.run(1000)
                right.run(-1000)
            elif controller.dpad() == 4:
                # Reverse / Right
                right.run(-1000)
                left.stop()
            elif controller.dpad() == 5:
                # Reverse
                left.run(-1000)
                right.run(-1000)
            elif controller.dpad() == 6:
                # Reverse / Left
                left.run(-1000)
                right.stop()
            elif controller.dpad() == 7:
                # Left
                left.run(-1000)
                right.run(1000)
            elif controller.dpad() == 8:
                # Forward / Left
                right.run(1000)
                left.stop()
            else:
                # Nothing, so stop.
                left.stop()
                right.stop()

async def main2():
    global right_end, left_end
    # This task will handle the motor that switches the function gearbox.
    # First it resets the gearbox by finding the start and end stops.
    await switch.run_until_stalled(500, Stop.COAST, 50)
    right_end = switch.angle()
    await switch.run_until_stalled(-500, Stop.COAST, 50)
    left_end = switch.angle()
    switch.reset_angle((left_end + 270 - right_end) / 2)
    await switch_function(0)
    # Now we run the main loop of operating the switch.
    while True:
        await wait(1)
        # Wait for the center switch to be pressed.
        while not any(controller.buttons.pressed()):
            await wait(1)
        # Then select the function based on which other button is pressed.
        if Button.X in controller.buttons.pressed():
            # Blade up/down function.
            await switch_function(0)
            while Button.X in controller.buttons.pressed():
                await wait(1)
        elif Button.A in controller.buttons.pressed():
            # Ripper function.
            await switch_function(1)
            while Button.A in controller.buttons.pressed():
                await wait(1)
        elif Button.Y in controller.buttons.pressed():
            # Ladder function.
            await switch_function(2)
            while Button.Y in controller.buttons.pressed():
                await wait(1)
        elif Button.B in controller.buttons.pressed():
            # Blade tilt function.
            await switch_function(3)
            while Button.B in controller.buttons.pressed():
                await wait(1)
        else:
            # No other button pressed, so don't switch.
            pass


async def main():
    await multitask(main1(), main2())

run_task(main())
Python representation of the block program.