All pages
Powered by GitBook
1 of 4

Loading...

Loading...

Loading...

Loading...

Part 4: Going Beyond!

Looking to take the next steps with your robot or to learn more about programming? In "Part 4: Going Beyond!" we have additional tutorials and short lessons to explore.

This section may continue to grow in the future so be sure to check back for new updates!

Quick Links

Exploring Functions
Programming Mecanum Drivetrains - Simplified
Programming Mecanum Drivetrains - Refined

Programming Mecanum - Simplified

This example is a simplified form of a mecanum drivetrain code intended to review the basics of mecanum movement and is not recommended for a FTC robot.

Check out for a competition ready example!

Configuration

Before getting started with programming we needed to create a configuration file. Below is an overview of how the robot is configured for the TeleOp code to function as expected:

Port Type
Port Number
Device Type
Name

Motor

0

Gamepad Input
Function

For this program, we'll set the motors to RUN_WITHOUT_ENCODER along with their direction

For a mecanum drivetrain all 4 motors will be given a command to follow when the left joystick is moved along the Y-axis of the joystick. For moving forward and back all wheels must turn the same direction.

For this example, strafing is controlled by the left stick's X-axis allowing the robot to slide left and right. In order to achieve this movement, the motors move in diagonal pairs, so frontLeft and backRight will move the opposit direction of backLeft and frontRight, similar to the X shape the wheels make.

Lastly, we have turning set by itself on the right joystick's X-axis. To turn our left and right pairs of wheels will spin in opposite directions.

Exploring Functions

What is a Function?

Functions act similar to a variable in that we are using one thing to represent another. However, where a variable typically is used in place of something short, such as a number, a function can take the place of several lines of code.

This can be incredibly useful if there is a section of code we know will be repeated or to break apart our code into chunks for easy editing.

Creating a new Function in Blocks

If we want to create a new function in our Blocks program, we start by pulling a block from the Functions menu:

Functions menu in Blocks showing the functions from the 2024-25 Starter Bot

Next we will replace "do something" with an appropriate name. Maybe in this case we are adding a new function for climbing:

Example function for climbing

Once our function is named it will appear in the "Functions" menu to be added to the main loop or wherever we need it within our code!

New function block
New function added to our main code

All that's left is to add whatever code we'd like to be within this function:

If the block is deleted this will remove the function from the "Functions" menu.

Let's say we are working on an autonomous code where we want our robot to drive roughly in a square. Remember that autonomous means this code will move the robot on its own when play is pressed:

Next, let's say we need the robot to do something between one of the turns, such as move its arm or open a servo's claw. There's a couple of ways we could approach this without functions:

Already our code is getting a little long so let's move our side motion and turn into a function:

Now our loop may look like this:

When we test our code we may notice our robot isn't exactly driving in a square shape. Thankfully with our function in place we only need to change the needed value in one place:

This change to the function will be reflected anywhere DRIVE_AND_TURN used.

Give it a try by changing the right motor's power or the timer to refine your square!

REV Robotics Ultraplanetary HD Hex Motor

frontLeft

Motor

1

REV Robotics Ultraplanetary HD Hex Motor

frontRight

Motor

2

REV Robotics Ultraplanetary HD Hex Motor

backLeft

Motor

3

REV Robotics Ultraplanetary HD Hex Motor

backRight

Left Joystick - Left/Right on X-Axis

Strafe Left/Right

Left Joystick - Forward/Backward on Y-Axis

Forward/Backward

Right Joystick - Left/Right on X-Axis

Example Program:

Gamepad Layout:

Programming Teleop - Blocks

Initialization:

Before diving into mecanum, double check the direction your motors and wheels are spinning. They may need to be reversed if you're experiencing jittering or inverted controls!

Adjust the block to change the set direction during initialization.

Moving Forward and Backwards:

Strafing:

Turning:

This version of the mecanum program does not account for diagonal movements of the joystick. Check out to create a fully responsive mecanum drive!

Recall that the Y-axis on the gamepad must be inverted.
Programming Mecanum - Refined
mecanumSimplified.blk
12KB
Open
Setting up the motors
All four motors are set to move in the same direction
Two motors will run in the opposite direction when strafing!
When turning the front and back motors rotate in opposite directions.

When to use Functions - Example

This example was originally created as part of the program, but can be followed on a different robot!

After , create a new to begin. Ours is named FunctionsDemo.

Example of a simple code within our function
Simple program for driving in a square
Full code with claw opening after driving two sides
Function containing the code for driving and turning
Using functions to drive in a square
Code for controlling the robot's turn

Turn Counter-Clockwise/Clockwise

Programing Mecanum - Refined
2024-25 FTC Starter Bot
configuring
OpMode

Programming Mecanum - Refined

How a Mecanum Drivetrain is programmed largely depends on the driver's preference for how the controller is configured.

In our provided example, the left joystick controls forward/back and strafe then the right joystick controls turning. This code is based on the sample provided by FIRST for Blocks (BasicOmniOpMode) available in the Robot Controller Console.

Mecanum Configuration

Port Type
Port Number
Device Type
Name

This example makes use of to help organize the code!

