-
Notifications
You must be signed in to change notification settings - Fork 0
/
Motor.hpp
46 lines (42 loc) · 977 Bytes
/
Motor.hpp
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
class Motor{
public:
Motor(const char&,const char&,const char&,const char&);
void stopMotor();
void setPWM();
int PWM;
private:
const char& motorP1,motorP2,encoderP1,encoderP2;
};
Motor::Motor(const char &motorP1,const char &motorP2,const char &encoderP1,const char &encoderP2):motorP1(motorP1),motorP2(motorP2),encoderP1(encoderP1),encoderP2(encoderP2){
gpioSetMode(motorP1,PI_OUTPUT);
gpioSetMode(motorP2,PI_OUTPUT);
gpioSetPWMfrequency(motorP1,50);
gpioPWM(motorP1,0);
gpioSetPWMfrequency(motorP2,50);
gpioPWM(motorP2,0);
gpioSetMode(encoderP1,PI_INPUT);
gpioSetMode(encoderP2,PI_INPUT);
}
void Motor::stopMotor()
{
gpioPWM(motorP1,0);
gpioPWM(motorP2,0);
}
void Motor::setPWM()
{
if(PWM < -255)
{
PWM = -255;
}else if(PWM > 255)
{
PWM = 255;
}
if (PWM < 0) {
gpioPWM(motorP1,-PWM);
gpioPWM(motorP2,0);
}
else {
gpioPWM(motorP2,PWM);
gpioPWM(motorP1,0);
}
}