-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDFRobotL298PShield.cpp
67 lines (59 loc) · 1.44 KB
/
DFRobotL298PShield.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
#include "DFRobotL298PShield.h"
// Constructor
DFRobotL298PShield::DFRobotL298PShield()
{
left_invert_direction_ = 1;
right_invert_direction_ = 1;
}
void DFRobotL298PShield::init()
{
// Initializes the pins
pinMode(LEFT_PWM, OUTPUT);
pinMode(LEFT_EN, OUTPUT);
pinMode(RIGHT_PWM, OUTPUT);
pinMode(RIGHT_EN, OUTPUT);
}
void DFRobotL298PShield::flip_left_motor_direction()
{
left_invert_direction_ *= -1;
}
void DFRobotL298PShield::flip_right_motor_direction()
{
right_invert_direction_ *= -1;
}
void DFRobotL298PShield::left_set_pwm(int16_t desired_pwm)
{
// Sets the pwm value. Will take negative pwm values.
if (desired_pwm < 0)
{
digitalWrite(LEFT_EN, LOW);
if (desired_pwm < -255)
desired_pwm = -255;
analogWrite(LEFT_PWM, left_invert_direction_ * -1 * desired_pwm);
}
else
{
digitalWrite(LEFT_EN, HIGH);
if (desired_pwm > 255)
desired_pwm = 255;
analogWrite(LEFT_PWM, left_invert_direction_ * desired_pwm);
}
}
void DFRobotL298PShield::right_set_pwm(int16_t desired_pwm)
{
// Sets the pwm value. Will take negative pwm values.
if (desired_pwm < 0)
{
digitalWrite(RIGHT_EN, LOW);
if (desired_pwm < -255)
desired_pwm = -255;
analogWrite(RIGHT_PWM, right_invert_direction_ * -1 * desired_pwm);
}
else
{
digitalWrite(RIGHT_EN, HIGH);
if (desired_pwm > 255)
desired_pwm = 255;
analogWrite(RIGHT_PWM, right_invert_direction_ * desired_pwm);
}
}