Initial draft of Arduino Sparkfun port for TB6612 Motor driver
Sparkfun_TB6612.h@0:2d64a320d78f, 2018-10-02 (annotated)
- Committer:
- ateyercheese
- Date:
- Tue Oct 02 14:36:09 2018 +0000
- Revision:
- 0:2d64a320d78f
Initial draft of Arduino Sparkfun port for TB6612 Motor driver
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ateyercheese | 0:2d64a320d78f | 1 | /****************************************************************************** |
ateyercheese | 0:2d64a320d78f | 2 | SparkFun_TB6612.h |
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 | 0:2d64a320d78f | 21 | |
ateyercheese | 0:2d64a320d78f | 22 | #ifndef SPARKFUN_TB6612_h |
ateyercheese | 0:2d64a320d78f | 23 | #define SPARKFUN_TB6612_h |
ateyercheese | 0:2d64a320d78f | 24 | |
ateyercheese | 0:2d64a320d78f | 25 | #include "mbed.h" |
ateyercheese | 0:2d64a320d78f | 26 | |
ateyercheese | 0:2d64a320d78f | 27 | //used in some functions so you don't have to send a speed |
ateyercheese | 0:2d64a320d78f | 28 | #define DEFAULTSPEED 255 |
ateyercheese | 0:2d64a320d78f | 29 | |
ateyercheese | 0:2d64a320d78f | 30 | |
ateyercheese | 0:2d64a320d78f | 31 | |
ateyercheese | 0:2d64a320d78f | 32 | class Motor |
ateyercheese | 0:2d64a320d78f | 33 | { |
ateyercheese | 0:2d64a320d78f | 34 | public: |
ateyercheese | 0:2d64a320d78f | 35 | // Constructor. Mainly sets up pins. |
ateyercheese | 0:2d64a320d78f | 36 | Motor(PinName pwm, PinName dir1, PinName dir2, PinName standby, int offset = 1); |
ateyercheese | 0:2d64a320d78f | 37 | |
ateyercheese | 0:2d64a320d78f | 38 | // Drive in direction given by sign, at speed given by magnitude of the |
ateyercheese | 0:2d64a320d78f | 39 | //parameter. |
ateyercheese | 0:2d64a320d78f | 40 | void drive(int speed); |
ateyercheese | 0:2d64a320d78f | 41 | |
ateyercheese | 0:2d64a320d78f | 42 | // drive(), but with a delay(duration) |
ateyercheese | 0:2d64a320d78f | 43 | void drive(int speed, int duration); |
ateyercheese | 0:2d64a320d78f | 44 | |
ateyercheese | 0:2d64a320d78f | 45 | //currently not implemented |
ateyercheese | 0:2d64a320d78f | 46 | //void stop(); // Stop motors, but allow them to coast to a halt. |
ateyercheese | 0:2d64a320d78f | 47 | //void coast(); // Stop motors, but allow them to coast to a halt. |
ateyercheese | 0:2d64a320d78f | 48 | |
ateyercheese | 0:2d64a320d78f | 49 | //Stops motor by setting both input pins high |
ateyercheese | 0:2d64a320d78f | 50 | void brake(); |
ateyercheese | 0:2d64a320d78f | 51 | |
ateyercheese | 0:2d64a320d78f | 52 | //set the chip to standby mode. The drive function takes it out of standby |
ateyercheese | 0:2d64a320d78f | 53 | //(forward, back, left, and right all call drive) |
ateyercheese | 0:2d64a320d78f | 54 | void standby(); |
ateyercheese | 0:2d64a320d78f | 55 | |
ateyercheese | 0:2d64a320d78f | 56 | // Turn off Standby |
ateyercheese | 0:2d64a320d78f | 57 | void enable_motor(); |
ateyercheese | 0:2d64a320d78f | 58 | |
ateyercheese | 0:2d64a320d78f | 59 | private: |
ateyercheese | 0:2d64a320d78f | 60 | //variables for the 2 inputs, PWM input, Offset value, and the Standby pin |
ateyercheese | 0:2d64a320d78f | 61 | PwmOut _pwm; |
ateyercheese | 0:2d64a320d78f | 62 | DigitalOut _dir1, _dir2, _standby; |
ateyercheese | 0:2d64a320d78f | 63 | int _offset; |
ateyercheese | 0:2d64a320d78f | 64 | |
ateyercheese | 0:2d64a320d78f | 65 | //private functions that spin the motor CC and CCW |
ateyercheese | 0:2d64a320d78f | 66 | void fwd(int speed); |
ateyercheese | 0:2d64a320d78f | 67 | void rev(int speed); |
ateyercheese | 0:2d64a320d78f | 68 | |
ateyercheese | 0:2d64a320d78f | 69 | |
ateyercheese | 0:2d64a320d78f | 70 | }; |
ateyercheese | 0:2d64a320d78f | 71 | |
ateyercheese | 0:2d64a320d78f | 72 | //Takes 2 motors and goes forward, if it does not go forward adjust offset |
ateyercheese | 0:2d64a320d78f | 73 | //values until it does. These will also take a negative number and go backwards |
ateyercheese | 0:2d64a320d78f | 74 | //There is also an optional speed input, if speed is not used, the function will |
ateyercheese | 0:2d64a320d78f | 75 | //use the DEFAULTSPEED constant. |
ateyercheese | 0:2d64a320d78f | 76 | void forward(Motor motor1, Motor motor2, int speed); |
ateyercheese | 0:2d64a320d78f | 77 | void forward(Motor motor1, Motor motor2); |
ateyercheese | 0:2d64a320d78f | 78 | |
ateyercheese | 0:2d64a320d78f | 79 | //Similar to forward, will take 2 motors and go backwards. This will take either |
ateyercheese | 0:2d64a320d78f | 80 | //a positive or negative number and will go backwards either way. Once again the |
ateyercheese | 0:2d64a320d78f | 81 | //speed input is optional and will use DEFAULTSPEED if it is not defined. |
ateyercheese | 0:2d64a320d78f | 82 | void back(Motor motor1, Motor motor2, int speed); |
ateyercheese | 0:2d64a320d78f | 83 | void back(Motor motor1, Motor motor2); |
ateyercheese | 0:2d64a320d78f | 84 | |
ateyercheese | 0:2d64a320d78f | 85 | //Left and right take 2 motors, and it is important the order they are sent. |
ateyercheese | 0:2d64a320d78f | 86 | //The left motor should be on the left side of the bot. These functions |
ateyercheese | 0:2d64a320d78f | 87 | //also take a speed value |
ateyercheese | 0:2d64a320d78f | 88 | void left(Motor left, Motor right, int speed); |
ateyercheese | 0:2d64a320d78f | 89 | void right(Motor left, Motor right, int speed); |
ateyercheese | 0:2d64a320d78f | 90 | |
ateyercheese | 0:2d64a320d78f | 91 | //This function takes 2 motors and and brakes them |
ateyercheese | 0:2d64a320d78f | 92 | void brake(Motor motor1, Motor motor2); |
ateyercheese | 0:2d64a320d78f | 93 | |
ateyercheese | 0:2d64a320d78f | 94 | |
ateyercheese | 0:2d64a320d78f | 95 | |
ateyercheese | 0:2d64a320d78f | 96 | |
ateyercheese | 0:2d64a320d78f | 97 | |
ateyercheese | 0:2d64a320d78f | 98 | |
ateyercheese | 0:2d64a320d78f | 99 | |
ateyercheese | 0:2d64a320d78f | 100 | |
ateyercheese | 0:2d64a320d78f | 101 | |
ateyercheese | 0:2d64a320d78f | 102 | |
ateyercheese | 0:2d64a320d78f | 103 | #endif |