How to make a motorgroup with 3 motors in VEXcode blocks

There was a comment here that blocks only allows two motors in a motorgroup.

While on the surface that is true, the graphical configuration only allows two motors, with a little hacking and using switch blocks we can add as many more as we want (and a little insider knowledge I will share).

Lets say we want motors on ports 10, 11 and 12 to be part of a motor group. We start by making a motor group using ports 10 and 11 (select 10 as the first motor when creating it), next we create a single motor on port 12.

Converting our project to text we can see the generated Python code that VEXcode has created, this is the relevant part.

motor_group_10_motor_a = Motor(Ports.PORT10, False)
motor_group_10_motor_b = Motor(Ports.PORT11, False)
motor_group_10 = MotorGroup(motor_group_10_motor_a, motor_group_10_motor_b)
motor_12 = Motor(Ports.PORT12, False)

VEXcode uses a fairly structured naming convention due to the way code is generated but this may change in future versions so be careful.

The two things we are most interested in is the name of the motor group, in this case motor_group_10 and the name of the additional motor we want to add to it motor_12

To store the motors in a motor group the class uses an internal list called _motors, the leading underscore is meant to imply this is a private variable, but the reality is that no such thing exists in Python (or at least in the version we use). Adding additional motors to this list is what we need to do. The line of python code to do that would be (for this specific example)

motor_group_10._motors.append(motor_12)

We are appending motor_12 to the list.

Here is how that is done using blocks and switch.

If you experiment with this please use a new project rather than your competition code, remember that if you convert a blocks project to text to see what VEXcode is generating as names use a copy as you cannot convert back.

Many other hacks are possible using switch, for example, a 6 motor drivetrain perhaps.

happy hacking.

6 Likes

investigating using REPL

So I obviously have more knowledge about how the VM works than most, but this could actually be figured out using the Python REPL as well (search the forum for info about how to connect with the Python REPL, you will need to run a Python program for it to be active).

Here is some output where I created two motors and a motor group interactively.

MicroPython v1.13-380-g55f09d3a8-dirty on 2024-07-29; Vex IQ2 with Cortex M4
Type "help()" for more information.
>>> m1=Motor(Ports.PORT10)
>>> m2=Motor(Ports.PORT11)
>>> mg=M
MM              MSEC            MV              Motor
MotorGroup
>>> mg=MotorGroup(m1, m2)
>>> mg.
__class__       __init__        __module__      __qualname__
count           stop            __dict__        convert_velocity
current         direction       efficiency      gear_cartridge
is_done         is_spinning     is_spinning_mode
position        power           reset_position  set_max_torque
set_position    set_stopping    set_timeout     set_velocity
spin            spin_for        spin_to_position
temperature     torque          velocity
__spin_for_distance             __spin_for_time
__waitForCompletionAll          _motors         _timeout
>>> mg._motors
[Motor(9) 1.00, Motor(10) 1.00]
>>> 

You can hit the tab key to see options using the REPL, so when I typed “mg.” and hit tab it showed options for completion, you can see the “_motors” variable I mentioned in the first post.

typing “mg._motors” printed the contents of that list showing that it contained two motors (which were printed showing port number and gear ratio, this was on an IQ2 so not really relevant here)

3 Likes

Interesting, does this also mean that 6 motor drivetrains are possible through blocks using this technique? I havnt looked at how python handles the SmartDrive, but if memory serves, weren’t they just two motor groups in C++?

I expect it will work.

1 Like