generated from github/welcome-to-github-and-desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TB66MotorController.cpp
105 lines (83 loc) · 2.33 KB
/
TB66MotorController.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "TB66MotorController.h"
TB66MotorController::TB66MotorController(int AIN1, int AIN2, int PWMA, int BIN1, int BIN2, int PWMB, int STBY ) {
pinAIN1 = AIN1;
pinAIN2 = AIN2;
pinPWMA = PWMA;
pinBIN1 = BIN1;
pinBIN2 = BIN2;
pinPWMB = PWMB;
pinSTBY = STBY;
}
void TB66MotorController::setup() {
pinMode(pinPWMA, OUTPUT);
pinMode(pinAIN1, OUTPUT);
pinMode(pinAIN2, OUTPUT);
pinMode(pinPWMB, OUTPUT);
pinMode(pinBIN1, OUTPUT);
pinMode(pinBIN2, OUTPUT);
pinMode(pinSTBY, OUTPUT);
}
void TB66MotorController::motorDrive(int motorNumber, int motorDirection, int motorSpeed)
{
/*
This Drives a specified motor, in a specific direction, at a specified speed:
- motorNumber: motor1 or motor2 ---> Motor 1 or Motor 2
- motorDirection: turnCW or turnCCW ---> clockwise or counter-clockwise
- motorSpeed: 0 to 255 ---> 0 = stop / 255 = fast
*/
boolean pinIn1; //Relates to AIN1 or BIN1 (depending on the motor number specified)
//Specify the Direction to turn the motor
//Clockwise: AIN1/BIN1 = HIGH and AIN2/BIN2 = LOW
//Counter-Clockwise: AIN1/BIN1 = LOW and AIN2/BIN2 = HIGH
if (motorDirection == MOTOR_FORWARD)
pinIn1 = HIGH;
else
pinIn1 = LOW;
//Select the motor to turn, and set the direction and the speed
if(motorNumber == MOTOR_A)
{
digitalWrite(pinAIN1, pinIn1);
digitalWrite(pinAIN2, !pinIn1); //This is the opposite of the AIN1
analogWrite(pinPWMA, motorSpeed);
}
else
{
digitalWrite(pinBIN1, pinIn1);
digitalWrite(pinBIN2, !pinIn1); //This is the opposite of the BIN1
analogWrite(pinPWMB, motorSpeed);
}
//Finally , make sure STBY is disabled - pull it HIGH
digitalWrite(pinSTBY, HIGH);
}
void TB66MotorController::motorBrake(int motorNumber)
{
/*
This "Short Brake"s the specified motor, by setting speed to zero
*/
if (motorNumber == MOTOR_A)
analogWrite(pinPWMA, 0);
else
analogWrite(pinPWMB, 0);
}
void TB66MotorController::motorStop(int motorNumber)
{
/*
This stops the specified motor by setting both IN pins to LOW
*/
if (motorNumber == MOTOR_A) {
digitalWrite(pinAIN1, LOW);
digitalWrite(pinAIN2, LOW);
}
else
{
digitalWrite(pinBIN1, LOW);
digitalWrite(pinBIN2, LOW);
}
}
void TB66MotorController::motorsStandby()
{
/*
This puts the motors into Standby Mode
*/
digitalWrite(pinSTBY, LOW);
}