Line |
Branch |
Decision |
Exec |
Source |
1 |
|
|
|
/** |
2 |
|
|
|
* @file dc_motors.h |
3 |
|
|
|
* |
4 |
|
|
|
* @date March 3, 2020 |
5 |
|
|
|
* @author Matthew Kennedy (c) 2020 |
6 |
|
|
|
*/ |
7 |
|
|
|
|
8 |
|
|
|
#pragma once |
9 |
|
|
|
|
10 |
|
|
|
#include "dc_motor.h" |
11 |
|
|
|
|
12 |
|
|
|
#include <cstddef> |
13 |
|
|
|
|
14 |
|
|
|
DcMotor* initDcMotor(const char *disPinMsg, const dc_io& io, size_t index, bool useTwoWires); |
15 |
|
|
|
DcMotor* initDcMotor(brain_pin_e coil_p, brain_pin_e coil_m, size_t index); |
16 |
|
|
|
|
17 |
|
|
|
// Manual control of motors for use by console commands |
18 |
|
|
|
void setDcMotorFrequency(size_t index, int hz); |
19 |
|
|
|
void setDcMotorDuty(size_t index, float duty); |
20 |
|
|
|
|
21 |
|
|
|
void showDcMotorInfo(int i); |
22 |
|
|
|
|
23 |
|
|
|
// Simple wrapper to use an OutputPin as "PWM" that can only do 0 or 1 |
24 |
|
|
|
struct PwmWrapper : public IPwm { |
25 |
|
|
|
OutputPin& m_pin; |
26 |
|
|
|
|
27 |
|
|
12 |
PwmWrapper(OutputPin& pin) : m_pin(pin) { } |
28 |
|
|
|
|
29 |
|
|
412 |
void setSimplePwmDutyCycle(float dutyCycle) override { |
30 |
|
|
412 |
m_pin.setValue(dutyCycle > 0.5f); |
31 |
|
|
412 |
} |
32 |
|
|
|
}; |
33 |
|
|
|
|
34 |
|
|
|
class DcHardware { |
35 |
|
|
|
private: |
36 |
|
|
|
OutputPin m_pinEnable; |
37 |
|
|
|
OutputPin m_pinDir1; |
38 |
|
|
|
OutputPin m_pinDir2; |
39 |
|
|
|
OutputPin m_disablePin; |
40 |
|
|
|
|
41 |
|
|
|
PwmWrapper wrappedEnable{m_pinEnable}; |
42 |
|
|
|
PwmWrapper wrappedDir1{m_pinDir1}; |
43 |
|
|
|
PwmWrapper wrappedDir2{m_pinDir2}; |
44 |
|
|
|
|
45 |
|
|
|
SimplePwm m_pwm1; |
46 |
|
|
|
SimplePwm m_pwm2; |
47 |
|
|
|
|
48 |
|
|
|
bool isStarted = false; |
49 |
|
|
|
public: |
50 |
|
|
|
|
51 |
|
|
4 |
DcHardware() : dcMotor(m_disablePin) {} |
52 |
|
|
|
|
53 |
|
|
|
void start(bool useTwoWires, |
54 |
|
|
|
brain_pin_e pinEnable, |
55 |
|
|
|
brain_pin_e pinDir1, |
56 |
|
|
|
brain_pin_e pinDir2, |
57 |
|
|
|
const char *disPinMsg, |
58 |
|
|
|
brain_pin_e pinDisable, |
59 |
|
|
|
bool isInverted, |
60 |
|
|
|
Scheduler* executor, |
61 |
|
|
|
int frequency); |
62 |
|
|
|
|
63 |
|
|
|
TwoPinDcMotor dcMotor; |
64 |
|
|
|
|
65 |
|
|
✗ |
void setFrequency(int frequency) { |
66 |
|
|
✗ |
m_pwm1.setFrequency(frequency); |
67 |
|
|
✗ |
m_pwm2.setFrequency(frequency); |
68 |
|
|
✗ |
} |
69 |
|
|
|
|
70 |
|
|
522954 |
const char *msg() { |
71 |
|
|
522954 |
return dcMotor.msg(); |
72 |
|
|
|
} |
73 |
|
|
|
|
74 |
|
|
|
void stop() { |
75 |
|
|
|
// todo: replace 'isStarted' with 'stop' |
76 |
|
|
|
} |
77 |
|
|
|
}; |
78 |
|
|
|
|
79 |
|
|
|
DcHardware *getPrimaryDCHardwareForLogging(); |
80 |
|
|
|
|