JGroxz

Digital Manufacturing MET0340 / Tartu / Spring 2021

Feb 26th, 2021 (edited)
397
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Course Moodle link: https://moodle.taltech.ee/course/view.php?id=30786
  2. //
  3. // Package with robot model: https://moodle.taltech.ee/mod/resource/view.php?id=267745
  4. // Package with project from the first week: https://moodle.taltech.ee/mod/resource/view.php?id=359501
  5. // Package with Vuforia AR markers: https://moodle.taltech.ee/mod/resource/view.php?id=361128
  6. // AR image target file: https://moodle.taltech.ee/mod/resource/view.php?id=361150
  7. //
  8. // Vuforia developer portal: https://developer.vuforia.com/
  9.  
  10.  
  11.  
  12. // Vuforia developer license key
  13.  
  14. AVzplhT/////AAABmWMwu8cGuUwhkPCvIfxSgcg9rFoJMxCW8fyW6A55qN089iVX/aB3Pyc1lcCddj8aTfM4Vpq4rEZvbq9ClNOPps7DjN84Bq222WnMfP0e7I2Pp7vsJdGVxc9Rvi6lPIecGIt/weckG1r0Et+puXd5xvS2Zrgaoycp8CYskinJZzrSa3IesCGh3SRbW0xJIMcxJs7Zb4/+vGBlR/rbB62I1DlALeKpglQGnY5YqPsSzqCqatQ/JhUPHXXSAs3AB19gOBtjtzhdt0KmSBSmE+FmO0JoE1DXp2IRFqjCG9HPRQKvUju3qMX0PuHwVVR/ccE/gkSzGzcD5wKRLCCoNebkhSXMVxRhguGi5RF1yZ7vTLhc
  15.  
  16.  
  17.  
  18. // RobotController script
  19.  
  20. using System.Collections;
  21. using System.Collections.Generic;
  22. using UnityEngine;
  23.  
  24. public class RobotController : MonoBehaviour
  25. {
  26.     [System.Serializable]
  27.     public class RobotJoint
  28.     {
  29.         public Transform joint;
  30.         public float speed = 60f;
  31.         public float minAngle = -90f;
  32.         public float maxAngle = 90f;
  33.         public float currentAngle = 0f;
  34.     }
  35.    
  36.     public float speed = 45f;
  37.     public List<RobotJoint> robotJoints = new List<RobotJoint>();
  38.     public int selectedJointIndex = 0;
  39.  
  40.     private bool isUiIncreasingJointAngle = false;
  41.     private bool isUiDecreasingJointAngle = false;
  42.    
  43.     private RobotJoint SelectedRobotJoint => robotJoints[selectedJointIndex];
  44.  
  45.     // Update is called once per frame
  46.     void Update()
  47.     {
  48.         float angleDelta = 0;
  49.        
  50.         // Apply inputs
  51.         angleDelta = ProcessKeyInputs(angleDelta);
  52.         angleDelta = ProcessUiInputs(angleDelta);
  53.        
  54.         // Apply limits
  55.         angleDelta = ApplyJointLimits(angleDelta);
  56.  
  57.         SelectedRobotJoint.currentAngle += angleDelta;
  58.         Vector3 rotation = new Vector3(0f, 0f, angleDelta);
  59.         SelectedRobotJoint.joint.Rotate(rotation);
  60.     }
  61.  
  62.     private float ProcessKeyInputs(float angleDelta)
  63.     {
  64.         bool leftPressed = Input.GetKey(KeyCode.LeftArrow);
  65.         if (leftPressed)
  66.         {
  67.             angleDelta += speed * Time.deltaTime;
  68.         }
  69.         bool rightPressed = Input.GetKey(KeyCode.RightArrow);
  70.         if (rightPressed)
  71.         {
  72.             angleDelta -= speed * Time.deltaTime;
  73.         }
  74.  
  75.         bool spacePressed = Input.GetKeyDown(KeyCode.Space);
  76.         if (spacePressed)
  77.         {
  78.             SelectNextJoint();
  79.         }
  80.  
  81.         return angleDelta;
  82.     }
  83.  
  84.     private float ProcessUiInputs(float angleDelta)
  85.     {
  86.         if (isUiIncreasingJointAngle)
  87.         {
  88.             angleDelta += speed * Time.deltaTime;
  89.         }
  90.         if (isUiDecreasingJointAngle)
  91.         {
  92.             angleDelta -= speed * Time.deltaTime;
  93.         }
  94.  
  95.         return angleDelta;
  96.     }
  97.  
  98.     private float ApplyJointLimits(float angleDelta)
  99.     {
  100.         float expectedAngle = SelectedRobotJoint.currentAngle + angleDelta;
  101.  
  102.         if (expectedAngle < SelectedRobotJoint.minAngle)
  103.         {
  104.             angleDelta = SelectedRobotJoint.minAngle - SelectedRobotJoint.currentAngle;
  105.         }
  106.         if (expectedAngle > SelectedRobotJoint.maxAngle)
  107.         {
  108.             angleDelta = SelectedRobotJoint.maxAngle - SelectedRobotJoint.currentAngle;
  109.         }
  110.  
  111.         return angleDelta;
  112.     }
  113.  
  114.     public void StartIncreasingJointAngle()
  115.     {
  116.         isUiIncreasingJointAngle = true;
  117.         isUiDecreasingJointAngle = false;
  118.     }
  119.  
  120.     public void StartDecreasingJointAngle()
  121.     {
  122.         isUiIncreasingJointAngle = false;
  123.         isUiDecreasingJointAngle = true;
  124.     }
  125.  
  126.     public void StopRobotMovement()
  127.     {
  128.         isUiIncreasingJointAngle = false;
  129.         isUiDecreasingJointAngle = false;
  130.     }
  131.  
  132.     public void SelectNextJoint()
  133.     {
  134.         selectedJointIndex += 1;
  135.  
  136.         if (selectedJointIndex >= robotJoints.Count)
  137.         {
  138.             selectedJointIndex = 0;
  139.         }
  140.     }
  141. }
  142.  
  143.  
  144.  
  145.  
  146.  
  147. // Updated contents of packages.json
  148.  
  149. {
  150.   "scopedRegistries": [
  151.     {
  152.       "name": "npmjs",
  153.       "url": "https://registry.npmjs.org/",
  154.       "scopes": [
  155.         "io.extendreality"
  156.       ]
  157.     }
  158.   ],
  159.   "dependencies": {
  160.     "io.extendreality.tilia.camerarigs.spatialsimulator.unity": "1.2.31",
  161.     "io.extendreality.tilia.camerarigs.trackedalias.unity": "1.5.7",
  162.     "io.extendreality.zinnia.unity": "1.30.0",
  163.     "com.unity.collab-proxy": "1.2.16",
  164.     "com.unity.ide.rider": "1.1.4",
  165.     "com.unity.ide.vscode": "1.2.3",
  166.     "com.unity.test-framework": "1.1.20",
  167.     "com.unity.textmeshpro": "2.1.1",
  168.     "com.unity.timeline": "1.2.17",
  169.     "com.unity.ugui": "1.0.0",
  170.     "com.unity.modules.ai": "1.0.0",
  171.     "com.unity.modules.androidjni": "1.0.0",
  172.     "com.unity.modules.animation": "1.0.0",
  173.     "com.unity.modules.assetbundle": "1.0.0",
  174.     "com.unity.modules.audio": "1.0.0",
  175.     "com.unity.modules.cloth": "1.0.0",
  176.     "com.unity.modules.director": "1.0.0",
  177.     "com.unity.modules.imageconversion": "1.0.0",
  178.     "com.unity.modules.imgui": "1.0.0",
  179.     "com.unity.modules.jsonserialize": "1.0.0",
  180.     "com.unity.modules.particlesystem": "1.0.0",
  181.     "com.unity.modules.physics": "1.0.0",
  182.     "com.unity.modules.physics2d": "1.0.0",
  183.     "com.unity.modules.screencapture": "1.0.0",
  184.     "com.unity.modules.terrain": "1.0.0",
  185.     "com.unity.modules.terrainphysics": "1.0.0",
  186.     "com.unity.modules.tilemap": "1.0.0",
  187.     "com.unity.modules.ui": "1.0.0",
  188.     "com.unity.modules.uielements": "1.0.0",
  189.     "com.unity.modules.umbra": "1.0.0",
  190.     "com.unity.modules.unityanalytics": "1.0.0",
  191.     "com.unity.modules.unitywebrequest": "1.0.0",
  192.     "com.unity.modules.unitywebrequestassetbundle": "1.0.0",
  193.     "com.unity.modules.unitywebrequestaudio": "1.0.0",
  194.     "com.unity.modules.unitywebrequesttexture": "1.0.0",
  195.     "com.unity.modules.unitywebrequestwww": "1.0.0",
  196.     "com.unity.modules.vehicles": "1.0.0",
  197.     "com.unity.modules.video": "1.0.0",
  198.     "com.unity.modules.vr": "1.0.0",
  199.     "com.unity.modules.wind": "1.0.0",
  200.     "com.unity.modules.xr": "1.0.0"
  201.   }
  202. }
RAW Paste Data