Example #1
0
    void Main_Initialization()
    {
        Agx_Simulation.Start(dt);              //Starts the sim.
        dir = Application.streamingAssetsPath; //Get the path of the streaming assets



        //If I start with 3 modules. Then, each time user clicks "Add Module", it adds a new module to the simulation (sim will be started, but not timestep).

        //LOAD:
        Scenario scenario = Deserialize <Scenario>();

        /* Loading the directories for the object files */
        load_FrameDirectories(scenario.robot);
        Load_Robot(scenario.robot);
        Load_Scene(scenario.scene);

        simulation_Running    = true;
        Visualization.enabled = true;

        if (Visualization.enabled)
        {
            Load_Vis();
        }

        SetContactPoints();//if custom contact points: move to MainInitialization().

        InvokeRepeating("Update_Sim", 0, dt);
    }
Example #2
0
    void Update_Sim()
    {
        if (simulation_Running)//Check if simulation is paused
        {
            Agx_Simulation.StepForward();
            //Check if a button has been pressed
            //CheckInputs();

            if (simulationTime >= 2)                          //Wait for robot to settle on terrain
            {
                if (!Dynamics.Control(robot, simulationTime)) //Movement
                {
                    Debug.Log("wrong command");
                }
            }

            robot.Update();

            if (Visualization.enabled)
            {
                Update_Vis();
            }

            simulationTime += Time.deltaTime;
        }
        //Else:
        //Start the canvas overlay to modify and create new modules
    }
Example #3
0
    public void LoadFromXml(string path)
    {
        //get robot
        //design finished
        //deserialize
        if (!simulation_Started)
        {
            dir = Application.streamingAssetsPath;//Get the path of the streaming assets
            Clear_Vis();
            Reset_Opti();
            Agx_Simulation.Start(dt);  //Starts the sim.

            simulation_Started = true; // true;
            simulation_Running = false;
            try { scenario = Deserialize <Scenario>(path); }
            catch (FileNotFoundException e)
            {
                Debug.Log("Scenario not found! " + e);
                AgX_Interface.AgX_Assembly.RemoveFromSim();
                AgX_Interface.Agx_Simulation.Stop();
                UnityEngine.SceneManagement.SceneManager.LoadScene(0);
            }


            SetContactFriction();//if custom contact points: move to MainInitialization().
            CancelInvoke();
            Dynamics.nextAction = "Idle";

            //scenario = Deserialize<Scenario>(Application.streamingAssetsPath + "/XML/Scenario.xml");



            robot = Load_Robot(scenario.robot);

            /* Loading the directories for the object files */
            Load_FrameDirectories(robot);

            sceneObjects = LoadSceneObjects(scenario.sceneObjects);

            //possibly remove this
            scene = new Scene(); Load_Scene(scenario.scene);

            Visualization.enabled = true;

            if (Visualization.enabled)
            {
                Load_Vis();
                Update_Vis(robot);
            }
            if (finalizeButton != null)
            {
                finalizeButton.SetActive(false);
            }
        }
        else
        {
            Debug.Log("Must load before simulation is started");//UnityEditor.EditorUtility.DisplayDialog("Load/Save error!", "Must load before simulation is started", "Ok");
        }
    }
Example #4
0
    void Stop()
    {
        Visualization.enabled = false;
        simulation_Running    = false;

        Agx_Simulation.RemoveSimObjects();
        simulation_Started = false;

        Clear_Vis();
    }
Example #5
0
 void SetContactFriction()
 {
     //Foreach in scenario.contactmats:
     if (scenario.contactFrictions.Count == 0)
     {
         Agx_Simulation.AddContactMaterial("Plastic", "Rock", 0.4f, 0.3f, 3.654E9);//Standard plastic and rock
     }
     foreach (ContactFriction cf in scenario.contactFrictions)
     {
         Agx_Simulation.AddContactMaterial(cf.material1, cf.material2, cf.restitution, cf.friction, cf.youngsModulus);
     }
 }
