RoboterControl.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* RoboterControl.cs
  2. * authors: Lydia Ebbinghaus, Yannic Seidler
  3. * modified: Jingyi
  4. */
  5. using System.Collections;
  6. using UnityEngine;
  7. using RosSharp.RosBridgeClient;
  8. // Class to ontrol the Robot in Unity and getting control signals to the controller.
  9. // Attached to robot prefabs (in folders Telemax, Asterix) that get instantiated in simulation scene.
  10. public class RoboterControl : MonoBehaviour
  11. {
  12. private RobotInformation info;
  13. float moveforward,movebackward,rotateL,rotateR;
  14. // Subscriber of position and inputreader of VR-controller.
  15. private PoseStampedSubscriber robot_pose_sub;
  16. public VRInput input { get; set; }
  17. Vector2 controlSignal;
  18. // Calibration variables.
  19. private int changed;
  20. public int calibrationtime;
  21. public float speed;
  22. // Start is called before the first frame update.
  23. // Starting coroutine and declerating variables.
  24. void Start()
  25. {
  26. changed=0;
  27. robot_pose_sub = GameObject.Find("RosBridge").GetComponent<PoseStampedSubscriber>();
  28. input = GameObject.Find("Input").GetComponent<VRInput>();
  29. info = this.GetComponent<RobotInformation>();
  30. StartCoroutine("timer");
  31. Debug.Log("Roboter Control: Starting to update position of robot");
  32. }
  33. // Update is called once per frame.
  34. // Calibrates robot in the first "calibrationtime" cycles.
  35. void Update()
  36. {
  37. if(changed<calibrationtime)
  38. {
  39. calibrate();
  40. changed++;
  41. }
  42. switch (info.robotType)
  43. {
  44. // Orientation of asterix different
  45. case robotType.asterix_ugv:
  46. controlSignal = input.getSignal();
  47. controlSignal.y = -controlSignal.y;
  48. break;
  49. default:
  50. controlSignal = input.getSignal();
  51. break;
  52. }
  53. // Enable to control robot in unity and in simulation parallel. Needs proper calibration.
  54. //moveRobot();
  55. }
  56. // Moving robot in unity.
  57. private void moveRobot()
  58. {
  59. Vector3 b = new Vector3(-movebackward,0,0)*Time.deltaTime;
  60. transform.Translate(b, Space.Self);
  61. Vector3 m = new Vector3(moveforward,0,0)*Time.deltaTime;
  62. transform.Translate(m, Space.Self);
  63. Vector2 r = new Vector2(rotateR, -rotateL) *speed*10* Time.deltaTime;
  64. transform.Rotate(0, r.x, 0);
  65. transform.Rotate(0, r.y, 0);
  66. }
  67. // Set position of robot to position of real robot.
  68. private void calibrate()
  69. {
  70. this.transform.position = robot_pose_sub.position;
  71. this.transform.rotation = robot_pose_sub.rotation;
  72. }
  73. // Timer in a coroutine to calibrate every 0.05f seconds.
  74. IEnumerator timer()
  75. {
  76. while(true)
  77. {
  78. // ! Test
  79. calibrate();
  80. yield return new WaitForSeconds(0.05f);
  81. }
  82. }
  83. // Returns control signal if robot is not locked. If robot locked : return (0,0,0).
  84. public Vector2 getControlSignal()
  85. {
  86. // ! Test
  87. return controlSignal;
  88. }
  89. }