Add commetns and refactoring

This commit is contained in:
Tobi 2019-07-29 16:29:21 +02:00
parent 0cfb60bea0
commit 021662632e
7 changed files with 681 additions and 594 deletions

View File

@ -1,9 +1,19 @@
using System.Collections;
using System.Collections.Generic;
///-----------------------------------------------------------------
/// Namespace: <Cozmo>
/// Class: <ImageProcessor>
/// Description: <Converts a rendertexture to a opencv mat in order to use the canny algorithm.
/// After processing the mat will be converted to a render texture back again.>
/// Author: <Tobias Hassel> Date: <29.07.2019>
/// Notes: <>
///-----------------------------------------------------------------
///
using UnityEngine;
using OpenCvSharp;
using System.Threading.Tasks;
namespace Cozmo
{
public class ImageProcessor : MonoBehaviour
{
[Header("RenderTexture")]
@ -73,6 +83,7 @@ public class ImageProcessor : MonoBehaviour
cannyImage = CropImage(cannyImage);
FindCenterOfGravity(cannyImage);
MatToTexture(cannyImage);
Graphics.Blit(finalProcessedCozmoTexture, destination);
Graphics.Blit(finalProcessedCozmoTexture, renderTextureCropped);
}
@ -86,7 +97,7 @@ public class ImageProcessor : MonoBehaviour
return croppedImage;
}
// Convert the rendertexture to a texture2d
private void RenderTextureToTexture2D(RenderTexture rTex)
{
RenderTexture.active = rTex;
@ -178,3 +189,4 @@ public class ImageProcessor : MonoBehaviour
}
}
}

View File

@ -1,26 +1,36 @@
using MLAgents;
using System.Collections;
using System.Collections.Generic;
///-----------------------------------------------------------------
/// Namespace: <Cozmo>
/// Class: <CozmoAcademy>
/// Description: <The academy used for cozmo. Sets cozmo back to its starting position if the academy gets reset.>
/// Author: <Tobias Hassel> Date: <29.07.2019>
/// Notes: <>
///-----------------------------------------------------------------
///
using MLAgents;
using UnityEngine;
namespace Cozmo
{
public class CozmoAcademy : Academy
{
[Tooltip("Reference to the cozmo gameobject in the scene.")]
public GameObject cozmo;
[Tooltip("Reference to the transform which represents the starting position for cozmo.")]
public Transform startPoint;
//public bool testAcademyReset = false;
public override void AcademyReset()
{
SetCozmoBackToStartingPosition();
}
// Resets Cozmos position and rotation to match its starting position
private void SetCozmoBackToStartingPosition()
{
cozmo.transform.position = startPoint.position;
cozmo.transform.rotation = startPoint.rotation;
}
//private void Update()
//{
// if (testAcademyReset)
// {
// AcademyReset();
// testAcademyReset = !testAcademyReset;
// }
//}
}
}

View File

@ -1,10 +1,20 @@
using MLAgents;
///-----------------------------------------------------------------
/// Namespace: <Cozmo>
/// Class: <CozmoAgent>
/// Description: <The actual agent in the scene. Collects observations and executes the actions.
/// Also rewards the agent and sets an actionmask.>
/// Author: <Tobias Hassel> Date: <29.07.2019>
/// Notes: <>
///-----------------------------------------------------------------
///
using MLAgents;
using OpenCvSharp;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Cozmo
{
public class CozmoAgent : Agent
{
// Possible Actions
@ -219,3 +229,4 @@ public class CozmoAgent : Agent
}
}
}

View File

