Artificial Intelligence Techniques
for Mobile Robots
Teacher: Alessandro SaffiottiRoom: T-2224Email: [email protected]
Lab assistant: Ali Abdul KhaliqRoom: T-1121Email: [email protected]
Course home page:
http://aass.oru.se/~asaffio/Teaching/AIMR/
Fuzzy Rule-Based Control
Outline
• Rule-Based Control
– rule-based systems in AI
– the “stimulus-reasponse” rules
• Case Studies
– example: go to position ⇐ (Lab opt)
– example: avoid obstacles ⇐ (Lab opt)
• From Crisp Rules to Fuzzy Rules
– the problem of discontinuity
– fuzzy sets and fuzzy predicates
– fuzzy rules
– structure of a fuzzy controller
• Fuzzy Control in Practice
– example: go to position ⇐ Lab
– example: avoid obstacles ⇐ Lab
127c© A. Saffiotti 2017
Reminder: Rule-based systems
• Language: facts expressed by simple predicates
– e.g.: ‘dog(snoopy)’, ‘professor(alex)’
– may include operators like OR, NOT, EXISTS, . . .
• Language: rules expressed in ‘IF – THEN’ form
– e.g.: ‘IF dog(X) THEN animal(X)’
– e.g.: ‘IF human(X) THEN animal(X)’
– e.g.: ‘IF professor(X) THEN human(X)’
• Knowledge Base (KB):
– a set of facts and rules
• Inference:
– instantiation:
from IF P(X) THEN Q(X)infer IF P(a) THEN Q(a)
– modus ponens:
from P ; IF P THEN Qinfer Q
– AND introduction:
from P ; Qinfer P AND Q
– and so on. . .
• Example:
–IF dog(X) THEN animal(X)
dog(snoopy) IF dog(snoopy) THEN animal(snoopy)animal(snoopy)
129c© A. Saffiotti 2017
Reminder: Rule-based systems
• Search: typically, forward chaining
– 1. mark all rules that match some fact in the KB
– 2. use a conflict resolution strategy to select one rule
– 3. assert the consequence of the selected rule in the KB
– 4. return to 1
• Until a given query is answered
– the query is ’proved’ to follow from the facts
– or not to follow
• Example:
– assert ‘animal(snoopy)’
– assert ‘human(alex)’
– assert ‘animal(alex)’
– ‘animal(alex) AND animal(snoopy)’
– . . .
• In practice, smarter procedures are used
– e.g.: logic programing
– e.g.: SAT systems
130c© A. Saffiotti 2017
A naive rule evaluation loop
procedure ForwardChainRules (Query)local vars changed, valrepeat
if TruthValue(Query) = 1 return truechanged := falsefor each rule i = “IF Si THEN Ri” in rule base
val := TruthValue(Si)if (val = 1)
Assert(Ri = 1)changed = true
until changed = falsereturn false
procedure TruthValue (S)local var resultswitch on the structure of S:
atomic P : result := fetch value of P¬S1: result := 1 - TruthValue(S1)S1 ∧ S2: result := min(TruthValue(S1), TruthValue(S2))S1 ∨ S2: result := max(TruthValue(S1), TruthValue(S2))
return result
• Note:
– all rules are tried and applied in each “repeat” loop
– this is a Breadth First search strategy
• Exercises:
– Change the procedure to do Depth First search
– Change the procedure to do backward chaining
131c© A. Saffiotti 2017
Rule-based systems for robots
• Link to the real world can be provided
by a human operator . . .
132c© A. Saffiotti 2017
Rule-based systems for robots
• Link to the real world can be provided
by a human operator . . .
• . . . or by a robot!
133c© A. Saffiotti 2017
Rule-based systems for acting
• Perception:
– predicates can denote perceived situations
– e.g.: obstacle-in-front(X)
• Action:
– rule consequents can be physical actions
– e.g.: IF obstacle-in-front(X) THEN ACT(TurnRightOf,X)
• Decision making:
– mark all rules that match some fact in the KB
– use a conflict resolution strategy to select one rule
– perform the action of the selected rule
• Implemented by “procedural attachments”:
– predicates are evaluated by calling sensor routines
– actions are evaluated by calling actuator routines
134c© A. Saffiotti 2017
Stimulus-response rules
IF stimulus THEN response
• Which response should be used for a stimulus
– rules are based on our experience with the system
– they are not based a formal analysis
• “Heuristic” control rules
– convenient when a linear model is not (easily) available
– but heuristic knowledge is available
• Control behavior defined piecewise
– each rule determines behavior in one class of situations
• Example: go to position
IF position left THEN Turn(Left)IF position right THEN Turn(Right)IF position ahead THEN Go(Ahead)
135c© A. Saffiotti 2017
Overall schema
• Recall: general feedback control loop
Error
Control values
Target position
Actual position
Control Program
Robot + Environment
• In this case: rule-based controller
target position
Robot + Environmentcontrol values
Reference value:
actual position
Co
ntro
ller ou
tpu
t
Resp
on
se
Stim
ulu
s
System output:
Co
ntro
ller inp
ut
Rules
Evaluate theEvaluate the
Stimulus
System input:
136c© A. Saffiotti 2017
Encoding the stimulus
θ
Y
r
ry
xr xt
yt
X
Eθ
Epos
• Simple stimulus = logical predicate
– e.g.: “position left”
– can be either true or false:
true = 1false = 0
• “Evaluate the stimulus”
– evaluate truth value of corresponding predicate
– truth value depends on input variables
• Examples
– “position left” is true if Eθ > 10
– “position right” is true if Eθ < −10
– “position ahead” is true if |Eθ| ≤ 10
– “position here” is true if Epos < 20mm
137c© A. Saffiotti 2017
More complex stimuli
θ
Y
r
ry
xr xt
yt
X
Eθ
Epos
• Stimulus = logical formula
– combine predicates using Boolean operators:
not: ¬and: ∧
or: ∨
– e.g.: “position left ∧ ¬position here”
• “Evaluate the stimulus”
– evaluate truth value of corresponding formula
– truth of formula depends on truth of predicates in it
P Q ¬P P ∧Q P ∨Q0 0 1 0 00 1 1 0 11 0 0 0 11 1 0 1 1
138c© A. Saffiotti 2017
Example
θ
Y
r
ry
xr xt
yt
X
Eθ
Epos
• suppose that:
Eθ = −50, and
Epos = 140mm
– truth value of “position left ∧ ¬position here” = ?
139c© A. Saffiotti 2017
Designing the rules
θ
Y
r
ry
xr xt
yt
X
Eθ
Epos
IF position left ∧ ¬position here THEN Turn(Left)IF position right ∧ ¬position here THEN Turn(Right)IF position ahead ∧ ¬position here THEN Go(Ahead)IF position here THEN Stop
• Each rules reacts to one stimulus
– either a simple stimulus or a complex stimulus
– the rule is active if its stimulus is true
• Each rules generates a response
– the rule is based on the “intuition” of the designer
– all the rules together form a control program
• Rules should be exhaustive and mutually exclusive
– exactly one rule is active for each input value
– or, we have default values (e.g.: no turn, no velocity)
140c© A. Saffiotti 2017
Decoding the response
• Response = vector of values for control variables
– usually, one value for each control variable
– e.g.: [vel, rot]
• Must transform the rule consequent in a value
• Examples
– “Turn(Left)” ⇒ vrot = 10 deg/sec
– “Turn(None)” ⇒ vrot = 0 deg/sec
– “Turn(Right)” ⇒ vrot = −10 deg/sec
– “Go(Ahead)” ⇒ vlin = 50 mm/sec
– “Go(None)” ⇒ vlin = 0 mm/sec
– “Go(Back)” ⇒ vlin = −20 mm/sec
141c© A. Saffiotti 2017
The control algorithm
target position
control values
Reference value:
actual positionRobot + Environment
Contro
ller input
Rules
Evaluate theEvaluate the
Stimulus
Evaluate truth of predicates
Truth values of predicates:
Decode response
System output: System input:
Stim
ulu
s
Resp
onse
V
Contro
ller outp
ut
1 V, ..., n
procedure RuleBasedControl ()local vars vel, rotfor P1, . . . , Pn predicates appearing in the rules
Vi = evaluate truth of Pi[vel, rot] := EvalRules(V1, . . . , Vn)[v lin, v rot] := decode [vel, rot]
procedure EvalRules (V1, . . . , Vn)local var vel, rotfor each rule i = “IF Si THEN GO(Ri)” in rule base
if (TruthValue(Si) = true) vel := Rifor each rule j = “IF Sj THEN TURN(Rj)” in rule base
if (TruthValue(Sj) = true) rot := Rjreturn [vel, rot]
procedure TruthValue (S, V1, . . . , Vn)switch on the structure of S:
atomic Pi: return (Vi)¬S1: return (1 - TruthValue(S1))S1 ∧ S2: return (min(TruthValue(S1), TruthValue(S2)))S1 ∨ S2: return (max(TruthValue(S1), TruthValue(S2)))
142c© A. Saffiotti 2017
Summary of rule-based control
• A set of predicates
– “position left” is true if Eθ > 15
– “position right” is true if Eθ < −15
– “position ahead” is true if |Eθ| ≤ 15
– “position here” is true if Epos < 20mm
• A set of control rules
IF position left ∧ ¬position here THEN Turn LeftIF position right ∧ ¬position here THEN Turn RightIF position ahead ∧ ¬position here THEN Go StraightIF position here THEN Stop
• A computing strategy
E
Predicates
Evaluate truth values
Control rules
vel
rot
Compute active rules
Eθ
pos
• Example:
– Eθ = −50
– Epos = 140mm
– What are the [v lin, v rot] sent to the robot?
143c© A. Saffiotti 2017
“Go To” rule-based controller
procedure GoToRules (x, y)
float err_th, err_pos;float ante;FPred Pos_Left, Pos_Right, Pos_Ahead, Pos_Here;
// First part: computation of the needed variables //// err_th, err_pos computed as in good old GoTo() //
// Second part: truth computation of the predicates //
if (err_th > 10) Pos_Left = 1.0;else Pos_Left = 0.0;
if (err_th < -10) Pos_Right = 1.0;else Pos_Right = 0.0;
if (ABS(err_th) <= 10) Pos_Ahead = 1.0;else Pos_Ahead = 0.0;
if (err_pos < 20) Pos_Here = 1.0;else Pos_Here = 0.0;
// Third part: the rules //
RULESET;
IF (AND(Pos_Left, NOT(Pos_Here))); ROT(LEFT);IF (AND(Pos_Right, NOT(Pos_Here))); ROT(RIGHT);IF (OR(Pos_Here, Pos_Ahead)); ROT(AHEAD);
IF(AND(Pos_Ahead, NOT(Pos_Here))); VEL(FAST);IF(OR(Pos_Here, NOT(Pos_Ahead))); VEL(NONE);
RULEND;;
144c© A. Saffiotti 2017
Notes
• FPred is the type of a predicate
– can be either 0.0 or 1.0
• The rule set is
– started by “RULESET”
– ended by “RULEND”
• Rotational velocity rules: IF(stimulus); ROT(value)
– value is either: LEFT, RIGHT, AHEAD
• Linear velocity rules: IF(stimulus); VEL(value)
– value is either: BACK, NONE, SLOW, FAST
• Stimulus is any logic formula
– one FPred, or many FPred combined by NOT, AND, OR
– AND and OR are two-place functions
– NOT is a one-place function
• There must be one active rule in each group
– one to set ROT, and one to set VEL
• After execution, the reactions are left in the ’vel’
and ’rot’ global variables
– Must be translated to actual velocities to be sent to
robot
145c© A. Saffiotti 2017
Behind the scenes
• ‘FPred’ is just a float (either 0.0 or 1.0)
• ‘IF’ is a macro that sets the value of ‘ante’
#define IF(form) (ante = (form))
• ‘NOT’, ‘AND’ and ‘OR’ are macros that computetruth values
#define NOT(form) (1.0 - (form))#define AND(f1, f2) ((f1) * (f2))#define OR(f1, f2) ((f1) + (f2) - ((f1) * (f2)))
• ‘IF’ is a macro that sets the value of ‘ante’
#define IF(form) (ante = (form))
• ‘VEL’ and ‘ROT’ are macros that set the valueof ‘vlin’ and ‘vrot’
#define VEL(value) (if (ante > 0.0) vlin = value)#define ROT(value) (if (ante > 0.0) vrot = value)
• ‘RULESET’ is a macro that initializes all values
#define RULESET vlin = NONE; vrot = AHEAD
146c© A. Saffiotti 2017
Example
RULESET;IF (AND(Pos_Left, NOT(Pos_Here))); ROT(LEFT);RULEND;
Is expanded to:
vlin = 0;vrot = 0;ante = Pos_left * (1.0 - PosHere);if (ante > 0.o)
vrot = LEFT;
147c© A. Saffiotti 2017
The top-level program
main ()
[current_x, current_y] = [0,0];[xt, yt] = read target position;
repeat
UpdatePosition();GoToRules(xt, yt);
vlin = reaction_to_vel(vel);vrot = reaction_to_rot(rot);SetPolarSpeed(vlin, vrot);
wait_some_time();until (at_target);
reaction_to_vel (int vel)
switch (vel) case BACK: return(-20); break;case NONE: return(0); break;case SLOW: return(20); break;case FAST: return(50); break;
. . . and similar for “reaction to rot”
148c© A. Saffiotti 2017
“Avoid” rule-based controller
#define DangerThreshold 0
procedure AvoidObstacles ()
unsigned int ir[8];FPred Obs_Left, Obs_Right, Obs_Ahead;
// First, read values of ir[0] ... ir[7] from the robot //
// Second, compute truth of predicates //if (ir[0] > DangerThreshold || ir[1] > DangerThreshold)
Obs_Left = 1.0;else Obs_Left = 0.0;
if (ir[2] > DangerThreshold || ir[3] > DangerThreshold)Obs_Ahead = 1.0;
else Obs_Ahead = 0.0;
if (ir[4] > DangerThreshold || ir[5] > DangerThreshold)Obs_Right = 1.0;
else Obs_Right = 0.0;
// Third, here come the rules //RULESET;
IF (AND(Obs_Left, NOT(Obs_Right))); ROT(RIGHT);IF (AND(Obs_Right, NOT(Obs_Left))); ROT(LEFT);IF (AND(Obs_Right, Obs_Left)); ROT(AHEAD);
IF (Obs_Ahead); VEL(BACK);IF (AND(OR(Obs_Right, Obs_Left)), NOT(Obs_Ahead)); VEL(SLOW);IF (NOT(OR(OR(Obs_Right,Obs_Left), Obs_Ahead))); VEL(FAST);
RULEND;;
149c© A. Saffiotti 2017
Exercises
• Find a good value of DangerThreshold
– see IR response in 1st lecture
• Find a better set of AvoidObstacles rules
– either simpler, but still produce a resonable behavior
– or produce a better obstacle avoidance behavior
• Write a “FollowObstacle” behavior
– the robot should approach any obstacle
– it should follow a moving obstacle
150c© A. Saffiotti 2017
Reminder: rule-based control
• A set of predicates
– “position left” is true if Eθ > 15
– “position right” is true if Eθ < −15
– “position ahead” is true if |Eθ| ≤ 15
– “position here” is true if Epos < 20mm
• A set of control rules
IF position left ∧ ¬position here THEN Turn LeftIF position right ∧ ¬position here THEN Turn RightIF position ahead ∧ ¬position here THEN Go StraightIF position here THEN Stop
• A computing strategy
E
Predicates
Evaluate truth values
Control rules
vel
rot
Compute active rules
Eθ
pos
• Example:
– Eθ = −50
– Epos = 140mm
– What are the [v lin, v rot] sent to the robot?
151c© A. Saffiotti 2017
The control surface
• Control function = f : X → U
– ~x and ~u can be muti-dimensional vectors
– for each input ~x we compute a control vector:
~u = f(~x)
• In our case, f has two components:vel = f1(Epos, Eθ)rot = f2(Epos, Eθ)
• The control surface
– shows how each control var depends on the input vars
– example: a non linear control function:
152c© A. Saffiotti 2017
The problem of discontinuity
• A rule-based controller computes a control func-
tionvel = f1(Epos, Eθ)rot = f2(Epos, Eθ)
• Our control surface for linear velocity
• Our control surface for rotational velocity
153c© A. Saffiotti 2017
Fuzzy sets
• Fuzzy set on a domain Ω
– generalizes the notion of a set
– characteristic function of a set A: µA : Ω→ 0,1
– membership function of a fuzzy set A: µA : Ω→ [0,1]
• E.g.: “small number”
– µ(x) tells how how much x is a “small number”
1 2 3 4 5 6 7 8 9
µ( )x
small−number1
0
0
• E.g.: “young”
– µ(x) tells how how much age x is “young”
µ( )x
1
0
young
5 10 15 20 25 30 35 40 450
156c© A. Saffiotti 2017
Operations on fuzzy sets
• Extend standard set operation
– defined in terms of membership functions
– collaps to standard operations on standard sets
• Complement: A
µA(x) = 1− µA(x)
0 1 2 3 4 5 6 7 8 9
1
0
small−number
• Intersection: A ∩B
µA∩B(x) = min(µA(x), µB(x))
1
0
5 10 15 20 25 30 35 40 45
young fertile
0
• Union: A ∪B
µA∪B(x) = max(µA(x), µB(x))
1
0
5 10 15 20 25 30 35 40 45
young fertile
0
157c© A. Saffiotti 2017
Fuzzy stimuli
θ
Y
r
ry
xr xt
yt
X
Eθ
Epos
• Simple stimulus = fuzzy predicate
– truth value is a real number in the [0,1] interval
• Truth of stimulus depends on input variables
– ie: membership degree of the value of input variable
• Example: Position Here
– assume that Epos = 30
– then, the truth value of Position Here is 0.7
0
1
0
10 20 50 60 70 80Epos
(mm)
Position_Here
µ( )x
0.7
4030
158c© A. Saffiotti 2017
Complex fuzzy stimuli
θ
Y
r
ry
xr xt
yt
X
Eθ
Epos
• Stimulus = logical formula
– combine predicates using fuzzy operators
not: ¬and: ∧
or: ∨– e.g.: “position left ∧ ¬position here”
• Truth of formula depends on truth of predicates
P Q ¬P P ∧Q P ∨Q
x y 1− x min(x, y) max(x, y)
• Example
– suppose: position left = 0.6 and position here = 0.3
– truth value of “position left ∧ ¬position here” = ?
159c© A. Saffiotti 2017
Fuzzy rules
θ
Y
r
ry
xr xt
yt
X
Eθ
Epos
IF position left ∧ ¬position here THEN Turn LeftIF position right ∧ ¬position here THEN Turn RightIF position ahead ∧ ¬position here THEN Go StraightIF position here THEN Stop
• The IF part is a fuzzy stimulus
– either simple or complex
• Each rule has a degree of activation
– this is the truth value of its stimulus
• Each rules generates a response
– the response is weighted by the degree of activation
– many rules can be active at any time !
• Responses from different rules must be combined
– taking the weight into account
161c© A. Saffiotti 2017
Example
Assume Eθ = 10 and Epos = 80
What are the truth values of these stimuli ?
position left ∧ ¬position here = ?
position right ∧ ¬position here = ?
position ahead ∧ ¬position here = ?
position here = ?
162c© A. Saffiotti 2017
The essence of fuzzy control
• A set of fuzzy predicates
• A set of fuzzy rules
– Note: The response terms must be defined
IF position left ∧ ¬position here THEN Turn LeftIF position right ∧ ¬position here THEN Turn RightIF position ahead ∧ ¬position here THEN Go StraightIF position here THEN Stop
• A computing strategy
– Note: “evaluate truth” often called “fuzzification”
– Note: “combine rules” often called “defuzzification”
Evaluate truth Evaluate rules
rulesFuzzy
Combine rules
Combiningstrategy
vel
rotpredicatesE
Fuzzy
θ
posE
163c© A. Saffiotti 2017
Combining the rules
1: IF position left ∧ ¬position here THEN Turn Left2: IF position right ∧ ¬position here THEN Turn Right3: IF position ahead ∧ ¬position here THEN Go Straight4: IF position here THEN Stop
• For rule i
– let: act(i) = level of activation (truth of stimulus)
– let: rot(i) or vel(i) = response (depends on which rule)
– eg: act(1) = 0.16; rot(1) = +10 deg/sec
• “MAX” technique
– just take the response with the highest activation
– does not resolve the problem of discontinuity
• “Center of Gravity” technique
– weighted combination of all the responses
rot =
k∑i=1
act(i) · rot(i)
k∑i=1
act(i)
vel =
n∑i=k+1
act(i) · vel(i)
n∑i=k+1
act(i)
(assume rules 1, . . . , k for rot, and k + 1, . . . , n for vel)
164c© A. Saffiotti 2017
Example
• Let
“Turn Left” is vrot = 10 deg/sec
“Turn Right” is vrot = −10 deg/sec
“Go Straight” is vrot = 0 and vlin = 50 mm/sec
“Stop” is vrot = 0 and vlin = 0
• Assume Eθ = 10 and Epos = 80.
• Then
position left ∧ ¬position here = 0.17
position right ∧ ¬position here = 0.0
position ahead ∧ ¬position here = 0.67
position here = 0.0
• And therefore
rot = 0.17·10+0.67·00.17+0.67
= 2.02 deg/sec
vel = 0.67·500.67
= 50 mm/sec
165c© A. Saffiotti 2017
Fuzzy controller for “GoToPos”
procedure GoToRules (x, y)
float err_th, err_pos;FPred Pos_Left, Pos_Right, Pos_Ahead, Pos_Here;
// First part: computation of the needed variables //// err_th, err_pos computed as in good old Reach() //
// Second part: computation of the fuzzy predicates //
Pos_Left = RampUp(err_th, 0, 60));Pos_Right = RampDown(err_th, -60, 0);Pos_Ahead = min(RampUp(err_th, -30, 0),
RampDown(err_th, 0, 30));Pos_Here = RampDown(err_pos, 10, 50);
// Third part: the fuzzy rules //
RULESET;
IF (AND(Pos_Left, NOT(Pos_Here))); ROT(LEFT);IF (AND(Pos_Right, NOT(Pos_Here))); ROT(RIGHT);IF (OR(Pos_Here, Pos_Ahead)); ROT(AHEAD);
IF (AND(Pos_Ahead, NOT(Pos_Here))); VEL(FAST);IF (OR(Pos_Here, NOT(Pos_Ahead))); VEL(NONE);
RULEND;;
167c© A. Saffiotti 2017
Notes
• FPred is the type for a fuzzy predicate
– a float between 0.0 and 1.0
• The rule set is
– started by “RULESET”
– ended by “RULEND”
• Rotational velocity rules: IF(stimulus); ROT(value)
– value is either: LEFT, RIGHT, AHEAD
• Linear velocity rules: IF(stimulus); VEL(value)
– value is either: BACK, NONE, SLOW, FAST
• Stimulus is any formula of fuzzy logic
– one FPred, or many FPred combined by NOT, AND, OR
– AND and OR are two-place functions
– NOT is a one-place function
• RampUp (similar for RampDown)
FPred RampUp (float x, float a, float b)
if (x >= b) return 1.0;if (x < a) return 0.0;else return ((x - a) / (b - a));
168c© A. Saffiotti 2017
Behind the scenes
• Local variables to keep intermediate valuues
float ante; // truth value of antefloat vrot[5]; // output fuzzy set for rotational velfloat vlin[5]; // output fuzzy set for linear vel
• ‘NOT’, ‘AND’ and ‘OR’ are macros that computetruth values
#define AND(x,y) (((x) < (y)) ? (x) : (y))#define OR(x,y) (((x) < (y)) ? (y) : (x))#define NOT(x) (1.0 - (x))
• ‘IF’ is a macro that sets the value of ‘ante’
#define IF(form) (ante = (form))
• ‘VEL’ and ‘ROT’ are macros that set the valueof ‘vlin’ and ‘vrot’
#define VEL(y) if (vlin[(y)] < ante) vlin[(y)] = ante#define ROT(y) if (vrot[(y)] < ante) vrot[(y)] = ante
• ‘RULESET’ is a macro that initializes all values
169c© A. Saffiotti 2017
Example
RULESET;IF (AND(Pos_Right, NOT(Pos_Here))); ROT(RIGHT);RULEND;
Is expanded to:
#define LEFT 4float ante;float vrot[5];...ante = (Pos_Left < (1.0 - PosHere)) ? Pos_Left : (1.0 - PosHere);if (vrot[LEFT] < ante) vrot[LEFT] = ante;...
If:
Pos_Left = 0.8, Pos_Here = 0.0
Then:
vrot = [0.8 0.0 0.0 0.0 0.0]
170c© A. Saffiotti 2017
From a fuzzy set to a control value
• Centroid defuzzification
void DeFuzzify (float fset[], int size, double* result)
sum = 0.0;wsum = 0.0;for (i = 0; i < size; i++)
sum += fset[i];wsum += fset[i] * (double)i;
*result = (wsum / sum) / size;
• Response to vel– result is a float between 0.0 and 1.0– convert it to int between VMIN and VMAX
– similar for rot
int ResponseToVel (float response)
return (VMIN + response*(VMAX-VMIN));
• Other functions may suit your hardware better
– eg: piecewise linear
171c© A. Saffiotti 2017
The top-level program
target position
Robot + Environmentcontrol values
Reference value:
actual position
Con
troller o
utp
ut
Resp
on
se
Stim
ulu
s
System output:
Con
troller in
pu
t
Rules
Evaluate theEvaluate the
Stimulus
System input:
main ()
[current_x, current_y] = [0,0];[xt, yt] = read target position;
repeat
UpdatePosition(); // Compute current pos
ClearFSet(vlin); // Reset Fuzzy setsClearFSet(vrot);GoToRules(xt, yt); // Run the behaviour
DeFuzzify(vrot, 3, &rot); // Defuzzify and set rot/velDeFuzzify(vlin, 4, &vel);
vlin = ResponseToVel(vel);vrot = ResponseToRot(rot);SetPolarSpeed(vlin, vrot); // Send commands to robot
wait_some_time();until (at_target);
172c© A. Saffiotti 2017
Recall: sensors in the ePuck
• Reminder: IR sensors in the iPuck
– 8 infrared light emitter/sensors (IR)
• Each IR sensor can be used in two modes
– passive: ambient light measurement
– active: distance measurement
• Distance measurement is extremely unprecise
– strongly depends on color and material of obstacle
– may be used to detect close obstacles
173c© A. Saffiotti 2017
Fuzzy rules for “AvoidObstacles”
#define NoDanger 3000 // if reading above this, no danger#define FullDanger 500 // if reading below this, max danger
procedure AvoidRules ()
unsigned int ir[8];FPred Obs_Left, Obs_Right, Obs_Ahead;
// First, read values of ir[0] ... ir[7] from the robot //
// Second, compute truth value of fuzzy predicates //
ObsLeft = RampDown(max(ir[5],ir[6]), FullDanger, NoDanger);ObsRight = RampDown(max(ir[1],ir[2]), FullDanger, NoDanger);ObsAhead = RampDown(max(ir[0],ir[7]), FullDanger, NoDanger);
// Third, the fuzzy rules //
RULESET;
IF (AND(Obs_Left, NOT(Obs_Right))) ROT(RIGHT);IF (AND(Obs_Right, NOT(Obs_Left))) ROT(LEFT);IF (AND(Obs_Right, Obs_Left)) ROT(AHEAD);
IF (Obs_Ahead) VEL(BACK);IF (AND(OR(Obs_Right, Obs_Left)), NOT(Obs_Ahead)) VEL(SLOW);IF (NOT(OR(OR(Obs_Right,Obs_Left), Obs_Ahead))) VEL(FAST);
RULEND;;
174c© A. Saffiotti 2017
The top-level program
free space all around (all IR = 0)
Robot + Environmentcontrol values
Reference value:
IR values
Evaluate the
Resp
onse
System output: System input:
Stimulus
Contro
ller outp
ut
Contro
ller input
RulesStim
ulu
s
Evaluate the
main ()
// initialization code
repeat
UpdatePosition(); // optional (WHY?)
ClearFSet(vlin); // Reset Fuzzy setsClearFSet(vrot);AvoidRules(); // Run the behaviour
DeFuzzify(vrot, 3, &rot); // Defuzzify and set rot/velDeFuzzify(vlin, 4, &vel);
vlin = ResponseToVel(vel);vrot = ResponseToRot(rot);SetPolarSpeed(vlin, vrot); // Send commands to robot
wait_some_time();
175c© A. Saffiotti 2017
Exercises
• Find good values for FullDanger an NoDanger
– consider the IR response graph
• Find a “better” set of AvoidObstacles rules
– eg: that is simpler, but still gives resonable behavior
– eg: that produces a better behavior
• Write a “FollowObstacle” behavior
– the robot should approach any obstacle
– it should follow a moving obstacle
176c© A. Saffiotti 2017