Your robot's motion is controlled by 2 mini continuous rotation servos. Before we get to the "continuous rotation" part let's look at ordinary servo motors.
The little plastic case housing a servo contains 4 parts.
A tiny DC electric motor.
A tiny plastic gearbox.
A control circuit.
A variable resistor.
The trick is the variable resistor connected to the gearbox. As the motor turns the gears the resistance changes. The control circuit uses the resistance value to determine the position of the output shaft. Therefore, servo motors have a 180 degree range of motion. They are constrained by the physical design of the variable resistor which can only move 180 degrees. In this way, servo motors are used to position the output shaft and then stop. The control circuit is able to compare the duty cycle of an incoming PWM signal to the resistance value and move the output shaft until the values are a match. PWM values will set the servo from 0 degrees to 180 degrees. The high ratio gearbox makes the motor spin many times to move the output very slowly. This gives the servo good torque. When the motor stops it takes a great deal of force to torque the motor and so the servo "sticks" wherever it is set (even when the power is removed).
The esp32 robot servos use the following colours:
Brown: ground
Red: power +5V
Orange: signal
This servo uses metal gears. It is quite powerful and robust. The esp32 robot servos use plastic gears. Do not turn a servo by hand. It could strip or over-heat the gears!
Servo motors have a variety of uses. One common use is to steer model aircraft, cars and boats. A tiny arm is attached to the servo output shaft and then the arm is linked with a stiff wire to the model airplane's rudder or the model car's steering or the model boat's rudder.
So how do we get "continuous" rotation?
The trick with a continuous rotation servo is to disconnect the output shaft from the resistor. The resistor is left set at mid-value. When the servo is powered up the control board "thinks" the servo is at 90 degrees. There is no microcontroller on board. The "thinking" part is accomplished using an analog comparator circuit. If a PWM signal tries to send the servo to 80 degrees the motor will start turning. Since the resistance value does not change, the servo keeps going. Since the variable resistor is physically disconnected the output shaft is able to continue rotating. Sending PWM values to move the servo to values less than 90 degrees will cause the output shaft to rotate in one direction. Sending PWM values to move the servo to values greater than 90 degrees will cause the output shaft to rotate in the other direction. The servo motor is generally on or off so speed control becomes a problem for coding that relies on the initial acceleration timing on the motor and gearbox.
In the Arduino and ESP32 universe, we don't have to worry too much about using analogWrite since there is a special servo library called servo.h. All we need to do is include this library in our code using #include servo.h. We make a servo object called myservo using the Servo myservo command. We use myservo.write to start the servo moving. If we write values greater than 90 degrees the servo moves in one direction. If we write values less than 90 degrees the servo moves in the other direction. If we write values around 90 degrees the servo will stop moving. NOTE: we can call our servo objects whatever we want. If we wanted to call our servo "Ike" we would use Servo Ike to create a servo object called Ike.
The following code should cause your left-hand servo to rock back and forth. You might want to put the robot onto a small object such that the wheels are off of the desktop. The ultrasound detector is the "front" of your robot. If your right-hand servo starts rocking then your connections are reversed. Simply swap the 2 jumpers at GPIO32 and GPIO33.
In order to use servos with an ESP32 we need the special ESP32Servo library. The old servo.h library we used with our Arduino does not work.
Open the Library Manager and type ESP32Servo into the search box. From the displayed libraries select the ESP32Servo library. When you do the OK button will appear. Click OK to install.
/* Sweep
by BARRAGAN <http://barraganstudio.com>
This example code is in the public domain.
modified 8 Nov 2013
by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/Sweep
Modified Feb 24, 2021
by Mike Druiven
https://sites.google.com/view/sparboticsesp32/
*/
#include <ESP32Servo.h>
Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
void setup() {
ESP32PWM::allocateTimer(0);
ESP32PWM::allocateTimer(1);
ESP32PWM::allocateTimer(2);
ESP32PWM::allocateTimer(3);
myservo.setPeriodHertz(50);
myservo.attach(32); // attaches the servo on pin 32 or 33 to the servo object
}
void loop() {
for (pos = 60; pos <= 120; pos += 1) { // goes from 60 degrees to 120 degrees
// in steps of 1 degree
myservo.write(pos); // continuous rotation servos will turn reverse then forward
delay(15); // waits 15ms
}
for (pos = 120; pos >= 60; pos -= 1) { // goes from 120 degrees to 60 degrees
myservo.write(pos); // continuous rotation servos will turn forward then reverse
delay(15); // waits 15ms
}
}