@ -1,33 +1,44 @@
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;
namespace Cozmo
{
public class CozmoMovement : MonoBehaviour
{
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
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.
[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 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 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()
{
m_Rigidbody = GetComponent<Rigidbody>();
cozmoRigidbody = GetComponent<Rigidbody>();
}
private void OnEnable()
{
// When the cozmo is turned on, make sure it's not kinematic.
m_Rigidbody.isKinematic = false;
cozmoRigidbody.isKinematic = false;
// Also reset the input values.
// reset movement and rotation values
m_MovementInputValue = 0f;
m_TurnInputValue = 0f;
}
@ -36,66 +47,74 @@ public class CozmoMovement : MonoBehaviour
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";
cozmoRigidbody.isKinematic = true;
}
private void Update()
{
// Store the value of both input axes.
m_MovementInputValue = Input.GetAxis(m_MovementAxisName);
m_TurnInputValue = Input.GetAxis(m_TurnAxisName);
m_MovementInputValue = Input.GetAxis(MOVEMENT_AXIS_NAME);
m_TurnInputValue = Input.GetAxis(TURN_AXIS_NAME);
}
private void FixedUpdate()
{
// Adjust the rigidbodies position and orientation in 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)
{
// Create a vector in the direction the cozmo is facing with a magnitude based on the input, speed and the time between frames.
// 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.
m_Rigidbody.MovePosition(m_Rigidbody.position + movement);
cozmoRigidbody.MovePosition(cozmoRigidbody.position + movement);
}
/// <summary>
/// Move cozmo based on the input value
/// </summary>
public void Move()
{
Move(m_MovementInputValue);
}
//public void Move(float[] act)
//{
//}
/// <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)
{
// Determine the number of degrees to be turned based on the input, speed and time between frames.
// 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.
m_Rigidbody.MoveRotation(m_Rigidbody.rotation * turnRotation);
cozmoRigidbody.MoveRotation(cozmoRigidbody.rotation * turnRotation);
}
}
}

View File

@ -1,8 +1,18 @@
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;
namespace Cozmo
{
// Different movementstates cozmo can use
public enum MovementState { Stop, Forward, Right, Left }
[RequireComponent(typeof(CozmoMovement))]
@ -14,7 +24,6 @@ public class CozmoMovementController : MonoBehaviour
private CozmoMovement movement; // Movement script of the robot
// Start is called before the first frame update
void Start()
{
movement = GetComponent<CozmoMovement>();
@ -43,3 +52,4 @@ public class CozmoMovementController : MonoBehaviour
}
}
}
}

View File

@ -1,9 +1,19 @@
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;
namespace CozmoHelpers
{
public enum TestStates { MovementSpeed, RotationSpeed } // enum to determine test scenarios
[RequireComponent(typeof(CozmoMovement))]
@ -22,7 +32,7 @@ public class CozmoMovementTester : MonoBehaviour
[Tooltip("Choose which speed of the virtual cozmo should be tested.")]
public TestStates testState = TestStates.MovementSpeed;
[Tooltip("Start and stop testing")]
[Tooltip("Start and stop testing from the inspector.")]
public bool isTesting = false;
@ -52,6 +62,7 @@ public class CozmoMovementTester : MonoBehaviour
}
}
// Measure time for rotation
private void TestRotationSpeed()
{
@ -83,6 +94,7 @@ public class CozmoMovementTester : MonoBehaviour
}
}
// 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
@ -111,3 +123,4 @@ public class CozmoMovementTester : MonoBehaviour
}
}
}
}

View File

@ -1,7 +1,17 @@
using System.Collections;
///-----------------------------------------------------------------
/// Namespace: <CozmoHelpers>
/// Class: <SceneHelper>
/// Description: <Used to deactivate specific objects that are only used in the sceneview not in the gameview>
/// Author: <Tobias Hassel> Date: <29.07.2019>
/// Notes: <>
///-----------------------------------------------------------------
///
using System.Collections.Generic;
using UnityEngine;
namespace CozmoHelpers
{
public class SceneHelper : MonoBehaviour
{
[Tooltip("All the objects in this list will be deactivated/activated when the game is running")]
@ -13,6 +23,7 @@ public class SceneHelper : MonoBehaviour
ToggleObjectList();
}
// Deactivate/Activate all gameObjects referenced in the toggleInPlayMode list
private void ToggleObjectList()
{
foreach (GameObject go in toggleInPlayMode)
@ -21,3 +32,4 @@ public class SceneHelper : MonoBehaviour
}
}
}
}