-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoveVNH.cpp
74 lines (54 loc) · 1.67 KB
/
RoveVNH.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
68
69
70
71
72
73
74
#include "RoveVNH.h"
RoveVNH::RoveVNH(const uint8_t pwmPin, const uint8_t forwardPin, const uint8_t reversePin) {
m_pwmPin = pwmPin;
m_forwardPin = forwardPin;
m_reversePin = reversePin;
m_hasCS = false;
}
RoveVNH::RoveVNH(const uint8_t pwmPin, const uint8_t forwardPin, const uint8_t reversePin, const uint8_t csPin) {
m_pwmPin = pwmPin;
m_forwardPin = forwardPin;
m_reversePin = reversePin;
m_csPin = csPin;
m_hasCS = true;
}
#if defined(ARDUINO)
#include <Arduino.h>
void RoveVNH::init() {
pinMode(m_forwardPin, OUTPUT);
pinMode(m_reversePin, OUTPUT);
pinMode(m_pwmPin, OUTPUT);
digitalWrite(m_forwardPin, LOW);
digitalWrite(m_reversePin, LOW);
analogWrite(m_pwmPin, 0);
if (m_hasCS) {
pinMode(m_csPin, INPUT);
}
}
void RoveVNH::configFrequency(const float frequency) {
analogWriteFrequency(m_pwmPin, frequency);
}
void RoveVNH::configCSScale(float gain) {
m_csGain = gain * 3.3 / 1023.0; // convert to ADC scale instead of volts
}
float RoveVNH::readCurrent() {
delayNanoseconds(50);
float current = analogRead(m_csPin) * m_csGain;
return current;
}
void RoveVNH::drive(int16_t decipercent) const {
int32_t rampedDecipercent = applyConfigs(decipercent);
uint8_t pwm = abs(rampedDecipercent) * 255 / 1000;
if (rampedDecipercent > 0) {
digitalWrite(m_forwardPin, HIGH);
digitalWrite(m_reversePin, LOW);
} else if (rampedDecipercent < 0) {
digitalWrite(m_forwardPin, LOW);
digitalWrite(m_reversePin, HIGH);
} else {
digitalWrite(m_forwardPin, LOW);
digitalWrite(m_reversePin, LOW);
}
analogWrite(m_pwmPin, pwm);
}
#endif