Fuzzy Logic for controlling DC motor

I want to design a Fuzzy logic control to control two DC motors of Rover robot actually I made a simulation for the controller in matlab/simulink and it worked successfully , now I want to design that controller in real world by using Arduino Uno board
actually I converted the MATLAB Fuzzy Inference System FIS to Arduino C by Converter in the "www.makeproto.com" site

Now for example if one of rules base of fuzzy controller is

if (HeadingAngle is Zero) and (Front_Sonser is Near) and (Right_Sonser is Near) and (Left_Sonser is Far) then (Wr is Turn_Small_Negative) and (Wl is Turn_Small_Positive)

Wr=torque to control right wheel (Right motor)
Wl=torque to control left wheel (left motor)
the membership functions of Wr and Wl in the simulation range from (-100 to 100)

Now if we supposed that the value of "Turn_Small_Negative" in the rule above is -20 and "Turn_Small_Positive" is 30 , How Could I make the two motors to spin in the given values from fuzzy control ?
Are the two outputs (Wr,Wl) of fuzzy control connected directly to DC motors of robot ?

The code of FIS after conversion in the attachment

fisFuzzyNewS.ino (15.4 KB)

=(???

Maria88:
Now if we supposed that the value of "Turn_Small_Negative" in the rule above is -20 and "Turn_Small_Positive" is 30 , How Could I make the two motors to spin in the given values from fuzzy control ?
Are the two outputs (Wr,Wl) of fuzzy control connected directly to DC motors of robot ?

No.... not if i understand correctly. Your outputs have direction (+ or -, say clockwise, anticlockwise) and speed (0-100) so you can't just feed those into a motor.

Assuming you have some kind of driver board with an h-bridge like say a 298 (else you can't get direction) you need to:

  • set the pair of ouputs to hi/lo or lo/hi depending on spin direction
  • pwm the enable pin from 0-255 mapped from the 0-100

What driver circuit do you have? My suggestion has a 298 type in mind: 3 pins per motor into the chip.

Standard fuzzy logic operates on real variables in the 0.0 -- 1.0 range,
very like representing probabilities. Your inputs and outputs should all be
interconverted into this range I think.

MarkT:
Standard fuzzy logic operates on real variables in the 0.0 -- 1.0 range,
very like representing probabilities.

Bart Kosko, in his book "Fuzzy thinking" explain that these values are not probabilities.

Ok let's not turn this into a debate on fuzzy logic and thus hijack the thread. AFAICS, she wants to turn motors in this spin direction or that, at this speed or that speed, according to the output she has from some algorithm or other. We can help with that regardless of the fuzzy logic debate.

My first thought was that in any case, the "fuzzy logic" in the title is a red herring, since her algorithm output is its output regardless of how it's calculated.

I answered according to my understanding of the OP's requirement... can you add to that?

True, fuzzy logic is a red-herring here, but note the OP mentions torque, not speed.

Is this really the intention - torque control of a motor is more complex and actually
poorly behaved in small motors where friction can dominate.

MarkT:
True, fuzzy logic is a red-herring here, but note the OP mentions torque, not speed.

Is this really the intention - torque control of a motor is more complex and actually
poorly behaved in small motors where friction can dominate.

Yeah I saw the torque thing too, and took it to mean speed from the context, not torque in the Fd sense, rather a loose use of the word in the sense of turn. Because all the robot needs to do is move left or right to some degree, based on the relative readings from the sensors. So I'm reasonably sure she means just "turn".

E&OE, Ts & Cs apply, YMMV etc etc

edit.... also the W as variable name might be capital omega which might be angular velocity....

JimboZA:

Maria88:
Now if we supposed that the value of "Turn_Small_Negative" in the rule above is -20 and "Turn_Small_Positive" is 30 , How Could I make the two motors to spin in the given values from fuzzy control ?
Are the two outputs (Wr,Wl) of fuzzy control connected directly to DC motors of robot ?

No.... not if i understand correctly. Your outputs have direction (+ or -, say clockwise, anticlockwise) and speed (0-100) so you can't just feed those into a motor.

Assuming you have some kind of driver board with an h-bridge like say a 298 (else you can't get direction) you need to:

  • set the pair of ouputs to hi/lo or lo/hi depending on spin direction
  • pwm the enable pin from 0-255 mapped from the 0-100

What driver circuit do you have? My suggestion has a 298 type in mind: 3 pins per motor into the chip.

yes I had a driver board ( h-bridge ) type 298 with four output (out 1 ,out 2, out 3 , out 4 ) two outputs for each motor

How could I set the pair of outputs to hi/lo or lo/hi ?
How to map the 0-100 to 0-255 ?

I have read the replies of members and that make me a little bit confused ,

actually the output of fuzzy controller is the angular velocity of motor. range (-100 to 100 )

sorry for this mistake

Wr=angular velocity of right wheel (Right motor)
Wl=angular velocity of left wheel (left motor)

the output of fuzzy controller is the angular velocity of motor

Yes I thought so.

How could I set the pair of outputs to hi/lo or lo/hi ?

pseudo code:
if W < 1
then digitalWrite(pin x,high) and digitalWrite(pin y, low)
or if W>1
then digitalWrite(pin x,low) and digitalWrite(pin y, high)

How to map the 0-100 to 0-255 ?

Look at map and use the [edit: absolute value of the -100 to +100 to give a] 0-255 value for an analogWrite to the EN pin

JimboZA:

the output of fuzzy controller is the angular velocity of motor

Yes I thought so.

How could I set the pair of outputs to hi/lo or lo/hi ?

pseudo code:

if W < 1
then digitalWrite(pin x,high) and digitalWrite(pin y, low)
or if W>1
then digitalWrite(pin x,low) and digitalWrite(pin y, high)






> How to map the 0-100 to 0-255 ?




Look at [map](http://arduino.cc/en/Reference/Map) and use the [edit: absolute value of the -100 to +100 to give a] 0-255 value for an analogWrite to the EN pin

thanks a lot JimboZA

Please , what is your opinion about the code below ?
It contains a outline for two steps that your are telling me about . Are there any bugs in it ?

void loop()
{


WL=g_fisOutput[0]   // Where  ' g_fisOutput[0] ' is the output of the fuzzy control (FIS) for left angular velocity after conversion in "www.makeproto.com"
WR=g_fisOutput[1]   // Where  ' g_fisOutput[1] ' is the output of the fuzzy control (FIS) for right angular velocity after conversion in "www.makeproto.com"


WL = map(WL, -100, 100, 0, 255);
  
if (WL < 1)
{
  digitalWrite(pin 1,high) and digitalWrite(pin 2, low)  // pin 1 goes directly to out1 in driver board 298 
else if (WL > 1)                                         // and pin 2 goes directly to out2 in the driver board 298
  digitalWrite(pin 1,low) and digitalWrite(pin 2, high)  
}
  
  
WR = map(WR, -100, 100, 0, 255);
  
  
if (WR < 1)
{
  digitalWrite(pin 3,high) and digitalWrite(pin 4, low)  // pin 3 goes directly to out3 in driver board 298 
else if (WR > 1)                                         // and pin 4 goes directly to out4 in the driver board 298
  digitalWrite(pin 3,low) and digitalWrite(pin 4, high)
}

  
}

Only one real way to tell if code has bugs: compile and run it. But yep this is buggy:

if (WL < 1)
{
  digitalWrite(pin 1,high) and digitalWrite(pin 2, low)  // pin 1 goes directly to out1 in driver board 298 
else if (WL > 1)                                         // and pin 2 goes directly to out2 in the driver board 298
  digitalWrite(pin 1,low) and digitalWrite(pin 2, high)  
}

There's no need for those "ands", that was probably me misleading you with my pseudo code. Also think your { } are wrong. More like this....

if (WL < 1)
{
  digitalWrite(pin 1,high);
digitalWrite(pin 2, low);
} 
else if (WL > 1)   
{                                      
  digitalWrite(pin 1,low);
digitalWrite(pin 2, high) ;
}

You also need to PWM the mapped 0-255 value to the 298's EN pin with an analogWrite.

JimboZA:
Only one real way to tell if code has bugs: compile and run it. But yep this is buggy:

if (WL < 1)

{
  digitalWrite(pin 1,high) and digitalWrite(pin 2, low)  // pin 1 goes directly to out1 in driver board 298
else if (WL > 1)                                         // and pin 2 goes directly to out2 in the driver board 298
  digitalWrite(pin 1,low) and digitalWrite(pin 2, high) 
}




There's no need for those "ands", that was probably me misleading you with my pseudo code. Also think your { } are wrong. More like this....



if (WL < 1)
{
  digitalWrite(pin 1,high);
digitalWrite(pin 2, low);
}
else if (WL > 1)   
{                                     
  digitalWrite(pin 1,low);
digitalWrite(pin 2, high) ;
}




You also need to PWM the mapped 0-255 value to the 2989's EN pin with an analogWrite.

Many thanks , yes , I meant that as pseudo code .

But please be patient with me , I am just beginner , I didn't understand this part "need to PWM the mapped 0-255 value to the 2989's EN pin with an analogWrite." could you give more about it ?

thanks

There are 3 pins for controlling each motor: the IN1 and IN2 pins you are already setting hi/lo or lo/hi for direction. But there's also the EN pin: that enables each motor. There's an ENA for one motor and ENB for the other. You must already have that high or else the motor wont work: you might have it wired to 5V, or set high from a digitalWrite, I don't know.

In any case, you now need to use it for speed control: PWM (Pulse Width Modulation) does that for us via analogWrite. It writes the value from 0 to 255 to the pin: it rapidly switches off and on, to give the illusion of a voltage between 0 and 5. So if you analogWrite (say) 130 you will get halfway between 0 and 5V since 130 is about halfway from 0 to 255. Note that sometimes motors won't fire at too low a PWM pseudo-voltage.... might be that a PWM of under 80 or 90 or 100 won't get it to move. You lose at least 2 real volts through the 298 anyway, up to about 4 or so at high currents.

PWM only works on the pins marked ~ on the board.

Read more here.

Attached pic refers; not the correct Arduino pin numbers but otherwise should be good.

JimboZA:
There are 3 pins for controlling each motor: the IN1 and IN2 pins you are already setting hi/lo or lo/hi for direction. But there's also the EN pin: that enables each motor. There's an ENA for one motor and ENB for the other. You must already have that high or else the motor wont work: you might have it wired to 5V, or set high from a digitalWrite, I don't know.

In any case, you now need to use it for speed control: PWM (Pulse Width Modulation) does that for us via analogWrite. It writes the value from 0 to 255 to the pin: it rapidly switches off and on, to give the illusion of a voltage between 0 and 5. So if you analogWrite (say) 130 you will get halfway between 0 and 5V since 130 is about halfway from 0 to 255. Note that sometimes motors won't fire at too low a PWM pseudo-voltage.... might be that a PWM of under 80 or 90 or 100 won't get it to move. You lose at least 2 real volts through the 298 anyway, up to about 4 or so at high currents.

PWM only works on the pins marked ~ on the board.

Read more here.

Attached pic refers; not the correct Arduino pin numbers but otherwise should be good.

Many thanks JimboZA , I don't know what words should I choose to explain about my deep thank to you , I really appropriated that support from you . I will try that and I hope to work successfully .

I don't know what words should I choose to explain about my deep thank to you

You're welcome: You'll pass the help on to someone else one day: that's the way it works.

11h30 pm here now so I'm out of here.... good luck.

i've do some project to control 4 motor and using fuzzy logic, and i also use that converter
when i compile this to my arduino IDE there's no problem and they can verify it and upload it
but when i run this,i relize that there is no changing and calculating with my output. when i change the input they're not chainging eventhouth i use around 570 rules :frowning:
so any body know what it can be happen?

Dengan_Fuzzy.ino (74.5 KB)

Maria88:
I want to design a Fuzzy logic control to control two DC motors of Rover robot

How Could I make the two motors to spin in the given values from fuzzy control ?
Are the two outputs (Wr,Wl) of fuzzy control connected directly to DC motors of robot ?

Generally, you use an H-bridge to control DC motors. There are plenty designed to work wit the arduino. You supply DC power to the H-bridge, and control it with two pins: a digital pin for forward/backward and a PWM pin for speed. The PWM pin has a range of 0-255.

Many h-bridge modules and boards come with a pair of bridges precisely because people use 'em for robots.

const byte leftDirectionPin = 4;
const byte leftSpeedPin = 5;
const byte rightSpeedPin = 6;
const byte rightDirectionPin = 7;

void setup() {
  pinMode(leftDirectionPin, OUTPUT);
  pinMode(leftSpeedPin, OUTPUT);
  pinMode(rightSpeedPin, OUTPUT);
  pinMode(rightDirectionPin, OUTPUT);
}

void loop() {
  if((millis() / 1000) % 2 == 0) {
    digitalWrite(leftDirectionPin, true);
    digitalWrite(leftSpeedPin, 20);
    digitalWrite(rightDirectionPin, true);
    digitalWrite(rightSpeedPin, 23);
  }
  else {
    digitalWrite(leftDirectionPin, false);
    digitalWrite(leftSpeedPin, 30);
    digitalWrite(rightDirectionPin, true);
    digitalWrite(rightSpeedPin, 5);
  }
}
[code]