5 years, 4 months ago.

how to make my mbed car move and count even and odd for the servo to move up and down. thanks

  1. include "mbed.h"
  2. include "Servo.h"
  3. include "Motor.h"
  4. include "QEI.h"

Servo myservo(p23);

Motor R(p22,p5,p6);

Motor L(p21,p7,p8);

DigitalIn DFL(p11);

DigitalIn DFR(p12);

DigitalIn BUT(p13);

DigitalIn FRM(p18);

DigitalOut myled(LED1),myled2(LED2);

QEI Lwheel (p9,p30, NC , 48,QEI :: X4_ENCODING);

QEI Rwheel (p29,p10, NC , 48,QEI :: X4_ENCODING);

int y=0;

void followline()

{

if(DFL == 1 && DFR == 1)

{

R.speed(0.5);

L.speed(0.5); }

else if(DFL == 0 && DFR == 1)

{

L.speed(0);

R.speed(0.5);

}

else if(DFL == 1 && DFR == 0)

{

L.speed(0.5);

R.speed(0); }

else if(DFL == 0 && DFR == 0)

{

R.speed(0.5);

L.speed(0.5);

}

}

int main()

{

for(y=0;y<10;y++)

{

followline();

if ((FRM == 0) && (y % 2))

{ myled=1

R.speed (0);

L.speed (0);

wait(0.2);

myservo = 0;

y=y+1;

followline();

}

if ((FRM == 0) && (( y % 2)== 0))

{

myled2 = 1;

R.speed (0);

L.speed (0);

wait(0.2);

myservo = 0.5;

y=y+1;

followline();

}

else

{

followline();

}

}

}

1 Answer

5 years, 4 months ago.

Firstly please use <<code>> and <</code>> when posting code (see the editing tips for details). Without it formatting gets lots and it's hard to tell the difference between comments and code.

Here's your code with a couple of tidy ups to remove duplicate lines and simplify the logic, functionally there is only one minor difference detailed below.

#include "mbed.h"
#include "Servo.h"
#include "Motor.h"
#include "QEI.h"

Servo myservo(p23);
Motor R(p22,p5,p6);
Motor L(p21,p7,p8);

DigitalIn DFL(p11);
DigitalIn DFR(p12);

DigitalIn BUT(p13);
DigitalIn FRM(p18);

DigitalOut myled(LED1),myled2(LED2);

QEI Lwheel (p9,p30, NC , 48,QEI :: X4_ENCODING);
QEI Rwheel (p29,p10, NC , 48,QEI :: X4_ENCODING);

int y=0;

void followline() {
// simplified from your version, rather than checking exact values do a comparison between the two.  
  if (DFR > DFL) {
    L.speed(0);
    R.speed(0.5);
  } else if (DFL > DFR) {
    L.speed(0.5);
    R.speed(0);
  } else {
    R.speed(0.5);
    L.speed(0.5);
  }
}

int main() {

  for(y=0;y<10;y++) { 
    followline();

    if (FRM == 0) { // both if's were identical other than one line so combine them 
       myled=1; // you missed the ; on this in the first if.
       R.speed (0);
       L.speed (0);
    
       wait(0.2);
       if (y%2)                    // this was the difference between the two.
         myservo = 0;
       else
         myservo = 0.5;

      y=y+1;
   }
  followline(); // every path through the if statement ended wit h this so moved outside the if
  }
}

There are two things that seem a little odd about this:

Firstly you call followline() twice per pass through your for loop, once at the start and once in each possible path through the if statement. (The difference between my version and your original code was that in the situation FRM == 0 and y%2 == 1 your code was calling followline() 3 times, once at the start, once in the if and one in the else clause for the second if statment)

Secondly followline looks to be setting motor speeds but you don't ever wait after setting them so the robot doesn't have any time to move, at the very least you need a wait() at the end of the for loop.

What would probably work better is if you had a global variable setting the speed you want to travel at and then use a ticker to call followline() at a constant rate (say 20Hz). Followline() would then set the motor speeds based on both the line sensors and the global speed variable.

This would allow your main code to not have to worry about following the line, it just sets the speed it wants to travel at and following the line at that speed happens automatically in the background.

Accepted Answer