Unity-Technologies / Unity-Robotics-Hub

Central repository for tools, tutorials, resources, and documentation for robotics simulation in Unity.
Apache License 2.0
2.05k stars 420 forks source link

4 wheel differential robot wrong movement #354

Closed argolomb closed 2 years ago

argolomb commented 2 years ago

Describe the bug I'm trying to reproduce this robot(https://global.agilex.ai/products/scout-2-0) in unity to replace gazebo as my simulation platform. Everything is fine with URDF-Importer and ROS-TCP Connection. I can get the joint state message and convert to movement with "ArticulationDrive drive = joint.xDrive;"

The mobile robot go forward and backward but no curves or spin around his center.

The problem occur when the wheel receive different velocities.

I've tried use cmd_vel msg and calculate the velocity in each wheel but with the same behavior.

Console logs / stack traces

void Start()
        {
            ros = ROSConnection.GetOrCreateInstance();
            ros.Subscribe<JointStateMsg>("joint_states", ReceiveROSCmd);
            rl_wheel = rear_left_wheel.GetComponent<ArticulationBody>();
            rr_wheel = rear_right_wheel.GetComponent<ArticulationBody>();
            fl_wheel = front_left_wheel.GetComponent<ArticulationBody>();
            fr_wheel = front_right_wheel.GetComponent<ArticulationBody>();

            SetParameters(rl_wheel);
            SetParameters(rr_wheel);
            SetParameters(fl_wheel);
            SetParameters(fr_wheel);
            Debug.Log("Start");
        }

        void ReceiveROSCmd(JointStateMsg joint_state_msg){
            Debug.Log("ReceiveROSCmd");
            rl_wheel_vel = joint_state_msg.velocity[2];
            rr_wheel_vel = joint_state_msg.velocity[3];
            fl_wheel_vel = joint_state_msg.velocity[0];
            fr_wheel_vel = joint_state_msg.velocity[1];

            SetSpeed(rl_wheel, (float) rl_wheel_vel);
            SetSpeed(rr_wheel, (float) rr_wheel_vel);
            SetSpeed(fl_wheel, (float) fl_wheel_vel);
            SetSpeed(fr_wheel, (float) fr_wheel_vel);

        }

        private void SetSpeed(ArticulationBody joint, float wheelSpeed = float.NaN)
        {
            ArticulationDrive drive = joint.xDrive;
            if (float.IsNaN(wheelSpeed))
            {
                drive.targetVelocity = ((2 * maxLinearSpeed) / wheelRadius) * Mathf.Rad2Deg;
            }
            else
            {
                drive.targetVelocity = wheelSpeed*Mathf.Rad2Deg;
            }
            joint.xDrive = drive;
        }

Expected behavior The same behavior in all software

Screenshots <img src="http://img.youtube.com/vi/1GrUhLVfuIg/0.jpg" alt="IMAGE ALT TEXT HERE" width="240" height="180" border="10" />

Environment (please complete the following information, where applicable):

vidurvij-Unity commented 2 years ago

Can you share the repository for the project. It would help me understand the Physics settings you are using. [AIRO-1702]

argolomb commented 2 years ago

Can you share the repository for the project. It would help me understand the Physics settings you are using. [AIRO-1702]

Yes, sure.

scout_beta

github-actions[bot] commented 2 years ago

This issue has been marked stale because it has been open for 14 days with no activity. Please remove the stale label or comment on this issue, or the issue will be automatically closed in the next 14 days.

DLu commented 2 years ago

I am also hoping for a solution here. @vidurvij-Unity any luck?

argolomb commented 2 years ago

I am also hoping for a solution here. @vidurvij-Unity any luck?

My solution was:

argolomb commented 2 years ago

The solution is set a rigidbody.inertiaTensor value.

        public Vector3 tensor;
        public Rigidbody rb;
        void Start()
        {
            ros = ROSConnection.GetOrCreateInstance();
            ros.Subscribe<TwistMsg>("cmd_vel", ReceiveROSCmd);
            rb.inertiaTensor = tensor;
            rb.centerOfMass = centerOfMass.transform.localPosition;
        }
ptiwari0664 commented 11 months ago

@argolomb I am trying to recreate another vehicle using similar concept. Could you please illustrate how have you calculate the inertia tensor ? My vehicle is not able to climb on the hills.