At the very beginning of our program, our MOTOR_SETTINGS function is called. Within it the drivetrain motors are set to RUN_WITHOUT_ENCODER and are set to run the appropriate direction.

Next, we need to create some new variables in order to use mecanum.

Let's break those down first:

Variable
Purpose

At the beginning of the MECANUM_DRIVE function, our variables for each movement direction are being set to the value generated by the movement of the matching joystick axis.

Since we now have four motors in play, our equation for setting the needed power to each motor gets a little more complicated.

Our robot first needs to determine the combined movement of the gamepads's left joystick:

Then calculate with the right stick's value:

All our calculations together allows for movement when the left joystick is moved at an angle, such as for strafing along a diagonal!

Let's take a closer look at how our motor power is being determined. For example, our leftFrontPower variable will equal:

So what if we move our left joystick all the way to the left side along the X-axis. To our robot, our equation will read something like this:

Take a moment to think: What would be the power of our other motors?

What about a more complicated example? What if we had the left joystick at an angle, all the way to the left and halfway towards the top? Or had our left stick forward and right stick all the way right?

For our last step, our robot sets the power of each pair of motors based on all our calculations!

While driving, there's a possibility a value may fall outside the range of the motor's power (-1 to 1). To help make sure no inputs are lost because of this, we can use a technique called "normalizing".

What normalizing does is take all of our calculated values and scales them appropriately to remain inside the intended range.

First we need to create a new variable called "max".

In Blocks, we use something called a "list", also known as an "array" to store a set of numbers. In this case, we will be storing all of our motor powers.

But first, we need to add a block from our "Math" menu. We will change this using the dropdown to "max", meaning it is returning the largest value from our list of motor powers.

Since our motor power will sometimes be negative, such as when turning in reverse, we want to make sure we're using the absolute value of our motor powers.

Next, we will set up our to check if our "max" is higher than 1 and therefore outside the motor's range.

Using this statement, we'll readjust each of our motor's power back to be within range proportionally by dividing each by the max value.

Now our full drivetrain function will look like the following:

leftFrontPower

Sets the front left motor power

rightFrontPower

Sets the front right motor power

leftBackPower

Sets the back left motor power

rightBackPower

Sets the back right motor power

Motor

0

REV Robotics Ultraplanetary HD Hex Motor

frontLeft

Motor

1

REV Robotics Ultraplanetary HD Hex Motor

backLeft

Motor

2

REV Robotics Ultraplanetary HD Hex Motor

frontRight

Motor

3

REV Robotics Ultraplanetary HD Hex Motor

backRight

forwardBack

Moving forward and backwards

strafe

Strafing side to side

turn

leftFrontPower=(forwardBack+strafe)+turnleftFrontPower = (forwardBack + strafe) + turnleftFrontPower=(forwardBack+strafe)+turn
leftFrontPower=(0+−1)+0leftFrontPower = (0 + -1)+ 0 leftFrontPower=(0+−1)+0

Mecanum Example Code

Mecanum Code Breakdown

Before diving into mecanum, double check the direction your motors and wheels are spinning. They may need to be reversed if you're experiencing jittering or inverted controls!

Adjust the block to change the set direction during initialization.

Establishing Variables

Calculating Motor Movement with the Gamepad

Quick Check! - Understanding Motor Power

What would be the power of our other motors?

Our other motor power would look like the following:

  • rightFrontPower = (0 - (-1)) + 0

  • leftBackPower = (0 - (-1)) - 0

  • rightBackPower = (0 + -1) - 0

Or simplified:

  • leftFrontPower = -1

  • rightFrontPower = 1

  • leftBackPower = 1

  • rightBackPower = -1

As we can see our motors are spinning the same direction as their diagonal partner, meaning the robot will strafe left!

What about a more complicated example?

Looking at our leftFrontPower again as an example the robot might calculate the following:

What if we had the left joystick at an angle, all the way to the left and halfway towards the top?

  • leftFrontPower = (0.5 + -1) + 0

In this situation, our leftFront motor would set the power to -0.5!

Or had our left stick forward and right stick all the way right?

  • leftFrontPower = (1 + 0) + 1

In this case, while our equation equals to 2, our motor cannot power higher than 1 so will cap out at full power! We'll discuss to remain within range below.

Adding telemetry to your program, as seen below, can help to see the values the motors are assigned during different scenarios:

Setting Motor Power

Normalizing Input Values

This section of code is not within the provided copy of the mecanum code used in this tutorial. Follow the steps below to add it!

Creating a Max Variable

Creating the If/Else Statement

functions
If/Else
mecanumFull.blk
13KB
Open
With functions are main loop appears simplified
Setting our motor settings within the function
All our variables for mecanum
Our variables are set to their appropriate joystick movement
Motor power being calculated with the left joystick movement
Motor power being calculated with both joysticks
Setting each motor's power to their variable
Normalizing the motor power values
Creating a new variable
Creating a list in Blocks
Setting our variable to the highest motor value
Finding the "absolute" block in the "Math" menu
Our max variable if/else statement
Full If/Else statement
Full mecanum drivetrain program with normalizing

Turning left and right

normalizing motor values