Skip to content

Commit

Permalink
Release 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
vincasmiliunas committed Jul 27, 2017
1 parent 03f528d commit 3a03947
Show file tree
Hide file tree
Showing 6 changed files with 257 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ESP32 Arduino library for controlling motors using TB6612FNG motor driver.
114 changes: 114 additions & 0 deletions TB6612FNG.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#include "TB6612FNG.h"
#include <driver/ledc.h>

// TB6612FNG has maximum PWM switching frequency of 100kHz.
#define DEFAULT_LEDC_FREQ 20000
#define DEFAULT_LEDC_RANGE LEDC_TIMER_10_BIT

/* ************************************************************************** */

Tb6612fngLedc::Tb6612fngLedc(int pin, int chan, int freq, int range)
: pin(pin), chan(chan), freq(freq), range(range), rangeMax((1 << range) - 1) {}

void Tb6612fngLedc::begin() {
ledcSetup(chan, freq, range);
ledcAttachPin(pin, chan);
}

void Tb6612fngLedc::write(float value) {
const auto v = max(0.0, min(1.0, value));
ledcWrite(chan, v * rangeMax);
}

/* ************************************************************************** */

Tb6612fngMotor::Tb6612fngMotor(int in1, int in2, int pwm)
: Tb6612fngMotor(in1, in2, Tb6612fngLedc(pwm, LEDC_CHANNEL_0, DEFAULT_LEDC_FREQ, DEFAULT_LEDC_RANGE)) {}

Tb6612fngMotor::Tb6612fngMotor(int in1, int in2, Tb6612fngLedc ledc) : in1(in1), in2(in2), ledc(ledc) {}

void Tb6612fngMotor::begin() {
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
ledc.begin();
}

void Tb6612fngMotor::drive(float velocity) {
direct(velocity >= 0, velocity <= 0);
ledc.write(abs(velocity));
}

void Tb6612fngMotor::brake() { direct(true, true); }

void Tb6612fngMotor::coast() { direct(false, false); }

void Tb6612fngMotor::direct(bool in1, bool in2) {
digitalWrite(this->in1, in1 ? HIGH : LOW);
digitalWrite(this->in2, in2 ? HIGH : LOW);
}

/* ************************************************************************** */

Tb6612fng::Tb6612fng(int standby, int in1A, int in2A, int pwmA)
: Tb6612fng(standby, in1A, in2A, Tb6612fngLedc(pwmA, LEDC_CHANNEL_0, DEFAULT_LEDC_FREQ, DEFAULT_LEDC_RANGE)) {}

Tb6612fng::Tb6612fng(int standby, int in1A, int in2A, Tb6612fngLedc pwmA)
: Tb6612fng(standby, new Tb6612fngMotor(in1A, in2A, pwmA)) {}

Tb6612fng::Tb6612fng(int standby, Tb6612fngMotor *motorA) : Tb6612fng(standby, motorA, nullptr) {}

Tb6612fng::Tb6612fng(int standby, int in1A, int in2A, int pwmA, int in1B, int in2B, int pwmB)
: Tb6612fng(standby, in1A, in2A, Tb6612fngLedc(pwmA, LEDC_CHANNEL_0, DEFAULT_LEDC_FREQ, DEFAULT_LEDC_RANGE), in1B,
in2B, Tb6612fngLedc(pwmB, LEDC_CHANNEL_1, DEFAULT_LEDC_FREQ, DEFAULT_LEDC_RANGE)) {}

Tb6612fng::Tb6612fng(int standby, int in1A, int in2A, Tb6612fngLedc pwmA, int in1B, int in2B, Tb6612fngLedc pwmB)
: Tb6612fng(standby, new Tb6612fngMotor(in1A, in2A, pwmA), new Tb6612fngMotor(in1B, in2B, pwmB)) {}

Tb6612fng::Tb6612fng(int standby, Tb6612fngMotor *motorA, Tb6612fngMotor *motorB)
: standby(standby), motorA(motorA), motorB(motorB) {}

Tb6612fng::~Tb6612fng() {
if (motorA)
delete motorA;
if (motorB)
delete motorB;
}

void Tb6612fng::begin() {
pinMode(standby, OUTPUT);
if (motorA)
motorA->begin();
if (motorB)
motorB->begin();
enable(true);
}

void Tb6612fng::enable(bool value) { digitalWrite(standby, value ? HIGH : LOW); }

void Tb6612fng::drive(float velocityA, float velocityB, int duration, bool stop) {
if (motorA)
motorA->drive(velocityA);
if (motorB)
motorB->drive(velocityB);
if (duration) {
delay(duration);
if (stop)
brake();
}
}

void Tb6612fng::brake() {
if (motorA)
motorA->brake();
if (motorB)
motorB->brake();
}

void Tb6612fng::coast() {
if (motorA)
motorA->coast();
if (motorB)
motorB->coast();
}

