-
Notifications
You must be signed in to change notification settings - Fork 584
Expand file tree
/
Copy pathCanMotion.cpp
More file actions
388 lines (348 loc) · 12.6 KB
/
Copy pathCanMotion.cpp
File metadata and controls
388 lines (348 loc) · 12.6 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
* CanMotion.cpp
*
* Created on: 11 Aug 2019
* Author: David
*/
#include "CanMotion.h"
#if SUPPORT_CAN_EXPANSION
#include <CanMessageBuffer.h>
#include <CanMessageFormats.h>
#include "CanInterface.h"
#include <Platform/Platform.h>
#include <GCodes/GCodes.h>
#include <Movement/Move.h>
#include <General/FreelistManager.h>
namespace CanMotion
{
enum class DriverStopState : uint8_t { inactive = 0, active, stopRequested, stopSent };
// Class to record drivers active and requests to stop them
class DriversStopList
{
public:
DECLARE_FREELIST_NEW_DELETE(DriversStopList)
DriversStopList(DriversStopList *p_next, CanAddress p_ba) noexcept : next(p_next), boardAddress(p_ba), sentRevertRequest(0) { }
DriversStopList *next;
CanAddress boardAddress;
uint8_t numDrivers;
bool sentRevertRequest;
volatile DriverStopState stopStates[MaxLinearDriversPerCanSlave];
volatile int32_t stopSteps[MaxLinearDriversPerCanSlave];
};
static CanMessageBuffer urgentMessageBuffer;
static CanMessageBuffer *movementBufferList = nullptr;
static DriversStopList *volatile stopList = nullptr;
static uint32_t currentMoveClocks;
static volatile bool revertAll = false;
static volatile bool revertedAll = false;
static volatile uint32_t whenRevertedAll;
static Mutex stopListMutex;
static uint8_t nextSeq[CanId::MaxCanAddress + 1] = { 0 };
static bool warnedNoPaSnapshotSupport[CanId::MaxCanAddress + 1] = { false };
static CanMessageBuffer *_ecv_null GetBuffer(const PrepParams& params, DriverId canDriver) noexcept;
static bool BoardSupportsMovementPaSnapshot(CanAddress boardAddress) noexcept;
static void FreeMovementBuffers() noexcept;
}
void CanMotion::Init() noexcept
{
movementBufferList = nullptr;
stopListMutex.Create("stopList");
}
void CanMotion::FreeMovementBuffers() noexcept
{
for (;;)
{
CanMessageBuffer *p = movementBufferList;
if (p == nullptr)
{
break;
}
movementBufferList = p->next;
CanMessageBuffer::Free(p);
}
}
bool CanMotion::BoardSupportsMovementPaSnapshot(CanAddress boardAddress) noexcept
{
const ExpansionBoardData *const boardData = reprap.GetExpansion().GetBoardDetails(boardAddress);
if (boardData == nullptr)
{
return false;
}
return boardData->supportsMovementPaSnapshot;
}
// This is called by DDA::Prepare at the start of preparing a movement
void CanMotion::StartMovement() noexcept
{
FreeMovementBuffers(); // there shouldn't be any movement buffers in the list, but free any that there may be
// Free up any stop list items left over from the previous move
MutexLocker lock(stopListMutex);
revertAll = revertedAll = false;
for (;;)
{
DriversStopList *p = stopList;
if (p == nullptr)
{
break;
}
stopList = p->next;
delete p;
}
}
// If there is an existing CAN buffer for this move and CAN address, return it; otherwise create one
CanMessageBuffer *_ecv_null CanMotion::GetBuffer(const PrepParams& params, DriverId canDriver) noexcept
{
if (canDriver.localDriver >= MaxLinearDriversPerCanSlave)
{
return nullptr; // can't handle a local driver number this large, the message isn't big enough
}
// Search for an existing buffer
CanMessageBuffer* buf = movementBufferList;
while (buf != nullptr && buf->id.Dst() != canDriver.boardAddress)
{
buf = buf->next;
}
if (buf == nullptr)
{
// Allocate a new movement buffer
buf = CanMessageBuffer::Allocate();
if (buf == nullptr)
{
reprap.GetPlatform().Message(ErrorMessage, "Out of CAN buffers\n");
return nullptr;
}
buf->next = movementBufferList;
movementBufferList = buf;
auto move = buf->SetupRequestMessageNoRid<CanMessageMovementLinearShaped>(CanInterface::GetCurrentMasterAddress(), canDriver.boardAddress);
// Common parameters
if (buf->next == nullptr)
{
// This is the first CAN-connected board for this movement
move->accelerationClocks = params.TotalAccelClocks();
move->steadyClocks = params.steadyClocks;
move->decelClocks = params.TotalDecelClocks();
currentMoveClocks = params.TotalClocks();
}
else
{
// Save some maths by using the values from the previous buffer
move->accelerationClocks = buf->next->msg.moveLinearShaped.accelerationClocks;
move->steadyClocks = buf->next->msg.moveLinearShaped.steadyClocks;
move->decelClocks = buf->next->msg.moveLinearShaped.decelClocks;
}
move->acceleration = params.acceleration/params.totalDistance; // scale the acceleration to correspond to unit distance
move->deceleration = -params.deceleration/params.totalDistance; // scale the deceleration to correspond to unit distance
move->pressureAdvanceClocks = 0.0;
move->extruderDrives = 0;
move->numDrivers = canDriver.localDriver + 1;
move->zero1 = move->zero2 = 0;
move->useLateInputShaping = params.useInputShaping;
// Clear out the per-drive fields. Can't use a range-based FOR loop on a packed struct.
for (size_t drive = 0; drive < ARRAY_SIZE(move->perDrive); ++drive)
{
move->perDrive[drive].Init();
}
}
else if (canDriver.localDriver >= buf->msg.moveLinearShaped.numDrivers)
{
buf->msg.moveLinearShaped.numDrivers = canDriver.localDriver + 1;
}
return buf;
}
// This is called by DDA::Prepare for each active CAN DM in the move
void CanMotion::AddAxisMovement(const PrepParams& params, DriverId canDriver, int32_t steps) noexcept
{
CanMessageBuffer * const buf = GetBuffer(params, canDriver);
if (buf != nullptr)
{
buf->msg.moveLinearShaped.perDrive[canDriver.localDriver].steps = steps;
}
}
void CanMotion::AddExtruderMovement(const PrepParams& params, DriverId canDriver, float extrusion, float pressureAdvanceClocks) noexcept
{
CanMessageBuffer * const buf = GetBuffer(params, canDriver);
if (buf != nullptr)
{
buf->msg.moveLinearShaped.perDrive[canDriver.localDriver].extrusion = extrusion;
buf->msg.moveLinearShaped.extruderDrives |= 1u << canDriver.localDriver;
buf->msg.moveLinearShaped.usePressureAdvance = (pressureAdvanceClocks > 0.0);
buf->msg.moveLinearShaped.pressureAdvanceClocks = pressureAdvanceClocks;
}
}
// This is called by DDA::Prepare when all DMs for CAN drives have been processed. Return the calculated move time in steps, or 0 if there are no CAN moves
uint32_t CanMotion::FinishMovement(const DDA& dda, uint32_t moveStartTime, bool simulating) noexcept
{
uint32_t clocks = 0;
if (simulating)
{
FreeMovementBuffers(); // it turned out that there was nothing to move
}
else
{
CanMessageBuffer *buf = movementBufferList;
if (buf != nullptr)
{
MutexLocker lock((dda.IsCheckingEndstops()) ? &stopListMutex : nullptr);
do
{
CanMessageBuffer * const nextBuffer = buf->next; // must get this before sending the buffer, because sending the buffer releases it
CanMessageMovementLinearShaped& msg = buf->msg.moveLinearShaped;
if (msg.HasMotion())
{
if (!BoardSupportsMovementPaSnapshot(buf->id.Dst()))
{
if (!warnedNoPaSnapshotSupport[buf->id.Dst()])
{
warnedNoPaSnapshotSupport[buf->id.Dst()] = true;
reprap.GetPlatform().MessageF(ErrorMessage,
"CAN board %u is missing movement PA snapshot support; flash matching firmware on all boards\n",
buf->id.Dst());
}
reprap.EmergencyStop();
CanMessageBuffer::Free(buf);
buf = nextBuffer;
continue;
}
msg.whenToExecute = moveStartTime;
uint8_t& seq = nextSeq[buf->id.Dst()];
msg.seq = seq;
seq = (seq + 1) & 0x7F;
buf->dataLength = msg.GetActualDataLength();
if (dda.IsCheckingEndstops())
{
// Set up the stop list
DriversStopList * const sl = new DriversStopList(stopList, buf->id.Dst());
const size_t nd = msg.numDrivers;
sl->numDrivers = (uint8_t)nd;
for (size_t i = 0; i < nd; ++i)
{
sl->stopStates[i] = (msg.perDrive[i].steps != 0) ? DriverStopState::active : DriverStopState::inactive;
}
stopList = sl;
}
CanInterface::SendMotion(buf); // queues the buffer for sending and frees it when done
clocks = currentMoveClocks;
}
else
{
CanMessageBuffer::Free(buf);
}
buf = nextBuffer;
} while (buf != nullptr);
movementBufferList = nullptr;
}
}
return clocks;
}
bool CanMotion::CanPrepareMove() noexcept
{
return CanMessageBuffer::GetFreeBuffers() >= MaxCanBoards;
}
// This is called by the CanSender task to check if we have any urgent messages to send
// The only urgent messages we may have currently are messages to stop drivers, or to tell them that all drivers have now been stopped and they need to revert to the requested stop position.
CanMessageBuffer *CanMotion::GetUrgentMessage() noexcept
{
if (!revertedAll)
{
MutexLocker lock(stopListMutex); // make sure the list isn't being changed while we traverse it
// We have to be careful of race conditions here. The stop list links won't change while we are scanning it because we hold the mutex,
// but ISR may change the stop states to StopRequested up until the time at which it changes revertAll from false to true.
const bool revertingAll = revertAll;
for (DriversStopList *sl = stopList; sl != nullptr; sl = sl->next)
{
if (!sl->sentRevertRequest) // if we've already reverted the drivers on this board, no more to do
{
// Set up a reversion message in case we are going to revert the drivers on this board
auto revertMsg = urgentMessageBuffer.SetupRequestMessageNoRid<CanMessageRevertPosition>(CanInterface::GetCanAddress(), sl->boardAddress);
uint16_t driversToStop = 0, driversToRevert = 0;
size_t numDriversReverted = 0;
for (size_t driver = 0; driver < sl->numDrivers; ++driver)
{
const DriverStopState ss = sl->stopStates[driver];
if (ss == DriverStopState::stopRequested)
{
driversToStop |= 1u << driver;
sl->stopStates[driver] = DriverStopState::stopSent;
}
else if (revertingAll && ss == DriverStopState::stopSent)
{
driversToRevert |= 1u << driver;
revertMsg->finalStepCounts[numDriversReverted++] = sl->stopSteps[driver];
}
}
if (driversToStop != 0)
{
auto stopMsg = urgentMessageBuffer.SetupRequestMessageNoRid<CanMessageStopMovement>(CanInterface::GetCanAddress(), sl->boardAddress);
stopMsg->whichDrives = driversToStop;
//debugPrintf("Stopping drivers %u on board %u\n", driversToStop, sl->boardAddress);
return &urgentMessageBuffer;
}
if (driversToRevert != 0)
{
sl->sentRevertRequest = true;
revertMsg->whichDrives = driversToRevert;
revertMsg->clocksAllowed = MillisToStepClocks(BasicDriverPositionRevertMillis);
urgentMessageBuffer.dataLength = revertMsg->GetActualDataLength(numDriversReverted);
//debugPrintf("Reverting drivers %u by %" PRIi32 " on board %u\n", driversToRevert,revertMsg->finalStepCounts[0], sl->boardAddress);
return &urgentMessageBuffer;
}
}
}
// We found nothing to send
if (revertingAll)
{
// All drivers have been stopped and reverted where requested
whenRevertedAll = millis();
revertedAll = true;
}
}
return nullptr;
}
// The next 4 functions may be called from the step ISR, so they can't send CAN messages directly
// Flag a CAN-connected driver as not moving when we haven't sent the movement message yet
void CanMotion::StopDriverWhenProvisional(DriverId driver) noexcept
{
// Search for the correct movement buffer
CanMessageBuffer* buf = movementBufferList;
while (buf != nullptr && buf->id.Dst() != driver.boardAddress)
{
buf = buf->next;
}
// If the move was found, set the steps to zero. We still send the message so that the drivers get enabled.
if (buf != nullptr)
{
buf->msg.moveLinearShaped.perDrive[driver.localDriver].steps = 0;
}
}
// Tell a CAN-connected driver to stop moving after we have sent the movement message
bool CanMotion::StopDriverWhenExecuting(DriverId driver, int32_t netStepsTaken) noexcept
{
DriversStopList *sl = stopList;
while (sl != nullptr)
{
if (sl->boardAddress == driver.boardAddress)
{
if (sl->stopStates[driver.localDriver] == DriverStopState::active) // if active and stop not yet requested
{
sl->stopSteps[driver.localDriver] = netStepsTaken; // must assign this one first
sl->stopStates[driver.localDriver] = DriverStopState::stopRequested;
return true;
}
break; // we found the right board, no point in searching further
}
sl = sl->next;
}
return false;
}
// Revert any stopped drivers that we haven't already and return true when there are no drivers to revert
bool CanMotion::RevertStoppedDrivers() noexcept
{
if (!revertAll && !revertedAll) // if not started reverting yet
{
revertAll = true;
CanInterface::WakeAsyncSender();
return false;
}
return !revertAll || (revertedAll && millis() - whenRevertedAll >= TotalDriverPositionRevertMillis);
}
#endif
// End