JMotor
Loading...
Searching...
No Matches
JMotorDriverPCA9685HBridge.h
Go to the documentation of this file.
1#ifndef JMOTOR_DRIVER_PCA9685HBRIDGE_H
2#define JMOTOR_DRIVER_PCA9685HBRIDGE_H
3
4#include "JMotorDriver.h"
5#include <Arduino.h>
6#include <PCA9685.h>
12protected:
13 bool enabled = false;
14 bool reverse;
19 PCA9685& pca9685;
20 float lastVal;
21
22public:
39 JMotorDriverPCA9685HBridge(PCA9685& _pca9685, byte _channelPos, byte _channelNeg, bool _reverse = false, bool _breakWhenEnabled = true, bool _breakWhenDisabled = false)
40 : pca9685(_pca9685)
41 {
42 channelPos = _channelPos;
43 channelNeg = _channelNeg;
44 enabled = false;
45 reverse = _reverse;
46 breakWhenEnabled = _breakWhenEnabled;
47 breakWhenDisabled = _breakWhenDisabled;
48 lastVal = NAN;
49 }
53 void setBreakWhenEnabled(bool _breakWhenEnabled)
54 {
55 breakWhenEnabled = _breakWhenEnabled;
56 }
60 void setBreakWhenDisabled(bool _breakWhenDisabled)
61 {
62 breakWhenDisabled = _breakWhenDisabled;
63 }
64 void setReverse(bool _reverse)
65 {
66 reverse = _reverse;
67 }
68 bool set(float val)
69 {
70 if (reverse) {
71 val = -val;
72 }
73 const byte dutyCycleMax = pca9685.getDutyCycleMax();
74 if (enabled) {
75 val = val * dutyCycleMax;
76 val = constrain(val, -dutyCycleMax, dutyCycleMax);
77 if (val != lastVal) {
78 if (val == 0 || isnan(val)) {
79 if (breakWhenEnabled) {
80 pca9685.setChannelDutyCycle(channelPos, dutyCycleMax);
81 pca9685.setChannelDutyCycle(channelNeg, dutyCycleMax);
82 } else {
83 pca9685.setChannelDutyCycle(channelPos, 0);
84 pca9685.setChannelDutyCycle(channelNeg, 0);
85 }
86 } else {
87 if (val > 0) {
88 pca9685.setChannelDutyCycle(channelNeg, 0);
89 pca9685.setChannelDutyCycle(channelPos, val);
90 } else {
91 pca9685.setChannelDutyCycle(channelPos, 0);
92 pca9685.setChannelDutyCycle(channelNeg, -val);
93 }
94 }
95 }
96 lastVal = val;
97 }
98 return abs(val) < dutyCycleMax;
99 }
100 bool setEnable(bool _enable)
101 {
102 if (_enable) {
103 if (!enabled) {
104 // actually enable
105 if (breakWhenEnabled) {
106 pca9685.setChannelDutyCycle(channelPos, pca9685.getDutyCycleMax());
107 pca9685.setChannelDutyCycle(channelNeg, pca9685.getDutyCycleMax());
108 } else {
109 pca9685.setChannelDutyCycle(channelPos, 0);
110 pca9685.setChannelDutyCycle(channelNeg, 0);
111 }
112 lastVal = NAN;
113 enabled = true;
114 return true;
115 }
116 } else { // disable
117 if (enabled) {
118 // actually disable
119 if (breakWhenDisabled) {
120 pca9685.setChannelDutyCycle(channelPos, pca9685.getDutyCycleMax());
121 pca9685.setChannelDutyCycle(channelNeg, pca9685.getDutyCycleMax());
122 } else {
123 pca9685.setChannelDutyCycle(channelPos, 0);
124 pca9685.setChannelDutyCycle(channelNeg, 0);
125 }
126 enabled = false;
127 return true;
128 }
129 }
130 return false;
131 }
133 {
134 return enabled;
135 }
137 {
138 return 1.0;
139 }
141 {
142 return -1.0;
143 }
144};
145
146#endif // JMOTOR_DRIVER_PCA9685HBRIDGE_H
defines common interface for all types of JMotorDrivers
Definition JMotorDriver.h:10
Controls an H-bridge motor driver controlled by a PCA9685 Tested on the Alfredo Systems NoU3 https://...
Definition JMotorDriverPCA9685HBridge.h:11
bool set(float val)
set motor power
Definition JMotorDriverPCA9685HBridge.h:68
bool setEnable(bool _enable)
use to enable or disable a motor, and sets up pin states
Definition JMotorDriverPCA9685HBridge.h:100
byte channelPos
Definition JMotorDriverPCA9685HBridge.h:17
void setReverse(bool _reverse)
Definition JMotorDriverPCA9685HBridge.h:64
bool getEnable()
get the enable state of the driver
Definition JMotorDriverPCA9685HBridge.h:132
float getMinRange()
low end of the range
Definition JMotorDriverPCA9685HBridge.h:140
JMotorDriverPCA9685HBridge(PCA9685 &_pca9685, byte _channelPos, byte _channelNeg, bool _reverse=false, bool _breakWhenEnabled=true, bool _breakWhenDisabled=false)
Controls an H-bridge motor driver controlled by a PCA9685.
Definition JMotorDriverPCA9685HBridge.h:39
float getMaxRange()
high end of the range
Definition JMotorDriverPCA9685HBridge.h:136
PCA9685 & pca9685
Definition JMotorDriverPCA9685HBridge.h:19
void setBreakWhenEnabled(bool _breakWhenEnabled)
activate electrical break mode when motor is enabled and speed is 0
Definition JMotorDriverPCA9685HBridge.h:53
float lastVal
Definition JMotorDriverPCA9685HBridge.h:20
bool enabled
Definition JMotorDriverPCA9685HBridge.h:13
bool breakWhenDisabled
Definition JMotorDriverPCA9685HBridge.h:16
bool breakWhenEnabled
Definition JMotorDriverPCA9685HBridge.h:15
bool reverse
Definition JMotorDriverPCA9685HBridge.h:14
void setBreakWhenDisabled(bool _breakWhenDisabled)
activate electrical break mode when motor is disabled
Definition JMotorDriverPCA9685HBridge.h:60
byte channelNeg
Definition JMotorDriverPCA9685HBridge.h:18