Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help  Changing rope length using variable
#1
Hello,

So I am trying to control/change rope length using control input received from python.
I am successfully receiving the data from python side (checked it by debugging in unity console).


However now when I am trying to change the rope length using
Code:
cursor.ChangeLength(control_ip);

I am not sure if I am doing anything wrong, Its just not working, control_ip is a float.

I have a script attached to cable from where I am getting the rope and cursor object into my main control script.

The main control script is as following:

Code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Globalization;
using System.Net.Sockets;
using UnityEngine;
using Obi;


public class Control_main : MonoBehaviour
{
// ------------------------------------------------------------------------------------------------------------------------------------------------//
    // Declaring variables and objects

    // Rope and cursor objects to manipulate cable lengths
    public ObiRope rope_1;
    public ObiRope rope_2;
    public ObiRope rope_3;
    public ObiRope rope_4;

    public ObiRopeCursor cursor_1;
    public ObiRopeCursor cursor_2;
    public ObiRopeCursor cursor_3;
    public ObiRopeCursor cursor_4;

    public float speed;


    // Current cable lengths
    public float c1_len;
    public float c2_len;
    public float c3_len;
    public float c4_len;

    // Robot object and position
    public GameObject robot_obj;
    public Vector3 robot_pos;

    // Control input
    public List<float> control_ip = new List<float>();

    // Control input variables (data received from python)
    public float c1_control_ip;    
    public float c2_control_ip;
    public float c3_control_ip;
    public float c4_control_ip;

    // Control script objects
    public GameObject c1;
    public GameObject c2;
    public GameObject c3;
    public GameObject c4;
    public GameObject listener;
    public GameObject sender;

    // Other scripts
    public Control_C1 c1Script;
    public Control_C2 c2Script;
    public Control_C3 c3Script;
    public Control_C4 c4Script;
    public TCPListenPipe listenerScript;
    public TCPSendPipe senderScript;

    // Misc
    public float delay = 10;
    float timer;
    bool running = true;

    // Simulation Status
    public string sim_stat = "sim_stat";
// ------------------------------------------------------------------------------------------------------------------------------------------------//

    // Start method
    public void Start()
    {
        // Waiting for simulation to be ready
        StartCoroutine(sim_steady());

        // Getting all required scene objects
        GetSceneObjects();

    }

// ------------------------------------------------------------------------------------------------------------------------------------------------//

    // Coroutine to wait for the simulation to get to steady state
    IEnumerator sim_steady()
    {
        sim_stat = "Simulation started";                    // changing simulation status to "started"
        Debug.Log("Simulation started");
        sim_stat = "Waiting for simulation to be steady";   // changing simulation status to "waiting"
        Debug.Log("Waiting for simulation to be steady");
        yield return new WaitForSeconds(10);
        sim_stat = "Simulation ready to use";               // changing simulation status to "ready to use"
        Debug.Log("Simulation ready to use");
    }

// ------------------------------------------------------------------------------------------------------------------------------------------------//
  
    // Method to get scene objects and parameters
    public void GetSceneObjects()
    {
        // Access cable objects
        c1 = GameObject.Find("Cable1");
        c2 = GameObject.Find("Cable2");
        c3 = GameObject.Find("Cable3");
        c4 = GameObject.Find("Cable4");
        listener = GameObject.Find("TrainingEnv");
        sender = GameObject.Find("TrainingEnv");

        // Access other scripts
        c1Script = FindObjectOfType<Control_C1>();
        c2Script = FindObjectOfType<Control_C2>();
        c3Script = FindObjectOfType<Control_C3>();
        c4Script = FindObjectOfType<Control_C4>();
        listenerScript = FindObjectOfType<TCPListenPipe>();
        senderScript = FindObjectOfType<TCPSendPipe>();


        // Getting rope and cursor objects
        rope_1 = c1Script.rope1;
        rope_2 = c2Script.rope2;
        rope_3 = c3Script.rope3;
        rope_4 = c4Script.rope4;

        cursor_1 = c1Script.cursor1;
        cursor_2 = c2Script.cursor2;
        cursor_3 = c3Script.cursor3;
        cursor_4 = c4Script.cursor4;

        speed = c1Script.speed;
    }

// ------------------------------------------------------------------------------------------------------------------------------------------------//

    // Method to receive robot position
    public void getRobPos()
    {
        // Getting robot object and position from TCPSendPipe
        robot_obj = senderScript.robot;
        robot_pos = senderScript.robPos;
    }

// ------------------------------------------------------------------------------------------------------------------------------------------------//

