-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKitMotor.cpp
More file actions
41 lines (35 loc) · 1.11 KB
/
KitMotor.cpp
File metadata and controls
41 lines (35 loc) · 1.11 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
#include "KitMotor.h"
/** Constructor
* @param int FWDpin The analog capable digital pin that the H-bridge is wired to, such that when it is high, the motor will drive forward
* @param int REVpin The analog capable digital pin that the H-bridge is wired to, such that when it is high, the motor will drive in reverse
**/
KitMotor::KitMotor(int FWDpin, int REVpin) {
_FWDpin = FWDpin;
_REVpin = REVpin;
}
/**
* Performs initial setup of the motor pins, and grounds both pins to ensure no movement
**/
void KitMotor::initialize() {
pinMode(_FWDpin, OUTPUT);
pinMode(_REVpin, OUTPUT);
digitalWrite(_FWDpin, LOW);
digitalWrite(_REVpin, LOW);
}
/**
* Sets the motor to move
* @param float setpoint Value from -1.0 (full reverse) to 1.0 (full forward)
**/
void KitMotor::setOutput(float setpoint) {
setpoint = constrain(setpoint, -1.0, 1.0);
if (setpoint > 0.0) {
analogWrite(_FWDpin, 255*setpoint);
digitalWrite(_REVpin, LOW);
} else if (setpoint < 0.0) {
analogWrite(_REVpin, -255*setpoint);
digitalWrite(_FWDpin, LOW);
} else {
digitalWrite(_FWDpin, LOW);
digitalWrite(_REVpin, LOW);
}
}