-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDrivetrain.cpp
More file actions
83 lines (74 loc) · 3.05 KB
/
Drivetrain.cpp
File metadata and controls
83 lines (74 loc) · 3.05 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
/** Abstraction for the Drivetrain subsytem on the robot.
* This class provides methods for driving the drivetrain
* @author Jordan Burklund
* @date Sept. 2015
**/
#include "Drivetrain.h"
/** Constructor
* Assumes the left and right motors are Vex motors geared 1:1
* @param leftMotorPin Pin that the left motor is plugged into
* @param rightMotorPin Pin that the right motor is plugged into
* Intentionally does not initialize the drivetrain
**/
Drivetrain::Drivetrain(int leftMotorPin, int rightMotorPin) : LEFT_PIN(leftMotorPin), RIGHT_PIN(rightMotorPin){
}
/** Initializes the drivetrain, and sets up the motor port
**/
void Drivetrain::initialize() {
leftMotor.attach(LEFT_PIN);
rightMotor.attach(RIGHT_PIN);
}
/** Drives the robot with the given speed and turn rate.
* Values range between -1 and 1 where 1 specifies full
* speed forward, or full speed turning left.
* @param speed Speed that the robot should travel at
* @param turnRate Rate that the robot should turn at
**/
void Drivetrain::drive(float speed, float turnRate) {
//Calculate the values for each motor based on speed and turn rate. Note right is reversed
int leftValue = MICROSECONDS_STOP - (MICROSECONDS_MAX_RANGE*speed) + ((float)MICROSECONDS_MAX_RANGE*turnRate);
int rightValue = MICROSECONDS_STOP + (MICROSECONDS_MAX_RANGE*speed) + ((float)MICROSECONDS_MAX_RANGE*turnRate);
//Constrain the values to valid ranges for the servo
leftValue = constrain(leftValue, MICROSECONDS_FULL_REVERSE, MICROSECONDS_FULL_FORWARD);
rightValue = constrain(rightValue, MICROSECONDS_FULL_REVERSE, MICROSECONDS_FULL_FORWARD);
//Write the values to the motors
leftMotor.writeMicroseconds(leftValue);
rightMotor.writeMicroseconds(rightValue);
}
/** Moves the robot through a swing turn at the given
* rate. Where one wheel is stopped, and the other moves
* at the specified rate. Values range from -1 to 1 where
* 1 is full speed swing turn left.
* @note Only swings forward
* @param turnRate Speed that the robot should turn at
**/
void Drivetrain::swingTurn(float turnRate) {
if(turnRate == 0.0) {
//Don't turn
stop();
} else if(turnRate > 0) {
//turn left
leftMotor.writeMicroseconds(MICROSECONDS_STOP);
rightMotor.writeMicroseconds(MICROSECONDS_STOP + (MICROSECONDS_MAX_RANGE*turnRate));
} else {
//turn right
leftMotor.writeMicroseconds(MICROSECONDS_STOP + (MICROSECONDS_MAX_RANGE*turnRate));
rightMotor.writeMicroseconds(MICROSECONDS_STOP);
}
}
/** Moves the robot through a point turn at the given
* rate, where the wheels rotate in opposite directions
* at the same speed. Values range from -1 to 1 where
* 1 is full speed turning left.
* @param turnRate Speed that the robot should turn at
**/
void Drivetrain::pointTurn(float turnRate){
leftMotor.writeMicroseconds(MICROSECONDS_STOP + (MICROSECONDS_MAX_RANGE*turnRate));
rightMotor.writeMicroseconds(MICROSECONDS_STOP + (MICROSECONDS_MAX_RANGE*turnRate));
}
/** Stops the drivetrain.
**/
void Drivetrain::stop() {
leftMotor.writeMicroseconds(MICROSECONDS_STOP);
rightMotor.writeMicroseconds(MICROSECONDS_STOP);
}