0

So I recently purchased:

DFRobot Wheel Encoders

DFRobot 3PA and 4WD Rovers (2pk)

Romeo V2 Arduino Controller

I was doing a bit of research online and I'm trying to figure out how to use the encoders to control the PWM applied to the motors which will make precise turns. This seems like a popular method to use. Here is some of my code. I have nothing for the encoders yet. I only have the sensors working the PWM control for the motors. All my code does is stop the motors when the front sensor detects a wall. My next step is to implement the encoders to give the robot a 90 degree turn. Does anyone have some sample code or tips on how to do this? Thanks in advanced!

  void stop(void)                      
    {
      digitalWrite(5,LOW);   
      digitalWrite(6,LOW);


    }   
    void forward(char a,char b)
    {
      analogWrite(5,a);
      digitalWrite(4,HIGH);    
      analogWrite(6,b);    
      digitalWrite(7,HIGH);

    }  
    void back(char a,char b)
    {
      analogWrite(5,a);
      digitalWrite(4,LOW);   
      analogWrite(6,b);    
      digitalWrite(7,LOW);


    }
    void Left(char a,char b)
    {
      analogWrite (5,a);
      digitalWrite(4,LOW);    
      analogWrite (6,b);    
      digitalWrite(7,HIGH);



    }
    void Right(char a,char b) 
    {
      analogWrite (5,a);
      digitalWrite(4,HIGH);    
      analogWrite (6,b);    
      digitalWrite(7,LOW);

    }
    void setup(void) 
    { 

      pinMode(buttonPin2, INPUT);
      pinMode(buttonPin3, INPUT);
      for(int i =4;i<=7;i++)
        pinMode(i, OUTPUT);  
      pinMode(LED_BUILTIN, OUTPUT);
     forward(100,100);


      Serial.begin(9600);


    }
void GetSensor()
{

  Sen_Val_B = SenAvg(Sensor_Back);
  Sen_Val_F = SenAvg(Sensor_Front);
  Sen_Val_R = SenAvg(Sensor_Right);
  Sen_Val_L = SenAvg(Sensor_Left);

}

int SenAvg(int SensorNumber)
{

  Arr[100]= 0;
  avg = 0;

  for(int i = 0; i<100; i++)
  {
    Arr[i] = analogRead(SensorNumber);
    avg = avg + Arr[i];
  }
    avg = avg/100;
    return avg;
} 
void loop(void) 
{

   GetSensor();
  if(Sen_Val_F > 200)
  {
   stop();
  }

}
0

All my code does is stop the motors when the front sensor detects a wall. My next step is to implement the encoders to give the robot a 90 degree turn.

would this work?

  if (wall_sensed()) turn_90right(); //if wall is sensed, turn 90 degrees to the right

You may also consider change

  digitalWrite(5,LOW);   
  digitalWrite(6,LOW);

to:

  digitalWrite(MOTOR_LF,STOP);   //stop left front motor
  digitalWrite(MOTOR_LR,STOP);   //stop left right motor

so that

  1. it is a lot more readable; and
  2. if you were to change pin assignment, it is done consistently throughout your code.
  • I can get the robot to turn but I'm trying to get precise 90 degree turns. I was doing the brute force approach but just using a delay. But this method seems to be inconsistent. – user367640 Feb 26 '17 at 18:53
  • Hmm, when making a turn, your wheels may slip. You could check the accuracy on a compass sensor. Because if you turn a few times, you'll slowly get an offset due the inaccuracies. With a compass you get an absolute heading instead of accumulating all turns (which accumulates the error). – Paul Jul 29 '17 at 9:49
0

What encoders do is give you clocked feedback on motor rotation, which you can count to get an idea of position and speed. So in order to turn by a specified amount, you will need to know how much your wheel has to turn for the given angle. You can then turn the wheel precisely by dividing your wheel circumference with the maximum encoder ticks per revolution.

Say we get 600 ticks per revolution and our wheel has a circumference of 10cm. We would then turn the wheel by 100mm / 600 = 0,16mm per encoder tick!

You could even write a function taking more parameters into account (robot diameter/distance between wheels) that calculates these values. A faster/more empirical approach than gathering all these values is to just drive your robot for x (i.e. 100) ticks and measure the angle it drove to get a direct relation between encoder ticks and robot angle.

Another, much more precise solution would be to utilize a compass or gyroscope to measure the robot's orientation and rely on that instead of the encoders.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.