-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRobotMotors.cpp
More file actions
86 lines (70 loc) · 1.97 KB
/
RobotMotors.cpp
File metadata and controls
86 lines (70 loc) · 1.97 KB
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
/*
* RobotMotors.cpp
*
*/
#include "RobotMotors.h"
#include "Arduino.h"
RobotMotors::RobotMotors() {
leftMotor = new AF_DCMotor(leftDriveID, MOTOR34_64KHZ);
rightMotor = new AF_DCMotor(rightDriveID, MOTOR34_64KHZ);
}
RobotMotors::~RobotMotors() {
fullStop();
delete leftMotor;
delete rightMotor;
}
void RobotMotors::processTask() {
if(reachedDeadline()) {
fullStop();
taskDeadline = 0;
}
}
void RobotMotors::driveForward(uint8_t speed, uint16_t duration) {
runDrives(speed, duration, FORWARD, FORWARD);
}
void RobotMotors::driveBackward(uint8_t speed, uint16_t duration) {
runDrives(speed, duration, BACKWARD, BACKWARD);
}
void RobotMotors::turnRight(uint8_t speed, uint16_t duration) {
runDrives(speed, duration, RELEASE, BACKWARD);
}
void RobotMotors::turnLeft(uint8_t speed, uint16_t duration) {
runDrives(speed, duration, BACKWARD, RELEASE);
}
void RobotMotors::runDrives(uint8_t speed, uint16_t duration,
uint8_t leftDirection, uint8_t rightDirection) {
leftMotor->setSpeed(speed);
rightMotor->setSpeed(speed);
leftMotor->run(leftDirection);
rightMotor->run(rightDirection);
scheduleTimedTask(duration);
}
void RobotMotors::fullStop() {
leftMotor->run(RELEASE);
rightMotor->run(RELEASE);
}
void RobotMotors::setMotorSpeed(uint16_t leftSpeed, uint16_t rightSpeed, uint16_t duration) {
leftSpeed = constrain(leftSpeed, 0, 511);
rightSpeed = constrain(rightSpeed, 0, 511);
if(leftSpeed == 255) {
leftMotor->run(RELEASE);
} else
if(leftSpeed > 255) {
leftMotor->setSpeed(leftSpeed-256);
leftMotor->run(FORWARD);
} else {
leftMotor->setSpeed(255 - leftSpeed);
leftMotor->run(BACKWARD);
}
if(rightSpeed == 255) {
rightMotor->run(RELEASE);
} else
if(rightSpeed > 255) {
rightMotor->setSpeed(rightSpeed-256);
rightMotor->run(FORWARD);
} else {
rightMotor->setSpeed(255 - rightSpeed);
rightMotor->run(BACKWARD);
}
scheduleTimedTask(duration);
}