Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Use acceleration input to control vehicle in standard car controller

Discussion in 'Scripting' started by toyotaitc_ics_2, Jun 28, 2018.

  1. toyotaitc_ics_2

    toyotaitc_ics_2

    Joined:
    Oct 12, 2015
    Posts:
    4
    Hi,

    I am working on a vehicle platooning scenario where an automated vehicle can follow its preceding vehicle by its front sensor (radar, lidar, etc). Upon measuring the acceleration, speed, and position of the preceding one, the ego-vehicle can calculate its own reference acceleration, so that it can maintain the same speed its predecessor, while keeping a constant gap between them.

    I am trying to implement this with the standard "CarController.cs" of unity assets, and an issue bothers me for weeks. I noticed there is a function "Move(steering, accel, footbrake, handbrake)" that controls the movement of a vehicle.

    public void Move(float steering, float accel, float footbrake, float handbrake)
    {
    for (int i = 0; i < 4; i++)
    {
    Quaternion quat;
    Vector3 position;
    m_WheelColliders.GetWorldPose(out position, out quat);
    m_WheelMeshes.transform.position = position;
    m_WheelMeshes.transform.rotation = quat;
    }
    //clamp input values
    steering = Mathf.Clamp(steering, -1, 1);
    AccelInput = accel = Mathf.Clamp(accel, 0, 1);
    BrakeInput = footbrake = -1*Mathf.Clamp(footbrake, -1, 0);
    handbrake = Mathf.Clamp(handbrake, 0, 1);
    //Set the steer on the front wheels.
    //Assuming that wheels 0 and 1 are the front wheels.
    m_SteerAngle = steering*m_MaximumSteerAngle;
    m_WheelColliders[0].steerAngle = m_SteerAngle;
    m_WheelColliders[1].steerAngle = m_SteerAngle;
    SteerHelper();
    ApplyDrive(accel, footbrake);
    CapSpeed();
    //Set the handbrake.
    //Assuming that wheels 2 and 3 are the rear wheels.
    if (handbrake > 0f)
    {
    var hbTorque = handbrake*m_MaxHandbrakeTorque;
    m_WheelColliders[2].brakeTorque = hbTorque;
    m_WheelColliders[3].brakeTorque = hbTorque;
    }
    CalculateRevs();
    GearChanging();
    AddDownForce();
    CheckForWheelSpin();
    TractionControl();
    }

    However, this accel input is relevant to the torque of wheels of the vehicle, and is clamped within [0,1]. I don't know the potential relationship between this "accel" variable and the acceleration (or speed) of the vehicle, so I have no idea how to control the vehicle by pre-calculated reference acceleration value.

    Does anyone know how to control the precise acceleration or speed of the vehicle, instead of using the torque? For example, I want the car to remain a constant acceleration of 1.5 m/s^2, how would I do that?

    I would really appreciate your help.
     
  2. toyotaitc_ics_2

    toyotaitc_ics_2

    Joined:
    Oct 12, 2015
    Posts:
    4
    The case is like this. I want to control the vehicles to allow them to form this kind of platoon. However, it seems like the accleration or velocity of vehicles cannot be directly controlled by using the "CarController.cs".
    Capture3.PNG
     
  3. wonderwall11

    wonderwall11

    Joined:
    Jun 29, 2018
    Posts:
    6
    I have the very same issue as you mentioned. I can control the vehicles to accelerate/decelerate whenever I want them to, but I cannot control the specific acceleration/deceleration they get from the wheel colliders.
     
  4. toyotaitc_ics_2

    toyotaitc_ics_2

    Joined:
    Oct 12, 2015
    Posts:
    4
    Thanks for the reply. Let's wait for some experts.
     
  5. toyotaitc_ics_2

    toyotaitc_ics_2

    Joined:
    Oct 12, 2015
    Posts:
    4
    Still looking for experts...