2

Author Topic: Bearing-based PID control  (Read 2085 times)

0 Members and 1 Guest are viewing this topic.

Offline rbtyingTopic starter

  • Supreme Robot
  • *****
  • Posts: 452
  • Helpful? 31
Bearing-based PID control
« on: September 15, 2010, 04:54:04 PM »
Dear all,
      My newest robot system will not have space for encoders, and thus my usual method for PID will not work (input = encoder reading, one PID loop / motor).  It is a differential drive robot, and will have a digital compass (HMC6352) with +/- 1 degree resolution.  How would I convert a bearing reading from the compass into a PID input, such that there are separate outputs for both motors?  Pseudocode, C, C++ are ok, this is for an Arduino Mega equivalent.

Offline madsci1016

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
Re: Bearing-based PID control
« Reply #1 on: September 15, 2010, 06:59:16 PM »
Are you using PWM to control the motors? Either way, let's assume 0 is off, and 255 is full on.

Let's say your speed of your robot is 150 PWM duty cycle.

The output of the PID will be some number that you add to one wheel's power, and subtract from the other. SO if your PID says turn left by +10, One wheel will get 160 , the other 140. Turn right would be -10, or 140, 160 on the wheels.

The two inputs would be goal heading, and actual heading, just like your usual PID; but you would have to handle 'wrap around'. IE, if set heading was 5 degrees, and actual was 355; your error would be +10, not -350.

Offline rbtyingTopic starter

  • Supreme Robot
  • *****
  • Posts: 452
  • Helpful? 31
Re: Bearing-based PID control
« Reply #2 on: September 15, 2010, 08:37:18 PM »
Yes, I am using PWM to control the motors.

Is the number I subtract from one motor's PWM equal to the amount I add to the other motor's PWM?  There's really two goals I'm going for here:  to be able to turn to a bearing, and to be able to hold course and drive straight on a bearing, the second more important than the first.  It seems to me that if the motors don't have the same slope as regards to PWM, there would need to be some variation on what gets added to the motors (unless I'm mistaken)

Offline madsci1016

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
Re: Bearing-based PID control
« Reply #3 on: September 15, 2010, 08:58:49 PM »
Is the number I subtract from one motor's PWM equal to the amount I add to the other motor's PWM? 


yes

Quote
It seems to me that if the motors don't have the same slope as regards to PWM, there would need to be some variation on what gets added to the motors (unless I'm mistaken)


Not sure what you mean by this.

Also, btw this is how i control my robot.



And i can do both of what you are asking to do.

Offline rbtyingTopic starter

  • Supreme Robot
  • *****
  • Posts: 452
  • Helpful? 31
Re: Bearing-based PID control
« Reply #4 on: September 15, 2010, 11:44:35 PM »
So SAGAR doesn't use encoders for the PID feedback?  My parts will arrive on Friday for actual testing, so thanks for the algorithmic help.

Psuedocode:
Code: [Select]
int pwm[2] = {127,127}; // initialize pwm vars
int desiredBearing = 0; // -180 to 180
int currentBearing = 0;

int kP;  // pid constants
int kD;
int kI;
int integral = 0;
int prevBearing = 0;


void PID() {
prevBearing = currentBearing;
currentBearing = read_compass;
currentBearing -=180;

int error = wrapAround(desiredBearing-currentBearing); // fix wraparound error
integral += error;
int deriv = (currentBearing - prevBearing) / deltaT;

int modification = kP*error + kI * integral + kD * deriv;

pwm[0] += modification;
pwm[1] -= modification;

// write pwm[] to motors

}

Does this pseudocode make sense?

Offline madsci1016

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
Re: Bearing-based PID control
« Reply #5 on: September 16, 2010, 05:32:18 PM »
Looks good.

Curious, why are making the heading coordinates -180 to 180 instead of 0-359?

You might also want to constrain the modification variable. It's just a way of limiting how hard the robot will turn. Every robot is different so you can try without and find you might not need it.

Offline rbtyingTopic starter

  • Supreme Robot
  • *****
  • Posts: 452
  • Helpful? 31
Re: Bearing-based PID control
« Reply #6 on: September 17, 2010, 02:19:53 AM »
No reason, really, but -180 to 180 seems to make it simpler.  I'll see when actual implementation goes through, my build time is tomorrow =)

 


Get Your Ad Here

data_list