Serial Wombat Arduino Library
SerialWombatAbstractScaledOutput.h
Go to the documentation of this file.
1 #pragma once
2 /*
3 Copyright 2021 Broadwell Consulting Inc.
4 
5 "Serial Wombat" is a registered trademark of Broadwell Consulting Inc. in
6 the United States. See SerialWombat.com for usage guidance.
7 
8 Permission 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 
15 The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17 
18 THE 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 
63 {
64 public:
71 
72  enum Period {
84  };
85 
101  int16_t writeTimeout(uint16_t timeout_mS, uint16_t timeoutOutputValue)
102  {
103 
104  uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
105  pin(),
106  swPinModeNumber(),
107  1,
108  SW_LE16(timeout_mS),
109  SW_LE16(timeoutOutputValue),
110  };
111 
112  int16_t result = _asosw.sendPacket(tx);
113 
114  return(result);
115 
116  }
117 
126  int16_t writeScalingEnabled(bool enabled, uint8_t sourcePin)
127  {
128  uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
129  pin(), swPinModeNumber(),
130  0, //Enable/disable scaling, set source pin
131  enabled,
132  sourcePin,
133  0x55, 0x55};
134 
135  int16_t result = _asosw.sendPacket(tx);
136  return(result);
137  }
138 
152  int16_t writeInputScaling(uint16_t inputMin, uint16_t inputMax)
153  {
154  uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
155  pin(),
156  swPinModeNumber(),
157  2, // Set input scaling
158  SW_LE16(inputMin),
159  SW_LE16(inputMax),
160  };
161  return(_asosw.sendPacket(tx));
162  }
163 
179  int16_t writeOutputScaling(uint16_t outputMin, uint16_t outputMax)
180  {
181  uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
182  pin(),
183  swPinModeNumber(),
184  5, //Set output scaling
185  SW_LE16(outputMin),
186  SW_LE16(outputMax),
187  };
188  return(_asosw.sendPacket(tx));
189  }
190 
198  int16_t writeScalingInvertedInput(bool inverted)
199  {
200  uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
201  pin(),
202  swPinModeNumber(),
203  3, // Set inverted/not inverted
204  inverted,
205  0x55,0x55,0x55,
206  };
207  return(_asosw.sendPacket(tx));
208  }
209 
222  int16_t writeScalingTargetValue(uint16_t target)
223  {
224  uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
225  pin(),
226  swPinModeNumber(),
227  6, // Set target value for PID controller
228  SW_LE16(target),0x55,0x55,
229  };
230  return(_asosw.sendPacket(tx));
231  }
232 
246  int16_t writeRateControl(Period samplePeriod, uint16_t maximumChangecounts)
247  {
248  {
249  uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
250  pin(),
251  swPinModeNumber(),
252  4, // Set filter mode
253  1, // Filter mode rate control
254  SW_LE16(maximumChangecounts),0x55,
255  };
256  int16_t result = _asosw.sendPacket(tx);
257  if (result < 0)
258  {
259  return(result);
260  }
261  }
262  {
263  uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
264  pin(),
265  swPinModeNumber(),
266  7, // Set Sample Rate
267  (uint8_t)samplePeriod,
268  0x55,0x55,0x55,
269  };
270  return(_asosw.sendPacket(tx));
271  }
272  }
273 
287  int16_t write1stOrderFiltering(Period sampleRate, uint16_t filterConstant)
288  {
289  {
290  uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
291  pin(),
292  swPinModeNumber(),
293  4, // Set filter mode
294  2, // Filter mode 1st Order
295  SW_LE16(filterConstant),0x55,
296  };
297  int16_t result = _asosw.sendPacket(tx);
298  if (result < 0)
299  {
300  return(result);
301  }
302  }
303  {
304  uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
305  pin(),
306  swPinModeNumber(),
307  7, // Set Sample Rate
308  (uint8_t)sampleRate,
309  0x55,0x55,0x55,
310  };
311  return(_asosw.sendPacket(tx));
312  }
313  }
314 
329  int16_t writeHysteresis(uint16_t lowLimit, uint16_t lowOutputValue, uint16_t highLimit, uint16_t highOutputValue, uint16_t initialOutputValue)
330  {
331  {
332  uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
333  pin(),
334  swPinModeNumber(),
335  50, // Set hysteresis high limit/output
336  SW_LE16(highLimit),
337  SW_LE16(highOutputValue)
338  };
339  int16_t result = _asosw.sendPacket(tx); if (result < 0) { return(result); }
340  }
341 
342  {
343  uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
344  pin(),
345  swPinModeNumber(),
346  51, // Set hysteresis low limit/output
347  SW_LE16(lowLimit),
348  SW_LE16(lowOutputValue)
349  };
350  int16_t result = _asosw.sendPacket(tx); if (result < 0) { return(result); }
351  }
352  {
353  uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
354  pin(),
355  swPinModeNumber(),
356  52, // Set hysteresis low limit/output
357  SW_LE16(initialOutputValue),
358  0x55,0x55
359  };
360  int16_t result = _asosw.sendPacket(tx); if (result < 0) { return(result); }
361  }
362  return(0);
363  }
364 
399  int16_t writePID(uint16_t kp, uint16_t ki, uint16_t kd,uint16_t target,Period samplePeriod)
400  {
401  {
402  uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
403  pin(),
404  swPinModeNumber(),
405  100, // Set kp and ki
406  SW_LE16(kp),
407  SW_LE16(ki)
408  };
409  int16_t result = _asosw.sendPacket(tx); if (result < 0) { return(result); }
410  }
411  {
412  uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
413  pin(),
414  swPinModeNumber(),
415  101, // Set kd
416  SW_LE16(kd),
417  0x55,0x55
418  };
419  int16_t result = _asosw.sendPacket(tx); if (result < 0) { return(result); }
420  }
421  writeScalingTargetValue(target);
422  {
423  uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
424  pin(),
425  swPinModeNumber(),
426  7, // Set Sample Rate
427  (uint8_t)samplePeriod,
428  0x55,0x55,0x55,
429  };
430  int16_t result = _asosw.sendPacket(tx); if (result < 0) { return(result); }
431  }
432  {
433  uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
434  pin(),
435  swPinModeNumber(),
436  102, // Reset Integrator
437  0x55,0x55,0x55,0x55
438  };
439  int16_t result = _asosw.sendPacket(tx); if (result < 0) { return(result); }
440  }
441  return 0;
442 
443  }
444 
445 
454  {
455  uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
456  pin(),
457  swPinModeNumber(),
458  9, // Read Last Value
459  0x55, 0x55,0x55,0x55,
460  };
461  uint8_t rx[8];
462  if (_asosw.sendPacket(tx, rx) >= 0)
463  {
464  return(rx[4] + (uint16_t)rx[5] * 256);
465  }
466  else
467  {
468  return (0);
469  }
470 
471  }
472 
473  int16_t writeScalingTargetValueResetIntegrator(uint16_t target)
474  {
475  uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
476  pin(),
477  swPinModeNumber(),
478  110, // Write target Value and Reset Integrator
479  (byte)(target & 0xFF),(byte)(target >> 8),0x55,0x55
480  };
481  return _asosw.sendPacket(tx);
482  }
483  int32_t PIDGetLastError()
484  {
485  uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
486  pin(),
487  swPinModeNumber(),
488  103, // Get Last Error
489  0x55,0x55,0x55,0x55
490  };
491  uint8_t rx[8];
492 
493  _asosw.sendPacket(tx, rx);
494  int32_t returnVal;
495  memcpy(&returnVal,&rx[4],4);
496  return returnVal;
497 
498  }
499 
500 
502  {
503  uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
504  pin(),
505  swPinModeNumber(),
506  104, // Get Last Integrator
507  0x55,0x55,0x55,0x55
508  };
509  uint8_t rx[8];
510 
511  _asosw.sendPacket(tx, rx);
512 
513  int32_t returnVal;
514  memcpy(&returnVal,&rx[4],4);
515  return returnVal;
516  }
517 
519  {
520  uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
521  pin(),
522  swPinModeNumber(),
523  105, // Get Last Integrator Effort
524  0x55,0x55,0x55,0x55
525  };
526  uint8_t rx[8];
527 
528  _asosw.sendPacket(tx, rx);
529 
530  int32_t returnVal;
531  memcpy(&returnVal,&rx[4],4);
532  return returnVal;
533  }
534 
536  {
537  uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
538  pin(),
539  swPinModeNumber(),
540  106, // Get Last Proportional Effort
541  0x55,0x55,0x55,0x55
542  };
543  uint8_t rx[8];
544 
545  _asosw.sendPacket(tx, rx);
546 
547  int32_t returnVal;
548  memcpy(&returnVal,&rx[4],4);
549  return returnVal;
550  }
551 
553  {
554  uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
555  pin(),
556  swPinModeNumber(),
557  107, // Get Last Derivative Effort
558  0x55,0x55,0x55,0x55
559  };
560  uint8_t rx[8];
561 
562  _asosw.sendPacket(tx, rx);
563 
564  int32_t returnVal;
565  memcpy(&returnVal,&rx[4],4);
566  return returnVal;
567  }
568 
570  {
571  uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
572  pin(),
573  swPinModeNumber(),
574  108, // Get Last Total Effort
575  0x55,0x55,0x55,0x55
576  };
577  uint8_t rx[8];
578 
579  _asosw.sendPacket(tx, rx);
580 
581  int32_t returnVal;
582  memcpy(&returnVal,&rx[4],4);
583  return returnVal;
584  }
585 
586  uint16_t ReadLastTarget()
587  {
588  uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
589  pin(),
590  swPinModeNumber(),
591  111, // Get Last Target
592  0x55,0x55,0x55,0x55
593  };
594  uint8_t rx[8];
595 
596  _asosw.sendPacket(tx, rx);
597 
598  return ((uint16_t)(rx[4] + 256 * rx[5]));
599  }
600 
612  int16_t Enable2DLookupOutputScaling(uint16_t IndexInUserMemory
613  )
614  {
615  uint8_t tx[] = {(uint8_t)SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE,
616  pin(),
617  swPinModeNumber(),
618  10, // Set 2D Lookup Index
619  SW_LE16(IndexInUserMemory),0x55,0x55 };
620  return _asosw.sendPacket(tx);
621  }
622 
624  virtual uint8_t pin() = 0;
626  virtual uint8_t swPinModeNumber() = 0;
627 
628 private:
629  SerialWombatChip& _asosw;
630 
631 };
632 
633 
SerialWombatAbstractScaledOutput::PERIOD_256mS
@ PERIOD_256mS
Definition: SerialWombatAbstractScaledOutput.h:81
SerialWombatAbstractScaledOutput::PIDGetLastDerivativeEffort
int32_t PIDGetLastDerivativeEffort()
Definition: SerialWombatAbstractScaledOutput.h:552
SerialWombatAbstractScaledOutput::writeTimeout
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.
Definition: SerialWombatAbstractScaledOutput.h:101
SerialWombatChip
Class for a Serial Wombat chip. Each Serial Wombat chip on a project should have its own instance.
Definition: SerialWombat.h:286
SerialWombatAbstractScaledOutput::PERIOD_64mS
@ PERIOD_64mS
Definition: SerialWombatAbstractScaledOutput.h:79
SerialWombatAbstractScaledOutput::Period
Period
Definition: SerialWombatAbstractScaledOutput.h:72
SerialWombatAbstractScaledOutput::PIDGetLastProportionalEffort
int32_t PIDGetLastProportionalEffort()
Definition: SerialWombatAbstractScaledOutput.h:535
SerialWombatAbstractScaledOutput::writeInputScaling
int16_t writeInputScaling(uint16_t inputMin, uint16_t inputMax)
Scale incoming values to a range of 0 to 65535.
Definition: SerialWombatAbstractScaledOutput.h:152
SerialWombatAbstractScaledOutput::writeScalingEnabled
int16_t writeScalingEnabled(bool enabled, uint8_t sourcePin)
Enable scaling and set which pin or public data is used as the input source.
Definition: SerialWombatAbstractScaledOutput.h:126
SerialWombatAbstractScaledOutput::SerialWombatAbstractScaledOutput
SerialWombatAbstractScaledOutput(SerialWombatChip &sw)
Constructor for the SerialWombatAbstractScaledOutput Class.
Definition: SerialWombatAbstractScaledOutput.h:70
SerialWombatAbstractScaledOutput::PERIOD_8mS
@ PERIOD_8mS
Definition: SerialWombatAbstractScaledOutput.h:76
SerialWombatAbstractScaledOutput::PERIOD_1024mS
@ PERIOD_1024mS
Definition: SerialWombatAbstractScaledOutput.h:83
SerialWombatAbstractScaledOutput::writeOutputScaling
int16_t writeOutputScaling(uint16_t outputMin, uint16_t outputMax)
Reduces the output range from 0 to 65535 to user specified range.
Definition: SerialWombatAbstractScaledOutput.h:179
SerialWombatAbstractScaledOutput
SerialWombatServo, SerialWombatPWM and other proportional ouptut classes inherit from this class....
Definition: SerialWombatAbstractScaledOutput.h:62
SerialWombat.h
SerialWombatAbstractScaledOutput::PERIOD_16mS
@ PERIOD_16mS
Definition: SerialWombatAbstractScaledOutput.h:77
SerialWombatAbstractScaledOutput::writeScalingTargetValueResetIntegrator
int16_t writeScalingTargetValueResetIntegrator(uint16_t target)
Definition: SerialWombatAbstractScaledOutput.h:473
SerialWombatAbstractScaledOutput::write1stOrderFiltering
int16_t write1stOrderFiltering(Period sampleRate, uint16_t filterConstant)
Definition: SerialWombatAbstractScaledOutput.h:287
SerialWombatAbstractScaledOutput::writeScalingInvertedInput
int16_t writeScalingInvertedInput(bool inverted)
if enabled subtract the input value from 65535 before doing any other processing.
Definition: SerialWombatAbstractScaledOutput.h:198
SerialWombatAbstractScaledOutput::PERIOD_512mS
@ PERIOD_512mS
Definition: SerialWombatAbstractScaledOutput.h:82
SerialWombatAbstractScaledOutput::ReadLastTarget
uint16_t ReadLastTarget()
Definition: SerialWombatAbstractScaledOutput.h:586
SerialWombatAbstractScaledOutput::PIDGetLastEffort
int32_t PIDGetLastEffort()
Definition: SerialWombatAbstractScaledOutput.h:569
SerialWombatAbstractScaledOutput::PIDGetLastError
int32_t PIDGetLastError()
Definition: SerialWombatAbstractScaledOutput.h:483
SerialWombatAbstractScaledOutput::pin
virtual uint8_t pin()=0
Facilitates inheritance.
SerialWombatAbstractScaledOutput::writeRateControl
int16_t writeRateControl(Period samplePeriod, uint16_t maximumChangecounts)
Definition: SerialWombatAbstractScaledOutput.h:246
SerialWombatAbstractScaledOutput::PERIOD_128mS
@ PERIOD_128mS
Definition: SerialWombatAbstractScaledOutput.h:80
SerialWombatChip::sendPacket
int sendPacket(uint8_t tx[], uint8_t rx[])
Send an 8 byte packet to the Serial Wombat chip and wait for 8 bytes back.
Definition: SerialWombat.cpp:115
SerialWombatAbstractScaledOutput::PIDGetLastIntegratorEffort
int32_t PIDGetLastIntegratorEffort()
Definition: SerialWombatAbstractScaledOutput.h:518
SerialWombatAbstractScaledOutput::PERIOD_4mS
@ PERIOD_4mS
Definition: SerialWombatAbstractScaledOutput.h:75
SerialWombatCommands::CONFIGURE_PIN_OUTPUTSCALE
@ CONFIGURE_PIN_OUTPUTSCALE
(210)
SerialWombatAbstractScaledOutput::PERIOD_32mS
@ PERIOD_32mS
Definition: SerialWombatAbstractScaledOutput.h:78
SerialWombatAbstractScaledOutput::writeScalingTargetValue
int16_t writeScalingTargetValue(uint16_t target)
The target input value for PID control.
Definition: SerialWombatAbstractScaledOutput.h:222
SerialWombatAbstractScaledOutput::Enable2DLookupOutputScaling
int16_t Enable2DLookupOutputScaling(uint16_t IndexInUserMemory)
Set Up 2D Lookup Output Scaling.
Definition: SerialWombatAbstractScaledOutput.h:612
SerialWombatAbstractScaledOutput::PERIOD_1mS
@ PERIOD_1mS
Definition: SerialWombatAbstractScaledOutput.h:73
SerialWombatAbstractScaledOutput::readLastOutputValue
uint16_t readLastOutputValue()
Request Last Output Value.
Definition: SerialWombatAbstractScaledOutput.h:453
SW_LE16
#define SW_LE16(_a)
Convert a uint16_t to two bytes in little endian format for array initialization.
Definition: SerialWombat.h:41
SerialWombatAbstractScaledOutput::swPinModeNumber
virtual uint8_t swPinModeNumber()=0
Facilitates inheritance.
SerialWombatAbstractScaledOutput::writeHysteresis
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.
Definition: SerialWombatAbstractScaledOutput.h:329
SerialWombatAbstractScaledOutput::writePID
int16_t writePID(uint16_t kp, uint16_t ki, uint16_t kd, uint16_t target, Period samplePeriod)
Configure the scaled output block into PID control mode.
Definition: SerialWombatAbstractScaledOutput.h:399
SerialWombatAbstractScaledOutput::PIDGetLastIntegrator
int32_t PIDGetLastIntegrator()
Definition: SerialWombatAbstractScaledOutput.h:501
SerialWombatAbstractScaledOutput::PERIOD_2mS
@ PERIOD_2mS
Definition: SerialWombatAbstractScaledOutput.h:74