Haptic Interfaces and Virtual...
Transcript of Haptic Interfaces and Virtual...
Haptic Interfaces and Virtual Environments
• A haptic interface allows for a human to interact with a
computer through the sense of touch.
- The user moves a “puck” (joystick, wheel) in the workspace.
- The CPU senses this motion, and computes reaction forces
that are transmitted to the human using the motor.
- A “force feedback” system
Human
HapticDevice
actuators(muscles)
CPU sensor(encoder, tach,strain gauge)
actuator(DC motor)
sensor(skin surface)
forcepositionvelocityforce
behavior
algorithm(virtual world)
• The algorithm used by the CPU to process the position and/or
velocity measurements to produce the reaction forces is called
a “virtual environment” or “haptic world”
- virtual wall [1]
- virtual sprung mass [1]
- surgery simulator (virtual body tissue) [2]
EECS461, Lecture 8, updated October 1, 2008 1
Virtual Wall
• Consider a puck in a linear workspace:
Puck
z
virtual wall
zw
workspace limit
workspace limit
• Our goal is to create a virtual wall to prevent us from moving
the puck all the way to the right of the workspace
• Idea
- If the puck is to the left of the “virtual wall”, do nothing, so
the puck moves freely.
- If the puck is to the right of the “virtual wall”, have the
motor exert a force to push it back to the left
• To implement a virtual wall, we require
- a position measurement from an encoder
- an algorithm to compute the force, implemented on a
microprocessor
- a motor to exert the force
EECS461, Lecture 8, updated October 1, 2008 2
Virtual Spring Algorithm
• To make a virtual wall, we suppose that when the puck is to
the right of the wall, it is pulled back to the wall with a spring:
Puck
z
zw
k
• Hence we need the motor to exert a force equal to the restoring
force of a spring
F = −k(z − zw)
• Pseudocode for virtual wall:
set z_wset spring constant kwhile(1) {
read puck position z from potentiometer (encoder)if z > z_w
compute force F = k*(z - z_w)output force to DAC (set PWM duty cycle)
elsedo nothing
end}
EECS461, Lecture 8, updated October 1, 2008 3
Issues with Virtual Wall
• Rotary vs. linear workspace is analogous:
- angular rather than linear displacement
- torque rather than force
• Chatter: an “artifact” that distinguishes a computer generated
wall from an actual wall. Occurs due to combination of
- large restoring force
- slow sample time
- large quantization error
• If a tachometer is used to sense velocity, then the velocity must
be numerically integrated to obtain position displacement
• For a more detailed schematic of linear and rotary haptic
interfaces, see [1]
EECS461, Lecture 8, updated October 1, 2008 4
Virtual Sprung Mass
• Suppose we wish the puck to behave as though it had a mass
attached to it with a spring:
Puck
z
w
k
m
• If we move the puck to a new position, z, then
- the spring will exert a force F = −k(w − z) on the mass
- the force will accelerate the mass according to Newton’s
laws: −k(w − z) = mw
- the position of the mass will change acccording to
w +k
mw =
k
mz (1)
- the puck experiences a reaction force equal in magnitude
and opposite in direction to the force exerted on the mass:
Puck
z
m
w
k F FF F
EECS461, Lecture 8, updated October 1, 2008 5
Simulink Model of Spring Mass System
• The differential equation
w =k
mz −
k
mw
has a block diagram (cf. the notes on second order systems
from Lecture 7):
Σ-
z
k/m
w
k/m
.w..
w
• It is easy to show that this diagram is equivalent to one from
which the force acting on the mass, F = −k(w− z), can be
directly computed:
Σ
-
z
1/m
w
k
.w
..wF
• an appropriate SIMULINK diagram:
wwdotwddotz
4
Force3
Puck Position
2
Velocity
1
Virtual Mass Position
k
spring constant
Step
1s
Integrator1
1s
Integrator
1/m
1/virtual mass
EECS461, Lecture 8, updated October 1, 2008 6
Simulated Response
• Let us move the puck one unit to the right and hold it there.
- The mass will respond according to (1)
- A reaction force will be exerted on the puck
- This force must be countered by the user to hold the puck
stationary!
• Matlab simulation1 of mass motion, velocity, and force exerted
on the puck (assumes that the puck and mass have no physical
dimension)
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5−10
−8
−6
−4
−2
0
2
4
6
8
10
time, seconds
k = 10, m = 1, period of oscillation = 1.9869
mass position, wmass velocity, dw/dtpuck position, zforce on mass, F=−k(w−z)reaction force on puck, −F
• Note: the spring-mass system is an undamped harmonic
oscillator with
- frequency in radians/second, ωn =p
k/m
- frequency in Hz, fn = ωn/2π
- period in seconds, Tn = 1/fn
1Obtained from Matlab files virtual spring mass.m and oscillator force.mdl.
EECS461, Lecture 8, updated October 1, 2008 7
Algorithm for Sprung Mass
• If puck is not held constant, then the force will depend on the
puck position.
• To simulate the motion of the puck requires that we model
- the response of the puck states (position, velocity) to the
reaction force.
- the effect of the human interacting with the puck
• To implement the haptic interface,
- we measure the position of the puck.
- we do not need to simulate puck motion or human
interaction.
• Pseudocode for sprung mass:
set k, mwhile(1) {
read puck position z from potentiometer (encoder)update w according to eqn.(1)compute reaction force F = k*(w-z)output force to DAC (set PWM duty cycle)
}
• Question: How to update w?
- Must numerically integrate a second order differential
equation
EECS461, Lecture 8, updated October 1, 2008 8
State Space Form of Differential Equations
• The differential equation governing the motion of the mass is
a second order equation:
Σ
-
z
1/m
w
k
.w
..wF
• It is convenient to represent this as two first order equations.
Define state variables position and velocity:
x1 , w
x2 , w
• Define the input as the puck position, u = z, and the output
as the reaction force, y = −F = k(w − z). Then the state
differential equations are
x1 = x2
x2 = −k
mx1 +
k
mu
y = kx1 − ku
EECS461, Lecture 8, updated October 1, 2008 9
Matrix Form of State Equations
• Define the state vector
x ,
»x1
x2
–=
»w
w
–• Then the state equations can be written in matrix form as
x = Ax + Bu
y = Cx + Du
where
A ,
»0 1
− km 0
–, B ,
»0km
–, C ,
ˆk 0
˜, D , −k
• It is true in general that any n-th order differential equation
may be written as a system of n first order differential
equations, one for each of n state variables
• Example:
- Suppose that, instead of holding the puck position fixed
after an initial step change, we allow the puck to move freely
in response to the reaction force exerted by the mass.
- Need four state variables (position and velocity of both puck
and mass)
EECS461, Lecture 8, updated October 1, 2008 10
Numerical Integration of State Equations
• Consider a differential equation x = f(x, u)
• Question: How to find the value of
x(t) = x(0) +
Z t
0
f(x, u)dt
given some initial condition x(0), and the input u(t)?
• Idea: Select a time interval T , and generate an approximating
sequence x(0), x(T ), x(2T ), . . . using a difference equation
x((n + 1)T ) = x(nT ) + Tf(x(nT ), u(nT )),
with initial condition x(0) = x(0).
• The forward Euler integration algorithm
f(x,u)
T 2T 3T 4T
• The difference x((n + 1)T ) − x(nT ) is the area of one
rectangle.
• We usually abbreviate x(k) , x(kT ) and suppress the ˜
notation.
EECS461, Lecture 8, updated October 1, 2008 11
Pseudocode for Virtual Spring-Mass
• In our case, f(x, u) = Ax + Bu, where
A =
»0 1
−k/m 0
–, B =
»0
k/m
–• Hence
x(n + 1) = x(n) + T (Ax(n) + Bu(n))
= (I + TA)x(n) + TBu(n)
• x1 = position, x2 = velocity
x1(n + 1) = x1(n) + Tx2(n)
x2(n + 1) = x2(n) + T (k/m)(−x1(n) + u(n))
• Pseudocode for sprung mass:
initialize parameters k,m;initialize states x1prev = z, x2prev = 0;set update period T;while(1) {
read in puck position u = z;x1 = x1prev + T*x2prev;x2 = x2prev + T*(-(k/m)*x1prev + (k/m)*u);compute reaction force y = F = k*(x1-u)x1prev = x1; x2prev = x2;output force value
}
EECS461, Lecture 8, updated October 1, 2008 12
Difference Equations and the z-Transform
• Analogous to Laplace transform for continuous time systems
and differential equations
• Given a discrete sequence
{y(k)} = . . . , y(−2), y(−1), y(0), y(1), y(2), . . . ,
the z-transform is
Y (z) =
∞Xk=−∞
y(k)z−k
• The z-transform of the time-shifted sequence {y(k)} =
{y(k + n)} is
Y (z) = znY (z)
• The transfer function for the system with difference equation
y(k + 2) + a1y(k + 1) + a2y(k) = b1u(k + 1) + b2u(k)
is found from
Y (z) = G(z)U(z), G(z) =(b1z + b2)
(z2 + a1z + a2)
EECS461, Lecture 8, updated October 1, 2008 13
Discrete Time Simulation
• Difference equations
x1(n + 1) = x1(n) + Tx2(n)
x2(n + 1) = x2(n) + T (k/m)(−x1(n) + u(n))
• Transfer functions
X1(z) =T
z − 1X2(z)
X2(z) =T
z − 1(k/m)(−X1(z) + U(z))
• For us, x1 = w, x2 = w, u = z
• SIMULINK model2
w
wdot
wddotz
3
Out3
2
Out2
1
Out1Step
1/m
Gain2
k
Gain1
T
z−1
Discrete−TimeIntegrator1
T
z−1
Discrete−TimeIntegrator
1
In1
Force
2discrete oscillator force.mdl
EECS461, Lecture 8, updated October 1, 2008 14
Issues with Sprung Mass
• How well does the solution to the difference equation match
the solution to the differential equation?
• Intuitively, if T is “small enough”, approximation should be
good
• Matlab simulation of the response of the system with k = 1,
m = 1 to a step change in puck position3:
• If T is “too large”(in this case T = 0.1), response becomes
unbounded:
0 2 4 6 8 10 12 14 16 18 20−2
−1
0
1
2
3
4
time, seconds
k = 1, m = 1, period = 6.2832 seconds, T = 0.1 seconds
mas
s po
sitio
n, w
differential equationdiscrete approximation
• What goes wrong?
3Obtained with Matlab files num integration.m, oscillator.mdl, anddiscrete oscillator.mdl.
EECS461, Lecture 8, updated October 1, 2008 15
Numerical Stability
• System of differential equations:
x = Ax + Bu (2)
y = Cx + Du
• Many different ways to obtain a discrete approximation
• Simplest is the (forward) Euler scheme:
x(nT ) ≈x((n + 1)T )− x(nT )
T≈ Ax(nT )+Bu(nT )
• Difference equations:
x((n + 1)T ) = Adx(nT ) + Bdu(nT ) (3)
y(nT ) = Cdx(nT ) + Ddu(nT )
where
Ad = I + TA, Bd = TB
Cd = C, Dd = D
• Both the continuous system and the discrete appoximation are
stable if
- any bounded input results in a bounded output
- the response to initial conditions decays to zero
• Question: Does stability of (2) imply stability of (3)?
EECS461, Lecture 8, updated October 1, 2008 16
Numerical Stability, II
• The difference equation
x(n + 1) = (I + TA)x(n) + TBu(n)
is stable if the eigenvalues of the matrix I + TA have
magnitude less than one.
• The discrete transfer function G(z) = N(z)/D(z) is stable
if all the roots of D(z) = 0 lie within the unit circle
• In our case, eigenvalues4 are at λ = 1± jTp
k/m
−1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1
−1
−0.8
−0.6
−0.4
−0.2
0
0.2
0.4
0.6
0.8
1
real
imag
inar
y
eigenvalues at 1 ± j0.1
• If Tp
k/m is too large, the algorithm will diverge rapidly
• Other (more complicated) techniques for numerical integration
do preserve stability – but does it matter?
4num integration.m
EECS461, Lecture 8, updated October 1, 2008 17
Discrete Approximation to an Integrator
• Consider the continuous time simulation of the harmonic
oscillator in Figure 1:
wwdotwddotz
4
Force3
Puck Position
2
Velocity
1
Virtual Mass Position
k
spring constant
Step
1s
Integrator1
1s
Integrator
1/m
1/virtual mass
Figure 1: Analog Harmonic Oscillator
• The forward Euler approximation of the harmonic oscillator in
Figure 2 is obtained by replacing each analog integrator by a
discrete approximation, 1/s → T/(z − 1):
w
wdot
wddotz
3
Out3
2
Out2
1
Out1Step
1/m
Gain2
k
Gain1
T
z−1
Discrete−TimeIntegrator1
T
z−1
Discrete−TimeIntegrator
1
In1
Force
Figure 2: Discrete Harmonic Oscillator, Forward Euler
• Other discrete approximations to the harmonic oscillator may
be obtained by replacing the analog integrators by other
discrete approximations.
EECS461, Lecture 8, updated October 1, 2008 18
Discrete Approximation to an Integrator, II
Suppose we wish to approximate an integrator,
y(t) =
Z t
0
u(t)dt
by a discrete sequence y(0), y(T ), y(2T ), . . .
• Forward Euler:
y((k + 1)T ) = y(kT ) + Tu(kT )
1
s→
T
(z − 1)
T 2T 3T 4T
u(t)
t
EECS461, Lecture 8, updated October 1, 2008 19
Discrete Approximation to an Integrator, II
• Backward Euler:
y((k + 1)T ) = y(kT ) + Tu((k + 1)T )
1
s→
Tz
(z − 1)
T 2T 3T 4T
u(t)
t
• Trapezoidal:
y((k + 1)T ) = y(kT ) +T
2(u(kT ) + u((k + 1)T ))
1
s→
T (z + 1)
2(z − 1)
T 2T 3T 4T
u(t)
t
EECS461, Lecture 8, updated October 1, 2008 20
Signal Differentiation
• Used to obtain a velocity estimate from position measurements
• Problem: Differentiation amplifies noise. Consider
y(t) = sin(t)| {z }signal
+ 0.1 sin(10t)| {z }noise
• The high frequency noise is amplified5:
0 1 2 3 4 5 6 7 8 9 10−2
−1.5
−1
−0.5
0
0.5
1
1.5
2
time, seconds
y(t) = sin(t) + 0.1sin(10t)
y dy/dt
• A problem even with analog differentiation! The transfer
function of a differentiator is s, and thus the frequency
response is unbounded at high frequencies!
• A pure differentiator is never implemented. Instead, an
approximate differentiator, s/(τs + 1), is used to lowpass
filter any noise
5Matlab m-file diff noise.m
EECS461, Lecture 8, updated October 1, 2008 21
Numerical Differentiation
• A digital approximation to velocity can be obtained by
approximating the derivative as
x(nT ) ≈x(nT )− x((n− 1)T )
T
• Hence, we can define a sequence of approximations ot the
derivative:
v(nT ) =x(nT )− x((n− 1)T )
T
• This procedure will still tend to amplify noise, which may still
need to be removed with a filter:
- analog filter (e.g., op amp circuit)
- digital FIR filter (e.g., moving average filter)
- digital IIR filter (e.g., lowpass filter)
EECS461, Lecture 8, updated October 1, 2008 22
Numerical Integration
• How to obtain a position estimate from velocity measurements?
• Assume that the velocity over a time interval is the average of
the velocity at the endpoints of the interval:
x(nT ) = x((n− 1)T ) +T (v(nT ) + v((n− 1)T ))
2
• Issues:
- small step size needed for rapidly varying signals, but can
cause quantization error to accumulate for slowly varying
signals
- long step size better for slowly varying signals, but can
“miss” high frequency variations.
EECS461, Lecture 8, updated October 1, 2008 23
References
[1] B. Gillespie. Tutorial on virtual environments for haptic
display. University of Michigan, April 2000.
[2] D. Sorid and S. K. Moore. The virtual surgeon. IEEE
Spectrum, pages 26–31, July 2000.
EECS461, Lecture 8, updated October 1, 2008 24