|
| SerialWombatAbstractScaledOutput (SerialWombatChip &sw) |
| Constructor for the SerialWombatAbstractScaledOutput Class. More...
|
|
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. More...
|
|
int16_t | writeScalingEnabled (bool enabled, uint8_t sourcePin) |
| Enable scaling and set which pin or public data is used as the input source. More...
|
|
int16_t | writeInputScaling (uint16_t inputMin, uint16_t inputMax) |
| Scale incoming values to a range of 0 to 65535. More...
|
|
int16_t | writeOutputScaling (uint16_t outputMin, uint16_t outputMax) |
| Reduces the output range from 0 to 65535 to user specified range. More...
|
|
int16_t | writeScalingInvertedInput (bool inverted) |
| if enabled subtract the input value from 65535 before doing any other processing. More...
|
|
int16_t | writeScalingTargetValue (uint16_t target) |
| The target input value for PID control. More...
|
|
int16_t | writeRateControl (Period samplePeriod, uint16_t maximumChangecounts) |
|
int16_t | write1stOrderFiltering (Period sampleRate, uint16_t filterConstant) |
|
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. More...
|
|
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. More...
|
|
uint16_t | readLastOutputValue () |
| Request Last Output Value. More...
|
|
int16_t | writeScalingTargetValueResetIntegrator (uint16_t target) |
|
int32_t | PIDGetLastError () |
|
int32_t | PIDGetLastIntegrator () |
|
int32_t | PIDGetLastIntegratorEffort () |
|
int32_t | PIDGetLastProportionalEffort () |
|
int32_t | PIDGetLastDerivativeEffort () |
|
int32_t | PIDGetLastEffort () |
|
uint16_t | ReadLastTarget () |
|
int16_t | Enable2DLookupOutputScaling (uint16_t IndexInUserMemory) |
| Set Up 2D Lookup Output Scaling. More...
|
|
virtual uint8_t | pin ()=0 |
| Facilitates inheritance. More...
|
|
virtual uint8_t | swPinModeNumber ()=0 |
| Facilitates inheritance. More...
|
|
SerialWombatServo, SerialWombatPWM and other proportional ouptut classes inherit from this class. it provides control blocks for manipulating output based on input.
This class encapsulates the Serial Wombat Scaled Output functional block. This block is included in all proportial output classes in the SerialWombat 18AB chip. Each pin's output block is separate from the others.
This block is designed to facilitate real time control of outputs based on configuration from the host without the need to issue additional commands after the initial configuration. The block also includes a timeout function which is capable of setting an output to pre-determined value if the host does not reset a countdown timer within a specified number of mS. This allows a controlled shutdown if the host crashes, the data bus becomes inoperable, etc. Because the output block is serviced every 1mS, real time control can be achived with higher performance than if control was performed over the data bus. This functionality also frees the host of the need to maintain timing sensitive communication with the Serial Wombat chip.
This block can limit output rate of change either by a limited amout of change per time, or by first-order filtering output changes. Rate limiting is useful to implement smooth motion over time (controlled within the Serial Wombat chip) such as a model railroad crossing gate attached to a servo. It also can prevent a current spike caused by requesting large changes in position at one time. First order filtering is useful to change position rapidly at the beginning, but slow near the end to reduce impact speed.
The target output value can be provided by the host, or the Scaled Output block can be configured to get its target value from another pin or a public data source such as the Serial Wombat 18AB chip's internal temperature sensor or source voltage measurement. This capability to act on another pin's data would be useful for example if one wanted to control a 6 DOF / 6 servo robotic arm with 6 potentiometer outputs. The Serial Wombat chip could filter user inputs to provide smooth movement as well as scale the outputs so that the full range of potentiometer travel can be mapped to each joint's range of servo motion in degrees. The Arduino or other host could monitor the controls and poentially intervene if higher level logic deemed it necessary, but would be freed of the need to constantly poll the pots and update servo pulse values.
The Scaled Output block can also do simple real-time control of an output based on an input. For instance, a heater could be set to pwm at some duty cycle if an analog input dropped below a threshold, then shut off when it rose above some other higher threshold. This is hystersis mode.
Another control method is Proportial/Integral/Derivative (PID) control. In this mode the host provides P I and D calibrations for the system, and specifies an input pin and target value. The output of the Scaled Output block is then controlled via PID to try and reach the target value. As an example, a motor's encoder output could be attached to a SerialWombatPulseInput pin configured to output the frequency of incoming pulses. The PID controller in the Scaled Output block could then vary the PWM driving a FET controlling the motor to keep the motor running at constant speed that adapts to changing motor load or source voltage. The PID controller requires that a postive output cause a positive input from the feedback system. If they are opposite then the invert function of the block can be used.
Scaling operations happen in the following sequence:
- Read the source pin's or data source's public data (Note that the Host can also provide the input value by setting the source pin to the output pin and writing that pin's public data. In this case the output value of the pin will not be written to the pin's public data)
- Scale the inputs from a specified Min/Max range to 0 to 65535
- Invert if configured by subtracting the scaled value from 65535
- Pass the input value to the specified control algorithm (PID, Hysteresis or PassThrough) to determine the output value
- Check to see if a communication timeout has occurred if configured. If so, substitute the default output value
- Perform output filtering if configured to smooth transitions in the output value
- Scale the output value from 0-65535 to some other range if configured. This is useful for example if a servo is physically limited to a portion of its normal rotation.
- Write the output data to the output pin's publc data (unless the pin is configured to use its own public data as an input source)
- Write the output data to the underlying pin mode (Servo, PWM, etc) so that the physical output is updated
int16_t SerialWombatAbstractScaledOutput::writePID |
( |
uint16_t |
kp, |
|
|
uint16_t |
ki, |
|
|
uint16_t |
kd, |
|
|
uint16_t |
target, |
|
|
Period |
samplePeriod |
|
) |
| |
|
inline |
Configure the scaled output block into PID control mode.
- Returns
- returns 0 or higher if success, or a negative error code
This pin mode implements Propotional / Integral / Derivative (PID) control. It attempts to drive the input to a target value based on calibration constants provided by the host.
For a discussion of PID control in general, this link is suggested: https://www.ni.com/en-us/innovations/white-papers/06/pid-theory-explained.html
This block implements PID using the diagram shown in this link where the kP term is applied after the sum of the error, 1/ki * integration and kd * derivative sum. This is different from the order used in some PID algorithms. The net effect is the same but the values of the ki and kd are different than if the constants were all applied first and the sum taken afterwards. This was done to best distribute values so that calculations could be done using integral math rather than floating point due to the performance cost of floating point on small microcontrollers.
Note that kp, ki and kd are unsigned integers. For this reason it is necessary that an increase in controller output creates a positive change in the measurement of the plant. The writeScalingInvertedInput function can be used to invert the input if the system reacts negatively to a an increase in output.
See the pid() function in the Serial Wombat 18AB firmware to see exactly how the math is implemented: https://github.com/BroadwellConsultingInc/SerialWombat/blob/main/SerialWombat18A_18B/SerialWombat18A_18B.X/outputScale.c
- Parameters
-
kp | The proportional constant applied to the sum of the error, 1/ki * error sum, and kd * derivative. Scaled to 1/256ths . |
ki | The integral constant applied to the integral. Scaled to 1/16384ths, as typically you want this value to be less than 1.0 |
kd | The derivative contant applied to the derivative. This value is scaled to 1/16384.
|
target | The value the output will attempt to control the input to |
samplePeriod | an enumerated time for how often the PID controller updates. This value should be based on how fast the system responds to change in output so that integral and derivative terms work correctly. |
- Returns
- returns 0 or higher if success, or a negative error code