Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Programming
Real Time Programming
Like the Pros
About Us
Chris Hibner
BSME University of Michigan, Ann Arbor (1996) Dynamics
and Control
MSME University of Michigan, Ann Arbor (2002) Control
Systems and Computational Mechanics
TRW Automotive; Algorithm Development and Applications
Group, 1996 present.
chiefdelphi.com user name: Chris Hibner
Michael Shaul
BSEE Rose-Hulman Institute of Technology (2002) Computer
Engineering
MSEE University of Michigan, Dearborn (2005) Control
Systems
TRW Automotive; Product Development Group, Software
Validation Group, and Core Components Group
chiefdelphi.com user name: Mike Shaul
Contents
typedefs
Fixed-Point
Math
Overflow Protection
Switch Debouncing
State Machines
Filtering
Oversampling
Use Braces
{
Always
}
Simple Readable Code
Typedefs
Using Naturally Named
Data Types
Why Typedef?
You
2)
3)
4)
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;
In your code:
unsigned char variable;
Is replaced with:
u8 variable;
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
=5
A/D Sample
(10-bit)
Shift left by 6 (i.e. A2D << 6;):
0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0
Whole part
Fractional part
= 5.0
Fractional part
Fractional part
(huh???)
Fractional part
(huh???)
Fractional part
0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0
0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0
=5
Without using the fixed point math the result of the addition
would have been 4 due to the truncation of the integer division.
Overflow Protection
Making Sure Your Code Is
Predictable
u8
s8
u16
s16
u32
s32
:
0 - 255
:
-128 - 127
:
0 - 65535
: -32768 - 32767
:
0 - 4.294967295e9
: -2.147483648e9 - 2.147483647e9
2.
3.
}
*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
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;
}
}
State Machines
Systematic Approach to
Complicated Logic
performing an automated
routine which has many steps.
When the logic is very large and/or
difficult to grasp. (Like C
programming: break up a large
program into smaller functions)
When the problem has physical
states (like a light it has two
states: ON and OFF).
Light
bulb example:
}
// end single shot state machine
Filtering
Smoothing Your Signals
Filter Types
Common
applications:
Since
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 Effects
Helps
Oversampling Effects
0.3!!!
Optimizations
Make your code run faster.
Optimizations