Machine Design
Machine Design
Schedule change
ES1 - admin
Tues
Wedn
Thurs
Thurs
Mechatronics Lab
1
Machine Design
Lecture 08-10
Lab 10-12
Lab 10-12
Lecture 08-10 (M3)
Lab 10-12
Mechatronics Lab
2
Machine Design
Final exam
Oct 21, 14.00-18.00
Rooms V32, V34
ES1 – CAN filtering
No books, no computers, no papers
Only allowed to use: pen, calculator
Bring ID, (fruit, snacks)
Allowed to ask clarifying questions
One small supervised break
Mechatronics Lab
3
Machine Design
Mechatronics Lab
4
Machine Design
CAN filtering and masks
Example
Mask = 0xF0;
Filter0 = 0x34;
Filter1 = 0xA3;
Filter0 let’s all messages from 0x30
to 0x3F through
Filter1 let’s all messages from 0xA0
to 0xAF through
Mechatronics Lab
5
Mechatronics Lab
6
1
Machine Design
Machine Design
Example again
Solution
Max ID-value is: 0x1FFFFFFF
Create a mask and a filter that let’s
messages 0x00123400 to
0x001235FF through
Mechatronics Lab
Mask = 0x1FFFFF00 (don’t bother
about the last two digits)
Filter0 = 0x00123400 (400-4FF)
Filter1 = 0x00123500 (500-5FF)
7
Machine Design
Mechatronics Lab
8
Machine Design
Again…
conclusion
One filter for all? Need to look at
binary code…
…400-5FF =
0100 0000 0000 to 0101 1111 1111
mask =
1110 0000 0000 (=0xE00)
Filter =
0100 0000 0000 (=0x400)
Mechatronics Lab
Mask = 0x1FFFFE00
Filter = 0x00123400
Let’s all messages through with ID:
0x00123400 to 0x001235FF
9
Machine Design
Mechatronics Lab
10
Machine Design
Why filter?
ES1 - DSP
If you filter out all irrelevant
messages, you don’t need to check
ID
Alternatively, accept all Ids and
check in program
Mechatronics Lab
11
Mechatronics Lab
12
2
Machine Design
Machine Design
Why?
Mathematical functions
Either implemented in software or
hardware
HW
o Special functions in the processor, coprocessor, DSP-core, MAC-unit etc.
o From 1 cycle (DSP) to several cycles (CISCarchitecture)
We need to do calculations fast.
SW
o Various (more or less nice) implementations of
mathematical functions, typically taylor series
Mechatronics Lab
13
Machine Design
Mechatronics Lab
14
Machine Design
Fundamental Data Types
Fundamental Data Types in C
(1)
Computers work in the binary number system.
The basic unit is the bit ("BInary digIT")
A bit can be either “1” or “0”
(High or Low, True or False, 5 Volts or 0 Volts)
The other basic units are:
Nibble
4 bits 0000 - 1111 (Binary), 0 - F (Hex)
Byte
8 bits 0000 0000 - 1111 1111 (Binary),
00 - FF (Hex)
Word
16 bits 0000 - FFFF (Hex)
Longword
32 bits 0000 0000 - FFFF FFFF (Hex)
Mechatronics Lab
15
Machine Design
In the “C” language (for AVR microcontrollers)
char c;
// signed 8 bit integer.
// range: -128..127
int i;
// signed 16 bit integer.
// range: -32768..+32767
long int l;
// signed 32 bit integer.
// -2147483648..+2147483647
unsigned char u;// unsigned 8 bit number.
// range: 0 .. 255
unsigned int v;// unsigned 16 bit number.
// range: 0 .. 65535
unsigned long w;// unsigned 32 bit number.
// range: 0 .. 4294967296
Mechatronics Lab
16
Machine Design
Signed number
representation
Signed Number
Representation
Hex numbers may be signed or unsigned.
Unsigned numbers are positive only.
Different ways of representing
negative numbers exists.
For 8 bits, they range from 0 .. 255 (0..FF hex)
Signed numbers are positive, negative or zero.
The most significant bit is used to represent the sign of
a number
For 8 bits, they range from -128 ..127 (80..7F hex)
Mechatronics Lab
17
Mechatronics Lab
18
3
Machine Design
Machine Design
Negative Number
representation
Decimal values
Byte numeric representation (8 bits = 1 byte)
Signed Unsigned
+127 127
-128 128
-4
252
-3
253
-2
254
-1
255
Binary
0111 1111
1000 0000
1111 1100
1111 1101
1111 1110
1111 1111
Hexadecimal Octal
7F
177
80
200
FC
374
FD
375
FE
376
FF
377
Mechatronics Lab
Various definitions and
representations…
Float in C: 32 bits
Double: 64 bits
19
Machine Design
Mechatronics Lab
20
Machine Design
Decimal values cont.d
The AVR family members
(3)
New high performance 32 bit MCU/DSP AVRs :
The AVR32 UC3B devices deliver 72 Dhrystone
MIPS (DMIPS) at 60 MHz, and include true singlecycle MACs and DSP arithmetic and consume only
23 mA at 3.3V.
Two major alternatives:
Floating point
Fixed point
DSP - Digital Signal Processor.
MAC - Multiply & ACcumulate - A typical function
performed by DSPs to implement Filters, Discrete/
Fast Fourier Transforms (butterfly) etc...
Mechatronics Lab
21
Machine Design
Mechatronics Lab
22
Machine Design
From the manual
Types of Computers
•
RISC - Reduced Instruction Set Computer
•
The instruction set is small, and most instructions
complete in one cycle (100 or less instruction types,
High Performance, Low Power
AVR®32 UC 32-Bit Microcontroller
smaller range of addressing modes).
• Multiply & Divide performed using add/subtract & shift
Compact Single-cycle RISC Instruction
Set Including DSP Instruction Set
Read-Modify-Write Instructions and
Atomic Bit Manipulation
•
DSP - Digital Signal Processor
•
The instruction set is specialized for common fixed
or floating point arithmetic. DSP performance is
measured in MFLOPS - Millions of Floating/Fixed
Point Operations Per Second.
Mechatronics Lab
23
Mechatronics Lab
24
4
Machine Design
Machine Design
Types of Computers
•
Basically…
CISC - Complex Instruction Set Computer
•
The instruction set is large, and offers great variety
of instructions (100 or more instruction types, many
addressing modes).
•
Few instructions complete in one cycle
•
Typically includes multiply & divide operations that
•
EG the 68HC11 takes- 10 Cycles for (8x8) MUL
•
41 cycles for 16 / 16 Divide. 200K mul/sec, 50K div /
1 cycle = 1/f
(1/60MHz)
may take many cycles to complete.
sec
Mechatronics Lab
25
Machine Design
Mechatronics Lab
Machine Design
Mathematical functions
Taylor series…
In mathematics, the Taylor series is a
representation of a function as an infinite sum of
terms calculated from the values of its
derivatives at a single point. The Taylor series
was formally introduced by the English
mathematician Brook Taylor in 1715. If the series
is centered at zero, the series is also called a
Maclaurin series, named after the Scottish
mathematician Colin Maclaurin who made
extensive use of this special case of Taylor's
series in the 18th century. It is common practice
to use a finite number of terms of the series to
approximate a function. The Taylor series may be
regarded as the limit of the Taylor polynomials.
Either implemented in software or
hardware
HW
o Special functions in the processor, coprocessor, DSP-core, MAC-unit etc.
o From 1 cycle (DSP) to several cycles (CISCarchitecture)
SW
o Various (more or less nice) implementations of
mathematical functions, typically taylor series
Mechatronics Lab
27
Machine Design
Mechatronics Lab
28
Machine Design
Mathematical functions
cont.d
AVR32 Fixed point support
Difficult and expensive to include full
DSP-functionality (complete one
operation in one cycle)
Various combinations and breeds of
HW and SW
Hardware support, some
mathematical instructions are
implemented in HW
Only for fixed point variables (not
floats)
Requires the user to use a special
library, special set of functions
AVR32: Some fixed point DSP
support
Mechatronics Lab
26
29
Mechatronics Lab
30
5
Machine Design
Machine Design
DSPLib
…from the manual…
If the DSPLib library is used, the
user does not have to bother how
it’s implemented in HW, doesn’t
even have to know if it’s
implemented or not.
The Atmel AVR32 DSPLib is a library
which provides a useful set of digital
signal processing functions
The result can be seen after
compilation, in the number of cycles
it requires.
Mechatronics Lab
31
Machine Design
Mechatronics Lab
32
Machine Design
Benchmarking experiments – math.h
First – consider the
alternatives
float x, z = 12.3;
while(1){
LED_Toggle(1);
for(i=0;i<=10;i++){
x = sqrt(z);
}
}
math.h (standard C library)
Implements, in SW, functions such
as
Sin()
Pow(x, y)
Mechatronics Lab
33
Machine Design
float x, z = 12.3;
while(1){
LED_Toggle(1);
for(i=0;i<=1000;i++){
x = sqrt(12.3);
}
}
Mechatronics Lab
34
Machine Design
Benchmarking experiments – math.h
Math.h
float x, z = 12.3;
while(1){
LED_Toggle(1);
for(i=0;i<=10;i++){
x = sin(z);
}
}
With math.h, SQRT() takes about
180 cycles
Mechatronics Lab
35
Mechatronics Lab
float x, z = 12.3;
while(1){
LED_Toggle(1);
for(i=0;i<=1500;i++){
x = sin(12.3);
}
}
36
6
Machine Design
Machine Design
Math.h
DSPLib (instead of math.h)
With math.h, SIN() takes about 150
cycles
Mechatronics Lab
37
Machine Design
Mechatronics Lab
38
Machine Design
DSPLib
Fixed Point Arithmetics definition
Allow any fixed-point Q-Formatted numbers.
16-bit and 32-bit data are supported.
Is compatible with any platforms that are
supported by GCC and IAR.
Can be customized with several algorithm
optimization options according to your needs.
The DSPLib architecture is composed of 6 submodules: filtering, operators, signal generation,
transforms, vectors and windowing.
Lays out an advanced library with more specific
applications.
Provides a useful set of debugging functions.
In computing, a fixed-point number
representation is a real data type for a
number that has a fixed number of digits
after (and sometimes also before) the radix
point (e.g., after the decimal point '.' in
English decimal notation). Fixed-point
number representation can be compared to
the more complicated (and more
computationally demanding) floating point
number representation.
Mechatronics Lab
39
Machine Design
Mechatronics Lab
40
Machine Design
Benchmarking
Representation
A value of a fixed-point data type is
essentially an integer that is scaled by a
specific factor determined by the type. For
example, the value 1.23 can be represented
as 1230 in a fixed-point data type with
scaling factor of 1/1000, and the value
1230000 can be represented as 1230 with a
scaling factor of 1000. Unlike floating-point
data types, the scaling factor is the same for
all values of the same type, and does not
change during the entire computation.
Mechatronics Lab
41
With DSPlib
With math.h
Sine/cosine: 48 cycles
(Q16)
Absolute: 1 cycle
Square root: 61 cycles
(Q16)
Exponential: 70 cycles
(Q16)
Sine/cosine: 150
cycles
Square root: 180
cycles
Mechatronics Lab
42
7
Machine Design
Machine Design
Mathematical operations
Q-format
Q16: 1 integer bit, 15 fractional bits
Resolution = 2-15 approx 0.00003
1. Scale value and convert to Q-format
(from float)
2. Use Fixed Point DSP-operator
Value from -1 to +0.99997
1. Sine(), square root(), multiplication()
3. Convert back from Q-format to float
Mechatronics Lab
Q16 sometimes referred to as Q1.15
43
Machine Design
Mechatronics Lab
Machine Design
Conversion
Conversion
Float to Q
To convert a number from floating
point to Qm.n format:
Multiply the floating point number by
2n
Round to the nearest integer
Q to Float
To convert a number from Qm.n
format to floating point:
Convert the number to floating point
as if it were an integer
Multiply by 2−n
Mechatronics Lab
45
Machine Design
Mechatronics Lab
46
Machine Design
Fixed-point format
Convert to Q-format
All DSP types in the DSPLib includes
the notion of 16-bit and 32-bit fixedpoint formats. It is important to
understand this format in order to
fastest and/or debug an application.
See Q Format for more details on
this format.
Mechatronics Lab
44
dsp16_t a, b, c;
b=DSP16_Q(0.91);
47
Mechatronics Lab
48
8
Machine Design
Machine Design
Multiplication
Multiplication
static dsp16_t
dsp16_op_mul (dsp16_t
num1, dsp16_t num2)
16-bit fixed point version of the
multiplication function.
Mechatronics Lab
49
Machine Design
Mechatronics Lab
50
Machine Design
More advanced arithmetics
Benchmarking
Addition
addition of two 256-point vectors: 1,252
cycles (41.7us @ 30MHz) for the 16-bit
version and 1,638 cycles (54.6us @
30MHz) for the 32-bit version.
Power
raised to the power of one 256-point
vectors with a real number: 51,927
cycles (1.73ms @ 30MHz) for the 16-bit
version and 167,276 cycles (5.58ms @
30MHz) for the 32-bit version.
Mechatronics Lab
51
Mechatronics Lab
52
Machine Design
How to use math.h
Question
How to use math.h in an AVR32 Studio Project?
Answer
•Here is the step by step procedure to apply for
linking the math library to your project: Open the
project properties.
•Select C/C++ Build item -> Settings --> Tool
Settings tab.
•Select AVR32/GNU C Linker -> Libraries.
•Add a library: m as shown in attached picture; m is
standind for math.
•Do not forget to include math.h in your C file.
•Build your project.
Mechatronics Lab
53
9
© Copyright 2026 Paperzz