-
Notifications
You must be signed in to change notification settings - Fork 35
/
ArduinoDemo.ino
178 lines (143 loc) · 4.02 KB
/
ArduinoDemo.ino
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <Arduino.h>
#include "ServoProjectController.h"
#define SIMULATE
#define DEBUG_SERIAL Serial
#ifndef SIMULATE
#define SERVO_BUS_SERIAL Serial1
#endif
constexpr size_t dof = 2;
constexpr int cycleTimeInMs = 18;
constexpr size_t maxNrOfTrajectoryItems = 21;
#ifdef SERVO_BUS_SERIAL
class ServoManager : public ServoManagerBase<dof>
{
public:
ServoManager() :
ServoManagerBase<dof>(::cycleTimeInMs, &servos),
servos{{{1, &com}, {2, &com}}}
{
servos[0].setOffsetAndScaling(360.0f / 4096.0f, -110.0f, 0);
servos[0].setControlSpeed(32);
servos[0].setBacklashControlSpeed(6, 180.0f, 0.0f);
servos[1].setOffsetAndScaling(180.0f / 1900.0f, 0.0f, 0.0f);
}
CppArray<DCServoCommunicator, dof> servos;
private:
SerialComOptimizer comOptimizer{&SERVO_BUS_SERIAL};
SerialCommunication com{comOptimizer};
};
#endif
#ifdef SIMULATE
class SimulatedServoManager : public ServoManagerBase<dof>
{
public:
SimulatedServoManager() :
ServoManagerBase<dof>(::cycleTimeInMs, &servos),
servos{{{1, &simCom}, {2, &simCom}}}
{
}
CppArray<DCServoCommunicator, dof> servos;
private:
SimulateCommunication<dof> simCom;
};
#endif
void setup()
{
delay(2000);
#ifdef DEBUG_SERIAL
DEBUG_SERIAL.begin(115200);
#endif
#ifdef SERVO_BUS_SERIAL
SERVO_BUS_SERIAL.begin(115200);
#endif
}
int skippPrintCounter = 0;
int state = 0;
void loop()
{
#ifdef SIMULATE
static SimulatedServoManager servoManager;
#else
static ServoManager servoManager;
#endif
static TrajectoryBuilder<dof, maxNrOfTrajectoryItems> trajectoryBuilder(servoManager.getDt());
static ServoReferenceSetter<dof> servoReferenceSetter(servoManager.getDt());
CppArray<float, dof> position;
bool added;
switch (state)
{
case 0:
servoManager.init();
trajectoryBuilder.clear();
position = servoManager.getPosition();
trajectoryBuilder.setStart(position);
trajectoryBuilder.addSmoothMove({{0.0f, 0.0f}}, 1.0f);
trajectoryBuilder.addWait(10.0f);
for (size_t i = 0; i != 3; ++i)
{
//trajectoryBuilder.addSmoothMove({{2.0f, 2.0f}}, 0.2f);
trajectoryBuilder.addSmoothMove({{-10.0f, -10.0f}}, 1.0f);
trajectoryBuilder.addSmoothMove({{0.0f, 0.0f}}, 3.0f);
trajectoryBuilder.addWait(3.0f);
//trajectoryBuilder.addSmoothMove({{-2.0f, -2.0f}}, 0.2f);
trajectoryBuilder.addSmoothMove({{10.0f, 10.0f}}, 1.0f);
trajectoryBuilder.addSmoothMove({{0.0f, 0.0f}}, 3.0f);
trajectoryBuilder.addWait(3.0f);
}
added = trajectoryBuilder.addWait(7.0f);
if (!added)
{
#ifdef DEBUG_SERIAL
DEBUG_SERIAL.println("maxNrOfTrajectoryItems exceeded");
#endif
// if last trajectory item exceeds maxNrOfTrajectoryItems
// go to error state
state = 1000;
}
else
{
state = 10;
}
break;
case 10:
position = trajectoryBuilder.get();
servoReferenceSetter.set(servoManager.servos, position);
if (!trajectoryBuilder.step())
{
state = 20;
}
position = servoManager.getPosition();
#ifdef DEBUG_SERIAL
if (skippPrintCounter == 10)
{
skippPrintCounter = 0;
DEBUG_SERIAL.print(position[0]);
DEBUG_SERIAL.print(", ");
DEBUG_SERIAL.println(position[1]);
}
++skippPrintCounter;
#endif
break;
case 20:
#ifdef DEBUG_SERIAL
DEBUG_SERIAL.println("Done");
#endif
state = 100;
break;
case 100:
break;
case 1000:
return;
}
CommunicationError error = servoManager.run();
if (!error.noError())
{
#ifdef DEBUG_SERIAL
DEBUG_SERIAL.print("Error on nodeNr ");
DEBUG_SERIAL.print(error.nodeNr);
DEBUG_SERIAL.print(" code ");
DEBUG_SERIAL.println(error.code);
#endif
state = 1000;
}
}