    // Method to get current cable lengths
    public void getCableLen()
    {
        // Access cable lengths (from Cable control scripts - cable observations)
        c1_len = c1Script.rope_c1;
        c2_len = c2Script.rope_c2;
        c3_len = c3Script.rope_c3;
        c4_len = c4Script.rope_c4;
    }

// ------------------------------------------------------------------------------------------------------------------------------------------------//

    // Method to receive actions
    public void Receive_actions()
    {
        // Access control inputs (from TCPListenPipe - data received from python)
        c1_control_ip = listenerScript.c1_action;
        c2_control_ip = listenerScript.c2_action;
        c3_control_ip = listenerScript.c3_action;
        c4_control_ip = listenerScript.c4_action;
    }
   
// ------------------------------------------------------------------------------------------------------------------------------------------------//
   
    // Method to perform control action
    public void set_control()
    {
        while (c1_len != c1_control_ip)
        {
            Debug.Log("Changing Cable Lengths");
            cursor_1.ChangeLength(c1_control_ip);
            if(c1_len == c1_control_ip)
            {
                Debug.Log("Action Completed");
                break;
            }
        }
    }

// ------------------------------------------------------------------------------------------------------------------------------------------------//
    // Method to print all data for debugging
    public void Debugging_data()
    {  
        Debug.Log("Current Cable Lengths: " + "\n");
        Debug.Log("cable 1:" + c1_len + "\n" + "cable 2 :" + c2_len + "\n" + "cable 3 :" + c3_len + "\n" + "cable 4 :" + c4_len + "\n");

        Debug.Log("Cable Control Inputs: " + "\n");
        Debug.Log("c1_control_ip :" + c1_control_ip + "\n" + "c2_control_ip :" + c2_control_ip + "\n" + "c3_control_ip :" + c3_control_ip + "\n" + "c4_control_ip :" + c4_control_ip + "\n");
       
        Debug.Log("Robot Position: " + "\n");
        Debug.Log("position: " + robot_pos);
    }

// ------------------------------------------------------------------------------------------------------------------------------------------------//

    // Update method (performs continuous control operation)
    void Update()
    {
        if (running)
        {
            if (sim_stat == "Simulation ready to use")
            {
                // Getting Control Actions
                Receive_actions();

                // Getting Current Cable Lengths
                getCableLen();

                // Getting Current Robot Position
                getRobPos();



                // Setting Control Actions
                //set_control();

                // Printing All Data
                Debugging_data();
            }
        }
    }
// ------------------------------------------------------------------------------------------------------------------------------------------------//
}

Any help is much appreciated.
Thank You.
Reply
#2
Hi!

ChangeLength() just sets the length of the rope to whatever you pass to it, as long as the rope has enough pooled particles to reach that length. Make sure the pool size of your rope's blueprint is not set to zero. The default is 100 particles, you can set it higher to preallocate more particles if needed.

See the red warning box in the manual regarding this:
http://obi.virtualmethodstudio.com/manua...ursor.html

kind regards,
Reply
#3
(23-02-2023, 11:26 AM)josemendez Wrote: Hi!

ChangeLength() just sets the length of the rope to whatever you pass to it, as long as the rope has enough pooled particles to reach that length. Make sure the pool size of your rope's blueprint is not set to zero. The default is 100 particles, you can set it higher to preallocate more particles if needed.

See the red warning box in the manual regarding this:
http://obi.virtualmethodstudio.com/manua...ursor.html

kind regards,

I have a pool particle size of 1000.

Unity was just freezing whenever it would try to run the
Code:
set_control()

from the set_control() function, I removed the check condition
Code:
        while (c1_len != c1_control_ip)

and directly tried to set the rope length and it worked.
However it just set the rope length instantly and did not change it continuously over time from the current length.

Any idea on how to achieve that.
Reply
#4
(23-02-2023, 11:35 AM)rohit_dhak Wrote: I have a pool particle size of 1000.

Unity was just freezing whenever it would try to run the
Code:
set_control()

from the set_control() function, I removed the check condition
Code:
        while (c1_len != c1_control_ip)

Unity froze in that while loop for an obvious reason: neither variable used as the condition to break the loop (c1_len and c1_control_ip) changes once inside the loop. So once execution enters the loop, it will never exit.


(23-02-2023, 11:35 AM)rohit_dhak Wrote: However it just set the rope length instantly and did not change it continuously over time from the current length.
Any idea on how to achieve that.

This is a basic programming question, not really related to Obi. If you want to increase a variable, you just add a value to it using the "+" operator.
Something like:

Code:
ropeLength = ropeLength + increment;
cursor.ChangeLength(ropeLength);

To make it frame rate independent, just multiply the increment by the time passed since the last frame:

Code:
ropeLength = ropeLength + increment * Time.deltaTime;
cursor.ChangeLength(ropeLength);

You have a working example of this in the included "Crane" sample scene.
Reply