-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBiWheel.cpp
123 lines (92 loc) · 2.28 KB
/
BiWheel.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
@ Filename: BiWheel.cpp
@ BiWheel is an Arduino library for chassis with two motors
@ driven by L293 or L298 H-bridge.
@ Created by Ilya S. Dubkov
@ e-mail: [email protected]
@ April 24, 2014
@ Last update: Jan 17, 2016
@ by Ilya S. Dubkov
@ License information:
@ This library is free software; you can redistribute it and/or
@ modify it under the terms of the GNU Lesser General Public
@ License as published by the Free Software Foundation; either
@ version 2.1 of the License, or (at your option) any later version.
@ This library is distributed in the hope that it will be useful,
@ but WITHOUT ANY WARRANTY; without even the implied warranty of
@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
@ Lesser General Public License for more details.
*/
#include <stdlib.h>
#include <Arduino.h>
#include "BiWheel.h"
biWheel::biWheel(int in1, int in2, int in3, int in4)
{
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
_in1 = in1;
_in2 = in2;
_in3 = in3;
_in4 = in4;
}
inline int biWheel::spdToPWMduty(int spdtpwmdt){
return map(spdtpwmdt, 0, 100, PWM_MIN, PWM_MAX);
}
void biWheel::leftMotorStop(){
digitalWrite(_in1, LOW);
digitalWrite(_in2, LOW);
}
void biWheel::rightMotorStop(){
digitalWrite(_in3, LOW);
digitalWrite(_in4, LOW);
}
void biWheel::leftMotorForwardPWM(int spdl){
_spdl = spdToPWMduty(spdl);
digitalWrite(_in2, LOW);
analogWrite(_in1, _spdl);
}
void biWheel::leftMotorBackwardPWM(int spdl){
_spdl = spdToPWMduty(spdl);
digitalWrite(_in1, LOW);
analogWrite(_in2, _spdl);
}
void biWheel::rightMotorForwardPWM(int spdr){
_spdr = spdToPWMduty(spdr);
digitalWrite(_in4, LOW);
analogWrite(_in3, _spdr);
}
void biWheel::rightMotorBackwardPWM(int spdr){
_spdr = spdToPWMduty(spdr);
digitalWrite(_in3, LOW);
analogWrite(_in4, _spdr);
}
void biWheel::leftMotor(int spdl){
if ( spdl == 0 ){
leftMotorStop();
}
else if ( spdl > 0 ){
leftMotorForwardPWM(spdl);
}
else {
leftMotorBackwardPWM(abs(spdl));
}
}
void biWheel::rightMotor(int spdr){
if ( spdr == 0 ){
rightMotorStop();
}
else if ( spdr > 0 ){
rightMotorForwardPWM(spdr);
}
else {
rightMotorBackwardPWM(abs(spdr));
}
}
void biWheel::drive(int spdl, int spdr){
_spdl = spdl;
_spdr = spdr;
leftMotor(_spdl);
rightMotor(_spdr);
}