-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDaulHBridge.cpp
More file actions
145 lines (108 loc) · 2.5 KB
/
DaulHBridge.cpp
File metadata and controls
145 lines (108 loc) · 2.5 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "DaulHBridge.h"
#define SPEEDPIN1 10 //9 // H-bridge enable pin for speed control motor1
#define MOTOR1APIN 0 // H-bridge leg 1
#define MOTOR2APIN 1 // H-bridge leg 2
#define SPEEDPIN2 12 // 10 // H-bridge enable pin for speed control motor2
#define MOTOR3APIN 3 // H-bridge leg 3
#define MOTOR4APIN 2 // H-bridge leg 2
DaulHBridge::DaulHBridge()
{
_pin = 11;
pinMode(_pin,OUTPUT);
_pwmL=SPEEDPIN1;
pinMode(_pwmL,OUTPUT);
_fwdL=MOTOR1APIN;
pinMode(_fwdL,OUTPUT);
_revL=MOTOR2APIN;
pinMode(_revL,OUTPUT);
_pwmR=SPEEDPIN2;
pinMode(_pwmR,OUTPUT);
_fwdR=MOTOR3APIN;
pinMode(_fwdR,OUTPUT);
_revR=MOTOR4APIN;
pinMode(_revR,OUTPUT);
digitalWrite(_pwmL, LOW);
digitalWrite(_fwdL, LOW);
digitalWrite(_revL, LOW);
digitalWrite(_pwmR, LOW);
digitalWrite(_fwdR, LOW);
digitalWrite(_revR, LOW);
}
void DaulHBridge::init()
{
pinMode(_pin,OUTPUT);
_pwmL=SPEEDPIN1;
pinMode(_pwmL,OUTPUT);
_fwdL=MOTOR1APIN;
pinMode(_fwdL,OUTPUT);
_revL=MOTOR2APIN;
pinMode(_revL,OUTPUT);
_pwmR=SPEEDPIN2;
pinMode(_pwmR,OUTPUT);
_fwdR=MOTOR3APIN;
pinMode(_fwdR,OUTPUT);
_revR=MOTOR4APIN;
pinMode(_revR,OUTPUT);
digitalWrite(_pwmL, LOW);
digitalWrite(_fwdL, LOW);
digitalWrite(_revL, LOW);
digitalWrite(_pwmR, LOW);
digitalWrite(_fwdR, LOW);
digitalWrite(_revR, LOW);
}
void DaulHBridge::on()
{
digitalWrite(_pin, HIGH);
}
void DaulHBridge::off()
{
digitalWrite(_pin, LOW);
}
// set speed for left motor; speed is a number between -400 and 400
void DaulHBridge::setLeftSpeed(int16_t speed)
{
bool reverse = 0;
if (speed < 0)
{
speed = -speed; // Make speed a positive quantity.
reverse = 1; // Preserve the direction.
}
if (speed > 400) // Max PWM duty cycle.
{
speed = 400;
}
if (reverse == 1)
{
digitalWrite(_fwdL, HIGH);
digitalWrite(_revL, LOW);
}else
{
digitalWrite(_fwdL, LOW);
digitalWrite(_revL, HIGH);
}
analogWrite(_pwmL,speed);
}
// set speed for right motor; speed is a number between -400 and 400
void DaulHBridge::setRightSpeed(int16_t speed)
{
bool reverse = 0;
if (speed < 0)
{
speed = -speed; // Make speed a positive quantity.
reverse = 1; // Preserve the direction.
}
if (speed > 400) // Max PWM duty cycle.
{
speed = 400;
}
if (reverse == 1)
{
digitalWrite(_fwdR, HIGH);
digitalWrite(_revR, LOW);
}else
{
digitalWrite(_fwdR, LOW);
digitalWrite(_revR, HIGH);
}
analogWrite(_pwmR,speed);
}