Skip to content

Drive Mechanism – V3

The drive mechanism for a swerve drive robot is considerably more complex than that of the simple tank drive that you created for the Minibot. If you don’t remember how to create a new mechanism, you can review the procedure from the Minibot tutorial Mechanism.

The robot’s motion is controlled by eight motors grouped in pairs, with one pair on each of the four corners of the robot. We will refer to each of these pairs of motors as a Swerve Module:

The orange box in this image shows one of these modules. If we view this module from a different angle we can see the two motors which control this wheel:

The motor marked Steering controls the orientation of the wheel. The wheel is free to rotate a full 360 degrees. The motor marked Drive powers the wheel and drives the robot.

SwerveModule class

Since we will have four of these modules for our robot, we need to create a class which we will call SwerveModule. Each instance of this class will control the two motors for that specific module. We will need to create four instances of this class, one for each corner of the robot. This class does not need to be defined as a mechanism, and should just be a simple class something like:

In the constructor we will be passing in the pin numbers needed to configure the motors and encoders as well as a “name”. The name is used for debugging purposes. If we need to output some debugging information we can tag it with the name to identify which of the four swerve modules it belongs.

Initializing motors

Since each Swerve Module will control two motors, and we have passe in the pin numbers for each of the motors. To instantiate the motors you will need something like this:

Initializing encoders

In addition, each motor has an associated encoder. For the Drive motor we use a relative Quadrature encoder. for the Steering motor we use an absolute AnalogRotational encoder. Hence we will also need the four pins the configure the two encoders. To instantiate the encoders you will need something like this:

Note the last parameter group. If this is set to true then reading of the speed or position for the encoders are grouped at a low level to reduce the time needed to retrieve this information from the motors. In order to have sufficient bandwidth to operate the swerve drive it is important that group be set to true for these two encoders.

Finally, don’t forget to set the feedback device for the two motors:

DriveMechanism

Finally, when you create the DriveMechanism, you will need to create four instances of your SwerveModule class. You will, of course, need to know all of the required pin number and these are defined by the Swerve class:

Hence your DriveMechanism class should look something like:

In the next section we will tune the PID for the Steering motors.

Next: Steering Motor Tuning