Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SwitchEndstop.cpp « Endstops « src - github.com/Duet3D/RepRapFirmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 747c8f416fdc0726f7d66a57472396f7937f1764 (plain)
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
/*
 * LocalSwitchEndstop.cpp
 *
 *  Created on: 15 Sep 2019
 *      Author: David
 */

#include <Endstops/SwitchEndstop.h>

#include "RepRap.h"
#include "Platform.h"
#include "Movement/Kinematics/Kinematics.h"
#include "GCodes/GCodeBuffer/GCodeBuffer.h"

#if SUPPORT_CAN_EXPANSION
# include "CanId.h"
# include "CanMessageBuffer.h"
# include "CanMessageFormats.h"
# include "CAN/CanInterface.h"
#endif

// Switch endstop
SwitchEndstop::SwitchEndstop(uint8_t axis, EndStopPosition pos) : Endstop(axis, pos), numPortsUsed(0)
{
	// ports will be initialised automatically by the IoPort default constructor
}

SwitchEndstop::~SwitchEndstop()
{
	ReleasePorts();
}

// Release any local and remote ports we have allocated and set numPortsUsed to zero
void SwitchEndstop::ReleasePorts()
{
	while (numPortsUsed != 0)
	{
		--numPortsUsed;
#if SUPPORT_CAN_EXPANSION
		const CanAddress bn = boardNumbers[numPortsUsed];
		if (bn != CanId::MasterAddress)
		{
			RemoteInputHandle h(RemoteInputHandle::typeEndstop, GetAxis(), numPortsUsed);
			String<StringLength100> reply;
			if (CanInterface::DeleteHandle(bn, h, reply.GetRef()) != GCodeResult::ok)
			{
				reply.cat('\n');
				reprap.GetPlatform().Message(ErrorMessage, reply.c_str());
			}
		}
#endif
		ports[numPortsUsed].Release();
	}
}

GCodeResult SwitchEndstop::Configure(GCodeBuffer& gb, const StringRef& reply, EndStopInputType inputType)
{
	String<StringLength50> portNames;
	if (!gb.GetReducedString(portNames.GetRef()))
	{
		reply.copy("Missing port name string");
		return GCodeResult::error;
	}

	return Configure(portNames.c_str(), reply, inputType);
}

GCodeResult SwitchEndstop::Configure(const char *pinNames, const StringRef& reply, EndStopInputType inputType)
{
	ReleasePorts();

#if SUPPORT_CAN_EXPANSION
	activeLow = (inputType == EndStopInputType::activeLow);
#endif

	// Parse the string into individual port names
	size_t index = 0;
	while (numPortsUsed < MaxDriversPerAxis)
	{
		// Get the next port name
		String<StringLength50> pn;
		char c;
		while ((c = pinNames[index]) != 0 && c != '+')
		{
			pn.cat(c);
			++index;
		}

#if SUPPORT_CAN_EXPANSION
		const CanAddress boardAddress = IoPort::RemoveBoardAddress(pn.GetRef());
		boardNumbers[numPortsUsed] = boardAddress;
		if (boardAddress != CanId::MasterAddress)
		{
			RemoteInputHandle h(RemoteInputHandle::typeEndstop, GetAxis(), numPortsUsed);
			const GCodeResult rslt = CanInterface::CreateHandle(boardAddress, h, pn.c_str(), 0, MinimumSwitchReportInterval, states[numPortsUsed], reply);
			if (rslt != GCodeResult::ok)
			{
				ReleasePorts();
				return rslt;
			}
		}
		else
#endif
		{
			// Try to allocate the port
			if (!ports[numPortsUsed].AssignPort(pn.c_str(), reply, PinUsedBy::endstop, PinAccess::read))
			{
				ReleasePorts();
				return GCodeResult::error;
			}
		}

		++numPortsUsed;
		if (c != '+')
		{
			break;
		}
		++index;					// skip the "+"
	}
	return GCodeResult::ok;
}

void SwitchEndstop::Reconfigure(EndStopPosition pos, EndStopInputType inputType)
{
	SetAtHighEnd(pos == EndStopPosition::highEndStop);

#if SUPPORT_CAN_EXPANSION
	activeLow = (inputType == EndStopInputType::activeLow);
#endif

	for (IoPort& pp : ports)
	{
		pp.SetInvert(inputType == EndStopInputType::activeLow);
	}
}

EndStopInputType SwitchEndstop::GetEndstopType() const
{
#if SUPPORT_CAN_EXPANSION
	return (activeLow)? EndStopInputType::activeLow : EndStopInputType::activeHigh;
#else
	return (ports[0].GetInvert()) ? EndStopInputType::activeLow : EndStopInputType::activeHigh;
#endif
}

// Test whether we are at or near the stop
EndStopHit SwitchEndstop::Stopped() const
{
	for (size_t i = 0; i < numPortsUsed; ++i)
	{
		if (IsTriggered(i))
		{
			return EndStopHit::atStop;
		}
	}
	return EndStopHit::noStop;
}

