public static UniTask <Cube.TargetMoveRespondType> TargetMove(Cube cube, int targetX, int targetY, int targetAngle, int configID = 0, int timeOut = 0, Cube.TargetMoveType targetMoveType = Cube.TargetMoveType.RotatingMove, int maxSpd = 80, Cube.TargetSpeedType targetSpeedType = Cube.TargetSpeedType.UniformSpeed, Cube.TargetRotationType targetRotationType = Cube.TargetRotationType.AbsoluteLeastAngle, Cube.ORDER_TYPE order = Cube.ORDER_TYPE.Strong) { var source = new UniTaskCompletionSource <Cube.TargetMoveRespondType>(); var callbackKey = Guid.NewGuid().ToString(); cube.targetMoveCallback.RemoveListener(callbackKey); cube.targetMoveCallback.AddListener(callbackKey, (_cube, _configId, _respondType) => { if (_configId == configID) { DelayedRemoveTargetMoveCallback(cube, callbackKey); source.TrySetResult(_respondType); } }); cube.TargetMove(targetX, targetY, targetAngle, configID, timeOut, targetMoveType, maxSpd, targetSpeedType, targetRotationType, order); return(source.Task); }
// COMMAND API public override void TargetMove( int targetX, int targetY, int targetAngle, int configID, int timeOut, Cube.TargetMoveType targetMoveType, int maxSpd, Cube.TargetSpeedType targetSpeedType, Cube.TargetRotationType targetRotationType ) { #if !RELEASE if (65535 < targetX || targetX < -1) { Debug.LogErrorFormat("[Cube.TargetMove]X座標範囲を超えました. targetX={0}", targetX); } if (65535 < targetY || targetY < -1) { Debug.LogErrorFormat("[Cube.TargetMove]Y座標範囲を超えました. targetY={0}", targetY); } if (8191 < targetAngle) { Debug.LogErrorFormat("[Cube.TargetMove]回転角度範囲を超えました. targetAngle={0}", targetAngle); } if (255 < configID) { Debug.LogErrorFormat("[Cube.TargetMove]制御識別値範囲を超えました. configID={0}", configID); } if (255 < timeOut) { Debug.LogErrorFormat("[Cube.TargetMove]制御時間範囲を超えました. timeOut={0}", timeOut); } if (this.maxMotor < maxSpd) { Debug.LogErrorFormat("[Cube.TargetMove]速度範囲を超えました. maxSpd={0}", maxSpd); } #endif MotorTargetCmd cmd = new MotorTargetCmd(); cmd.x = (ushort)(targetX == -1?65535:Mathf.Clamp(targetX, 0, 65535)); cmd.y = (ushort)(targetY == -1?65535:Mathf.Clamp(targetY, 0, 65535)); cmd.deg = (ushort)Mathf.Clamp(targetAngle, 0, 8191); cmd.configID = (byte)Mathf.Clamp(configID, 0, 255); cmd.timeOut = (byte)Mathf.Clamp(timeOut, 0, 255); cmd.targetMoveType = targetMoveType; cmd.maxSpd = (byte)Mathf.Clamp(maxSpd, 0, this.maxMotor); cmd.targetSpeedType = targetSpeedType; cmd.targetRotationType = targetRotationType; cmd.tRecv = Time.time; MotorSetCmd(MotorCmdType.MotorTargetCmd, cmd); }
// --------- 2.1.0 -------- public void TargetMove( int targetX, int targetY, int targetAngle, int configID, int timeOut, Cube.TargetMoveType targetMoveType, int maxSpd, Cube.TargetSpeedType targetSpeedType, Cube.TargetRotationType targetRotationType ) { DelayCommand(() => impl.TargetMove(targetX, targetY, targetAngle, configID, timeOut, targetMoveType, maxSpd, targetSpeedType, targetRotationType)); }
public void MultiTargetMove( int[] targetXList, int[] targetYList, int[] targetAngleList, Cube.TargetRotationType[] multiRotationTypeList, int configID, int timeOut, Cube.TargetMoveType targetMoveType, int maxSpd, Cube.TargetSpeedType targetSpeedType, Cube.MultiWriteType multiWriteType ) { DelayCommand(() => impl.MultiTargetMove(targetXList, targetYList, targetAngleList, multiRotationTypeList, configID, timeOut, targetMoveType, maxSpd, targetSpeedType, multiWriteType)); }
// COMMAND API public override void MultiTargetMove( int[] targetXList, int[] targetYList, int[] targetAngleList, Cube.TargetRotationType[] multiRotationTypeList, int configID, int timeOut, Cube.TargetMoveType targetMoveType, int maxSpd, Cube.TargetSpeedType targetSpeedType, Cube.MultiWriteType multiWriteType ) { #if !RELEASE if (targetXList.Length == 0 || targetYList.Length == 0 || targetAngleList.Length == 0 || !(targetXList.Length == targetYList.Length && targetYList.Length == targetAngleList.Length)) { Debug.LogErrorFormat("[Cube.TargetMove]座標・角度リストのサイズが不一致または0. targetXList.Length={0}, targetYList.Length={1}, targetAngleList.Length={2}", targetXList.Length, targetYList.Length, targetAngleList.Length); } if (255 < configID) { Debug.LogErrorFormat("[Cube.TargetMove]制御識別値範囲を超えました. configID={0}", configID); } if (255 < timeOut) { Debug.LogErrorFormat("[Cube.TargetMove]制御時間範囲を超えました. timeOut={0}", timeOut); } if (this.maxMotor < maxSpd) { Debug.LogErrorFormat("[Cube.TargetMove]速度範囲を超えました. maxSpd={0}", maxSpd); } #endif MotorMultiTargetCmd cmd = new MotorMultiTargetCmd(); cmd.xs = Array.ConvertAll(targetXList, new Converter <int, ushort>(x => (ushort)(x == -1?65535:Mathf.Clamp(x, 0, 65534)))); if (cmd.xs.Length == 0) { return; } cmd.xs = cmd.xs.Take(29).ToArray(); cmd.ys = Array.ConvertAll(targetYList, new Converter <int, ushort>(x => (ushort)(x == -1?65535:Mathf.Clamp(x, 0, 65534)))); if (cmd.ys.Length == 0) { return; } cmd.ys = cmd.ys.Take(29).ToArray(); cmd.degs = Array.ConvertAll(targetAngleList, new Converter <int, ushort>(x => (ushort)x)); if (cmd.degs.Length == 0) { return; } cmd.degs = cmd.degs.Take(29).ToArray(); cmd.rotTypes = multiRotationTypeList != null? multiRotationTypeList : new Cube.TargetRotationType[cmd.xs.Length]; cmd.configID = (byte)Mathf.Clamp(configID, 0, 255); cmd.timeOut = (byte)Mathf.Clamp(timeOut, 0, 255); cmd.maxSpd = (byte)Mathf.Clamp(maxSpd, 0, this.maxMotor); cmd.targetMoveType = targetMoveType; cmd.targetSpeedType = targetSpeedType; cmd.multiWriteType = multiWriteType; cmd.tRecv = Time.time; MotorSetCmd(MotorCmdType.MotorMultiTargetCmd, cmd); }
protected (float, float) TargetMove_MoveControl( float elipsed, ushort x, ushort y, byte maxSpd, Cube.TargetSpeedType targetSpeedType, float acc, Cube.TargetMoveType targetMoveType ) { float spd = maxSpd; float translate = 0, rotate = 0; Vector2 targetPos = new Vector2(x, y); Vector2 pos = new Vector2(this.x, this.y); var dpos = targetPos - pos; var dir2tar = Vector2.SignedAngle(Vector2.right, dpos); var deg2tar = Deg(dir2tar - this.deg); // use when moving forward var deg2tar_back = (deg2tar + 360) % 360 - 180; // use when moving backward bool tarOnFront = Mathf.Abs(deg2tar) <= 90; // 速度変化タイプ switch (targetSpeedType) { case (Cube.TargetSpeedType.UniformSpeed): // 速度一定 { break; } case (Cube.TargetSpeedType.Acceleration): // 目標地点まで徐々に加速 { spd = Mathf.Clamp(this.deadzone + acc * elipsed, this.deadzone, maxSpd); break; } case (Cube.TargetSpeedType.Deceleration): // 目標地点まで徐々に減速 { spd = Mathf.Clamp(maxSpd - acc * elipsed, this.deadzone, maxSpd); break; } case (Cube.TargetSpeedType.VariableSpeed): // 中間地点まで徐々に加速し、そこから目標地点まで減速 { spd = Mathf.Clamp(maxSpd - 2 * acc * Mathf.Abs(elipsed - (maxSpd - this.deadzone) / acc / 2), this.deadzone, maxSpd); break; } } // 移動タイプ switch (targetMoveType) { case (Cube.TargetMoveType.RotatingMove): // 回転しながら移動 { rotate = tarOnFront? deg2tar : deg2tar_back; translate = tarOnFront? spd : -spd; break; } case (Cube.TargetMoveType.RoundForwardMove): // 回転しながら移動(後退なし) { rotate = deg2tar; translate = spd; break; } case (Cube.TargetMoveType.RoundBeforeMove): // 回転してから移動 { rotate = deg2tar; if (Mathf.Abs(deg2tar) < motorTargetDegTol) { translate = spd; } break; } } rotate *= 0.6f; rotate = Mathf.Clamp(rotate, -this.maxMotor, this.maxMotor); return(translate, rotate); }