-
Notifications
You must be signed in to change notification settings - Fork 1
Getting Started
The robot created in this example will be a basic tank drive robot with 1 simple intake mechanism
Note: You must first set up an FTC robot project using the official FTC project files, this tutorial also assumes that you are using Android Studio
The first thing when setting up an Alloy robot is to download the .jar file and add it as a dependency you can get that Here
If you already know how to add a dependency, you can skip this part
Once you have everything set up you can now start writing code for your robot. The first thing you should do is create a class named Robot, this is where you will be writing the main code for the robot.
Note: You can organize your code how you want, but in keeping with conventions the code will be stored in TeamCode/src/main/java/org/firstinspires/ftc/teamcode
All robots using Alloy as their core framework need to extend the Alloy Class
public class robot extends Alloy{
}After this, you will need to implement the different control methods. These are robotSetup(), initialization(), and periodic(). The robot class itself normally acts like a teleop mode, but if you want to have multiple teleop modes, only define a robotSetup() in the robot class, and have the teleop modes extend the robot class and implement, and implement the robotSetup() and initialization() there.
You can read more about the control methods and multiple teleop modes here
public class robot extends Alloy{
/**
* The robotSetup is where all code specific to robot setup is placed
* If you only have one teleop this can be done in the initialization
* Method. robotSetup is called right after the robot core is initialized
*/
public void robotSetup(){
}
/**
* The initialization method is were everything specific to the OpMode
* Should be set up. Initialization will be the first thing called after
* The robot setup.
*/
public void initialization(){
}
/**
* Although most of the periodic actions are taken care by the updater, the user may want to
* add their own methods and code that need to be updated or run periodically, this can be done in
* the periodic() method
* periodic will be run every loop.
*/
public abstract void periodic(){
}
}These methods both have the same functionality unless you wish to use multiple teleop modes. This is where you should define any components for the robot, and setup any objects or variables that your robot will need to use during runtime.
This method is called every loop and is useful for updating any information or variables, as well as controlling any systems that are not already handled by the alloy framework remember, alloy has its own updating system, so most things are already being updated automatically and should not be updated here.
Before you continue on by setting up your drivetrain, you will need to figure out what type of drivetrain you have, and what mapper or drivetrain object you should use. The drivetrain code can be fully customizable but has a few built-in features that make coding the robot much easier. You can read more about The built-in drivetrains and how to create custom ones Here
For this guide, we will be using a general purpose tank drivetrain that has 2 motors on each side. Since the tank drivetrain is built in, it can be created quite easily.
Before any drivetrain is created, you will need to create the motors, and the motor modules. We will create one module consisting of 2 motors for each side.
FTCMotor r1, r2, l1, l2;
r1 = new FTCMotor("right_front");
r1 = new FTCMotor("right_front");
l2 = new FTCMotor("right_front");
l2 = new FTCMotor("right_front");
DriveModule rightModule = new DriveModule(
new XY(1, 0), // Specify the module
r1, r2 // Add the motors
);
DriveModule rightModule = new DriveModule(
new XY(-1, 0), // Specify the module is on the left side
l1, l2
);As you can see, the modules were defined to be on the right and left sides by using a Cartesian vector. If you are unfamiliar with vectors, for this case you can think of them as being coordinates, so -1 would refer as one unit to the left and 1 would refer to one unit to the right. The unit does not matter, as long as it is consistent. So in this situation the motors would be evenly spaced on either side of the robot.
note: For use with FRC, you will need to include encoders on your motor modules to allow for more complete control and regulation of speed
Once you have your modules created, you will need to add them to a drive train. Built in drive trains in Alloy come with built in mappers, but if you wish to create your own, you will need to specify the mapper.
Since we are creating a tank drive, this is very easy
DriveTrain driveTrain = new TankDrive(rightModule, leftModule);You now need to create and assign an input to your drivetrain. Alloy has a number of built-in inputs for easily mapping a controller, but for the most basic case, a joystick can be passed directly as an input
// create a new drive stick, accessing the gamepad1 from the `RobotCore` class and specify the right side
FTCJoystic driveStick = new FTCJoystick(RobotCore.getGamepad1(), FTCJoystick.Side.RIGHT);You may want to add modifiers to this joystick, such as inverting, squaring, or adding a dead zone to the joystick. These are considered "Steps" that get applied in order to the input. You can create your own steps, but alloy also has some build in, for example, we will add a dead zone.
// A deadzone will make a small zone on the joystick amount to 0 input, this way, very small nudges do not affect the
// input
driveStick.addStep(new Deadzone());You can add as many steps as you want. They will be applied in order from first added to last added.
The last thing to do is to set the input to the drivetrain
drivetrain.setInput(driveStick);