Example #6
0
 // Update is called once per frame
 void Update_Sim()
 {
     //Update physics:
     Agx_Simulation.StepForward();
     //Update the robot:
     robot.Update();
     //Update the visualization:
     {
         //Frames:
         FrameDemoVis[0].transform.position = AgxHelper(robot.modules[0].frames[0].position);
         FrameDemoVis[0].transform.rotation = (AgxHelper(robot.modules[0].frames[0].quatRotation));
         FrameDemoVis[1].transform.position = AgxHelper(robot.modules[0].frames[1].position);
         FrameDemoVis[1].transform.rotation = (AgxHelper(robot.modules[0].frames[1].quatRotation));
         //Scene object:
         sceneobjvis.transform.position = AgxHelper(sceneobj.position);
     }
     robot.modules[0].joint.SetAngle(2);
 }
Example #7
0
    Robot robot;//Global for pos/rot update

    //Robot OriginalRobot = new Robot();
    void Main_Initialization()
    {
        dir = Application.streamingAssetsPath;//Get the path of the streaming assets
        if (!simulation_Started)
        {
            Clear_Vis();
            Reset_Opti();
            Agx_Simulation.Start(dt);//Starts the sim.

            simulation_Started = true;
            CancelInvoke();
            Dynamics.nextAction = "Idle";


            //Load:
            scenario = Deserialize <Scenario>(Application.streamingAssetsPath + "/XML/Scenario.xml");

            SetContactFriction();

            // Loading the directories for the object files
            Load_FrameDirectories(scenario.robot);

            robot = Load_Robot(scenario.robot);

            sceneObjects = LoadSceneObjects(scenario.sceneObjects);

            scene = new Scene(); Load_Scene(scenario.scene);

            //SetMovementVariables();


            Visualization.enabled = true;

            if (Visualization.enabled)
            {
                Load_Vis();
                Update_Vis(robot);
            }
        }
    }
Example #8
0
 void SetContactPoints()
 {
     Agx_Simulation.AddContactMaterial("Plastic", "Rock", 0.4f, 0.3f, (float)3.654E9);
 }
Example #9
0
    /* Movement commands for the robot: */

    /*void CheckInputs()
     * {
     *  if (Input.GetButtonUp("Turn"))
     *  {
     *      Dynamics.SetMovement("Turn", 0, Math.Sign(Input.GetAxis("Turn")));
     *  }
     *  if (Input.GetButtonUp("Forward"))
     *  {
     *      Dynamics.SetMovement("Forward", Math.Sign(Input.GetAxis("Forward")), 0);
     *  }
     *  if (Input.GetButtonUp("Reset"))
     *  {
     *      Dynamics.SetMovement("Reset", 0, 0);
     *  }
     *  if (Input.GetButtonUp("Idle"))
     *  {
     *      Dynamics.SetMovement("Idle", 0, 0);
     *  }
     *  if (Input.GetButtonUp("Speed"))
     *  {
     *      Dynamics.ChangeSpeed((float)Math.Sign(Input.GetAxis("Speed")));
     *  }
     * }*/


    void OnApplicationQuit()///When Unity closes, shutdown AgX.
    {
        Agx_Simulation.Stop();
        Debug.Log("Application ending after " + Time.time + " seconds");
    }
