Upload
others
View
9
Download
0
Embed Size (px)
Citation preview
FTC advanced programmingwith Android Studio
David Austin
Allendale Robotics
HexaSonics, FTC Team 7023
David Austin FTC Programming with Android Studio
OpModes
Most of the programming team’s effort is in writing OpModes.
OpModes extend OpMode as in
public class AmazingAuto extends OpMode
You must register OpModes with the app by including anannotation:
@Autonomous(name="Amazing auto!", group="Wow")// @Disabledpublic class AmazingAuto extends OpMode
You may also extend LinearOpMode.
David Austin FTC Programming with Android Studio
OpModes
Most of the programming team’s effort is in writing OpModes.
OpModes extend OpMode as in
public class AmazingAuto extends OpMode
You must register OpModes with the app by including anannotation:
@Autonomous(name="Amazing auto!", group="Wow")// @Disabledpublic class AmazingAuto extends OpMode
You may also extend LinearOpMode.
David Austin FTC Programming with Android Studio
OpModes
Most of the programming team’s effort is in writing OpModes.
OpModes extend OpMode as in
public class AmazingAuto extends OpMode
You must register OpModes with the app by including anannotation:
@Autonomous(name="Amazing auto!", group="Wow")// @Disabledpublic class AmazingAuto extends OpMode
You may also extend LinearOpMode.
David Austin FTC Programming with Android Studio
The life cycle of an OpMode
OpModes operate within an event driven model. The codeexecutes in roughly 25 ms cycles. Whatever you do in onecycle has to finish within that time interval.There are a number of methods you need to construct to definean OpMode.
init(): Called once after Init is pressed.
init loop(): Called every 25 milliseconds after Init ispressed.start(): This is executed once after you press the Startbutton on the Driver Station.loop(): While the opmode is running, this method isexecuted roughly every 25 milliseconds.stop(): Instructions to perform when the Stop button ispressed; e.g. set the power of all the motors to zero.
David Austin FTC Programming with Android Studio
The life cycle of an OpMode
OpModes operate within an event driven model. The codeexecutes in roughly 25 ms cycles. Whatever you do in onecycle has to finish within that time interval.There are a number of methods you need to construct to definean OpMode.
init(): Called once after Init is pressed.init loop(): Called every 25 milliseconds after Init ispressed.
start(): This is executed once after you press the Startbutton on the Driver Station.loop(): While the opmode is running, this method isexecuted roughly every 25 milliseconds.stop(): Instructions to perform when the Stop button ispressed; e.g. set the power of all the motors to zero.
David Austin FTC Programming with Android Studio
The life cycle of an OpMode
OpModes operate within an event driven model. The codeexecutes in roughly 25 ms cycles. Whatever you do in onecycle has to finish within that time interval.There are a number of methods you need to construct to definean OpMode.
init(): Called once after Init is pressed.init loop(): Called every 25 milliseconds after Init ispressed.start(): This is executed once after you press the Startbutton on the Driver Station.
loop(): While the opmode is running, this method isexecuted roughly every 25 milliseconds.stop(): Instructions to perform when the Stop button ispressed; e.g. set the power of all the motors to zero.
David Austin FTC Programming with Android Studio
The life cycle of an OpMode
OpModes operate within an event driven model. The codeexecutes in roughly 25 ms cycles. Whatever you do in onecycle has to finish within that time interval.There are a number of methods you need to construct to definean OpMode.
init(): Called once after Init is pressed.init loop(): Called every 25 milliseconds after Init ispressed.start(): This is executed once after you press the Startbutton on the Driver Station.loop(): While the opmode is running, this method isexecuted roughly every 25 milliseconds.
stop(): Instructions to perform when the Stop button ispressed; e.g. set the power of all the motors to zero.
David Austin FTC Programming with Android Studio
The life cycle of an OpMode
OpModes operate within an event driven model. The codeexecutes in roughly 25 ms cycles. Whatever you do in onecycle has to finish within that time interval.There are a number of methods you need to construct to definean OpMode.
init(): Called once after Init is pressed.init loop(): Called every 25 milliseconds after Init ispressed.start(): This is executed once after you press the Startbutton on the Driver Station.loop(): While the opmode is running, this method isexecuted roughly every 25 milliseconds.stop(): Instructions to perform when the Stop button ispressed; e.g. set the power of all the motors to zero.
David Austin FTC Programming with Android Studio
The life cycle of an OpMode
These are the methods you will mainly use:
init(): Load the robot configuration into your code.DcMotor leftDrive =
hardwareMap.dcMotor.get(‘‘leftDrive’’);
loop(): This is the main part of your code. You will listento sensors, determine how to respond, and set powerlevels to motors.stop(): Turn everything off.leftDrive.setPower(0);
David Austin FTC Programming with Android Studio
The life cycle of an OpMode
These are the methods you will mainly use:
init(): Load the robot configuration into your code.DcMotor leftDrive =
hardwareMap.dcMotor.get(‘‘leftDrive’’);
loop(): This is the main part of your code. You will listento sensors, determine how to respond, and set powerlevels to motors.
stop(): Turn everything off.leftDrive.setPower(0);
David Austin FTC Programming with Android Studio
The life cycle of an OpMode
These are the methods you will mainly use:
init(): Load the robot configuration into your code.DcMotor leftDrive =
hardwareMap.dcMotor.get(‘‘leftDrive’’);
loop(): This is the main part of your code. You will listento sensors, determine how to respond, and set powerlevels to motors.stop(): Turn everything off.leftDrive.setPower(0);
David Austin FTC Programming with Android Studio
The problem
What do you do when you want to combine a couple ofseparate actions?
Drive forward 50 inches.Turn on shooter motors.Turn on shooter feed mechanism.
David Austin FTC Programming with Android Studio
The problem
What do you do when you want to combine a couple ofseparate actions?
Drive forward 50 inches.
Turn on shooter motors.Turn on shooter feed mechanism.
David Austin FTC Programming with Android Studio
The problem
What do you do when you want to combine a couple ofseparate actions?
Drive forward 50 inches.Turn on shooter motors.
Turn on shooter feed mechanism.
David Austin FTC Programming with Android Studio
The problem
What do you do when you want to combine a couple ofseparate actions?
Drive forward 50 inches.Turn on shooter motors.Turn on shooter feed mechanism.
David Austin FTC Programming with Android Studio
Two solutions
There are two solutions to this problem.
Write your code in a class extending LinearOpMode,which itself extends OpMode.
Pros: You write sequential code:do A, then B, then C, ...
Cons: It feels like a hack (because it is).Just extend OpMode and use a state machine to keep trackof the actions.
Pros: Good opportunity to learn an important computerscience paradigm
Cons: See Pros.
David Austin FTC Programming with Android Studio
Two solutions
There are two solutions to this problem.
Write your code in a class extending LinearOpMode,which itself extends OpMode.
Pros: You write sequential code:do A, then B, then C, ...
Cons: It feels like a hack (because it is).
Just extend OpMode and use a state machine to keep trackof the actions.
Pros: Good opportunity to learn an important computerscience paradigm
Cons: See Pros.
David Austin FTC Programming with Android Studio
Two solutions
There are two solutions to this problem.
Write your code in a class extending LinearOpMode,which itself extends OpMode.
Pros: You write sequential code:do A, then B, then C, ...
Cons: It feels like a hack (because it is).
Just extend OpMode and use a state machine to keep trackof the actions.
Pros: Good opportunity to learn an important computerscience paradigm
Cons: See Pros.
David Austin FTC Programming with Android Studio
Two solutions
There are two solutions to this problem.
Write your code in a class extending LinearOpMode,which itself extends OpMode.
Pros: You write sequential code:do A, then B, then C, ...
Cons: It feels like a hack (because it is).
Just extend OpMode and use a state machine to keep trackof the actions.
Pros: Good opportunity to learn an important computerscience paradigm
Cons: See Pros.
David Austin FTC Programming with Android Studio
Two solutions
There are two solutions to this problem.
Write your code in a class extending LinearOpMode,which itself extends OpMode.
Pros: You write sequential code:do A, then B, then C, ...
Cons: It feels like a hack (because it is).Just extend OpMode and use a state machine to keep trackof the actions.
Pros: Good opportunity to learn an important computerscience paradigm
Cons: See Pros.
David Austin FTC Programming with Android Studio
Two solutions
There are two solutions to this problem.
Write your code in a class extending LinearOpMode,which itself extends OpMode.
Pros: You write sequential code:do A, then B, then C, ...
Cons: It feels like a hack (because it is).Just extend OpMode and use a state machine to keep trackof the actions.
Pros: Good opportunity to learn an important computerscience paradigm
Cons: See Pros.
David Austin FTC Programming with Android Studio
Two solutions
There are two solutions to this problem.
Write your code in a class extending LinearOpMode,which itself extends OpMode.
Pros: You write sequential code:do A, then B, then C, ...
Cons: It feels like a hack (because it is).Just extend OpMode and use a state machine to keep trackof the actions.
Pros: Good opportunity to learn an important computerscience paradigm
Cons: See Pros.
David Austin FTC Programming with Android Studio
LinearOpMode
In a LinearOpMode, you do not override the usual methods,init(), loop(), etc.
Everything goes in one method
public class OurAuton extends LinearOpMode {public void runOpMode() {
do initialization stuff
waitForStart();
do task Ado task Bdo task C
}}
David Austin FTC Programming with Android Studio
LinearOpMode
In a LinearOpMode, you do not override the usual methods,init(), loop(), etc.
Everything goes in one method
public class OurAuton extends LinearOpMode {public void runOpMode() {
do initialization stuff
waitForStart();
do task Ado task Bdo task C
}}
David Austin FTC Programming with Android Studio
LinearOpMode
Example: Run a motor for 10 seconds
public void runOpMode() {DcMotor motor
= hardwareMap.dcMotor.get(‘‘motor’’);
waitForStart();
motor.setPower(1);sleep(10000);motor.setPower(0);
}
David Austin FTC Programming with Android Studio
Motor run modes
Motors can be in one of four “run modes:”
RUN WITHOUT ENCODER: the setPower method does justthat. This is the default mode.STOP AND RESET ENCODER: this mode resets the motor’sencoder. Follow with an idle() command to allow timefor the reset.RUN TO POSITION: the setPower method causes themotor to drive to the desired encoder count, which is setwith setTargetPosition.RUN USING ENCODER: the setPower method causes themotor to turn at a specified percentage of its maximumspeed, which is set with setMaxSpeed.
David Austin FTC Programming with Android Studio
Motor run modes
Motors can be in one of four “run modes:”
RUN WITHOUT ENCODER: the setPower method does justthat. This is the default mode.
STOP AND RESET ENCODER: this mode resets the motor’sencoder. Follow with an idle() command to allow timefor the reset.RUN TO POSITION: the setPower method causes themotor to drive to the desired encoder count, which is setwith setTargetPosition.RUN USING ENCODER: the setPower method causes themotor to turn at a specified percentage of its maximumspeed, which is set with setMaxSpeed.
David Austin FTC Programming with Android Studio
Motor run modes
Motors can be in one of four “run modes:”
RUN WITHOUT ENCODER: the setPower method does justthat. This is the default mode.STOP AND RESET ENCODER: this mode resets the motor’sencoder. Follow with an idle() command to allow timefor the reset.
RUN TO POSITION: the setPower method causes themotor to drive to the desired encoder count, which is setwith setTargetPosition.RUN USING ENCODER: the setPower method causes themotor to turn at a specified percentage of its maximumspeed, which is set with setMaxSpeed.
David Austin FTC Programming with Android Studio
Motor run modes
Motors can be in one of four “run modes:”
RUN WITHOUT ENCODER: the setPower method does justthat. This is the default mode.STOP AND RESET ENCODER: this mode resets the motor’sencoder. Follow with an idle() command to allow timefor the reset.RUN TO POSITION: the setPower method causes themotor to drive to the desired encoder count, which is setwith setTargetPosition.
RUN USING ENCODER: the setPower method causes themotor to turn at a specified percentage of its maximumspeed, which is set with setMaxSpeed.
David Austin FTC Programming with Android Studio
Motor run modes
Motors can be in one of four “run modes:”
RUN WITHOUT ENCODER: the setPower method does justthat. This is the default mode.STOP AND RESET ENCODER: this mode resets the motor’sencoder. Follow with an idle() command to allow timefor the reset.RUN TO POSITION: the setPower method causes themotor to drive to the desired encoder count, which is setwith setTargetPosition.RUN USING ENCODER: the setPower method causes themotor to turn at a specified percentage of its maximumspeed, which is set with setMaxSpeed.
David Austin FTC Programming with Android Studio
Hardware
Encapsulate your hardware into a single class that can beshared between OpModes. Easy to maintain and reuse.
public void Robot() {DcMotor motor;public Robot(HardwareMap hardwaremap) {
motor = hardwareMap.dcMotor.get(‘‘motor’’);}public void setMotorPower(double power) {
motor.setPower(power);}public double getMotorPosition() {
return motor.getCurrentPosition();}
}
David Austin FTC Programming with Android Studio
Hardware
Instantiate and reference from within your OpModes.
public void runOpMode() {
Robot robot = new Robot(hardwareMap);waitForStart();robot.setMotorPower(1);while (isOpModeActive() &&
robot.getMotorPosition() < 5000) {telemetry.addData(‘‘Motor: ‘‘,
robot.getMotorPosition());telemetry.update();
}robot.setMotorPower(0);
}
David Austin FTC Programming with Android Studio
State machine
An alternative to LinearOpModes:
Mimic FRC’s command-based programming paradigm. Createan interface BasicCommand with methods
public void init()
public void execute()
public boolean isFinished()
public void stop()
Create commands to perform specific actions, such asDriveForward(double inches).
David Austin FTC Programming with Android Studio
State machine
An alternative to LinearOpModes:
Mimic FRC’s command-based programming paradigm. Createan interface BasicCommand with methods
public void init()
public void execute()
public boolean isFinished()
public void stop()
Create commands to perform specific actions, such asDriveForward(double inches).
David Austin FTC Programming with Android Studio
State machine
An alternative to LinearOpModes:
Mimic FRC’s command-based programming paradigm. Createan interface BasicCommand with methods
public void init()
public void execute()
public boolean isFinished()
public void stop()
Create commands to perform specific actions, such asDriveForward(double inches).
David Austin FTC Programming with Android Studio
State machine
An alternative to LinearOpModes:
Mimic FRC’s command-based programming paradigm. Createan interface BasicCommand with methods
public void init()
public void execute()
public boolean isFinished()
public void stop()
Create commands to perform specific actions, such asDriveForward(double inches).
David Austin FTC Programming with Android Studio
State machine
An alternative to LinearOpModes:
Mimic FRC’s command-based programming paradigm. Createan interface BasicCommand with methods
public void init()
public void execute()
public boolean isFinished()
public void stop()
Create commands to perform specific actions, such asDriveForward(double inches).
David Austin FTC Programming with Android Studio
State machine
An alternative to LinearOpModes:
Mimic FRC’s command-based programming paradigm. Createan interface BasicCommand with methods
public void init()
public void execute()
public boolean isFinished()
public void stop()
Create commands to perform specific actions, such asDriveForward(double inches).
David Austin FTC Programming with Android Studio
State machine
Create an OpMode as a list of BasicCommands. At any onetime, there is a currentCommand.
Within the loop() method, we are in of four states:INIT, EXECUTE, STOP, DONE
State machine:
INIT: initialize currentCommand and go to EXECUTE
EXECUTE: execute currentCommand. IfcurrentCommand.isFinished(), go to STOP.STOP: stop currentCommand. If there is anothercommand, go to INIT. If not, go to DONE
DONE: just relax.
David Austin FTC Programming with Android Studio
State machine
Create an OpMode as a list of BasicCommands. At any onetime, there is a currentCommand.
Within the loop() method, we are in of four states:INIT, EXECUTE, STOP, DONE
State machine:INIT: initialize currentCommand and go to EXECUTE
EXECUTE: execute currentCommand. IfcurrentCommand.isFinished(), go to STOP.STOP: stop currentCommand. If there is anothercommand, go to INIT. If not, go to DONE
DONE: just relax.
David Austin FTC Programming with Android Studio
State machine
Create an OpMode as a list of BasicCommands. At any onetime, there is a currentCommand.
Within the loop() method, we are in of four states:INIT, EXECUTE, STOP, DONE
State machine:INIT: initialize currentCommand and go to EXECUTE
EXECUTE: execute currentCommand. IfcurrentCommand.isFinished(), go to STOP.
STOP: stop currentCommand. If there is anothercommand, go to INIT. If not, go to DONE
DONE: just relax.
David Austin FTC Programming with Android Studio
State machine
Create an OpMode as a list of BasicCommands. At any onetime, there is a currentCommand.
Within the loop() method, we are in of four states:INIT, EXECUTE, STOP, DONE
State machine:INIT: initialize currentCommand and go to EXECUTE
EXECUTE: execute currentCommand. IfcurrentCommand.isFinished(), go to STOP.STOP: stop currentCommand. If there is anothercommand, go to INIT. If not, go to DONE
DONE: just relax.
David Austin FTC Programming with Android Studio
State machine
Create an OpMode as a list of BasicCommands. At any onetime, there is a currentCommand.
Within the loop() method, we are in of four states:INIT, EXECUTE, STOP, DONE
State machine:INIT: initialize currentCommand and go to EXECUTE
EXECUTE: execute currentCommand. IfcurrentCommand.isFinished(), go to STOP.STOP: stop currentCommand. If there is anothercommand, go to INIT. If not, go to DONE
DONE: just relax.
David Austin FTC Programming with Android Studio
Resources
Resources:FTC resources:https://www.firstinspires.org/node/5291
Github repository:https://github.com/ftctechnh/ftc app
User’s manual:https://github.com/ftctechnh/ftc app/releases
FTC Control System Wiki:https://github.com/ftctechnh/ftc app/wiki
David Austin: [email protected]
David Austin FTC Programming with Android Studio
Resources
Resources:FTC resources:https://www.firstinspires.org/node/5291
Github repository:https://github.com/ftctechnh/ftc app
User’s manual:https://github.com/ftctechnh/ftc app/releases
FTC Control System Wiki:https://github.com/ftctechnh/ftc app/wiki
David Austin: [email protected]
David Austin FTC Programming with Android Studio
Resources
Resources:FTC resources:https://www.firstinspires.org/node/5291
Github repository:https://github.com/ftctechnh/ftc app
User’s manual:https://github.com/ftctechnh/ftc app/releases
FTC Control System Wiki:https://github.com/ftctechnh/ftc app/wiki
David Austin: [email protected]
David Austin FTC Programming with Android Studio
Resources
Resources:FTC resources:https://www.firstinspires.org/node/5291
Github repository:https://github.com/ftctechnh/ftc app
User’s manual:https://github.com/ftctechnh/ftc app/releases
FTC Control System Wiki:https://github.com/ftctechnh/ftc app/wiki
David Austin: [email protected]
David Austin FTC Programming with Android Studio
Resources
Resources:FTC resources:https://www.firstinspires.org/node/5291
Github repository:https://github.com/ftctechnh/ftc app
User’s manual:https://github.com/ftctechnh/ftc app/releases
FTC Control System Wiki:https://github.com/ftctechnh/ftc app/wiki
David Austin: [email protected]
David Austin FTC Programming with Android Studio