We've got to make an PID line follower as school project. I got most of the program finished. But I got stuck on the PID part..
I'm using the PID library for arduino. After the calculation has been made I have an outcome and I need to use that outcome to control my HBridge and eventually my DC engines.
Now this is the part that confuses me, how do I go from the PID outcome to a signal for my engines. I would like to use timer0 (pin5 and 6).
From my (little) experience the trick is to map() the pid output to the range you actuator expects.
In this case I think it's something conceptually analogous to map(pidOut, 0, 1, 0, 255), where 0 and 1 are the min and max values that the pid out variable can reach.
my 2 cents.
PS: it usually helps to post the full code one is having trouble with or questions about.
how do I go from the PID outcome to a signal for my engines.
It depends on what kind of output you get from your pid process and what kind of input your engine needs.
Your pid could be producing a numerical value in a range, and your engine could be expecting a pwm input. In that case, simply use the pid output to drive a pwm generator;
Your pid could be producing a on/off signal too. In that case, you may have to figure out what kind of pwm signal means 1 to your engine and what means 0. and configure your pwm generator accordingly.
dhenry:
Your pid could be producing a numerical value in a range, and your engine could be expecting a pwm input. In that case, simply use the pid output to drive a pwm generator;
It produces a numerical value in a range from 0 to 255. And I want to use my pid output to drive a pwm generator I just dont know how.. I'm kinda new to arduino..
I would like to use pin 5 an 6 for this (so timer0). Can you tell me how I can change the frequentie of this timer using the outcome of my pid library..
Giete:
how do I go from the PID outcome to a signal for my engines.
Exactly what is it you want to be controlled by the PID algorithm? I don't mean what hardware - obviously, you're controlling the motors - but what aspect of your line-follower's behaviour is the PID intended to control?
(I guess I know what the answer is, but unless you know then you won't succeed in controlling it.)
And I want to use my pid output to drive a pwm generator I just dont know how..
analogWrite() would be a starting point.
Whether it works for your engine is another question.
I'm kinda new to arduino..
In that case, you may want to spend some time reading the documentation for the compiler / ide. Diving into programming with tools you don't quite understand is counter-productive.
PeterH:
Exactly what is it you want to be controlled by the PID algorithm? I don't mean what hardware - obviously, you're controlling the motors - but what aspect of your line-follower's behaviour is the PID intended to control?
There are two engines one on the left side of the line follower and one on the right side. The purpose is to control these engines in a way that if the robot goes the wrong way or comes up to a turn that one engine can get more speed than the other (using PWM). I just dont know how to manipulate this waves.
I know the basics :p. I am trying to manipulate the waves using CTC mode with the following code:
PeterH:
Exactly what is it you want to be controlled by the PID algorithm? I don't mean what hardware - obviously, you're controlling the motors - but what aspect of your line-follower's behaviour is the PID intended to control?
There are two engines one on the left side of the line follower and one on the right side. The purpose is to control these engines in a way that if the robot goes the wrong way or comes up to a turn that one engine can get more speed than the other (using PWM). I just dont know how to manipulate this waves.
As I thought - what you're trying to control is steering.
In order to achieve this your PID algorithm would need to output a steering demand. For example, you might define the error term as the difference between the position of the line and the middle of your sensing array, and make the steering try to keep it centered. (This doesn't really need a PID, but if your objective is to use PID then there's no harm in using it.)
So, the output from PID will be a steering demand. It's up to you to define how that is represented, but for example you might decide that a positive value means steer right and a negative value means steer left - zero means keep going straight forwards. To implement the steering from that value you would subtract the steering value from the right hand motor speed and add it to the left hand motor speed. Having worked out the speeds, you would just output them to the 'enable' pins of your H bridge drivers using analogWrite().
Depending how sharp a turn you need to make, you may need to have a motor actually stop and go backwards. That would correspond to your calculated speed being negative. In that case you would reverse the polarity of the H bridge and do the analogWrite() just as before.