Add commetns and refactoring
This commit is contained in:
@@ -1,101 +1,120 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
///-----------------------------------------------------------------
|
||||
/// Namespace: <Cozmo>
|
||||
/// Class: <CozmoMovement>
|
||||
/// Description: <Defines the possible movement of the virtual cozmo.>
|
||||
/// Author: <Tobias Hassel> Date: <29.07.2019>
|
||||
/// Notes: <>
|
||||
///-----------------------------------------------------------------
|
||||
///
|
||||
|
||||
|
||||
using UnityEngine;
|
||||
|
||||
public class CozmoMovement : MonoBehaviour
|
||||
namespace Cozmo
|
||||
{
|
||||
|
||||
public float m_Speed = 12f; // How fast the tank moves forward and back.
|
||||
public float m_TurnSpeed = 180f; // How fast the tank turns in degrees per second.
|
||||
|
||||
|
||||
private string m_MovementAxisName; // The name of the input axis for moving forward and back.
|
||||
private string m_TurnAxisName; // The name of the input axis for turning.
|
||||
private Rigidbody m_Rigidbody; // Reference used to move the tank.
|
||||
private float m_MovementInputValue; // The current value of the movement input.
|
||||
private float m_TurnInputValue; // The current value of the turn input.
|
||||
|
||||
|
||||
private void Awake()
|
||||
public class CozmoMovement : MonoBehaviour
|
||||
{
|
||||
m_Rigidbody = GetComponent<Rigidbody>();
|
||||
private const string MOVEMENT_AXIS_NAME = "Vertical"; // name of the movement input axis
|
||||
private const string TURN_AXIS_NAME = "Horizontal"; // name of the rotation input axis
|
||||
|
||||
[Tooltip("Determines how fast the robot is moving forward and backwards.")]
|
||||
public float m_Speed = 12f;
|
||||
[Tooltip("Determines how fast the robot is turning in degrees per second.")]
|
||||
public float m_TurnSpeed = 180f;
|
||||
|
||||
private Rigidbody cozmoRigidbody; // Reference to cozmos rigidbody
|
||||
private float m_MovementInputValue; // current input value for movement
|
||||
private float m_TurnInputValue; // current input value for rotation
|
||||
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
cozmoRigidbody = GetComponent<Rigidbody>();
|
||||
}
|
||||
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
// When the cozmo is turned on, make sure it's not kinematic.
|
||||
cozmoRigidbody.isKinematic = false;
|
||||
|
||||
// reset movement and rotation values
|
||||
m_MovementInputValue = 0f;
|
||||
m_TurnInputValue = 0f;
|
||||
}
|
||||
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
// When the cozmo is turned off, set it to kinematic so it stops moving.
|
||||
cozmoRigidbody.isKinematic = true;
|
||||
}
|
||||
|
||||
|
||||
private void Update()
|
||||
{
|
||||
// Store the value of both input axes.
|
||||
m_MovementInputValue = Input.GetAxis(MOVEMENT_AXIS_NAME);
|
||||
m_TurnInputValue = Input.GetAxis(TURN_AXIS_NAME);
|
||||
}
|
||||
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
// Adjust position and rotation
|
||||
Move();
|
||||
Turn();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Move the cozmo forward (0 < directionValue <= 1) or backwards (0 > directionvalue >= -1)
|
||||
/// </summary>
|
||||
/// <param name="directionValue"></param>
|
||||
public void Move(float directionValue)
|
||||
{
|
||||
// clamp directionValue to make sure its between -1 and 1
|
||||
Mathf.Clamp(directionValue, -1, 1);
|
||||
|
||||
Vector3 movement = directionValue * transform.forward * m_Speed * Time.deltaTime;
|
||||
|
||||
// Apply this movement to the rigidbody's position.
|
||||
cozmoRigidbody.MovePosition(cozmoRigidbody.position + movement);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Move cozmo based on the input value
|
||||
/// </summary>
|
||||
public void Move()
|
||||
{
|
||||
Move(m_MovementInputValue);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Rotate cozmo based on the input value
|
||||
/// </summary>
|
||||
public void Turn()
|
||||
{
|
||||
Turn(m_TurnInputValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Rotate cozmo
|
||||
/// </summary>
|
||||
/// <param name="turnValue"></param>
|
||||
public void Turn(float turnValue)
|
||||
{
|
||||
// clamp turnValue to make sure its between -1 and 1
|
||||
Mathf.Clamp(turnValue, -1, 1);
|
||||
|
||||
float turn = turnValue * m_TurnSpeed * Time.deltaTime;
|
||||
|
||||
// Make this into a rotation in the y axis.
|
||||
Quaternion turnRotation = Quaternion.Euler(0f, turn, 0f);
|
||||
|
||||
// Apply this rotation to the rigidbody's rotation.
|
||||
cozmoRigidbody.MoveRotation(cozmoRigidbody.rotation * turnRotation);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
// When the cozmo is turned on, make sure it's not kinematic.
|
||||
m_Rigidbody.isKinematic = false;
|
||||
|
||||
// Also reset the input values.
|
||||
m_MovementInputValue = 0f;
|
||||
m_TurnInputValue = 0f;
|
||||
}
|
||||
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
// When the cozmo is turned off, set it to kinematic so it stops moving.
|
||||
m_Rigidbody.isKinematic = true;
|
||||
}
|
||||
|
||||
|
||||
private void Start()
|
||||
{
|
||||
m_MovementAxisName = "Vertical";
|
||||
m_TurnAxisName = "Horizontal";
|
||||
}
|
||||
|
||||
|
||||
private void Update()
|
||||
{
|
||||
// Store the value of both input axes.
|
||||
m_MovementInputValue = Input.GetAxis(m_MovementAxisName);
|
||||
m_TurnInputValue = Input.GetAxis(m_TurnAxisName);
|
||||
}
|
||||
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
// Adjust the rigidbodies position and orientation in FixedUpdate.
|
||||
Move();
|
||||
Turn();
|
||||
}
|
||||
|
||||
public void Move(float directionValue)
|
||||
{
|
||||
// Create a vector in the direction the cozmo is facing with a magnitude based on the input, speed and the time between frames.
|
||||
Vector3 movement = directionValue * transform.forward * m_Speed * Time.deltaTime;
|
||||
|
||||
// Apply this movement to the rigidbody's position.
|
||||
m_Rigidbody.MovePosition(m_Rigidbody.position + movement);
|
||||
}
|
||||
|
||||
public void Move()
|
||||
{
|
||||
Move(m_MovementInputValue);
|
||||
}
|
||||
|
||||
//public void Move(float[] act)
|
||||
//{
|
||||
|
||||
//}
|
||||
|
||||
public void Turn()
|
||||
{
|
||||
Turn(m_TurnInputValue);
|
||||
}
|
||||
|
||||
public void Turn(float turnValue)
|
||||
{
|
||||
// Determine the number of degrees to be turned based on the input, speed and time between frames.
|
||||
float turn = turnValue * m_TurnSpeed * Time.deltaTime;
|
||||
|
||||
// Make this into a rotation in the y axis.
|
||||
Quaternion turnRotation = Quaternion.Euler(0f, turn, 0f);
|
||||
|
||||
// Apply this rotation to the rigidbody's rotation.
|
||||
m_Rigidbody.MoveRotation(m_Rigidbody.rotation * turnRotation);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,45 +1,55 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
///-----------------------------------------------------------------
|
||||
/// Namespace: <Cozmo>
|
||||
/// Class: <CozmoMovementController>
|
||||
/// Description: <Statemachine to control the movement of the virtual cozmo.>
|
||||
/// Author: <Tobias Hassel> Date: <29.07.2019>
|
||||
/// Notes: <>
|
||||
///-----------------------------------------------------------------
|
||||
///
|
||||
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
public enum MovementState { Stop, Forward, Right, Left }
|
||||
|
||||
[RequireComponent(typeof(CozmoMovement))]
|
||||
public class CozmoMovementController : MonoBehaviour
|
||||
namespace Cozmo
|
||||
{
|
||||
[Tooltip("Current MovementState of the Robot. Change this to make the robot move.")]
|
||||
public MovementState currentMovementState = MovementState.Stop;
|
||||
// Different movementstates cozmo can use
|
||||
public enum MovementState { Stop, Forward, Right, Left }
|
||||
|
||||
private CozmoMovement movement; // Movement script of the robot
|
||||
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
[RequireComponent(typeof(CozmoMovement))]
|
||||
public class CozmoMovementController : MonoBehaviour
|
||||
{
|
||||
movement = GetComponent<CozmoMovement>();
|
||||
}
|
||||
[Tooltip("Current MovementState of the Robot. Change this to make the robot move.")]
|
||||
public MovementState currentMovementState = MovementState.Stop;
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
switch (currentMovementState)
|
||||
private CozmoMovement movement; // Movement script of the robot
|
||||
|
||||
|
||||
void Start()
|
||||
{
|
||||
case MovementState.Stop:
|
||||
movement.Move(0);
|
||||
break;
|
||||
case MovementState.Forward:
|
||||
movement.Move(1);
|
||||
break;
|
||||
case MovementState.Right:
|
||||
movement.Turn(1);
|
||||
break;
|
||||
case MovementState.Left:
|
||||
movement.Turn(-1);
|
||||
break;
|
||||
default:
|
||||
movement.Move(0);
|
||||
throw new ArgumentException("No real Movementstate was given. Default 'Stop' was chosen.");
|
||||
movement = GetComponent<CozmoMovement>();
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
switch (currentMovementState)
|
||||
{
|
||||
case MovementState.Stop:
|
||||
movement.Move(0);
|
||||
break;
|
||||
case MovementState.Forward:
|
||||
movement.Move(1);
|
||||
break;
|
||||
case MovementState.Right:
|
||||
movement.Turn(1);
|
||||
break;
|
||||
case MovementState.Left:
|
||||
movement.Turn(-1);
|
||||
break;
|
||||
default:
|
||||
movement.Move(0);
|
||||
throw new ArgumentException("No real Movementstate was given. Default 'Stop' was chosen.");
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,113 +1,126 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
///-----------------------------------------------------------------
|
||||
/// Namespace: <CozmoHelpers>
|
||||
/// Class: <CozmoMovementTester>
|
||||
/// Description: <Used to measure the time for rotating and moving the virtual cozmo to copy the
|
||||
/// real cozmos movement behaviour.>
|
||||
/// Author: <Tobias Hassel> Date: <29.07.2019>
|
||||
/// Notes: <>
|
||||
///-----------------------------------------------------------------
|
||||
///
|
||||
|
||||
using Cozmo;
|
||||
using System.Diagnostics;
|
||||
using UnityEngine;
|
||||
|
||||
public enum TestStates { MovementSpeed, RotationSpeed } // enum to determine test scenarios
|
||||
|
||||
[RequireComponent(typeof(CozmoMovement))]
|
||||
public class CozmoMovementTester : MonoBehaviour
|
||||
namespace CozmoHelpers
|
||||
{
|
||||
public enum TestStates { MovementSpeed, RotationSpeed } // enum to determine test scenarios
|
||||
|
||||
private const float DISTANCE_TO_MEASURE_SPEED = 1f; // Distance that is used to calculate cozmos movementspeed (in m)
|
||||
private const float ROTATION_ANGLE = 90f; // Angle that is used to calculate cozmos rotationspeed
|
||||
|
||||
private CozmoMovement movement; // Reference to the movement script of cozmo
|
||||
private Vector3 startPositionCozmo; // Original position of cozmo
|
||||
private float lastY; // Last Y of cozmo
|
||||
private Stopwatch movementWatch; // Stopwatch to measure the time for the movement usecase
|
||||
private Stopwatch rotationWatch; // Stopwatch to measure the time for the rotation usecase
|
||||
|
||||
[Tooltip("Choose which speed of the virtual cozmo should be tested.")]
|
||||
public TestStates testState = TestStates.MovementSpeed;
|
||||
|
||||
[Tooltip("Start and stop testing")]
|
||||
public bool isTesting = false;
|
||||
|
||||
|
||||
void Start()
|
||||
{
|
||||
movement = GetComponent<CozmoMovement>();
|
||||
movementWatch = new Stopwatch();
|
||||
rotationWatch = new Stopwatch();
|
||||
|
||||
|
||||
startPositionCozmo = movement.transform.position; // Cache the starting position of Cozmo
|
||||
lastY = movement.transform.rotation.y; // Cache the rotation Vector of Cozmo
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
if (isTesting)
|
||||
{
|
||||
if (testState == TestStates.MovementSpeed)
|
||||
{
|
||||
TestMovementSpeed();
|
||||
}
|
||||
else if (testState == TestStates.RotationSpeed)
|
||||
{
|
||||
TestRotationSpeed();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void TestRotationSpeed()
|
||||
[RequireComponent(typeof(CozmoMovement))]
|
||||
public class CozmoMovementTester : MonoBehaviour
|
||||
{
|
||||
|
||||
float currentY = movement.transform.rotation.eulerAngles.y;
|
||||
private const float DISTANCE_TO_MEASURE_SPEED = 1f; // Distance that is used to calculate cozmos movementspeed (in m)
|
||||
private const float ROTATION_ANGLE = 90f; // Angle that is used to calculate cozmos rotationspeed
|
||||
|
||||
// If distance between start and current position of cozmo reached the DISTANCE_TO_MEASURE_SPEED stop the stopwatch
|
||||
if (currentY >= lastY + ROTATION_ANGLE || currentY <= lastY - ROTATION_ANGLE)
|
||||
private CozmoMovement movement; // Reference to the movement script of cozmo
|
||||
private Vector3 startPositionCozmo; // Original position of cozmo
|
||||
private float lastY; // Last Y of cozmo
|
||||
private Stopwatch movementWatch; // Stopwatch to measure the time for the movement usecase
|
||||
private Stopwatch rotationWatch; // Stopwatch to measure the time for the rotation usecase
|
||||
|
||||
[Tooltip("Choose which speed of the virtual cozmo should be tested.")]
|
||||
public TestStates testState = TestStates.MovementSpeed;
|
||||
|
||||
[Tooltip("Start and stop testing from the inspector.")]
|
||||
public bool isTesting = false;
|
||||
|
||||
|
||||
void Start()
|
||||
{
|
||||
if (rotationWatch.IsRunning)
|
||||
{
|
||||
rotationWatch.Stop();
|
||||
UnityEngine.Debug.Log("Cozmo: " + gameObject.name +
|
||||
"\nElapsed time in milliseconds (Rotation): " + rotationWatch.ElapsedMilliseconds);
|
||||
}
|
||||
movement = GetComponent<CozmoMovement>();
|
||||
movementWatch = new Stopwatch();
|
||||
rotationWatch = new Stopwatch();
|
||||
|
||||
isTesting = false;
|
||||
return;
|
||||
|
||||
startPositionCozmo = movement.transform.position; // Cache the starting position of Cozmo
|
||||
lastY = movement.transform.rotation.y; // Cache the rotation Vector of Cozmo
|
||||
}
|
||||
else
|
||||
{
|
||||
// Start stopwatch to measure the time cozmo needs for a specific rotation
|
||||
if (!rotationWatch.IsRunning)
|
||||
{
|
||||
rotationWatch = Stopwatch.StartNew();
|
||||
}
|
||||
|
||||
// Turn right
|
||||
movement.Turn(1);
|
||||
private void FixedUpdate()
|
||||
{
|
||||
if (isTesting)
|
||||
{
|
||||
if (testState == TestStates.MovementSpeed)
|
||||
{
|
||||
TestMovementSpeed();
|
||||
}
|
||||
else if (testState == TestStates.RotationSpeed)
|
||||
{
|
||||
TestRotationSpeed();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void TestMovementSpeed()
|
||||
{
|
||||
// If distance between start and current position of cozmo reached the DISTANCE_TO_MEASURE_SPEED stop the stopwatch
|
||||
if (Vector3.Distance(startPositionCozmo, movement.transform.position) >= DISTANCE_TO_MEASURE_SPEED)
|
||||
// Measure time for rotation
|
||||
private void TestRotationSpeed()
|
||||
{
|
||||
if (movementWatch.IsRunning)
|
||||
{
|
||||
movementWatch.Stop();
|
||||
UnityEngine.Debug.Log("Cozmo: " + gameObject.name +
|
||||
"/nElapsed time in milliseconds (Movement): " + movementWatch.ElapsedMilliseconds);
|
||||
}
|
||||
|
||||
isTesting = false;
|
||||
return;
|
||||
float currentY = movement.transform.rotation.eulerAngles.y;
|
||||
|
||||
// If distance between start and current position of cozmo reached the DISTANCE_TO_MEASURE_SPEED stop the stopwatch
|
||||
if (currentY >= lastY + ROTATION_ANGLE || currentY <= lastY - ROTATION_ANGLE)
|
||||
{
|
||||
if (rotationWatch.IsRunning)
|
||||
{
|
||||
rotationWatch.Stop();
|
||||
UnityEngine.Debug.Log("Cozmo: " + gameObject.name +
|
||||
"\nElapsed time in milliseconds (Rotation): " + rotationWatch.ElapsedMilliseconds);
|
||||
}
|
||||
|
||||
isTesting = false;
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Start stopwatch to measure the time cozmo needs for a specific rotation
|
||||
if (!rotationWatch.IsRunning)
|
||||
{
|
||||
rotationWatch = Stopwatch.StartNew();
|
||||
}
|
||||
|
||||
// Turn right
|
||||
movement.Turn(1);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Start stopwatch to measure the time cozmo needs for a specific distance
|
||||
if (!movementWatch.IsRunning)
|
||||
{
|
||||
movementWatch = Stopwatch.StartNew();
|
||||
}
|
||||
|
||||
// Move cozmo in forward direction
|
||||
movement.Move(1);
|
||||
// Measure time for movement
|
||||
private void TestMovementSpeed()
|
||||
{
|
||||
// If distance between start and current position of cozmo reached the DISTANCE_TO_MEASURE_SPEED stop the stopwatch
|
||||
if (Vector3.Distance(startPositionCozmo, movement.transform.position) >= DISTANCE_TO_MEASURE_SPEED)
|
||||
{
|
||||
if (movementWatch.IsRunning)
|
||||
{
|
||||
movementWatch.Stop();
|
||||
UnityEngine.Debug.Log("Cozmo: " + gameObject.name +
|
||||
"/nElapsed time in milliseconds (Movement): " + movementWatch.ElapsedMilliseconds);
|
||||
}
|
||||
|
||||
isTesting = false;
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Start stopwatch to measure the time cozmo needs for a specific distance
|
||||
if (!movementWatch.IsRunning)
|
||||
{
|
||||
movementWatch = Stopwatch.StartNew();
|
||||
}
|
||||
|
||||
// Move cozmo in forward direction
|
||||
movement.Move(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user