Skip to main content

Programming the Driver period

In Head-to-Head Matches, two (2) Alliances—one (1) “red” and one (1) “blue”—composed of two (2) Teams each, compete in Matches consisting of a fifteen (15) second Autonomous Period followed by a one minute and forty-five second (1:45) Driver Controlled Period.

The Driver Controlled Period is one of the most important aspects in VEX. For that reason, Lightning provides the ability to easily program the driver period for your drivetrain using various configurations that can help you.

In this tutorial, you will learn how to program the driver period using Lightning.

Ensure you have declared your drive train

You are not sure to how declare your chassis?, check the Declaring your chassis tutorial.

Your chassis declaration needs to be global. This means that each function can interact with your chassis. The easiest way to do this is to make your chassis declaration at the top of the main.cpp file.

You need more information about Global Variables? check this for more details.

main.cpp
#include "main.h"
//Here is your drive-train
//Declaring a simple drive train
lightning::TankChassis my_chassis(
//Odometry configuration
lightning::tank_odom_e_t::ADI_ONE_ODOM,
//Declaration of drivetrain motors
{-11,-12,-13,-14}, //Left side ports (using a negative number will reverse it!)
{20,19,18,17}, //Right side ports (using a negative number will reverse it!)
5, //IMU port
pros::E_MOTOR_GEAR_600, //Which motor cartride are you using, blue,red,green?
3.25, //Wheel Diameter
1.3333, //what is the gear ratio (Is the result of Driven/Driving, Drive:Driving)
); //Forward tracking wheel diameter


Once our chassis is declared it is time to program the driver period.

Program in the opcontrol function

Inside main.cpp,you need to locate the opcontrol function. This function is designated for Driver Controlled Period.

#include "main.h"

void initialize() {
}

void disabled() {}

void competition_initialize() {}

void autonomous() {
}

void opcontrol() {
pros::Controller master(pros::E_CONTROLLER_MASTER);
while (true) {

}
}

The code that we will program in the opcontrol function will act in the Driver Controlled Period.

Chose your driver configuration

There are different driver configuration. Choose your favorite!🥳

info

Currently, lightning supports only tank-drive chassis.

Tank

It is one of the most common driver configurations. Basically, the left joystick controls the left side of the chassis, while the right joystick controls the right side of the chassis.

Coding

Using the TANK function, we can add the tank driver configuration to our robot.

main.cpp
void opcontrol() {
pros::Controller master(pros::E_CONTROLLER_MASTER);
while (true) {

my_chassis.tank(master);

pros::delay(lightning::util::DELAY_TIME);
}
}

As a result, we could control our robot using the tank configuration.


Arcade

With the ARCADE configuration, you can control the entire chassis using just one joystick.

Left Arcade

In this configuration, you can control the chassis using the left joystick.

To add the left configuration, you just need to use the arcade() function, passing the first parameter.

main.cpp
void opcontrol() {
pros::Controller master(pros::E_CONTROLLER_MASTER);
while (true) {
my_chassis.arcade(master);

pros::delay(lightning::util::DELAY_TIME);
}
}

However, you can specify the configuration using the same function.

main.cpp
void opcontrol() {
pros::Controller master(pros::E_CONTROLLER_MASTER);
while (true) {
my_chassis.arcade(master,E_TANK_OP_ARCADE_LEFT);

pros::delay(lightning::util::DELAY_TIME);
}
}

Right Arcade

In this configuration, you can control the chassis using the right joystick.

To add the left configuration, you just need to use the arcade() function as follows:

main.cpp
void opcontrol() {
pros::Controller master(pros::E_CONTROLLER_MASTER);
while (true) {
my_chassis.arcade(master,E_TANK_OP_ARCADE_RIGHT);

pros::delay(lightning::util::DELAY_TIME);
}
}

Double Arcade

This configuration allows you to control robot´s forward and backward direction with the left joystick´s Y-axis, and control the turning direciton with the right right joystick´s X-axis.

note

Although you can't control the chassis using just one joystick with the double arcade configuration, it is still considered an arcade configuration in the library for practice purposes.

To add the double configuration, you just need to use the arcade() function as follows:

main.cpp
void opcontrol() {
pros::Controller master(pros::E_CONTROLLER_MASTER);
while (true) {
my_chassis.arcade(master,E_TANK_OP_ARCADE_DOUBLE);

pros::delay(lightning::util::DELAY_TIME);
}
}

Double arcade alternative

This configuration allows you to control robot´s forward and backward direction with the right joystick´s Y-axis, and control the turning direciton with the left right joystick´s X-axis.

tip

This is an alternative way to drive the chassis, istead of using the default double configuration.

To add the double configuration, you just need to use the arcade() function as follows:

main.cpp
void opcontrol() {
pros::Controller master(pros::E_CONTROLLER_MASTER);
while (true) {
my_chassis.arcade(master,E_TANK_OP_ARCADE_DOUBLE_ALTERNATIVE);

pros::delay(lightning::util::DELAY_TIME);
}
}

Adding rates to arcade configurations

When the robot moves too quickly, controlling the chassis can become challenging. To address this, you can adjust the rates to modify the joystick output, which will help slow down the robot.

For instance, you might want to reduce the sensitivity for turning while maintaining the same output for forward and backward movement.

This feature can be added to the differents arcade configurations using the arcade function with all the parameters.

void arcade(pros::Controller& control, 
const lightning::tank_op_arcade_e_t arcade = lightning::E_TANK_OP_ARCADE_LEFT ,
const float power_rate=1, const float turn_rate=1);

Where:

Parameters
controlThe control.
arcadeThe tank_op_arcade_e_t to set for the chassis.
power_rateThe rate for the forward and backwards movements (Default is 1).
turn_rateThe rate for the right and left turns (Default is 1).
note

If power_rate and turn_rate are both set to 1, there will be no modification to the output.


Examples

Let's suppose you want to limit the turning power while still using the full power for forward and backward movements. For example, you might decide to use only half of the power for turning.

The code will look as follows:

main.cpp
void opcontrol() {
pros::Controller master(pros::E_CONTROLLER_MASTER);
while (true) {
my_chassis.arcade(master,E_TANK_OP_ARCADE_LEFT,1,.5);

pros::delay(lightning::util::DELAY_TIME);
}
}

Arcade with exponential

You may notice that simply using rates is not the most effective way to control your chassis. This is because applying rates always reduces the robot's velocity. In short, you will never fully utilize your drive-train while using rates.

However, there is a different approach that allows you to have more precise control while using 100% of your chassis power.

To achieve this, we use the following equation:

y=xn127(n1)y = \frac{x^{n}}{127^{(n-1)}}

This function smooths the joystick input based on a specified exponent and maximum value. The smoothing function adjusts the joystick value to create a more gradual response.

If we plot the equation´s output, we get this: smoothing_plot

You may notice that as we increase the exponent, the output becomes smoother. Additionally, if the exponent is an even number, the output function will be purely positive. However, don't worry; in the template, you can use odd or even numbers without any issues!

You can see more information about this equation here.

Also thanks to _Colossus for providing valuable information!

Well and how i use it 🤔?

Easy, just use arcade_exponential()!.

Function prototype

main.cpp
void arcade_exponential(pros::Controller &control,
const lightning::tank_op_arcade_e_t arcade,
int n_x,
int n_y)

Where:

Parameters
controlThe control.
arcadeThe tank_op_arcade_e_t to set for the chassis.
n_xThe exponential for the joystick x axis.
n_yThe exponential for the joystick y axis.

With that established, in this function you can set an arcade configuration and then select the exponential for each movement(turns or backward-forward). For example, if you want smoother output from turns , you should increase n_x, and if you want to increase the smoothing for forward and backward movement you have to increase n_y.

note

If you want a normal output, set the corresponding exponent (n_x or n_y) to 1.

Examples

Lets suppose that we want a smoother output for turns.

main.cpp
void opcontrol() {
pros::Controller master(pros::E_CONTROLLER_MASTER);
while (true) {
my_chassis.arcade_exponential(master,E_TANK_OP_ARCADE_LEFT,3,1);

pros::delay(lightning::util::DELAY_TIME);
}
}


Ready to drive! 🏎️

Congratulations, with this guide you now are able to do:

  • Programing the driver period.
  • Use tank configuration.
  • Use different arcade configurations.
  • Use an exponential method to smooth your joystick output for more control.