-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsixiDriver.cpp
140 lines (107 loc) · 3.38 KB
/
sixiDriver.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
/**************************************
* Title : sixiDriver.cpp
* Author : Ian Gagnon
* Created : 2021-04-21
*************************************/
#include "sixiDriver.h"
SixiDriver::SixiDriver(unsigned long publicationPeriodMs, bool verbose): verbose_(verbose), publicationPeriodMs_(publicationPeriodMs) {
for (int i=0; i<NUM_STEPPERS; i++) {
pAccelStepperObjectArray_[i] = new AccelStepper(AccelStepper::DRIVER, STEP_PINS_[i], DIR_PINS_[i]);
pinMode(ENABLE_PINS_[i], OUTPUT);
}
}
SixiDriver::~SixiDriver() {
for (int i=0; i<NUM_STEPPERS; i++)
delete pAccelStepperObjectArray_[i];
}
void SixiDriver::start() {
enableMotors();
sensorManager_.setup();
rosserialInterface_.setup();
attachTool();
}
void SixiDriver::moveToHome() {
updateSteppersPositions();
setMaxSpeedForAll(DEFAULT_SPEEDS_);
setAccelerationForAll(DEFAULT_ACCELS_);
moveAllTo(HOME_POSITION_);
}
void SixiDriver::updateSensors() {
sensorManager_.updateAll();
for (int i=0; i<NUM_STEPPERS; i++)
positions_[i] = sensorManager_.sensors[i].angle;
}
void SixiDriver::updateSteppersPositions() {
sensorManager_.updateAll();
for (int i=0; i<NUM_STEPPERS; i++){
positions_[i] = sensorManager_.sensors[i].angle;
long pos = round(STEPS_PER_DEGREE_[i] * positions_[i]);
pAccelStepperObjectArray_[i]->setCurrentPosition(pos);
}
if (verbose_){
Serial.println("-------- SENSORS POSITIONS (DEGREES) --------");
for (int i=0; i<NUM_SENSORS; i++){
Serial.print(sensorManager_.sensors[i].angle); Serial.print("\t");
}
Serial.println();
}
}
void SixiDriver::attachTool() {
if (verbose_) Serial.println("Attaching servo!");
servoTool_.attach(SERVO_PIN_);
}
void SixiDriver::enableMotors() {
for (int i=0; i<NUM_STEPPERS; i++)
digitalWrite(ENABLE_PINS_[i], LOW);
}
void SixiDriver::disableMotors() {
for (int i=0; i<NUM_STEPPERS; i++)
digitalWrite(ENABLE_PINS_[i], HIGH);
}
void SixiDriver::setMaxSpeedForAll(const float* speedArray) {
for (int i=0; i<NUM_STEPPERS; i++)
pAccelStepperObjectArray_[i]->setMaxSpeed(speedArray[i]);
}
void SixiDriver::setAccelerationForAll(const float* accelArray) {
for (int i=0; i<NUM_STEPPERS; i++)
pAccelStepperObjectArray_[i]->setAcceleration(accelArray[i]);
}
void SixiDriver::moveAllTo(float* positionArray) {
if (verbose_) Serial.println("-------- MOVING (STEPS) --------");
for (int i=0; i<NUM_STEPPERS; i++) {
long pos = round(STEPS_PER_DEGREE_[i] * positionArray[i]);
pAccelStepperObjectArray_[i]->moveTo(pos);
if (verbose_){
Serial.print(pos); Serial.print("\t");
}
}
if (verbose_) {
Serial.println();
}
}
void SixiDriver::moveTool(int positionTool) {
if (verbose_) Serial.println("-------- MOVING TOOL (DEG) --------");
servoTool_.write(positionTool);
if (verbose_) {
Serial.print(positionTool); Serial.println();
}
}
void SixiDriver::runAll() {
for (int i=0; i<NUM_STEPPERS; i++) {
if (pAccelStepperObjectArray_[i]->distanceToGo() != 0)
pAccelStepperObjectArray_[i]->run();
}
}
void SixiDriver::main() {
if (millis() - lastTime_ >= publicationPeriodMs_){
lastTime_ = millis();
updateSensors();
rosserialInterface_.publish(positions_);
}
if (rosserialInterface_.newCommandReceived()){
float* desiredPosition = rosserialInterface_.getCommand();
moveAllTo(desiredPosition);
}
rosserialInterface_.main();
runAll();
}