element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Rotating the Servo Using PID controller and SONAR
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 6 replies
  • Subscribers 388 subscribers
  • Views 1326 views
  • Users 0 members are here
  • arduino-robot
  • help
  • servo_motor_control
Related

Rotating the Servo Using PID controller and SONAR

nafis
nafis over 3 years ago

I have to make a bot which has front wheel steering by one servo motor like real cars..and one driving motor to run the robot..by rotating the servo the bot will turn right and left..there is Two SONAR on the left and right side to detect the distance from left and right wall. By taking the distance from both sides of the wall the robot has to be moved continuously forward at the middle position by steering the front wheel with help of the Servo.
How can I rotate the servo with PID for smooth turning? At 118degree the servo is straight and the robot is entirely forward. that means at 118degree of servo the robot will be completely straight.
How can I code the PID algorithm to rotate the servo by taking the distimageance from two SONAR?image

  • Sign in to reply
  • Cancel

Top Replies

  • WumblingBorkeys
    WumblingBorkeys over 3 years ago +1
    I'd start by tackling the PID loop, once you understand the PID loop you can work to implement it. There are some great PID tuning and creation videos on youtube! What I did in a similar project was…
  • michaelkellett
    michaelkellett over 3 years ago +1
    What makes you think that PID is the best way to control the servo in this applicaton ? PID control doesn't care about offset (that's what the I term is for). Any real PID control system needs to be…
  • robogary
    robogary over 3 years ago in reply to nafis +1
    The Arduino IDE has example code for servos and the ping sensors, also a PID library.
  • WumblingBorkeys
    WumblingBorkeys over 3 years ago

    I'd start by tackling the PID loop, once you understand the PID loop you can work to implement it.

    There are some great PID tuning and creation videos on youtube!

    What I did in a similar project was 3 if/while commands, one for when the left side is closer,

    one for roughly center +- sensor error, and another for the right side closer.

    Then based on those statements get your difference and use that as your Porpotinal part of your PID loop. You might want to use the Map(int, int, int, int) command to change the distance from the wall to an appropriate steering angle.

    I don't have my code anymore unfortunately otherwise I'd send it over to you! I hope this helped a little bit.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • michaelkellett
    michaelkellett over 3 years ago

    What makes you think that PID is the best way to control the servo in this applicaton ?

    PID control doesn't care about offset (that's what the I term is for).

    Any real PID control system needs to be initialised so you could preset the integral term then.

    Alternatively you could add the offset to the output of the PID controller.

    MK

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • nafis
    nafis over 3 years ago in reply to michaelkellett

    i think if i use PID then it it will be more smooth

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • nafis
    nafis over 3 years ago in reply to WumblingBorkeys

    thanks for your reply...can you please give the code?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • robogary
    robogary over 3 years ago

    Sounds like a school homework project. The servo is a PID. Tell it angle by the PWM pulse width, it regulates it.

    Not sure what 118 degrees is reference to, is the servo 0-180 degrees, with 90 degrees being your steering zero point ?  If so, the servo horn can be popped off while the controller tells it to be at 90, to set the zero closer to the servo 90 degree mark as possible. 

    Ping left wall & ping right wall - the difference is your error signal into a PID - IF the goal is to track the middle between them. 

    The "D" part of PID is not needed because nothing here is high inertia.   The Integral part  can be thought as "memory" , again, once a steering correction has been made, the wheels go straight and the car stays on the angled path anyway until another steering correction is made.

    So down to the "P" part depends on the steering rod linkage adjustment. Does, for example, the full servo 90 degrees in both directions make the tires turn 90 degrees, 45 degrees, or maybe even 30 degrees ? 

    The desired angle output needs then converted to a PWM width , where 1.5 msec is straight, 1 msec is full left (or right) , 2 msec is full right (left), the proportional gain is a scalar adjusted based on the desired corrections. 

     

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • robogary
    robogary over 3 years ago in reply to nafis

    The Arduino IDE has example code for servos and the ping sensors, also a PID library. 

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube