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
254 int16_t writeRateControl(Period samplePeriod, uint16_t maximumChangecounts, uint16_t maximumDecrementCounts = 0)
255 {
256 {
257 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
258 pin(),
260 4, // Set filter mode
261 1, // Filter mode rate control
262 SW_LE16(maximumChangecounts),0x55,
263 };
264 int16_t result = _asosw.sendPacket(tx);
265 if (result < 0)
266 {
267 return(result);
268 }
269 }
270 if (maximumDecrementCounts > 0)
271 {
272 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
273 pin(),
275 8, // Set filter mode
276 SW_LE16(maximumDecrementCounts),0x55,0x55
277 };
278 int16_t result = _asosw.sendPacket(tx);
279 if (result < 0)
280 {
281 return(result);
282 }
283 }
284 {
285 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
286 pin(),
288 7, // Set Sample Rate
289 (uint8_t)samplePeriod,
290 0x55,0x55,0x55,
291 };
292 return(_asosw.sendPacket(tx));
293 }
294 }
295
309 int16_t write1stOrderFiltering(Period samplePeriod, uint16_t filterConstant)
310 {
311 {
312 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
313 pin(),
315 4, // Set filter mode
316 2, // Filter mode 1st Order
317 SW_LE16(filterConstant),0x55,
318 };
319 int16_t result = _asosw.sendPacket(tx);
320 if (result < 0)
321 {
322 return(result);
323 }
324 }
325 {
326 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
327 pin(),
329 7, // Set Sample Rate
330 (uint8_t)samplePeriod,
331 0x55,0x55,0x55,
332 };
333 return(_asosw.sendPacket(tx));
334 }
335 }
336
351 int16_t writeHysteresis(uint16_t lowLimit, uint16_t lowOutputValue, uint16_t highLimit, uint16_t highOutputValue, uint16_t initialOutputValue)
352 {
353 {
354 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
355 pin(),
357 50, // Set hysteresis high limit/output
358 SW_LE16(highLimit),
359 SW_LE16(highOutputValue)
360 };
361 int16_t result = _asosw.sendPacket(tx); if (result < 0) { return(result); }
362 }
363
364 {
365 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
366 pin(),
368 51, // Set hysteresis low limit/output
369 SW_LE16(lowLimit),
370 SW_LE16(lowOutputValue)
371 };
372 int16_t result = _asosw.sendPacket(tx); if (result < 0) { return(result); }
373 }
374 {
375 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
376 pin(),
378 52, // Set hysteresis low limit/output
379 SW_LE16(initialOutputValue),
380 0x55,0x55
381 };
382 int16_t result = _asosw.sendPacket(tx); if (result < 0) { return(result); }
383 }
384 return(0);
385 }
386
407 int16_t writeRamp(uint16_t slowIncrement, uint16_t incrementThreshold, uint16_t fastIncrement,Period samplePeriod, RampMode rampMode)
408 {
409 {
410 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
411 pin(),
413 7, // Set Sample Rate
414 (uint8_t) samplePeriod,
415 0x55,0x55,0x55
416 };
417 int16_t result = _asosw.sendPacket(tx); if (result < 0) { return(result); }
418 }
419 {
420 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
421 pin(),
423 60, // Set ramp slow increment, threshold
424 SW_LE16(slowIncrement),
425 SW_LE16(incrementThreshold)
426
427 };
428 int16_t result = _asosw.sendPacket(tx); if (result < 0) { return(result); }
429 }
430 {
431 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
432 pin(),
434 61, // Set ramp fast increment, mode
435 SW_LE16(fastIncrement),
436 (uint8_t) rampMode, 0x55
437 };
438 int16_t result = _asosw.sendPacket(tx); if (result < 0) { return(result); }
439 }
440 return 0;
441
442 }
443
479 int16_t writePID(uint16_t kp, uint16_t ki, uint16_t kd,uint16_t target,Period samplePeriod, uint8_t targetPin = 255, bool biDirectional = false)
480 {
481 {
482 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
483 pin(),
485 100, // Set kp and ki
486 SW_LE16(kp),
487 SW_LE16(ki)
488 };
489 int16_t result = _asosw.sendPacket(tx); if (result < 0) { return(result); }
490 }
491 {
492 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
493 pin(),
495 101, // Set kd
496 SW_LE16(kd),
497 0x55,0x55
498 };
499 int16_t result = _asosw.sendPacket(tx); if (result < 0) { return(result); }
500 }
502 {
503 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
504 pin(),
506 7, // Set Sample Rate
507 (uint8_t)samplePeriod,
508 0x55,0x55,0x55,
509 };
510 int16_t result = _asosw.sendPacket(tx); if (result < 0) { return(result); }
511 }
512 {
513 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
514 pin(),
516 102, // Reset Integrator
517 0x55,0x55,0x55,0x55
518 };
519 int16_t result = _asosw.sendPacket(tx); if (result < 0) { return(result); }
520 }
521 {
522 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
523 pin(),
525 109, // Configure target and bidirectional
526 targetPin,
527 biDirectional,
528 0x55,0x55
529 };
530 int16_t result = _asosw.sendPacket(tx); if (result < 0) { return(result); }
531 }
532 return 0;
533
534 }
535
536
545 {
546 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
547 pin(),
549 9, // Read Last Value
550 0x55, 0x55,0x55,0x55,
551 };
552 uint8_t rx[8];
553 if (_asosw.sendPacket(tx, rx) >= 0)
554 {
555 return(rx[4] + (uint16_t)rx[5] * 256);
556 }
557 else
558 {
559 return (0);
560 }
561
562 }
563
565 {
566 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
567 pin(),
569 110, // Write target Value and Reset Integrator
570 (byte)(target & 0xFF),(byte)(target >> 8),0x55,0x55
571 };
572 return _asosw.sendPacket(tx);
573 }
575 {
576 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
577 pin(),
579 103, // Get Last Error
580 0x55,0x55,0x55,0x55
581 };
582 uint8_t rx[8];
583
584 _asosw.sendPacket(tx, rx);
585 int32_t returnVal;
586 memcpy(&returnVal,&rx[4],4);
587 return returnVal;
588
589 }
590
591
593 {
594 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
595 pin(),
597 104, // Get Last Integrator
598 0x55,0x55,0x55,0x55
599 };
600 uint8_t rx[8];
601
602 _asosw.sendPacket(tx, rx);
603
604 int32_t returnVal;
605 memcpy(&returnVal,&rx[4],4);
606 return returnVal;
607 }
608
610 {
611 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
612 pin(),
614 105, // Get Last Integrator Effort
615 0x55,0x55,0x55,0x55
616 };
617 uint8_t rx[8];
618
619 _asosw.sendPacket(tx, rx);
620
621 int32_t returnVal;
622 memcpy(&returnVal,&rx[4],4);
623 return returnVal;
624 }
625
627 {
628 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
629 pin(),
631 106, // Get Last Proportional Effort
632 0x55,0x55,0x55,0x55
633 };
634 uint8_t rx[8];
635
636 _asosw.sendPacket(tx, rx);
637
638 int32_t returnVal;
639 memcpy(&returnVal,&rx[4],4);
640 return returnVal;
641 }
642
644 {
645 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
646 pin(),
648 107, // Get Last Derivative Effort
649 0x55,0x55,0x55,0x55
650 };
651 uint8_t rx[8];
652
653 _asosw.sendPacket(tx, rx);
654
655 int32_t returnVal;
656 memcpy(&returnVal,&rx[4],4);
657 return returnVal;
658 }
659
661 {
662 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
663 pin(),
665 108, // Get Last Total Effort
666 0x55,0x55,0x55,0x55
667 };
668 uint8_t rx[8];
669
670 _asosw.sendPacket(tx, rx);
671
672 int32_t returnVal;
673 memcpy(&returnVal,&rx[4],4);
674 return returnVal;
675 }
676
677 uint16_t ReadLastTarget()
678 {
679 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
680 pin(),
682 111, // Get Last Target
683 0x55,0x55,0x55,0x55
684 };
685 uint8_t rx[8];
686
687 _asosw.sendPacket(tx, rx);
688
689 return ((uint16_t)(rx[4] + 256 * rx[5]));
690 }
691
703 int16_t Enable2DLookupOutputScaling(uint16_t IndexInUserMemory
704 )
705 {
707 pin(),
709 10, // Set 2D Lookup Index
710 SW_LE16(IndexInUserMemory),0x55,0x55 };
711 return _asosw.sendPacket(tx);
712 }
713
715 virtual uint8_t pin() = 0;
717 virtual uint8_t swPinModeNumber() = 0;
718
719private:
720 SerialWombatChip& _asosw;
721
722};
723
724
#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)
Controls how fast an ouput can change in counts.
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 samplePeriod, uint16_t filterConstant)
Controls how fast an ouput can change based on filtering.
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.