Initial draft of Arduino Sparkfun port for TB6612 Motor driver

Committer:
ateyercheese
Date:
Tue Oct 02 14:48:00 2018 +0000
Revision:
1:9d2787060b3e
Parent:
0:2d64a320d78f
file renamed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ateyercheese 0:2d64a320d78f 1 /******************************************************************************
ateyercheese 0:2d64a320d78f 2 TB6612.cpp
ateyercheese 0:2d64a320d78f 3 TB6612FNG H-Bridge Motor Driver Example code
ateyercheese 0:2d64a320d78f 4 Michelle @ SparkFun Electronics
ateyercheese 0:2d64a320d78f 5 8/20/16
ateyercheese 0:2d64a320d78f 6 https://github.com/sparkfun/SparkFun_TB6612FNG_Arduino_Library
ateyercheese 0:2d64a320d78f 7
ateyercheese 0:2d64a320d78f 8 Uses 2 motors to show examples of the functions in the library. This causes
ateyercheese 0:2d64a320d78f 9 a robot to do a little 'jig'. Each movement has an equal and opposite movement
ateyercheese 0:2d64a320d78f 10 so assuming your motors are balanced the bot should end up at the same place it
ateyercheese 0:2d64a320d78f 11 started.
ateyercheese 0:2d64a320d78f 12
ateyercheese 0:2d64a320d78f 13 Resources:
ateyercheese 0:2d64a320d78f 14 TB6612 SparkFun Library
ateyercheese 0:2d64a320d78f 15
ateyercheese 0:2d64a320d78f 16 Development environment specifics:
ateyercheese 0:2d64a320d78f 17 Developed on Arduino 1.6.4
ateyercheese 0:2d64a320d78f 18 Developed with ROB-9457
ateyercheese 0:2d64a320d78f 19 ******************************************************************************/
ateyercheese 0:2d64a320d78f 20
ateyercheese 1:9d2787060b3e 21 #include "Sparkfun_TB6612.h"
ateyercheese 0:2d64a320d78f 22
ateyercheese 0:2d64a320d78f 23 Motor::Motor(PinName pwm, PinName dir1, PinName dir2, PinName standby, int offset) : _pwm(pwm), _dir1(dir1), _dir2(dir2), _standby(standby) {
ateyercheese 0:2d64a320d78f 24
ateyercheese 0:2d64a320d78f 25 _offset = offset;
ateyercheese 0:2d64a320d78f 26 }
ateyercheese 0:2d64a320d78f 27
ateyercheese 0:2d64a320d78f 28 void Motor::drive(int speed)
ateyercheese 0:2d64a320d78f 29 {
ateyercheese 0:2d64a320d78f 30 enable_motor();
ateyercheese 0:2d64a320d78f 31 speed = speed * _offset;
ateyercheese 0:2d64a320d78f 32 if (speed>=0) fwd(speed);
ateyercheese 0:2d64a320d78f 33 else rev(-speed);
ateyercheese 0:2d64a320d78f 34 }
ateyercheese 0:2d64a320d78f 35
ateyercheese 0:2d64a320d78f 36 void Motor::drive(int speed, int duration)
ateyercheese 0:2d64a320d78f 37 {
ateyercheese 0:2d64a320d78f 38 drive(speed);
ateyercheese 0:2d64a320d78f 39 wait_ms(duration);
ateyercheese 0:2d64a320d78f 40 }
ateyercheese 0:2d64a320d78f 41
ateyercheese 0:2d64a320d78f 42 void Motor::fwd(int speed)
ateyercheese 0:2d64a320d78f 43 {
ateyercheese 0:2d64a320d78f 44 _dir1 = 1;
ateyercheese 0:2d64a320d78f 45 _dir2 = 0;
ateyercheese 0:2d64a320d78f 46 _pwm = speed; // NOTE, speed is -255,255 Arduino analog.
ateyercheese 0:2d64a320d78f 47 }
ateyercheese 0:2d64a320d78f 48
ateyercheese 0:2d64a320d78f 49 void Motor::rev(int speed)
ateyercheese 0:2d64a320d78f 50 {
ateyercheese 0:2d64a320d78f 51 _dir1 = 0;
ateyercheese 0:2d64a320d78f 52 _dir2 = 1;
ateyercheese 0:2d64a320d78f 53 _pwm = speed; // NOTE, speed is -255,255 Arduino analog.
ateyercheese 0:2d64a320d78f 54 }
ateyercheese 0:2d64a320d78f 55
ateyercheese 0:2d64a320d78f 56 void Motor::brake()
ateyercheese 0:2d64a320d78f 57 {
ateyercheese 0:2d64a320d78f 58 _dir1 = 1;
ateyercheese 0:2d64a320d78f 59 _dir2 = 1;
ateyercheese 0:2d64a320d78f 60 _pwm = 0.0f; // NOTE, speed is -255,255 Arduino analog.
ateyercheese 0:2d64a320d78f 61 }
ateyercheese 0:2d64a320d78f 62
ateyercheese 0:2d64a320d78f 63 void Motor::standby()
ateyercheese 0:2d64a320d78f 64 {
ateyercheese 0:2d64a320d78f 65 _standby = 0;
ateyercheese 0:2d64a320d78f 66 }
ateyercheese 0:2d64a320d78f 67
ateyercheese 0:2d64a320d78f 68 void Motor::enable_motor() {
ateyercheese 0:2d64a320d78f 69 _standby = 1;
ateyercheese 0:2d64a320d78f 70 }
ateyercheese 0:2d64a320d78f 71
ateyercheese 0:2d64a320d78f 72 void forward(Motor motor1, Motor motor2, int speed)
ateyercheese 0:2d64a320d78f 73 {
ateyercheese 0:2d64a320d78f 74 motor1.drive(speed);
ateyercheese 0:2d64a320d78f 75 motor2.drive(speed);
ateyercheese 0:2d64a320d78f 76 }
ateyercheese 0:2d64a320d78f 77
ateyercheese 0:2d64a320d78f 78 void forward(Motor motor1, Motor motor2)
ateyercheese 0:2d64a320d78f 79 {
ateyercheese 0:2d64a320d78f 80 motor1.drive(DEFAULTSPEED);
ateyercheese 0:2d64a320d78f 81 motor2.drive(DEFAULTSPEED);
ateyercheese 0:2d64a320d78f 82 }
ateyercheese 0:2d64a320d78f 83
ateyercheese 0:2d64a320d78f 84 void back(Motor motor1, Motor motor2, int speed)
ateyercheese 0:2d64a320d78f 85 {
ateyercheese 0:2d64a320d78f 86 int temp = abs(speed);
ateyercheese 0:2d64a320d78f 87 motor1.drive(-temp);
ateyercheese 0:2d64a320d78f 88 motor2.drive(-temp);
ateyercheese 0:2d64a320d78f 89 }
ateyercheese 0:2d64a320d78f 90 void back(Motor motor1, Motor motor2)
ateyercheese 0:2d64a320d78f 91 {
ateyercheese 0:2d64a320d78f 92 motor1.drive(-DEFAULTSPEED);
ateyercheese 0:2d64a320d78f 93 motor2.drive(-DEFAULTSPEED);
ateyercheese 0:2d64a320d78f 94 }
ateyercheese 0:2d64a320d78f 95 void left(Motor left, Motor right, int speed)
ateyercheese 0:2d64a320d78f 96 {
ateyercheese 0:2d64a320d78f 97 int temp = abs(speed)/2;
ateyercheese 0:2d64a320d78f 98 left.drive(-temp);
ateyercheese 0:2d64a320d78f 99 right.drive(temp);
ateyercheese 0:2d64a320d78f 100
ateyercheese 0:2d64a320d78f 101 }
ateyercheese 0:2d64a320d78f 102
ateyercheese 0:2d64a320d78f 103
ateyercheese 0:2d64a320d78f 104 void right(Motor left, Motor right, int speed)
ateyercheese 0:2d64a320d78f 105 {
ateyercheese 0:2d64a320d78f 106 int temp = abs(speed)/2;
ateyercheese 0:2d64a320d78f 107 left.drive(temp);
ateyercheese 0:2d64a320d78f 108 right.drive(-temp);
ateyercheese 0:2d64a320d78f 109
ateyercheese 0:2d64a320d78f 110 }
ateyercheese 0:2d64a320d78f 111 void brake(Motor motor1, Motor motor2)
ateyercheese 0:2d64a320d78f 112 {
ateyercheese 0:2d64a320d78f 113 motor1.brake();
ateyercheese 0:2d64a320d78f 114 motor2.brake();
ateyercheese 0:2d64a320d78f 115 }