-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
dcmotor.cpp
579 lines (484 loc) · 18.9 KB
/
dcmotor.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
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
// SPDX-License-Identifier: GPL-3.0-or-later
//
// Copyright (c) 2017-2023 plan44.ch / Lukas Zeller, Zurich, Switzerland
//
// Author: Lukas Zeller <[email protected]>
//
// This file is part of p44utils.
//
// p44ayabd is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// p44ayabd is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with p44ayabd. If not, see <http://www.gnu.org/licenses/>.
//
#include "dcmotor.hpp"
#include "consolekey.hpp"
#include "application.hpp"
#include <math.h>
using namespace p44;
// MARK: - DCMotorDriver
DcMotorDriver::DcMotorDriver(AnalogIoPtr aPWMOutput, DigitalIoPtr aCWDirectionOutput, DigitalIoPtr aCCWDirectionOutput) :
mCurrentPower(0),
mCurrentDirection(0)
{
mPwmOutput = aPWMOutput;
// - direction control
mCWdirectionOutput = aCWDirectionOutput;
mCCWdirectionOutput = aCCWDirectionOutput;
mPowerOffset = 0;
mPowerScaling = 1;
setPower(0, 0);
}
DcMotorDriver::~DcMotorDriver()
{
// stop power to motor
setPower(0, 0);
}
void DcMotorDriver::setStopCallback(DCMotorStatusCB aStoppedCB)
{
mStoppedCB = aStoppedCB;
}
void DcMotorDriver::setEndSwitches(DigitalIoPtr aPositiveEnd, DigitalIoPtr aNegativeEnd, MLMicroSeconds aDebounceTime, MLMicroSeconds aPollInterval)
{
mPositiveEndInput = aPositiveEnd;
mNegativeEndInput = aNegativeEnd;
if (mPositiveEndInput) {
#if ENABLE_DCMOTOR_SCRIPT_FUNCS && ENABLE_P44SCRIPT
mPositiveEndInput->setChangeDetection(aDebounceTime, aPollInterval);
mPositiveEndInput->registerForEvents(mEndSwitchHandler);
#else
mPositiveEndInput->setInputChangedHandler(boost::bind(&DcMotorDriver::endSwitch, this, true, _1), aDebounceTime, aPollInterval);
#endif
}
if (mNegativeEndInput) {
#if ENABLE_DCMOTOR_SCRIPT_FUNCS && ENABLE_P44SCRIPT
mNegativeEndInput->setChangeDetection(aDebounceTime, aPollInterval);
mNegativeEndInput->registerForEvents(mEndSwitchHandler);
#else
mNegativeEndInput->setInputChangedHandler(boost::bind(&DcMotorDriver::endSwitch, this, false, _1), aDebounceTime, aPollInterval);
#endif
}
#if ENABLE_DCMOTOR_SCRIPT_FUNCS && ENABLE_P44SCRIPT
mEndSwitchHandler.setHandler(boost::bind(&DcMotorDriver::endSwitchEvent, this, _1, _2));
#endif
}
#if ENABLE_DCMOTOR_SCRIPT_FUNCS && ENABLE_P44SCRIPT
void DcMotorDriver::endSwitchEvent(P44Script::ScriptObjPtr aEvent, P44Script::EventSource &aSource)
{
endSwitch(&aSource == static_cast<EventSource*>(mPositiveEndInput.get()), aEvent->boolValue());
}
#endif
void DcMotorDriver::endSwitch(bool aPositiveEnd, bool aNewState)
{
// save direction and power as known BEFORE stopping
if (aNewState) {
double pwr = mCurrentPower;
int dir = mCurrentDirection;
stop();
LOG(LOG_INFO, "stopped with power=%.2f, direction=%d because %s end switch reached", pwr, dir, aPositiveEnd ? "positive" : "negative");
autoStopped(pwr, dir, new DcMotorDriverError(DcMotorDriverError::endswitchStop));
}
}
void DcMotorDriver::autoStopped(double aPower, int aDirection, ErrorPtr aError)
{
if (mStoppedCB) {
// stop callback does not reset
mStoppedCB(aPower, aDirection, aError);
}
motorStatusUpdate(aError);
}
void DcMotorDriver::motorStatusUpdate(ErrorPtr aStopCause)
{
mStopCause = aStopCause;
if (mRampDoneCB) {
// ramp done CB must be set for every ramp separately
DCMotorStatusCB cb = mRampDoneCB;
mRampDoneCB = NoOP;
cb(mCurrentPower, mCurrentDirection, mStopCause);
}
#if ENABLE_DCMOTOR_SCRIPT_FUNCS && ENABLE_P44SCRIPT
if (hasSinks()) {
sendEvent(getStatusObj());
}
#endif
}
#if ENABLE_DCMOTOR_SCRIPT_FUNCS && ENABLE_P44SCRIPT
P44Script::ScriptObjPtr DcMotorDriver::getStatusObj()
{
return new P44Script::DcMotorStatusObj(this);
}
#endif
void DcMotorDriver::setCurrentSensor(AnalogIoPtr aCurrentSensor, MLMicroSeconds aSampleInterval)
{
// - current sensor
#if ENABLE_DCMOTOR_SCRIPT_FUNCS && ENABLE_P44SCRIPT
if (mCurrentSensor) mCurrentSensor->unregisterFromEvents(mCurrentHandler); // make sure previous sensor no longer sends events
#endif
mCurrentSensor = aCurrentSensor;
mSampleInterval = aSampleInterval;
#if ENABLE_DCMOTOR_SCRIPT_FUNCS && ENABLE_P44SCRIPT
if (mCurrentSensor) mCurrentSensor->registerForEvents(mCurrentHandler); // we want to see events of the new sensor
#endif
}
void DcMotorDriver::setCurrentLimits(double aStopCurrent, MLMicroSeconds aHoldOffTime, double aMaxStartCurrent)
{
// - current sensor
mStopCurrent = aStopCurrent;
mCurrentLimiterHoldoffTime = aHoldOffTime;
mMaxStartCurrent = aMaxStartCurrent;
}
void DcMotorDriver::setOutputParams(double aPowerScaling, double aPowerOffset)
{
mPowerScaling = aPowerScaling;
mPowerOffset = aPowerOffset;
}
void DcMotorDriver::setDirection(int aDirection)
{
if (mCWdirectionOutput) {
mCWdirectionOutput->set(aDirection>0);
if (mCCWdirectionOutput) {
mCCWdirectionOutput->set(aDirection<0);
}
}
if (aDirection!=mCurrentDirection) {
OLOG(LOG_INFO, "Direction changed to %d", aDirection);
mCurrentDirection = aDirection;
}
}
void DcMotorDriver::setPower(double aPower, int aDirection)
{
if (aPower<=0) {
// no power
// - disable power completely (0, even when there is a mPowerOffset)
mPwmOutput->setValue(0);
// - off (= hold/brake with no power)
setDirection(0);
// disable current sampling
if (mCurrentSensor) mCurrentSensor->setAutopoll(0);
}
else {
// determine current direction
if (mCurrentDirection!=0 && aDirection!=0 && aDirection!=mCurrentDirection) {
// avoid reversing direction with power on
mPwmOutput->setValue(0);
setDirection(0);
}
// check end switch, do not allow setting power towards already active end switch
if (
(aDirection<0 && mNegativeEndInput && mNegativeEndInput->isSet()) ||
(aDirection>0 && mPositiveEndInput && mPositiveEndInput->isSet())
) {
OLOG(LOG_INFO, "Cannot start in direction %d, endswitch is active", aDirection);
// count this as running (again) into an end switch and cause callback to fire
endSwitch(aDirection>0, true);
return;
}
// start current sampling when starting to apply power
if (mCurrentSensor && mStopCurrent>0 && aDirection!=0 && mCurrentPower==0) {
mStartMonitoring = MainLoop::now()+mCurrentLimiterHoldoffTime;
#if ENABLE_DCMOTOR_SCRIPT_FUNCS && ENABLE_P44SCRIPT
mCurrentSensor->setAutopoll(mSampleInterval, mSampleInterval/4);
mCurrentHandler.setHandler(boost::bind(&DcMotorDriver::checkCurrent, this));
#else
mCurrentSensor->setAutopoll(mSampleInterval, mSampleInterval/4, boost::bind(&DcMotorDriver::checkCurrent, this));
#endif
}
// now set desired direction and power
setDirection(aDirection);
mPwmOutput->setValue(mPowerOffset+aPower*mPowerScaling);
}
if (aPower!=mCurrentPower) {
OLOG(LOG_DEBUG, "Power changed to %.2f%%", aPower);
mCurrentPower = aPower;
}
}
void DcMotorDriver::checkCurrent()
{
if (mStopCurrent>0) {
// event from current sensor, process value
double v = fabs(mCurrentSensor->processedValue()); // takes abs, in case we're not using processing that already takes abs values
OLOG(LOG_DEBUG, "checkCurrent: processed: %.3f, last raw value: %.3f", v, mCurrentSensor->lastValue());
if (
(v>=mStopCurrent && MainLoop::now()>=mStartMonitoring) || // normal limit
(mMaxStartCurrent>0 && v>=mMaxStartCurrent) // limit during startup
) {
if (mCurrentPower>0) {
double pwr = mCurrentPower;
int dir = mCurrentDirection;
stop();
OLOG(LOG_INFO, "stopped because processed current (%.3f) exceeds max (%.3f) - last raw sample = %.3f", v, mStopCurrent, mCurrentSensor->lastValue());
autoStopped(pwr, dir, new DcMotorDriverError(DcMotorDriverError::overcurrentStop));
}
}
}
}
#define RAMP_STEP_TIME (20*MilliSecond)
void DcMotorDriver::stop()
{
stopSequences();
setPower(0, 0);
}
void DcMotorDriver::stopSequences()
{
mSequenceTicket.cancel();
}
void DcMotorDriver::rampToPower(double aPower, int aDirection, double aRampTime, double aRampExp, DCMotorStatusCB aRampDoneCB)
{
OLOG(LOG_INFO, "+++ new ramp: power: %.2f%%..%.2f%%, direction:%d..%d with ramp time %.3f Seconds, exp=%.2f", mCurrentPower, aPower, mCurrentDirection, aDirection, aRampTime, aRampExp);
mStopCause.reset(); // clear status from previous run
mRampDoneCB = aRampDoneCB;
MainLoop::currentMainLoop().cancelExecutionTicket(mSequenceTicket);
if (aDirection!=mCurrentDirection) {
if (mCurrentPower!=0) {
// ramp to zero first, then ramp up to new direction
OLOG(LOG_INFO, "Ramp trough different direction modes -> first ramp power down, then up again");
if (aRampTime>0) aRampTime /= 2; // for absolute ramp time specificiation, just use half of the time for ramp up or down, resp.
rampToPower(0, mCurrentDirection, aRampTime, aRampExp, boost::bind(&DcMotorDriver::rampToPower, this, aPower, aDirection, aRampTime, aRampExp, aRampDoneCB));
return;
}
// set new direction
setDirection(aDirection);
}
// limit
if (aPower>100) aPower=100;
else if (aPower<0) aPower=0;
// ramp to new value
double rampRange = aPower-mCurrentPower;
MLMicroSeconds totalRampTime;
if (aRampTime<0) {
// specification is 0..100 ramp time, scale according to power difference
totalRampTime = fabs(rampRange)/100*(-aRampTime)*Second;
}
else {
// absolute specification
totalRampTime = aRampTime*Second;
}
int numSteps = (int)(totalRampTime/RAMP_STEP_TIME)+1;
OLOG(LOG_INFO, "Ramp power from %.2f%% to %.2f%% in %lld uS (%d steps)", mCurrentPower, aPower, totalRampTime, numSteps);
// now execute the ramp
rampStep(mCurrentPower, aPower, numSteps, 0, aRampExp);
}
void DcMotorDriver::rampStep(double aStartPower, double aTargetPower, int aNumSteps, int aStepNo , double aRampExp)
{
OLOG(LOG_DEBUG, "ramp step #%d/%d, %d%% of ramp", aStepNo, aNumSteps, aStepNo*100/aNumSteps);
if (aStepNo++>=aNumSteps) {
// finalize
setPower(aTargetPower, mCurrentDirection);
OLOG(LOG_INFO, "--- end of ramp");
// update status + run callback
motorStatusUpdate(ErrorPtr());
}
else {
// set power for this step
double f = (double)aStepNo/aNumSteps;
if (aRampExp!=0) {
f = (exp(f*aRampExp)-1)/(exp(aRampExp)-1);
}
// - scale the power
double pwr = aStartPower + (aTargetPower-aStartPower)*f;
OLOG(LOG_DEBUG, "- f=%.3f, pwr=%.2f", f, pwr);
setPower(pwr, mCurrentDirection);
// schedule next step
mSequenceTicket.executeOnce(boost::bind(
&DcMotorDriver::rampStep, this, aStartPower, aTargetPower, aNumSteps, aStepNo, aRampExp),
RAMP_STEP_TIME
);
}
}
void DcMotorDriver::runSequence(SequenceStepList aSteps, DCMotorStatusCB aSequenceDoneCB)
{
stopSequences();
if (aSteps.size()==0) {
// done
if (aSequenceDoneCB) aSequenceDoneCB(mCurrentPower, mCurrentDirection, ErrorPtr());
}
// next step
SequenceStep step = aSteps.front();
rampToPower(step.power, step.direction, step.rampTime, step.rampExp, boost::bind(&DcMotorDriver::sequenceStepDone, this, aSteps, aSequenceDoneCB, _3));
}
void DcMotorDriver::sequenceStepDone(SequenceStepList aSteps, DCMotorStatusCB aSequenceDoneCB, ErrorPtr aError)
{
if (!Error::isOK(aError)) {
// error, abort sequence
if (aSequenceDoneCB) aSequenceDoneCB(mCurrentPower, mCurrentDirection, aError);
return;
}
// launch next step after given run time
SequenceStep step = aSteps.front();
aSteps.pop_front();
MainLoop::currentMainLoop().executeTicketOnce(mSequenceTicket, boost::bind(&DcMotorDriver::runSequence, this, aSteps, aSequenceDoneCB), step.runTime*Second);
}
// MARK: - script support
#if ENABLE_DCMOTOR_SCRIPT_FUNCS && ENABLE_P44SCRIPT
using namespace P44Script;
DcMotorStatusObj::DcMotorStatusObj(DcMotorDriverPtr aDcMotorDriver) :
mDcMotorDriver(aDcMotorDriver)
{
// create snapshot of status right now
setMemberByName("power", new NumericValue(mDcMotorDriver->mCurrentPower));
setMemberByName("direction", new IntegerValue(mDcMotorDriver->mCurrentDirection));
if (mDcMotorDriver->mStopCause) {
string cause;
if (mDcMotorDriver->mStopCause->isError(DcMotorDriverError::domain(), DcMotorDriverError::overcurrentStop)) cause = "overcurrent";
else if (mDcMotorDriver->mStopCause->isError(DcMotorDriverError::domain(), DcMotorDriverError::endswitchStop)) cause = "endswitch";
else cause = mDcMotorDriver->mStopCause->text();
setMemberByName("stoppedby", new StringValue(cause));
}
if (mDcMotorDriver->mCurrentSensor) {
setMemberByName("current", new NumericValue(mDcMotorDriver->mCurrentSensor->lastValue()));
}
}
void DcMotorStatusObj::deactivate()
{
mDcMotorDriver.reset();
inherited::deactivate();
}
string DcMotorStatusObj::getAnnotation() const
{
return "DC motor event";
}
TypeInfo DcMotorStatusObj::getTypeInfo() const
{
return inherited::getTypeInfo()|freezable; // can be frozen
}
bool DcMotorStatusObj::isEventSource() const
{
return mDcMotorDriver.get(); // yes if it exists
}
void DcMotorStatusObj::registerForFilteredEvents(EventSink* aEventSink, intptr_t aRegId)
{
if (mDcMotorDriver) mDcMotorDriver->registerForEvents(aEventSink, aRegId); // no filtering
}
// status()
static void status_func(BuiltinFunctionContextPtr f)
{
DcMotorObj* dc = dynamic_cast<DcMotorObj*>(f->thisObj().get());
assert(dc);
f->finish(new DcMotorStatusObj(dc->dcMotor()));
}
// stop()
static void stop_func(BuiltinFunctionContextPtr f)
{
DcMotorObj* dc = dynamic_cast<DcMotorObj*>(f->thisObj().get());
assert(dc);
dc->dcMotor()->stop();
f->finish();
}
#define DEFAULT_CURRENT_POLL_INTERVAL (333*MilliSecond)
// currentsensor(sensor [, sampleinterval])
FUNC_ARG_DEFS(currentsensor, { text|objectvalue }, { numeric|optionalarg } );
static void currentsensor_func(BuiltinFunctionContextPtr f)
{
DcMotorObj* dc = dynamic_cast<DcMotorObj*>(f->thisObj().get());
assert(dc);
AnalogIoPtr sens = AnalogIoObj::analogIoFromArg(f->arg(0), false, 0);
MLMicroSeconds interval = DEFAULT_CURRENT_POLL_INTERVAL; // sensible default
if (f->arg(1)->defined()) interval = f->arg(1)->doubleValue()*Second;
if (!sens) {
interval = 0; // no polling if we have no sensor
}
dc->dcMotor()->setCurrentSensor(sens, interval);
f->finish();
}
// currentlimit(limit [, startuptime [, maxstartcurrent]])
FUNC_ARG_DEFS(currentlimit, { numeric }, { numeric|optionalarg }, { numeric|optionalarg } );
static void currentlimit_func(BuiltinFunctionContextPtr f)
{
DcMotorObj* dc = dynamic_cast<DcMotorObj*>(f->thisObj().get());
assert(dc);
double limit = f->arg(0)->doubleValue();
MLMicroSeconds holdoff = 0;
if (f->arg(1)->defined()) holdoff = f->arg(1)->doubleValue()*Second;
double maxlimit = limit*2; // default to twice the normal limit
if (f->arg(2)->defined()) maxlimit = f->arg(2)->doubleValue();
dc->dcMotor()->setCurrentLimits(limit, holdoff, maxlimit);
f->finish();
}
#define DEFAULT_ENDSWITCH_DEBOUNCE_TIME (80*MilliSecond)
// endswitches(positiveend, negativeend [, debouncetime [, pollinterval]])
FUNC_ARG_DEFS(endswitches, { text|objectvalue|null }, { text|objectvalue|optionalarg }, { numeric|optionalarg }, { numeric|optionalarg } );
static void endswitches_func(BuiltinFunctionContextPtr f)
{
DcMotorObj* dc = dynamic_cast<DcMotorObj*>(f->thisObj().get());
assert(dc);
DigitalIoPtr pos = DigitalIoObj::digitalIoFromArg(f->arg(0), false, false);
DigitalIoPtr neg = DigitalIoObj::digitalIoFromArg(f->arg(1), false, false);
MLMicroSeconds interval = 0; // automatic
MLMicroSeconds debouncetime = DEFAULT_ENDSWITCH_DEBOUNCE_TIME; // usually, we need some debounce to avoid stopping while driving out of end switch
if (f->arg(2)->defined()) debouncetime = f->arg(2)->doubleValue()*Second;
if (f->arg(3)->defined()) interval = f->arg(2)->doubleValue()*Second;
dc->dcMotor()->setEndSwitches(pos, neg, debouncetime, interval);
f->finish();
}
// outputparams(scaling [, offset])
FUNC_ARG_DEFS(outputparams, { numeric }, { numeric|optionalarg } );
static void outputparams_func(BuiltinFunctionContextPtr f)
{
DcMotorObj* dc = dynamic_cast<DcMotorObj*>(f->thisObj().get());
assert(dc);
dc->dcMotor()->setOutputParams(f->arg(0)->doubleValue(), f->arg(1)->doubleValue());
f->finish();
}
// power(power [, direction [, ramptime [, rampexponent]]])
FUNC_ARG_DEFS(power, { numeric }, { numeric|optionalarg }, { numeric|optionalarg }, { numeric|optionalarg } );
static void power_func(BuiltinFunctionContextPtr f)
{
DcMotorObj* dc = dynamic_cast<DcMotorObj*>(f->thisObj().get());
assert(dc);
double power = f->arg(0)->doubleValue();
int direction = 1; // default to forward (simplest case, just unidirectional DC motor)
double ramptime = -1; // default to 1 second full scale ramp. NOTE: THESE ARE SECONDS, not MLMicroSeconds!
double rampexponent = 1; // default to linear ramp
if (f->arg(1)->defined()) direction = f->arg(1)->intValue();
if (f->arg(2)->defined()) ramptime = f->arg(2)->doubleValue(); // NOTE: THESE ARE SECONDS, not MLMicroSeconds!
if (f->arg(3)->defined()) rampexponent = f->arg(3)->doubleValue();
dc->dcMotor()->rampToPower(power, direction, ramptime, rampexponent);
f->finish();
}
static const BuiltinMemberDescriptor dcmotorFunctions[] = {
FUNC_DEF_W_ARG(outputparams, executable|null),
FUNC_DEF_W_ARG(endswitches, executable|null),
FUNC_DEF_W_ARG(currentsensor, executable|null),
FUNC_DEF_W_ARG(currentlimit, executable|null),
FUNC_DEF_W_ARG(power, executable|null),
FUNC_DEF_NOARG(status, executable|objectvalue),
FUNC_DEF_NOARG(stop, executable|null),
{ NULL } // terminator
};
static BuiltInMemberLookup* sharedDCMotorFunctionLookupP = NULL;
DcMotorObj::DcMotorObj(DcMotorDriverPtr aDCMotor) :
mDCMotor(aDCMotor)
{
registerSharedLookup(sharedDCMotorFunctionLookupP, dcmotorFunctions);
}
// dcmotor(output [, CWdirection [, CCWdirection]])
FUNC_ARG_DEFS(dcmotor, { text|objectvalue }, { text|objectvalue|optionalarg }, { text|objectvalue|optionalarg } );
static void dcmotor_func(BuiltinFunctionContextPtr f)
{
AnalogIoPtr power = AnalogIoObj::analogIoFromArg(f->arg(0), true, 0);
if (!power) {
f->finish(new ErrorValue(ScriptError::Invalid, "missing analog output"));
return;
}
DigitalIoPtr cwd = DigitalIoObj::digitalIoFromArg(f->arg(1), true, false);
DigitalIoPtr ccwd = DigitalIoObj::digitalIoFromArg(f->arg(2), true, false);
DcMotorDriverPtr dcmotor = new DcMotorDriver(power, cwd, ccwd);
f->finish(new DcMotorObj(dcmotor));
}
static const BuiltinMemberDescriptor dcmotorGlobals[] = {
FUNC_DEF_W_ARG(dcmotor, executable|null),
{ NULL } // terminator
};
DcMotorLookup::DcMotorLookup() :
inherited(dcmotorGlobals)
{
}
#endif // ENABLE_DCMOTOR_SCRIPT_FUNCS && ENABLE_P44SCRIPT