3
\$\begingroup\$

I want to drive a vehicle in x and y coordinate area with range from (0,0) to (6000,5000). I am using Visual Components (using Python API).

On input I have speed data and steering angle of wheels from Simulink. For example speed of 30 (0.25 m/sec) with the steering angle of wheels from 35 to -35.

I can use something like x = speed * COS( steeringAngle ) and y = speed * SIN( steeringAngle ) while adding up the values of x and y together respectively for each loop.

But the problem is it goes in one direction. If the vehicle turn and comes to area below the value of current x and y. I don't know how to actually get the right x and y value when the turn is so much that it goes to reduced values of x and y. A concept photo is attached. Anyone knows how to solve this?

Here is my code:

Speed= [1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000] #Incoming Speed
  Angle= [0,0,0,10,0,0,-20,-20,0,35,35,35,35,35,35,35,35] #Incoming Angle
  item = app.findComponent('Ball') #Find component in VC
  delay(0.1)

  newposition = [10,2500,0,90,0,180] #New position array
  x = newposition[0]
  y = newposition[1]
  z = newposition[2]
  w = newposition[3]
  p = newposition[4]
  r = newposition[5]   #wpr is yaw, pitch, roll
  Xold=0
  Yold=2500
  Rold=0     #store x y and r

  for s, a in zip(Speed, Angle):        #get values from array
    delay(1)
    print "(Speed, Angle) ->",(s, a)
    x=s
    y=Yold
    r=a         #just store

    rad=math.radians(a)
    x=math.cos(rad)*s
    y=math.sin(rad)*s           #convert to radians

    Xold=Xold+x
    Yold=Yold+y
    Rold=Rold+r              #add to prev value
    x=Xold
    y=Yold
    r=Rold
    print x,y,r

    mtx = item.PositionMatrix         #get position of BALL
    mtx.P = vcVector.new(x,y,z)   #Position
    mtx.WPR = vcVector.new(w,p,r) #Orientation as yaw,pitch,roll
    item.PositionMatrix = mtx             #store new location
    item.update()
    app.render()
\$\endgroup\$
  • 1
    \$\begingroup\$ It looks like you might need to separate the concept of "steering wheel angle" - the amount of turn you're adding - from the concept of "heading" - the direction you're currently travelling in. Your velocity vector calculation is correct for a heading angle, but it looks like your input is a steering angle, which may act more like the derivative of your heading, depending on your control model. \$\endgroup\$ – DMGregory Apr 29 '18 at 12:19
  • \$\begingroup\$ I have simply a speed data which can be just taken as a forward movement. Another input is simply the angle of the wheel in - and + degrees where 0 is the non-turned wheel. \$\endgroup\$ – Haroon May 2 '18 at 9:34
0
\$\begingroup\$

If I understand it correctly, you want to create a steering effect. Inorder to achieve that you will have increase/decrease the value of the angle depending on input. Like so

    public float steeringSensitivity = 2;
    float angle = 0;
    public float speed = 4;
    void Update() {
        float direction = Input.GetAxis ("Horizontal");
        if (direction < 0) {
            angle -= steeringSensitivity * Time.deltaTime;
        } else if(direction > 0) {
            angle += steeringSensitivity * Time.deltaTime;
        }
        Vector3 offset = new Vector3 (speed * Mathf.Cos( angle), speed * Mathf.Sin(angle), 0);
        transform.position += offset * Time.deltaTime;
    }
\$\endgroup\$
  • \$\begingroup\$ I already have incoming steering angle between +/-35. The speed is coming along at the same time. But I want to move it in X and Y. Eg you have the vehicle currently on 3000,2000 position and some angle. Now if the incoming angle is 200 degrees, So obviously it will go in back direction after turning 200 degrees with some speed. So I have problem detecting the new X and Y position. Eg X and Y should be something like 3200,1800 because of almost 180 turn. How to get this value if the angle is such that the new position is then reduced X and Y value. How would I actually know the right position? \$\endgroup\$ – Haroon Mar 26 '18 at 13:32
  • \$\begingroup\$ So want to calculate the new position, given an angle and speed? The way to calculate the new position is to do something like Vector3 newPosition = transform.position + new Vector3 (speed * Mathf.Cos( Mathf.Deg2Rad * angle), speed * Mathf.Sin(Mathf.Deg2Rad * angle), 0); Given the angle is in degrees, otherwide you won't need the Deg2Rad multiplication \$\endgroup\$ – Aj_ Mar 26 '18 at 13:50
  • \$\begingroup\$ Yes it seems to work somewhat. Thank you. But the main problem is still there. Since I am moving in x and y axis, the increment is always linear. Like if the angle is 170 degrees it should be detected and the vehicle should go towards the -X axis because of near 180 turn. How to do this? \$\endgroup\$ – Haroon Mar 27 '18 at 8:09
  • \$\begingroup\$ In my (python) code, I am not fully sure but the negative in Y axis works as it uses sine. But X is cosine so there is no negative in that and it always go increasing. I think it might be fine with Y to get reduced with angle but X stays positive. How this "driving" can be achieved in X-Y axis when only speed and angle is there? \$\endgroup\$ – Haroon Mar 27 '18 at 8:19
  • 2
    \$\begingroup\$ @Haroon Please use answer posts as answers. To add more details, please use the edit feature of the question and add the new details there. \$\endgroup\$ – Alexandre Vaillancourt Mar 27 '18 at 16:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.