-
Notifications
You must be signed in to change notification settings - Fork 1
/
BURT_TMC.cpp
145 lines (124 loc) · 3.91 KB
/
BURT_TMC.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "BURT_TMC.h"
const int blockDelay = 10; // ms
StepperMotor::StepperMotor(StepperMotorPins pins, StepperMotorConfig config) :
pins(pins),
config(config),
driver(TMC5160Stepper(SPI, pins.chipSelect, 0.075))
{ }
StepperMotor::StepperMotor(StepperMotorPins pins, StepperMotorConfig config, LimitSwitch limitSwitch) :
pins(pins),
config(config),
driver(TMC5160Stepper(SPI, pins.chipSelect, 0.075)),
limitSwitch(limitSwitch)
{ }
bool StepperMotor::isMoving() {
return driver.XTARGET() != driver.XACTUAL();
}
int StepperMotor::currentSteps() {
return driver.XACTUAL() + limitSwitch.offset + limitSwitch.position * config.stepsPerUnit;
}
int StepperMotor::targetSteps() {
return driver.XTARGET() + limitSwitch.offset + limitSwitch.position * config.stepsPerUnit;
}
double StepperMotor::currentPosition() {
return currentSteps() / config.stepsPerUnit;
}
double StepperMotor::targetPosition() {
return targetSteps() / config.stepsPerUnit;
}
void StepperMotor::presetup() {
pinMode(pins.chipSelect, OUTPUT);
digitalWrite(pins.chipSelect, HIGH);
}
void StepperMotor::reset_driver() {
driver.begin();
driver.reset();
digitalWrite(pins.enable, HIGH); // disable driver to clear the cache
delay(1000);
digitalWrite(pins.enable, LOW); // re-enable drive, to start loading in parameters
}
void StepperMotor::check_driver() {
TMC5160Stepper::IOIN_t ioin { driver.IOIN() };
if (ioin.version == 0xFF || ioin.version == 0) {
Serial.print("\nDriver communication error on motor: ");
Serial.println(config.name);
while (true);
} else if (ioin.sd_mode) {
Serial.println("Motor is configured for Step & Direction mode: ");
Serial.println(config.name);
while (true);
} else if (ioin.drv_enn) {
Serial.println("Motor is not hardware enabled: ");
Serial.println(config.name);
while (true);
}
}
void StepperMotor::write_settings() {
// TODO: Decide if everything below this is needed:
// See https://github.com/BinghamtonRover/arm-firmware/issues/6
driver.GSTAT(7);
driver.rms_current(config.current);
driver.tbl(2);
driver.toff(9);
driver.pwm_freq(1);
driver.a1(config.acceleration);
driver.v1(config.speed);
driver.AMAX(config.acceleration);
driver.VMAX(config.speed);
driver.DMAX(config.acceleration);
driver.d1(config.acceleration);
driver.vstop(100);
driver.vstart(100);
driver.RAMPMODE(0);
}
void StepperMotor::setup() {
Serial.print("Initializing motor ");
Serial.print(config.name);
Serial.print("... ");
pinMode(pins.enable, OUTPUT);
digitalWrite(pins.enable, LOW);
if (limitSwitch.pin != -1) pinMode(limitSwitch.pin, INPUT_PULLUP);
reset_driver();
check_driver();
write_settings();
Serial.println("Done!");
}
void StepperMotor::calibrate() {
// if (!limitSwitch.isAttached()) return;
// while (!limitSwitch.isPressed()) {
// moveBySteps(10 * limitSwitch.direction);
// }
stop();
int limitSteps = limitSwitch.position * config.stepsPerUnit;
// limitSwitch.offset = limitSteps - driver.XACTUAL() * limitSwitch.direction;
limitSwitch.offset = -driver.XACTUAL();
}
void StepperMotor::update() {
int target = driver.XTARGET();
int current = driver.XACTUAL();
bool isMovingTowardsLimit = limitSwitch.direction > 0
? target > current : target < current;
if (limitSwitch.isPressed() && limitSwitch.isBlocking && isMovingTowardsLimit) stop();
}
void StepperMotor::stop() {
driver.XTARGET(driver.XACTUAL());
}
void StepperMotor::block() {
while (isMoving()) delay(blockDelay);
}
void StepperMotor::moveTo(double position) {
if (!limitSwitch.isValid(position)) return;
int steps = position * config.stepsPerUnit;
moveToSteps(steps);
}
void StepperMotor::moveBy(double offset) {
int steps = offset * config.stepsPerUnit;
moveBySteps(steps);
}
void StepperMotor::moveToSteps(int steps) {
driver.XTARGET(steps);
}
void StepperMotor::moveBySteps(int steps) {
int target = driver.XACTUAL() + steps;
driver.XTARGET(target);
}