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 distance from two sonar sensors?
Rotating the Servo Using PID controller and SONAR
-
nafis -
September 1, 2022 at 2:53 PM -
Thread is Unresolved
-
-
PID controller with accelerometer and servo motorUsing the PID library to control a servo is usually overkill. A servo is already a closed loop control system and wrapping it with a PID controller is only…forum.arduino.cc
-
Read the thread quoted by Skyefire and you will see clearer. There is a good explanation of what you need in that thread in the arduino forum.
-
Read the thread quoted by Skyefire and you will see clearer. There is a good explanation of what you need in that thread in the arduino forum.
Can you please tell how can I turn the servo by checking the distance from SONAR..I dont need PID..I have to set angle at myservo.write();
Actually it is an autonomous RC car with SONAR sensor to always drive at middle of the track -
Can you please tell how can I turn the servo by checking the distance from SONAR..I dont need PID..I have to set angle at myservo.write();
Actually it is an autonomous RC car with SONAR sensor to always drive at middle of the track -
You definitely need something like pid controller.
As in the arduino thread described you have to ask:
- What is my setpoint? - - middle of the track
- What is my feedback? - - difference of the two sensors.
- what is my error? - - - see thread, difference of setpoint and feedback.
In addition to the thread, I would ask
- How can I minimize the error? - - change direction of the car via servo.
-
Can you please tell how can I turn the servo by checking the distance from SONAR..I dont need PID..I have to set angle at myservo.write();
Your first post said you needed PID. Please be consistent.
Assuming you are using the standard servo.h Arduino library, you need to generate a number between 0 and 180 degrees, and pass that to servo.write(). On a typical hobby servo, 90deg is the "middle" position. However, these cheap hobby servos have a fair amount of error, plus any mounting/linkage error. So you will need to determine what value in servo.write() makes the wheels centered.
When both sonar sensors are providing the same values, you should send that "center" value to servo.write(). If there is a difference in the range values of the sonar sensors, you need to turn the wheels towards the sensor reporting the higher range.
So, assuming the following:
- servo.write(90) centers the wheels
- servo.write(80) will angle the wheels slightly left
- servo.write(100) will angle the wheels slightly right
the value to servo.write() should be something like:90+((Sensor.Right-Sensor.Left)*ScalingValue)
The Scaling value needs to be determined experimentally, but should start with something small, like 0.01. The sonar sensors report large values, so you don't want small sensor differences to slam the servo to the limits. You should also set an error tolerance window and low-pass filter to deal with the fact that the sonar sensors are noisy, and some soft limits to ensure servo.write() is never sent any value too far from 90. Those limits you will need to determine experimentally, but I would start by limiting it to 80-100 degrees, and expand those values if necessary.