Skip to content

Commit

Permalink
provide PWM control for motors
Browse files Browse the repository at this point in the history
  • Loading branch information
bigalex committed Jun 5, 2020
1 parent f773deb commit af115a1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
12 changes: 5 additions & 7 deletions src/app_httpd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,14 +446,12 @@ void startCameraServer(){
}


// set motor directions and toggle motors on/off
// TODO: control motor speed with PWM
// set motor directions and speed
// TODO: fix bad SoC, this routine has nothing to do with the web server
void updateMotors(){
// motor is switched on if the speed is >0.5 or <-0.5, else motor off
abs(current_speed_left)>0.5 ? digitalWrite(MOTOR_L_PWM, HIGH) : digitalWrite(MOTOR_L_PWM, LOW);
abs(current_speed_right)>0.5 ? digitalWrite(MOTOR_R_PWM, HIGH) : digitalWrite(MOTOR_R_PWM, LOW);
ledcWrite(MOTOR_L_PWM_CHAN, abs(current_speed_left)*1023);
ledcWrite(MOTOR_R_PWM_CHAN, abs(current_speed_right)*1023);
// directions depends on whether speed is negative or positive
current_speed_left > 0 ? digitalWrite(MOTOR_L_DIR, HIGH) : digitalWrite(MOTOR_L_DIR, LOW);
current_speed_right > 0 ? digitalWrite(MOTOR_R_DIR, HIGH) : digitalWrite(MOTOR_R_DIR, LOW);
current_speed_left > 0 ? digitalWrite(MOTOR_L_DIR_PIN, HIGH) : digitalWrite(MOTOR_L_DIR_PIN, LOW);
current_speed_right > 0 ? digitalWrite(MOTOR_R_DIR_PIN, HIGH) : digitalWrite(MOTOR_R_DIR_PIN, LOW);
}
23 changes: 14 additions & 9 deletions src/esp32-cam-webserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,14 @@ char myVer[] PROGMEM = __DATE__ " @ " __TIME__;
int lampVal = -1; // disable Lamp
#endif
int lampChannel = 7; // a free PWM channel (some channels used by camera)
// according to https://esp32.com/viewtopic.php?t=11379 those are channel 1 and 2
const int pwmfreq = 50000; // 50K pwm frequency
const int pwmresolution = 9; // duty cycle bit range
// https://diarmuid.ie/blog/pwm-exponential-led-fading-on-arduino-or-other-platforms
const int pwmIntervals = 100; // The number of Steps between the output being on and off
float lampR; // The R value in the PWM graph equation (calculated in setup)


void startCameraServer();
void flashLED(int flashtime);

Expand Down Expand Up @@ -154,15 +156,18 @@ void setup() {
#endif

// Mötör init
pinMode(MOTOR_L_PWM, OUTPUT);
digitalWrite(MOTOR_L_PWM, LOW); //off
pinMode(MOTOR_R_PWM, OUTPUT);
digitalWrite(MOTOR_R_PWM, LOW); //off
pinMode(MOTOR_L_DIR, OUTPUT);
digitalWrite(MOTOR_L_DIR, LOW);
pinMode(MOTOR_R_DIR, OUTPUT);
digitalWrite(MOTOR_R_DIR, LOW);

pinMode(MOTOR_L_PWM_PIN, OUTPUT);
ledcSetup(MOTOR_L_PWM_CHAN, MOTOR_PWM_FREQ_HZ, MOTOR_PWM_RES_BIT);
ledcAttachPin(MOTOR_L_PWM_PIN, MOTOR_L_PWM_CHAN);
ledcWrite(MOTOR_L_PWM_CHAN, 0); //off
pinMode(MOTOR_R_PWM_PIN, OUTPUT);
ledcSetup(MOTOR_R_PWM_CHAN, MOTOR_PWM_FREQ_HZ, MOTOR_PWM_RES_BIT);
ledcAttachPin(MOTOR_R_PWM_PIN, MOTOR_R_PWM_CHAN);
ledcWrite(MOTOR_R_PWM_CHAN, 0); //off
pinMode(MOTOR_L_DIR_PIN, OUTPUT);
digitalWrite(MOTOR_L_DIR_PIN, LOW);
pinMode(MOTOR_R_DIR_PIN, OUTPUT);
digitalWrite(MOTOR_R_DIR_PIN, LOW);

// Feedback that hardware init is complete and we are now attempting to connect
Serial.println("");
Expand Down
16 changes: 11 additions & 5 deletions src/robot_pins.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
// Pin definitions for robot peripherals (actuators, LEDs, ...)
// Pin and PWM definitions for robot peripherals (actuators, LEDs, ...)

// TB6612FNG with
// GPIO 12 - PWM A left motor
// GPIO 13 - PWM B right motor
// GPIO 14 - DIR A
// GPIO 15 - DIR B

#define MOTOR_L_PWM 12
#define MOTOR_R_PWM 13
#define MOTOR_L_DIR 14
#define MOTOR_R_DIR 15
#define MOTOR_L_PWM_PIN 12
#define MOTOR_R_PWM_PIN 13
#define MOTOR_L_DIR_PIN 14
#define MOTOR_R_DIR_PIN 15

#define MOTOR_L_PWM_CHAN 3
#define MOTOR_R_PWM_CHAN 4
// PWM resolution
#define MOTOR_PWM_RES_BIT 10
// PWM frequency
#define MOTOR_PWM_FREQ_HZ 10000

0 comments on commit af115a1

Please sign in to comment.