### Author Topic: Guidance with PID algorithm using pots  (Read 1294 times)

0 Members and 1 Guest are viewing this topic.

#### Robotics12

• Jr. Member
• Posts: 33
##### Guidance with PID algorithm using pots
« on: March 23, 2012, 08:08:55 PM »
I was given an article by Daanii: http://www.inpharmix.com/jps/PID_Controller_For_Lego_Mindstorms_Robots.html
I understand the Proportional, but have a few questions.

I am attempting to write the 'P' algorithm for the project and I understand the Proportional, but have a few questions.

Pots view, feedback and target and also wiper motor to turn:
http://img703.imageshack.us/img703/9350/fullfrontpots.jpg

Example:If I have a control input from 200 to 800 (center of 500) and a measurement ranging from 0 to 800 (center of 400). Then: target = (input - 500) * (500/300) + 400;

First question, to get the 0 to 800 with center of 400, is that just determined off of the control input values?
To get the control input values that were used of 200 to 800 with the center being 500. Is this the same as me using the min and max of the steering wheel pot?
Using this image:http://img560.imageshack.us/img560/7393/topsteeringwheelpin.jpg
you can see I have a pin that allows me to go left and right only a certain distance. I have code wrote to output the value of the current steering input, so I need to find the min, max, and center of my pot using this method?

I will be able to use:
target = (input - 500) * (500/300) + 400;
Once I find the values of my pot, with them replacing the example ones of course?

This second arduino will be able to compare the 'target' and 'current' values to use the 'P'. I plan to trial and error the Kp. Now it can use the:
error = target-current;
Turn = error * kP;

Also, I plan to send the Turn value to the roboclaw to determine left/right and how fast. Is this correct way? And actually sending it to the roboclaw I am slightly confused.
« Last Edit: March 24, 2012, 01:10:11 AM by Robotics12 »

#### Daanii

• Robot Overlord
• Posts: 138
##### Re: Guidance with PID algorithm using pots
« Reply #1 on: March 24, 2012, 12:14:28 PM »
You're making a lot of good progress, and I think are close to getting there.

As you say, you need to get the pot values for the minimums and maximums of both the steering wheel and the wheels. Once you have those, you can write your code. Be sure to leave room for those values changing somewhat. You may want to check the values from time to time to re-calibrate your code.

Your formula looks right. Or you could just use the Arduino map function. For example, you could do something like this:

Code: [Select]
`steeringWheelInput = Serial.read(); steeringWheelInput = constrain(steeringWheelInput, pot1Min, pot1Max);targetPosition = map(steeringWheelInput, pot1Min, pot1Max, -100, 100); wheelsInput = analogRead(A0);wheelsInput = constrain(wheelsInput, pot2Min, pot2Max);currentPosition = map(wheelsInput, pot2Min, pot2Max, -100, 100);error = targetPosition - currentPosition;if (error > 10) {     turn = Kp * error;     turn = constrain(turn, -63, 63);     turn = turn + 64;     (send turn to Roboclaw)     }`
That will send a signal to the Roboclaw that will be between 1 (maximum speed one way) and 127 (maximum speed the other way, with 64 being a stop.

The best way to proceed is probably to wire things up and start trying bits of the project. For example, reading the pots. Spinning the motor (unlink it from the steering first, so you can experiment).

If you run into trouble doing one of those things, seek help here or at the Arduino forum. You can usually get help on something like that when you are stuck. It's the more vague questions that get few if any responses.

#### Robotics12

• Jr. Member
• Posts: 33
##### Re: Guidance with PID algorithm using pots
« Reply #2 on: March 24, 2012, 12:52:48 PM »
Perfect, I was along the right path then. It is MUCH easier, for a beginner, seeing these things like your post rather than trying to imagine what to do since I have this is all fairly new to me. I see where you get the 1-127 as I looked in the roboclaw datasheet and depending on the channel, 1-127 will tell it reverse, stop, forward. Putting this into practice was little confusing until I saw it.

Should be able to get the min,max later today and hopefully try to start testing very soon....excited. Thanks for all the help so far, I am sure I will be back!

#### Robotics12

• Jr. Member
• Posts: 33
##### Re: Guidance with PID algorithm using pots
« Reply #3 on: March 26, 2012, 11:45:51 AM »
Yea, I have tried reading the pots, just about that got ready to go. My main question now is, to send data to the roboclaw would something like this work? This is the part I am concerned about right now as I have not been able to see how data is actually sent to the roboclaw so here is my shot(this is only part of code):

This is the brake example, I want to either apply full forward or full reverse on the actuator. My problem is the code sending to the roboclaw. I will be using channel 2 on the roboclaw which is why I use 128 and 255.
Code: [Select]
`#include <SoftwareSerial.h>//RECEIVE VALUES FROM ARDUINO1char string[4];int var;int index;boolean started=false;boolean ended=false;//SoftwareSerial steer(2); //steering connected to pin 0//SoftwareSerial brake(4); //brake connected to pin 1void setup(){  Serial.begin(9600);  pinMode(2, OUTPUT);  pinMode(4, OUTPUT);}void loop(){else if(ltr == 'B') //if pot1 changed, do this using value above    {            //This will be on channel 2. 128 = full reverse, 192=stop, 255=full forward      if(val > 150)      {        //send 128 to the roboclaw Channel 2 to apply full brake        digitalWrite(4, 128);        //brake.write(128);      }      else      {        //send 255 to roboclaw to not brake        digitalWrite(4, 255);        //brake.write(255);      }              }}`
« Last Edit: March 26, 2012, 04:08:36 PM by Robotics12 »

#### Soeren

• Supreme Robot
• Posts: 4,672
• Mind Reading: 0.0
##### Re: Guidance with PID algorithm using pots
« Reply #4 on: March 26, 2012, 04:41:13 PM »
Hi,

Would you mind posting your software questions in "Software"?
Regards,
Søren

A rather fast and fairly heavy robot with quite large wheels needs what? A lot of power?
Engineering is based on numbers - not adjectives

#### Daanii

• Robot Overlord
• Posts: 138