-
Notifications
You must be signed in to change notification settings - Fork 0
/
dcmotor.cpp
60 lines (53 loc) · 1.21 KB
/
dcmotor.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
#include "dcmotor.h"
DCMotor::DCMotor(int nPinMotorA, int nPinMotorB, int nPinMotorE)
{
m_nPinMotorA=nPinMotorA;
m_nPinMotorB=nPinMotorB;
m_nPinMotorE=nPinMotorE;
m_nPWM = 0;
}
bool DCMotor::initPins()
{
bool bCondA = m_nPinMotorA <= 29 && !(m_nPinMotorA < 0);
bool bCondB = m_nPinMotorB <= 29 && !(m_nPinMotorB < 0);
bool bCondE = m_nPinMotorE <= 29 && !(m_nPinMotorE < 0);
if(bCondA && bCondB && bCondE)
{
pinMode(m_nPinMotorA,OUTPUT);
pinMode(m_nPinMotorB,OUTPUT);
pinMode(m_nPinMotorE,PWM_OUTPUT);
softPwmCreate(m_nPinMotorE,s_nMinPWM,s_nMaxPWM);
return true;
}
else return false;
}
void DCMotor::forward()
{
digitalWrite(m_nPinMotorA,0);
digitalWrite(m_nPinMotorB,1);
softPwmWrite(m_nPinMotorE,m_nPWM);
}
void DCMotor::backward()
{
digitalWrite(m_nPinMotorA,1);
digitalWrite(m_nPinMotorB,0);
softPwmWrite(m_nPinMotorE,m_nPWM);
}
void DCMotor::stop()
{
softPwmWrite(m_nPinMotorE,s_nMinPWM);
}
int DCMotor::setPWM(int nPWM)
{
if(nPWM < s_nMinPWM)
m_nPWM = s_nMinPWM;
else if(nPWM > s_nMaxPWM)
m_nPWM = s_nMaxPWM;
else
m_nPWM = nPWM;
return m_nPWM;
}
int DCMotor::getPWM()
{
return m_nPWM;
}