Java 为状态机使用arraylist
我希望能够像数组列表一样向枚举变量状态机添加案例。为此,我创建了一个数组列表,并使用大小告诉它有多少个案例,并使用for循环相应地执行它们。问题是在我的方法调用完成之前,它将进入下一个案例。如何使程序在不使用时间延迟的情况下等待方法完成,因为时间可能因不同情况而异。case和方法位于loop()方法中代码的底部Java 为状态机使用arraylist,java,android,arraylist,state-machine,robotics,Java,Android,Arraylist,State Machine,Robotics,我希望能够像数组列表一样向枚举变量状态机添加案例。为此,我创建了一个数组列表,并使用大小告诉它有多少个案例,并使用for循环相应地执行它们。问题是在我的方法调用完成之前,它将进入下一个案例。如何使程序在不使用时间延迟的情况下等待方法完成,因为时间可能因不同情况而异。case和方法位于loop()方法中代码的底部 package com.qualcomm.ftcrobotcontroller.opmodes; 进口com.qualcomm.hardware.HiTechnicNxtCompassS
package com.qualcomm.ftcrobotcontroller.opmodes;
进口com.qualcomm.hardware.HiTechnicNxtCompassSensor;
导入com.qualcomm.robotcore.eventloop.opmode.opmode;
进口com.qualcomm.robotcore.hardware.DcMotor;
导入com.qualcomm.robotcore.hardware.DcMotorController;
导入com.qualcomm.robotcore.util.ElapsedTime;
导入java.util.ArrayList;
/**
*由Robotics于2015年11月12日创建。
*/
公共类机器人动作扩展了OpMode{
//高科技NXTCompassSensor compassSensor;
//double compassvalue=compassSensor.getDirection();
双罗盘价值观;
双罗盘值;
直流电机右驱动;
直流电机驱动;
最终双满功率=1;
双角度;
双倍距离;
最终双轮流量计=4.0;
最终双周长=车轮直径*Math.PI;
最终双传动比=1;
最终双计数曲线=1440;
双轮旋转=距离/周长;
双电机旋转=车轮旋转*传动比;
双编码器计数=电机旋转*计数箭头;
国际货币基金组织;
整数步;
延迟时间;
字符串状态;
弦作用;
公共枚举相对位置{北、东北、东、东南、南、西南、西、西北}
相对位置相对位置;
私有无效操作(双角度、整数compassvalue、双编码器计数、相对位置){
action=“running”;
lastcompassvalue=compassvalue;
开关(相对位置){
案例北:
currentpositionright=rightdrive.getCurrentPosition();
if(rightdrive.getCurrentPosition()lastcompassvalue-(90角)){
右驱动。设置方向(直流电机。方向。前进);
rightdrive.setPower(全功率);
leftdrive.setPower(全功率);
}
否则{
if(rightdrive.getCurrentPosition()lastcompassvalue-90){
右驱动。设置方向(直流电机。方向。前进);
rightdrive.setPower(全功率);
leftdrive.setPower(全功率);
}
否则{
if(rightdrive.getCurrentPosition()la)
package com.qualcomm.ftcrobotcontroller.opmodes;
import com.qualcomm.hardware.HiTechnicNxtCompassSensor;
import com.qualcomm.robotcore.eventloop.opmode.OpMode;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.DcMotorController;
import com.qualcomm.robotcore.util.ElapsedTime;
import java.util.ArrayList;
/**
* Created by Robotics on 12/11/2015.
*/
public class RobotAction extends OpMode {
//HiTechnicNxtCompassSensor compassSensor;
//double compassvalue = compassSensor.getDirection();
double compassvalue;
double lastcompassvalue;
DcMotor rightdrive;
DcMotor leftdrive;
final double fullpower = 1;
double angle;
double distance;
final double wheelediameter = 4.0;
final double circumference = wheelediameter * Math.PI;
final double gearratio = 1;
final double countsperrotation = 1440;
double wheelrotations = distance / circumference;
double motorrotations = wheelrotations * gearratio;
double encodercounts = motorrotations * countsperrotation;
int currentpositionright;
int step;
ElapsedTime time;
String status;
String action;
public enum RelativePosition {North, NorthEast, East, SouthEast, South, SouthWest, West, NorthWest}
RelativePosition relativeposition;
private void Action(double angle, int compassvalue, double encodercounts, RelativePosition relativeposition) {
action = "running";
lastcompassvalue = compassvalue;
switch(relativeposition) {
case North:
currentpositionright = rightdrive.getCurrentPosition();
if (rightdrive.getCurrentPosition() < encodercounts) {
rightdrive.setTargetPosition((int) (encodercounts + 1));
leftdrive.setTargetPosition((int) (encodercounts + 1));
rightdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION);
leftdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION);
rightdrive.setDirection(DcMotor.Direction.REVERSE);
rightdrive.setPower(.5);
leftdrive.setPower(.5);
}
else {
rightdrive.setPower(0);
leftdrive.setPower(0);
rightdrive.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS);
leftdrive.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS);
}
break;
case NorthEast:
currentpositionright = rightdrive.getCurrentPosition();
if (compassvalue > lastcompassvalue - (90-angle)) {
rightdrive.setDirection(DcMotor.Direction.FORWARD);
rightdrive.setPower(fullpower);
leftdrive.setPower(fullpower);
}
else {
if(rightdrive.getCurrentPosition() < encodercounts) {
rightdrive.setTargetPosition((int) encodercounts + 1);
leftdrive.setTargetPosition((int) encodercounts + 1);
rightdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION);
leftdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION);
rightdrive.setDirection(DcMotor.Direction.REVERSE);
rightdrive.setPower(fullpower);
leftdrive.setPower(fullpower);
}
else {
rightdrive.setPower(0);
leftdrive.setPower(0);
rightdrive.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS);
leftdrive.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS);
}
}
break;
case East:
currentpositionright = rightdrive.getCurrentPosition();
if (compassvalue > lastcompassvalue - 90) {
rightdrive.setDirection(DcMotor.Direction.FORWARD);
rightdrive.setPower(fullpower);
leftdrive.setPower(fullpower);
}
else {
if(rightdrive.getCurrentPosition() < encodercounts) {
rightdrive.setTargetPosition((int) encodercounts + 1);
leftdrive.setTargetPosition((int) encodercounts + 1);
rightdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION);
leftdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION);
rightdrive.setDirection(DcMotor.Direction.REVERSE);
rightdrive.setPower(fullpower);
leftdrive.setPower(fullpower);
}
else {
rightdrive.setPower(0);
leftdrive.setPower(0);
rightdrive.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS);
leftdrive.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS);
}
}
break;
case SouthEast:
currentpositionright = rightdrive.getCurrentPosition();
if (compassvalue > lastcompassvalue - (90+angle)) {
rightdrive.setDirection(DcMotor.Direction.FORWARD);
rightdrive.setPower(fullpower);
leftdrive.setPower(fullpower);
}
else {
if(rightdrive.getCurrentPosition() < encodercounts) {
rightdrive.setTargetPosition((int) encodercounts);
leftdrive.setTargetPosition((int) encodercounts);
rightdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION);
leftdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION);
rightdrive.setDirection(DcMotor.Direction.REVERSE);
rightdrive.setPower(fullpower);
leftdrive.setPower(fullpower);
}
else {
rightdrive.setPower(0);
leftdrive.setPower(0);
rightdrive.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS);
leftdrive.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS);
}
}
break;
case South:
currentpositionright = rightdrive.getCurrentPosition();
if (compassvalue > lastcompassvalue - 180) {
rightdrive.setDirection(DcMotor.Direction.FORWARD);
rightdrive.setPower(fullpower);
leftdrive.setPower(fullpower);
}
else {
if(rightdrive.getCurrentPosition() < encodercounts) {
rightdrive.setTargetPosition((int) encodercounts + 1);
leftdrive.setTargetPosition((int) encodercounts + 1);
rightdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION);
leftdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION);
rightdrive.setDirection(DcMotor.Direction.REVERSE);
rightdrive.setPower(fullpower);
leftdrive.setPower(fullpower);
}
else {
rightdrive.setPower(0);
leftdrive.setPower(0);
rightdrive.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS);
leftdrive.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS);
}
}
break;
case SouthWest:
currentpositionright = rightdrive.getCurrentPosition();
if (compassvalue < lastcompassvalue + (90+angle) ) {
rightdrive.setPower(fullpower);
leftdrive.setDirection(DcMotor.Direction.REVERSE);
leftdrive.setPower(fullpower);
}
else {
if(rightdrive.getCurrentPosition() < encodercounts) {
rightdrive.setTargetPosition((int) encodercounts + 1);
leftdrive.setTargetPosition((int) encodercounts + 1);
rightdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION);
leftdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION);
rightdrive.setPower(fullpower);
leftdrive.setDirection(DcMotor.Direction.FORWARD);
leftdrive.setPower(fullpower);
}
else {
rightdrive.setPower(0);
leftdrive.setPower(0);
rightdrive.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS);
leftdrive.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS);
}
}
break;
case West:
currentpositionright = rightdrive.getCurrentPosition();
if (compassvalue < lastcompassvalue + 90) {
rightdrive.setPower(fullpower);
leftdrive.setDirection(DcMotor.Direction.REVERSE);
leftdrive.setPower(fullpower);
}
else {
if(rightdrive.getCurrentPosition() < encodercounts) {
rightdrive.setTargetPosition((int) encodercounts + 1);
leftdrive.setTargetPosition((int) encodercounts + 1);
rightdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION);
leftdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION);
rightdrive.setPower(fullpower);
leftdrive.setDirection(DcMotor.Direction.FORWARD);
leftdrive.setPower(fullpower);
}
else {
rightdrive.setPower(0);
leftdrive.setPower(0);
rightdrive.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS);
leftdrive.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS);
}
}
break;
case NorthWest:
currentpositionright = rightdrive.getCurrentPosition();
if (compassvalue < lastcompassvalue + (90-angle)) {
rightdrive.setPower(fullpower);
leftdrive.setDirection(DcMotor.Direction.REVERSE);
leftdrive.setPower(fullpower);
}
else {
if(rightdrive.getCurrentPosition() < encodercounts) {
rightdrive.setTargetPosition((int) encodercounts + 1);
leftdrive.setTargetPosition((int) encodercounts + 1);
rightdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION);
leftdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION);
rightdrive.setPower(fullpower);
leftdrive.setDirection(DcMotor.Direction.FORWARD);
leftdrive.setPower(fullpower);
}
else {
rightdrive.setPower(0);
leftdrive.setPower(0);
rightdrive.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS);
leftdrive.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS);
}
}
break;
}
action = "done";
}
private final ArrayList<Integer> Step = new ArrayList<>();
@Override
public void init() {
rightdrive = hardwareMap.dcMotor.get("right_drive");
leftdrive = hardwareMap.dcMotor.get("left_drive");
rightdrive.setDirection(DcMotor.Direction.REVERSE);
leftdrive.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS);
rightdrive.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS);
step = 1;
Step.add(step);
//compassSensor = (HiTechnicNxtCompassSensor) hardwareMap.compassSensor.get("compass");
compassvalue = 0;
relativeposition = RelativePosition.North;
distance = 12;
angle = 60;
}
public void start() {
status = "Running";
time = new ElapsedTime();
time.reset();
}
@Override
public void loop() {
telemetry.addData("Status", status);
double currenttime = time.time();
int size = Step.size();
for (int i=0; i<size; i++) {
int element = Step.get(i);
switch (element) {
case 1:
Action(angle, (int) compassvalue,encodercounts,relativeposition);
step++;
Step.add(step);
break;
case 2:
rightdrive.setPower(0);
leftdrive.setPower(0);
status = "Done";
}
}
}
}