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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Servo motor

Discussion in 'Scripting' started by ivanbolcina, Nov 6, 2013.

  1. ivanbolcina

    ivanbolcina

    Joined:
    Oct 27, 2013
    Posts:
    41
    Hi.
    How to create servo motor? Like those in RC toys.

    I guess I could use hidge joint and add script to it to reinforce moving to right angle?

    Any ideas?

    THX
     
  2. ivanbolcina

    ivanbolcina

    Joined:
    Oct 27, 2013
    Posts:
    41
  3. Brian-Stone

    Brian-Stone

    Joined:
    Jun 9, 2012
    Posts:
    222
    You already have the basic idea. You can use the HingeJoint motor and limit features to simulate servo control. Setting an angle can be as simple as turning the motor "on" by setting the HingeJoint.motor.targetVelocity to a positive or negative value, then reading the HingeJoint.angle until the desired angle is reached, and then turning the motor "off" by setting HingeJoint.motor.targetVelocity to zero. You can obviously get more sophisticated if you need realistic servo behavior.
     
  4. karnarex

    karnarex

    Joined:
    Dec 22, 2014
    Posts:
    3
    Hi, Brian!
    Hope you'll read this message.
    I am also trying to create servo in unity, and I've tried a number of approach already, each having it's own flaws. When you set HingeJoint.motor.targetVelocity to zero, it doesn't stay at the point you've reached, it slowly goes to lowest energy position (look at video in the link, the left model).

    https://drive.google.com/file/d/0B4-yUbCiwsheemphRXVlTHI4Qmc/view?usp=sharing

    In the right model I am using configurable joint (with drive mode position), but it seems to act like a spring too much and I am now trying to come up with idea how to fix it, if it's possible.

    Also accuraccy is terrible - it's around +/- 3 degrees ):
     
    Last edited: May 3, 2015
  5. Wowo51

    Wowo51

    Joined:
    Oct 12, 2015
    Posts:
    25
    I've got a servo controller that I wrote that I might dig out and donate to the Unity community. It's in C#. It works pretty good, decelerates nicely and doesn't overshoot the target, accuracy is good. It's a position controller, not a velocity controller. Let me know if you need it...
     
  6. Wowo51

    Wowo51

    Joined:
    Oct 12, 2015
    Posts:
    25
  7. riz2o

    riz2o

    Joined:
    Mar 24, 2018
    Posts:
    2
    Many thanks Wowo51!