I'm using a quadrature encoder on inputs 3&4 to measure the motor's position.Ĭode: /*-*/ I call this position the "target position", I have a function called simpleSetTarget to do that. To move the motor to a new position we have to change the position where we want the motor to be. We call this function at a constant rate, either in the main loop of the program (not so good) or running in the background by using a repeating timer in Eas圜 or a task in ConVEX/PROS or ROBOTC. limit the motor control value to the range +/- 127 and send to the motor.Īll of the above are put into a function I'm calling simplePid in the example code. Do the PID calculation, this calculates the motor control value to send to the motor.ħ. Limit the integral variable so it doesn't get to large.Ħ. Add the error into the sum of all the errors from previous calculations, we call this variable the "integral".ĥ. Calculate the difference between the current error and the error last time you did the calculation, we will call this variable the "derivative"Ĥ. Calculate the difference between where you want the motor to be and where it actually is, we will call this variable the "error"ģ. Read the current position of the motor using the designated sensor.Ģ. The PID calculation consists of the following steps.ġ.
#Robotc pid control update#
I recommend you just update the PID variables at a constant rate. You need a way of updating the PID variables at a constant rate OR you need to modify the calculations to include time (or rather the change in time). It is, however, also possible to use a gyro, accelerometer or ultrasonic sensor as part of a PID closed loop controller.ī. For many of the robots you are designing this will usually be an IME, a quadrature encoder or a potentiometer. You need something to measure the position of the controlled motor. There are some pre-requisites for any system using PID.Ī.
#Robotc pid control how to#
So here is some simple code that shows the basics of how to do it. lets say you had four sensors, if the left most sensor sees the line then you could turn the right motor off while if the second sensor from the left sees the line you could simply slow the right motor, this way it would react more abruptly to sharp corners or if it moves farther off the line for some reason.Everyone keeps asking about PID control in Eas圜. In addition, if you add even more sensors you could add tiers of adjustments. The optional fourth case would say, if none of the sensors see the line, either turn the motors off or maybe spin until it finds the line again. The third case would say, if the line is under the middle sensor, set both motors to the same speed in an attempt to maintain a straight path.
![robotc pid control robotc pid control](https://s3.studylib.net/store/data/009745585_1-678347f2046e7018ce1ee8cce2f6c782.png)
Typically you put the third sensor in the middle and in addition to adjusting like you would with a two sensor line follower, you add a third and even a fourth case. The idea behind three sensor line following is that it makes the two sensor a little more robust.
![robotc pid control robotc pid control](https://i.ytimg.com/vi/hn5pCIyXbv4/maxresdefault.jpg)
The following should give you an idea of what this code would look like, I have not tested this code so proceed with caution.