JMotor
Loading...
Searching...
No Matches
JMotorDriverTMC7300.h
Go to the documentation of this file.
1#ifndef JMOTOR_DRIVER_TMC7300_H
2#define JMOTOR_DRIVER_TMC7300_H
3#include "JMotorDriver.h"
4#include <Arduino.h>
5#include <TMC7300.h>
14protected:
15 boolean enabled;
16 boolean channel;
17 boolean checkDriver;
18 unsigned long lastCheckedIC;
20
21public:
25 TMC7300IC& ic;
26
27public:
35 JMotorDriverTMC7300(TMC7300IC& _ic, boolean _channel, boolean _checkDriver = true, byte _enablePin = -1)
36 : ic(_ic)
37 {
38 enabled = false;
39 channel = _channel;
40 checkDriver = _checkDriver;
41 lastCheckedIC = 0;
42 enablePin = _enablePin;
43 }
44 bool set(float val)
45 {
46 if (checkDriver) {
47 // spread out driver checks across the 4 drivers that might be connected
48 if (((millis() + ic.getChipAddress() * 125 + 500 * channel) % 1000) <= 125) {
49 if (millis() - lastCheckedIC > 900) {
50 lastCheckedIC = millis();
51 ic.checkDriver();
52 }
53 }
54 }
55 val = constrain(val, -1, 1);
56 uint32_t value = (uint32_t)(val * 255) & 0b111111111;
57 if (enabled) {
58 if (!channel) {
59 ic.writeField(TMC7300_PWM_A, value, true);
60 } else {
61 ic.writeField(TMC7300_PWM_B, value, true);
62 }
63 }
64 return abs(val) < 1;
65 }
66 bool setEnable(bool _enable)
67 {
68 if (_enable == false) {
69 if (!channel) {
70 ic.writeField(TMC7300_PWM_A, 0);
71 } else {
72 ic.writeField(TMC7300_PWM_B, 0);
73 }
74 if (enablePin >= 0 && enabled) {
75 digitalWrite(enablePin, LOW);
76 }
77 }
78 if (_enable == true && enabled == false) {
79 ic.checkDriver();
80 if (!channel) {
81 ic.writeField(TMC7300_PWM_A, 0);
82 } else {
83 ic.writeField(TMC7300_PWM_B, 0);
84 }
85 if (enablePin >= 0) {
86 pinMode(enablePin, OUTPUT);
87 digitalWrite(enablePin, HIGH);
88 }
89 }
90 enabled = _enable;
91 return true;
92 }
93 bool getEnable()
94 {
95 return enabled;
96 }
98 {
99 return 1;
100 }
102 {
103 return -1;
104 }
105 bool enable()
106 {
107 return setEnable(true);
108 }
109 bool disable()
110 {
111 return setEnable(false);
112 }
113};
114
115#endif // JMOTOR_DRIVER_TMC7300_H
defines common interface for all types of JMotorDrivers
Definition JMotorDriver.h:10
control a motor connected to a TMC7300 motor driver chip
Definition JMotorDriverTMC7300.h:13
byte enablePin
Definition JMotorDriverTMC7300.h:19
bool set(float val)
set motor power
Definition JMotorDriverTMC7300.h:44
bool disable()
Definition JMotorDriverTMC7300.h:109
TMC7300IC & ic
reference to TMC7300IC class, use ic.writeField() to change advanced settings
Definition JMotorDriverTMC7300.h:25
unsigned long lastCheckedIC
Definition JMotorDriverTMC7300.h:18
bool setEnable(bool _enable)
use to enable or disable a motor, and sets up pin states
Definition JMotorDriverTMC7300.h:66
boolean enabled
Definition JMotorDriverTMC7300.h:15
boolean channel
Definition JMotorDriverTMC7300.h:16
float getMinRange()
low end of the range
Definition JMotorDriverTMC7300.h:101
bool enable()
Definition JMotorDriverTMC7300.h:105
JMotorDriverTMC7300(TMC7300IC &_ic, boolean _channel, boolean _checkDriver=true, byte _enablePin=-1)
control a motor connected to a TMC7300 dual motor driver chip
Definition JMotorDriverTMC7300.h:35
bool getEnable()
get the enable state of the driver
Definition JMotorDriverTMC7300.h:93
boolean checkDriver
Definition JMotorDriverTMC7300.h:17
float getMaxRange()
high end of the range
Definition JMotorDriverTMC7300.h:97