Example #10
0
    void Update_Sim()
    {
        if (simulation_Running)//Check if simulation is paused
        {
            //Debug.Log(robot.modules[0].position.x+","+robot.modules[0].position.y+","+robot.modules[0].position.z);

            Agx_Simulation.StepForward();
            //Check if a button has been pressed
            //CheckInputs();

            if (simulationTime >= 2)                                            //Wait for robot to settle on terrain
            {
                if (!Dynamics.Control(robot, simulationTime, dynamicVariables)) //Movement
                {
                    Debug.Log("wrong command");
                }
            }

            robot.Update();
            //Update scene objects
            foreach (SceneObject so in sceneObjects)
            {
                so.Update();
            }

            /*
             * //distancesensor
             * string distances = "";
             * foreach (DistanceSensor ds in robot.sensorModules[0].distanceSensors)
             * {
             *  ds.CalculateDistance(sceneObjects);
             *  distances += ds.GetSensorDistance().ToString()+",";Debug.Log("working");
             * }
             *
             * if(distances !="")
             *  Debug.Log(distances);
             */

            //Update visualization
            if (Visualization.enabled)
            {
                Update_Vis(robot);
            }

            //Debug.Log(robot.sensorModules[0].forceSensor.forceValue);

            simulationTime += Time.deltaTime;
        }

        //calculate distances
        foreach (SensorModule mod in robot.sensorModules)
        {
            foreach (DistanceSensor ds in mod.distanceSensors)
            {
                ds.CalculateDistance(sceneObjects);
            }
        }

        //IF Analytics checked, saveData.(if count = 10, count = 0 and saveData?)
        if (Analytics_Visualization.Enabled)
        {
            int result = Analytics_Visualization.SaveData(robot, Time.time);
            switch (result)
            {
            case 0: break;

            case 1: break;

            case 2: break;

            case 3: break;

            case 4: break;

            case 5: break;

            case 6: Debug.Log("Read/Write error(folder not existing)"); break;

            case 7: Debug.Log("No Filename"); break;

            default: Debug.Log("Unspecified Error"); break;
            }
        }
    }
Example #11
0
    void Start()
    {
        //Start simulation:
        Agx_Simulation.Start(0.01);

        //Setting position and rotations:
        var pos    = new AgX_Interface.Vector3(0, 0, 0);
        var u_quat = UnityEngine.Quaternion.Euler(0, 90, 0);

        AgX_Interface.Quaternion frame_rot = new AgX_Interface.Quaternion(u_quat.x, u_quat.y, u_quat.z, u_quat.w);

        //Create Frames:
        Frame[] frames = new Frame[2];
        for (int i = 0; i < 2; i++)
        {
            frames[i] = new Frame()
            {
                guid         = Guid.NewGuid(),
                position     = pos,
                scale        = 10,
                quatRotation = frame_rot,
                //rotation = rot,
                mass         = 10,
                isStatic     = false,
                materialName = "Plastic"
            };
        }

        //download mesh obj file:
        loadmesh();

        //Set mesh of frames:
        frames[0].SetMesh(AgxHelper(leftmesh.vertices), AgxHelper(leftmesh.uv), leftmesh.triangles);
        frames[1].SetMesh(AgxHelper(rightmesh.vertices), AgxHelper(rightmesh.uv), rightmesh.triangles);

        //Create Joint:
        Simulation_Core.Joint joint = new Simulation_Core.Joint()
        {
            guid            = Guid.NewGuid(),
            leftFrameGuid   = frames[0].guid,
            rightFrameGuid  = frames[1].guid,
            type            = "Hinge",
            lowerRangeLimit = -Math.PI / 2,
            upperRangeLimit = Math.PI / 2,
            max_vel         = Math.PI / 6,
            Kp = 3
        };

        //Create Module:
        var module = new Module();

        module.Create(frames[0], joint, frames[1]);

        //Add module to robot:
        robot.Add_Module(module);

        //Add second module to robot:
        //robot.Add_Module(module2,new Simulation_Core.Joint());

        //Initialize robot:
        robot.Initialize();

        //Scene object:
        sceneobj = new SceneObject()
        {
            guid         = Guid.NewGuid(),
            shape        = "Box",
            size         = new AgX_Interface.Vector3(5, 1, 10),
            position     = new AgX_Interface.Vector3(0, -2, 0),
            quatRotation = new AgX_Interface.Quaternion(0, 0, 0, 1),
            materialName = "Rock",
            mass         = 10,
            isStatic     = true
        };
        sceneobj.Initialize();

        //Load vis from mesh and robot + scene object:
        Load_Vis();

        //Start sim update loop:
        InvokeRepeating("Update_Sim", 0.01f, 0.01f);
    }