// This is called to prime axis endstops
bool SwitchEndstop::Prime(const Kinematics& kin, const AxisDriversConfig& axisDrivers)
{
	// Decide whether we stop just the driver, just the axis, or everything
	stopAll = ((kin.GetConnectedAxes(GetAxis()) & ~MakeBitmap<AxesBitmap>(GetAxis())) != 0);
	numPortsLeftToTrigger = (numPortsUsed != axisDrivers.numDrivers) ? 1 : numPortsUsed;
	portsLeftToTrigger = LowestNBits<PortsBitmap>(numPortsUsed);

#if SUPPORT_CAN_EXPANSION
	// For each remote switch, check that the expansion board knows about it, and make sure we have an up-to-date state
	for (size_t i = 0; i < numPortsUsed; ++i)
	{
		if (boardNumbers[i] != 0)
		{
			RemoteInputHandle h(RemoteInputHandle::typeEndstop, GetAxis(), i);
			String<StringLength100> reply;
			if (CanInterface::EnableHandle(boardNumbers[i], h, states[i], reply.GetRef()) != GCodeResult::ok)
			{
				reply.cat('\n');
				reprap.GetPlatform().Message(ErrorMessage, reply.c_str());
				return false;
			}
		}
	}
#endif

	return true;
}

// Check whether the endstop is triggered and return the action that should be performed. Don't update the state until Acknowledge is called.
// Called from the step ISR.
EndstopHitDetails SwitchEndstop::CheckTriggered(bool goingSlow)
{
	EndstopHitDetails rslt;				// initialised by default constructor
	if (portsLeftToTrigger != 0)
	{
		for (size_t i = 0; i < numPortsUsed; ++i)
		{
			if (IsBitSet(portsLeftToTrigger, i) && IsTriggered(i))
			{
				rslt.axis = GetAxis();
				if (stopAll)
				{
					rslt.SetAction(EndstopHitAction::stopAll);
					if (GetAtHighEnd())
					{
						rslt.setAxisHigh = true;
					}
					else
					{
						rslt.setAxisLow = true;
					}
				}
				else if (numPortsLeftToTrigger == 1)
				{
					rslt.SetAction(EndstopHitAction::stopAxis);
					if (GetAtHighEnd())
					{
						rslt.setAxisHigh = true;
					}
					else
					{
						rslt.setAxisLow = true;
					}
				}
				else
				{
					rslt.SetAction(EndstopHitAction::stopDriver);
					rslt.internalUse = i;			// remember which port it is, for the call to Acknowledge
					rslt.driver = reprap.GetPlatform().GetAxisDriversConfig(GetAxis()).driverNumbers[i];
				}
				break;
			}
		}
	}

	return rslt;
}

// This is called by the ISR to acknowledge that it is acting on the return from calling CheckTriggered. Called from the step ISR.
// Return true if we have finished with this endstop or probe in this move.
bool SwitchEndstop::Acknowledge(EndstopHitDetails what)
{
	switch (what.GetAction())
	{
	case EndstopHitAction::stopAll:
	case EndstopHitAction::stopAxis:
		return true;

	case EndstopHitAction::stopDriver:
		ClearBit(portsLeftToTrigger, what.internalUse);
		--numPortsLeftToTrigger;
		return false;

	default:
		return false;
	}
}

void SwitchEndstop::AppendDetails(const StringRef& str)
{
	str.catf("%s on pin(s)",
#if SUPPORT_CAN_EXPANSION
				(activeLow)
#else
				(ports[0].GetInvert())
#endif
				? "active low switch" : "active high switch");

	for (size_t i = 0; i < numPortsUsed; ++i)
	{
		str.cat(' ');
#if SUPPORT_CAN_EXPANSION
		if (boardNumbers[i] != CanId::MasterAddress)
		{
			RemoteInputHandle h(RemoteInputHandle::typeEndstop, GetAxis(), i);
			String<StringLength100> reply;
			if (CanInterface::GetHandlePinName(boardNumbers[i], h, states[i], reply.GetRef()) == GCodeResult::ok)
			{
				str.cat(reply.c_str());
			}
			else
			{
				reply.cat('\n');
				reprap.GetPlatform().Message(ErrorMessage, reply.c_str());
				str.catf("%u.???", boardNumbers[i]);
			}
		}
		else
#endif
		{
			ports[i].AppendPinName(str);
		}
	}
}

#if SUPPORT_CAN_EXPANSION

// Process a remote endstop input change that relates to this endstop
void SwitchEndstop::HandleRemoteInputChange(CanAddress src, uint8_t handleMinor, bool state)
{
	if (handleMinor < numPortsUsed && boardNumbers[handleMinor] == src)
	{
		states[handleMinor] = state;
	}
}

#endif

// End