/* ************************************************************************** */
74 changes: 74 additions & 0 deletions TB6612FNG.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#pragma once

#include <Arduino.h>

/* ************************************************************************** */

class Tb6612fngLedc {
public:
Tb6612fngLedc(int pin, int chan, int freq, int range);
void begin();
void write(float value);

private:
int pin, chan, freq, range, rangeMax;
};

/* ************************************************************************** */

class Tb6612fngMotor {
public:
Tb6612fngMotor(int in1, int in2, int pwm);
Tb6612fngMotor(int in1, int in2, Tb6612fngLedc ledc);
void begin();

// Velocity: -1.0..+1.0. Motor duty cycle with positive/negative direction.
void drive(float velocity);

// Braking enables low resistance circuit across the motor coil (short).
void brake();

// Coasting enables high resistance circuit across the motor coil (standby).
void coast();

protected:
void direct(bool in1, bool in2);

private:
int in1, in2;
Tb6612fngLedc ledc;
};

/* ************************************************************************** */

class Tb6612fng {
public:
Tb6612fng(int standby, int in1A, int in2A, int pwmA);
Tb6612fng(int standby, int in1A, int in2A, Tb6612fngLedc pwmA);
Tb6612fng(int standby, Tb6612fngMotor *motorA);
Tb6612fng(int standby, int in1A, int in2A, int pwmA, int in1B, int in2B, int pwmB);
Tb6612fng(int standby, int in1A, int in2A, Tb6612fngLedc pwmA, int in1B, int in2B, Tb6612fngLedc pwmB);
Tb6612fng(int standby, Tb6612fngMotor *motorA, Tb6612fngMotor *motorB);
~Tb6612fng();
void begin();

Tb6612fngMotor &getA() { return *motorA; }
Tb6612fngMotor &getB() { return *motorB; }
void enable(bool value);

// Velocity: -1.0..+1.0. Motor duty cycle with positive/negative direction.
// Duration: a delay of ms after setting motor speed.
// Stop: Braking after a delay.
void drive(float velocity, int duration = 0, bool stop = true) { drive(velocity, velocity, duration, stop); }
void drive(float velocityA, float velocityB, int duration = 0, bool stop = true);

// Braking enables low resistance circuit across the motor coil (short).
void brake();

// Coasting enables high resistance circuit across the motor coil (standby).
void coast();

private:
int standby;
Tb6612fngMotor *motorA, *motorB;
};
28 changes: 28 additions & 0 deletions examples/SingleMotor/SingleMotor.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <TB6612FNG.h>

// 27 - Standby pin
// 14 - AIN1 pin
// 12 - AIN2 pin
// 13 - PWMA pin
// To reverse forward motor direction, switch the AIN1 and AIN2 pin numbers.
Tb6612fng motor(27, 14, 12, 13);
// Tb6612fng motor(27, 12, 14, 13); // Reversed forward motor direction.

void setup() {
Serial.begin(115200);
motor.begin();
}

void loop() {
Serial.printf("Loop: %d\n", millis());
// 500ms forwards
motor.drive(0.5, 500);
// 500ms backwards
motor.drive(-0.5, 500);
// Full range of motor speed
for (auto i = 1; i <= 10; i += 1) {
motor.drive(0.1 * i, 200, false);
}
motor.brake();
delay(1000);
}
31 changes: 31 additions & 0 deletions examples/TwoMotors/TwoMotors.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <TB6612FNG.h>

// 27 - Standby pin
// 14 - AIN1 pin
// 12 - AIN2 pin
// 13 - PWMA pin
// 26 - BIN1 pin
// 25 - BIN2 pin
// 33 - PWMB pin
// These pins are in a straight line on the left side of WEMOS LOLIN32 board (skip 34 and 35 as they are input-only).
// To reverse forward motor direction, switch the AIN1 and AIN2 or BIN1 and BIN2 pin numbers.
Tb6612fng motors(27, 14, 12, 13, 26, 25, 33);
// Tb6612fng motors(27, 12, 14, 13, 25, 26, 33); // Reversed forward motor direction.

void setup() {
Serial.begin(115200);
motors.begin();
}

void loop() {
Serial.printf("Loop: %d\n", millis());
// 500ms forwards
motors.drive(0.5, 500);
// 500ms backwards
motors.drive(-0.5, 500);
// 500ms left
motors.drive(-1.0, 1.0, 500);
// 500ms right
motors.drive(1.0, -1.0, 500);
delay(1000);
}
9 changes: 9 additions & 0 deletions library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name=ESP32-Arduino-TB6612FNG
version=1.0.0
author=vincasmiliunas
maintainer=vincasmiliunas
sentence=TB6612FNG for ESP32 Arduino
paragraph=TB6612FNG motor driver library for ESP32 Arduino
category=Device Control
url=https://github.com/vincasmiliunas/ESP32-Arduino-TB6612FNG
architectures=*

0 comments on commit 3a03947

Please sign in to comment.