-
Notifications
You must be signed in to change notification settings - Fork 0
/
motors.cpp
66 lines (61 loc) · 1.38 KB
/
motors.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
#include "motors.h"
#include <Arduino.h>
#include <HardwareSerial.h>
void Motors::Attach() {
pinMode(left_fwd, OUTPUT);
pinMode(left_rev, OUTPUT);
pinMode(right_fwd, OUTPUT);
pinMode(right_rev, OUTPUT);
pinMode(left_enable, OUTPUT);
pinMode(right_enable, OUTPUT);
Stop();
}
void Motors::Stop() {
analogWrite(left_enable, 0);
analogWrite(right_enable, 0);
}
void Motors::SetSpeeds(int left, int right) {
Serial.print(left);
Serial.print(", ");
Serial.println(right);
if (left == 0)
{
digitalWrite(left_enable, LOW);
}
else
{
if (left > 0)
{
digitalWrite(left_fwd, LOW);
digitalWrite(left_rev, HIGH);
}
else
{
digitalWrite(left_fwd, HIGH);
digitalWrite(left_rev, LOW);
}
int v = abs(left) + MOTOR_MIN;
if (v > 255) v = 255;
analogWrite(left_enable, v);
}
if (right == 0)
{
digitalWrite(right_enable, LOW);
}
else
{
if (right > 0)
{
digitalWrite(right_fwd, LOW);
digitalWrite(right_rev, HIGH);
}
else
{
digitalWrite(right_fwd, HIGH);
digitalWrite(right_rev, LOW);
}
int v = abs(right) + MOTOR_MIN;
if (v > 255) v = 255;
analogWrite(right_enable, v);
}
}