Advanced C Programming: Real Time Programming Like The Pros
Advanced C Programming: Real Time Programming Like The Pros
Contents
typedefs
Fixed-Point
Filtering
Math
Oversampling Overflow
*Disclaimer: Not all code in this presentation follows these practices due to space limitations
Typedefs
Using Naturally Named Data Types
Why Typedef?
You
use variable with logical names, why not use data types with logical names? Is an int 8-bits or 16-bits? Whats a long? Better question: why memorize it? Most integer data types are platform dependent!!! typedefs make your code more portable.
typedef.h Example
typedef typedef typedef typedef typedef typedef unsigned char signed char unsigned short signed short unsigned long signed long u8; s8; u16; s16; u32; s32;
Fixed-Point Math
Fractional Numbers Using Integer Data Types
Creating Fractions
Fractions are created by using extra bits below your whole numbers. The programmer is responsible for knowing where the decimal place is. Move the decimal place by using the shift operator (<< or >>). Shifting is multiplying by powers of 2. Ex.: x<<5 = x*2^5; x>>5 = x*2^-5
Whole part
Fractional part
know 5/2 = 2.5 If we used pure integers, 5/2 = 2 (i.e. the number is rounded toward negative infinity) Using a fixed-point fractional portion can recover the lost decimal portion.
Whole part
Fractional part
Whole part
Fractional part
The whole part: 0000000010(binary) = 2(decimal) The fractional part: 100000(binary) = 32 (huh???)
(huh???)
-The more bits you use in your fractional part, the more accuracy you will have.
-Accuracy is 2^-(fraction bits). -For example, if we have 6 bits in our fractional part (like the above example), our accuracy is 2^-6 = 0.015625. In other words, every bit is equal to 0.015625
Once our math operations are complete, we right shift our data to regain our original resolution and data position.
0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 =5
Without using the fixed point math the result of the addition would have been 4 due to the truncation of the integer division.
Filtering
Smoothing Your Signals
Filter Types
Filters are classified by what they allow to pass through (NOT what they filter out). For example, a low pass filter (abv. LPF) allows low frequencies to pass through it therefore removes high frequencies. The most common filters are: high pass filters, low pass filters, and band pass filters. We will only cover low pass filters.
Pass Filters (LPFs) are used to smooth out the signal. applications:
Removing sensor noise
Removing unwanted signal frequencies Signal averaging
Common
filters act just like electrical resistor-capacitor filters. IIR filters allow the output of the filter to move a fixed fraction of the way toward the input.
more filtering effect, use more data points in the average. you are adding a lot of numbers, there is a high chance of overflow take precautions
Since
more filtering effect, make the shift factor bigger. precautions for overflow. can get more resolution by using more shift factors. For example, have your filter constant be
Take You
(1 2^-SHIFT1 2^-2SHIFT2)
(youll have to work out the math!)
Oversampling
Gain resolution and make your data more reliable.
Oversampling Basics
Simple
oversampling: sample more data (i.e. faster sample rate) than you need and average the samples. Even if you dont sample faster, averaging (or filtering the data) can be beneficial.
Oversampling Effects
Helps
to smooth the data. Helps to hide a bad or unreliable sample. Increases A/D resolution if noise is present.
Oversampling Effects
Overflow Protection
Making Sure Your Code Is Predictable
is when you try to store a number that is too large for its data type. For example, what happens to the following code?
s8 test = 100; test = test + 50;
2.
3.
Create a new temporary variable using a data type with a larger range to do the calculation. Compare the sign of the variable before and after the calculation. Did the sign change when it shouldnt have? (for signed variables) Compare the variable after the calculation to the value before. Did the value decrease when it should have increased?
}
*This example uses a s32 (larger) data value for overflow checking
}
*This example uses 16 bit values only to check for overflow on signed values this provides improved efficiency on 16 bit platforms.
}
*This example checks for overflow on unsigned values
Switch Debouncing
Having Confidence in Switch Inputs
a switch input reduces erroneous inputs. Use debouncing when pressing a switch starts a sequence or changes the state of something.
when a single push of the switch changes a state. Examples: - pneumatic gripper - motorized gripper where a single push causes the motor to go until a limit switch is reached Do not debounce if constant driver input is needed.
What is Debouncing?
Basically, debouncing means to require a certain number of samples before you confirm the switch input. Various debounce schemes can be used: - require N consecutive samples (i.e. reset the counter if one sample fails) - count up / count down (i.e., if one sample fails, decrement the counter by 1 rather than resetting to zero.
Debounce Example
// debounce opening gripper if (!pneumaticGripperOpenState) { if (gripperOpenSwitch == ON) gripperOpenDebCount++; else gripperOpenDebCount = 0; if (gripperOpenDebCount >= DEBOUNCE_LIMIT) { gripperOpenDebCount = 0; pneumaticGripperOpenState = TRUE; } }