Serial Wombat Arduino Library
Loading...
Searching...
No Matches
SerialWombatAbstractScaledOutput.h
Go to the documentation of this file.
1#pragma once
2/*
3Copyright 2021-2025 Broadwell Consulting Inc.
4
5"Serial Wombat" is a registered trademark of Broadwell Consulting Inc. in
6the United States. See SerialWombat.com for usage guidance.
7
8Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14
15The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
25*/
26
27#include "SerialWombat.h"
28#include "limits.h"
29
32
33
63{
64public:
71
85
91
92
108 int16_t writeTimeout(uint16_t timeout_mS, uint16_t timeoutOutputValue)
109 {
110
111 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
112 pin(),
114 1,
115 SW_LE16(timeout_mS),
116 SW_LE16(timeoutOutputValue),
117 };
118
119 int16_t result = _asosw.sendPacket(tx);
120
121 return(result);
122
123 }
124
133 int16_t writeScalingEnabled(bool enabled, uint8_t sourcePin)
134 {
135 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
136 pin(), swPinModeNumber(),
137 0, //Enable/disable scaling, set source pin
138 enabled,
139 sourcePin,
140 0x55, 0x55};
141
142 int16_t result = _asosw.sendPacket(tx);
143 return(result);
144 }
145
159 int16_t writeInputScaling(uint16_t inputMin, uint16_t inputMax)
160 {
161 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
162 pin(),
164 2, // Set input scaling
165 SW_LE16(inputMin),
166 SW_LE16(inputMax),
167 };
168 return(_asosw.sendPacket(tx));
169 }
170
186 int16_t writeOutputScaling(uint16_t outputMin, uint16_t outputMax)
187 {
188 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
189 pin(),
191 5, //Set output scaling
192 SW_LE16(outputMin),
193 SW_LE16(outputMax),
194 };
195 return(_asosw.sendPacket(tx));
196 }
197
205 int16_t writeScalingInvertedInput(bool inverted)
206 {
207 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
208 pin(),
210 3, // Set inverted/not inverted
211 inverted,
212 0x55,0x55,0x55,
213 };
214 return(_asosw.sendPacket(tx));
215 }
216
229 int16_t writeScalingTargetValue(uint16_t target)
230 {
231 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
232 pin(),
234 6, // Set target value for PID controller
235 SW_LE16(target),0x55,0x55,
236 };
237 return(_asosw.sendPacket(tx));
238 }
239
253 int16_t writeRateControl(Period samplePeriod, uint16_t maximumChangecounts, uint16_t maximumDecrementCounts = 0)
254 {
255 {
256 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
257 pin(),
259 4, // Set filter mode
260 1, // Filter mode rate control
261 SW_LE16(maximumChangecounts),0x55,
262 };
263 int16_t result = _asosw.sendPacket(tx);
264 if (result < 0)
265 {
266 return(result);
267 }
268 }
269 {
270 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
271 pin(),
273 8, // Set filter mode
274 SW_LE16(maximumDecrementCounts),0x55,0x55
275 };
276 int16_t result = _asosw.sendPacket(tx);
277 if (result < 0)
278 {
279 return(result);
280 }
281 }
282 {
283 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
284 pin(),
286 7, // Set Sample Rate
287 (uint8_t)samplePeriod,
288 0x55,0x55,0x55,
289 };
290 return(_asosw.sendPacket(tx));
291 }
292 }
293
307 int16_t write1stOrderFiltering(Period sampleRate, uint16_t filterConstant)
308 {
309 {
310 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
311 pin(),
313 4, // Set filter mode
314 2, // Filter mode 1st Order
315 SW_LE16(filterConstant),0x55,
316 };
317 int16_t result = _asosw.sendPacket(tx);
318 if (result < 0)
319 {
320 return(result);
321 }
322 }
323 {
324 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
325 pin(),
327 7, // Set Sample Rate
328 (uint8_t)sampleRate,
329 0x55,0x55,0x55,
330 };
331 return(_asosw.sendPacket(tx));
332 }
333 }
334
349 int16_t writeHysteresis(uint16_t lowLimit, uint16_t lowOutputValue, uint16_t highLimit, uint16_t highOutputValue, uint16_t initialOutputValue)
350 {
351 {
352 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
353 pin(),
355 50, // Set hysteresis high limit/output
356 SW_LE16(highLimit),
357 SW_LE16(highOutputValue)
358 };
359 int16_t result = _asosw.sendPacket(tx); if (result < 0) { return(result); }
360 }
361
362 {
363 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
364 pin(),
366 51, // Set hysteresis low limit/output
367 SW_LE16(lowLimit),
368 SW_LE16(lowOutputValue)
369 };
370 int16_t result = _asosw.sendPacket(tx); if (result < 0) { return(result); }
371 }
372 {
373 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
374 pin(),
376 52, // Set hysteresis low limit/output
377 SW_LE16(initialOutputValue),
378 0x55,0x55
379 };
380 int16_t result = _asosw.sendPacket(tx); if (result < 0) { return(result); }
381 }
382 return(0);
383 }
384
405 int16_t writeRamp(uint16_t slowIncrement, uint16_t incrementThreshold, uint16_t fastIncrement,Period samplePeriod, RampMode rampMode)
406 {
407 {
408 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
409 pin(),
411 7, // Set Sample Rate
412 (uint8_t) samplePeriod,
413 0x55,0x55,0x55
414 };
415 int16_t result = _asosw.sendPacket(tx); if (result < 0) { return(result); }
416 }
417 {
418 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
419 pin(),
421 60, // Set ramp slow increment, threshold
422 SW_LE16(slowIncrement),
423 SW_LE16(incrementThreshold)
424
425 };
426 int16_t result = _asosw.sendPacket(tx); if (result < 0) { return(result); }
427 }
428 {
429 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
430 pin(),
432 61, // Set ramp fast increment, mode
433 SW_LE16(fastIncrement),
434 (uint8_t) rampMode, 0x55
435 };
436 int16_t result = _asosw.sendPacket(tx); if (result < 0) { return(result); }
437 }
438 return 0;
439
440 }
441
475 int16_t writePID(uint16_t kp, uint16_t ki, uint16_t kd,uint16_t target,Period samplePeriod, uint8_t targetPin = 255, bool biDirectional = false)
476 {
477 {
478 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
479 pin(),
481 100, // Set kp and ki
482 SW_LE16(kp),
483 SW_LE16(ki)
484 };
485 int16_t result = _asosw.sendPacket(tx); if (result < 0) { return(result); }
486 }
487 {
488 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
489 pin(),
491 101, // Set kd
492 SW_LE16(kd),
493 0x55,0x55
494 };
495 int16_t result = _asosw.sendPacket(tx); if (result < 0) { return(result); }
496 }
498 {
499 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
500 pin(),
502 7, // Set Sample Rate
503 (uint8_t)samplePeriod,
504 0x55,0x55,0x55,
505 };
506 int16_t result = _asosw.sendPacket(tx); if (result < 0) { return(result); }
507 }
508 {
509 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
510 pin(),
512 102, // Reset Integrator
513 0x55,0x55,0x55,0x55
514 };
515 int16_t result = _asosw.sendPacket(tx); if (result < 0) { return(result); }
516 }
517 {
518 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
519 pin(),
521 109, // Configure target and bidirectional
522 targetPin,
523 biDirectional,
524 0x55,0x55
525 };
526 int16_t result = _asosw.sendPacket(tx); if (result < 0) { return(result); }
527 }
528 return 0;
529
530 }
531
532
541 {
542 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
543 pin(),
545 9, // Read Last Value
546 0x55, 0x55,0x55,0x55,
547 };
548 uint8_t rx[8];
549 if (_asosw.sendPacket(tx, rx) >= 0)
550 {
551 return(rx[4] + (uint16_t)rx[5] * 256);
552 }
553 else
554 {
555 return (0);
556 }
557
558 }
559
561 {
562 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
563 pin(),
565 110, // Write target Value and Reset Integrator
566 (byte)(target & 0xFF),(byte)(target >> 8),0x55,0x55
567 };
568 return _asosw.sendPacket(tx);
569 }
571 {
572 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
573 pin(),
575 103, // Get Last Error
576 0x55,0x55,0x55,0x55
577 };
578 uint8_t rx[8];
579
580 _asosw.sendPacket(tx, rx);
581 int32_t returnVal;
582 memcpy(&returnVal,&rx[4],4);
583 return returnVal;
584
585 }
586
587
589 {
590 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
591 pin(),
593 104, // Get Last Integrator
594 0x55,0x55,0x55,0x55
595 };
596 uint8_t rx[8];
597
598 _asosw.sendPacket(tx, rx);
599
600 int32_t returnVal;
601 memcpy(&returnVal,&rx[4],4);
602 return returnVal;
603 }
604
606 {
607 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
608 pin(),
610 105, // Get Last Integrator Effort
611 0x55,0x55,0x55,0x55
612 };
613 uint8_t rx[8];
614
615 _asosw.sendPacket(tx, rx);
616
617 int32_t returnVal;
618 memcpy(&returnVal,&rx[4],4);
619 return returnVal;
620 }
621
623 {
624 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
625 pin(),
627 106, // Get Last Proportional Effort
628 0x55,0x55,0x55,0x55
629 };
630 uint8_t rx[8];
631
632 _asosw.sendPacket(tx, rx);
633
634 int32_t returnVal;
635 memcpy(&returnVal,&rx[4],4);
636 return returnVal;
637 }
638
640 {
641 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
642 pin(),
644 107, // Get Last Derivative Effort
645 0x55,0x55,0x55,0x55
646 };
647 uint8_t rx[8];
648
649 _asosw.sendPacket(tx, rx);
650
651 int32_t returnVal;
652 memcpy(&returnVal,&rx[4],4);
653 return returnVal;
654 }
655
657 {
658 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
659 pin(),
661 108, // Get Last Total Effort
662 0x55,0x55,0x55,0x55
663 };
664 uint8_t rx[8];
665
666 _asosw.sendPacket(tx, rx);
667
668 int32_t returnVal;
669 memcpy(&returnVal,&rx[4],4);
670 return returnVal;
671 }
672
673 uint16_t ReadLastTarget()
674 {
675 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
676 pin(),
678 111, // Get Last Target
679 0x55,0x55,0x55,0x55
680 };
681 uint8_t rx[8];
682
683 _asosw.sendPacket(tx, rx);
684
685 return ((uint16_t)(rx[4] + 256 * rx[5]));
686 }
687
699 int16_t Enable2DLookupOutputScaling(uint16_t IndexInUserMemory
700 )
701 {
703 pin(),
705 10, // Set 2D Lookup Index
706 SW_LE16(IndexInUserMemory),0x55,0x55 };
707 return _asosw.sendPacket(tx);
708 }
709
711 virtual uint8_t pin() = 0;
713 virtual uint8_t swPinModeNumber() = 0;
714
715private:
716 SerialWombatChip& _asosw;
717
718};
719
720
#define SW_LE16(_a)
Convert a uint16_t to two bytes in little endian format for array initialization.
int16_t writeScalingEnabled(bool enabled, uint8_t sourcePin)
Enable scaling and set which pin or public data is used as the input source.
int16_t writeTimeout(uint16_t timeout_mS, uint16_t timeoutOutputValue)
Enable a timeout value which will cause the output to go to a default value if not updated.
int16_t writeHysteresis(uint16_t lowLimit, uint16_t lowOutputValue, uint16_t highLimit, uint16_t highOutputValue, uint16_t initialOutputValue)
Controls the output based on hystersis control.
int16_t Enable2DLookupOutputScaling(uint16_t IndexInUserMemory)
Set Up 2D Lookup Output Scaling.
SerialWombatAbstractScaledOutput(SerialWombatChip &sw)
Constructor for the SerialWombatAbstractScaledOutput Class.
int16_t writeOutputScaling(uint16_t outputMin, uint16_t outputMax)
Reduces the output range from 0 to 65535 to user specified range.
int16_t writeRateControl(Period samplePeriod, uint16_t maximumChangecounts, uint16_t maximumDecrementCounts=0)
int16_t writeRamp(uint16_t slowIncrement, uint16_t incrementThreshold, uint16_t fastIncrement, Period samplePeriod, RampMode rampMode)
Configure the scaled output block into Ramp control mode.
int16_t writeScalingTargetValue(uint16_t target)
The target input value for PID control.
virtual uint8_t pin()=0
Facilitates inheritance.
int16_t write1stOrderFiltering(Period sampleRate, uint16_t filterConstant)
uint16_t readLastOutputValue()
Request Last Output Value.
int16_t writeInputScaling(uint16_t inputMin, uint16_t inputMax)
Scale incoming values to a range of 0 to 65535.
int16_t writeScalingTargetValueResetIntegrator(uint16_t target)
int16_t writePID(uint16_t kp, uint16_t ki, uint16_t kd, uint16_t target, Period samplePeriod, uint8_t targetPin=255, bool biDirectional=false)
Configure the scaled output block into PID control mode.
int16_t writeScalingInvertedInput(bool inverted)
if enabled subtract the input value from 65535 before doing any other processing.
virtual uint8_t swPinModeNumber()=0
Facilitates inheritance.
Class for a Serial Wombat chip. Each Serial Wombat chip on a project should have its own instance.