|
楼主 |
发表于 2011-2-6 10:01:17
|
显示全部楼层
PID算法巡线例程
例程来自这里... 同时有小车的LDD图及程序代码下载。
http://www.techbricks.nl/My-NXT-projects/nxtlinefollower.html这段代码在论坛里看到过,不过不完整。
- /* Techbricks.nl Line Follower
- Based on a PID controller, using the NXT 2.0 color sensor
- NXC firmware 1.28
- www.techbricks.nl
- last modified 03/03/2010 */
- /* Proportional gain, straight forward reaction of the controller
- Larger values typically mean faster response since the larger the error,
- the larger the proportional term compensation.
- An excessively large proportional gain will lead to process instability and oscillation.*/
- #define Kproportional 6
- /* Integral gain, improves the controller accuracy
- Larger values imply steady state errors are eliminated more quickly.
- The trade-off is larger overshoot: any negative error integrated during transient response
- must be integrated away by positive error before reaching steady state.*/
- #define Kintegral 0.0002
- /* Derivative gain, improves the controller speed
- Larger values decrease overshoot, but slow down transient response
- and may lead to instability due to signal noise amplification in the differentiation of the error.*/
- #define Kderivative 100
- // Sample time, determined the reaction rate
- #define dt 25
- // NXT 2.0 Color sensor connected to port 2.
- #define COLORSENSOR SENSOR_2
- task main()
- {
- int error = 0;
- float previous_error = 0;
- float setpoint = 0;
- float actual_position = 0;
- float proportional = 0;
- int integral = 0;
- float derivative = 0;
- float output = 0;
- float left = 0;
- float right = 0;
- // Set the motor speed.
- float speed=50;
- // Set the color sensor light on.
- SetSensorColorFull(IN_2);
- // Read the value from the light sensor at the start position. The sensor must be place above the black line.
- TextOut(1,LCD_LINE1,"Setpoint");
- setpoint = COLORSENSOR;
- NumOut(50,LCD_LINE1,setpoint);
- // never ending loop.
- while (true)
- {
- // Read the actual color sensor value.
- actual_position = COLORSENSOR;
- TextOut(1,LCD_LINE2,"Actual");
- NumOut(50,LCD_LINE2,actual_position);
- // Calculate the error, the differance between the setpoint and actual position.
- error = setpoint - actual_position;
- // Play a sound when the sensor is off the line
- if (error <> 0) PlayTone(TONE_B7, 1);
- // Proportional term makes a change to the output that is proportional to the current error value.
- proportional = Kproportional * error;
- // Integrate, sum of errors
- integral = integral + error;
- // Derivative, rate of change of the process error is calculated by determining the slope of the error over time.
- derivative = (error - previous_error) / dt;
- // Calculate sum of Proportional, Integral and Derivative.
- output = proportional + Kintegral * dt * integral + Kderivative * derivative;
- // save error value for period.
- previous_error = error;
- // Controll left motor
- left = speed - output;
-
- // Controll right motor
- right = speed + output;
- // Adjust the left and right motor value.
- if (left > 100) left = 100;
- if (left < -100) left = -100;
- if (right > 100) right = 100;
- if (right < -100) right = -100;
- if (left < 0 )
- {
- OnFwd(OUT_A,-left);
- TextOut(1,LCD_LINE4,"Left Rev");
- NumOut(55,LCD_LINE4,-left);
- }
- else
- {
- OnRev(OUT_A,left);
- TextOut(1,LCD_LINE4,"Left Fwd");
- NumOut(55,LCD_LINE4,left);
- }
- if (right < 0 )
- {
- OnFwd(OUT_B,-right);
- TextOut(1,LCD_LINE5,"Right Rev");
- NumOut(55,LCD_LINE5,-right);
- }
- else
- {
- OnRev(OUT_B,right);
- TextOut(1,LCD_LINE5,"Right Fwd");
- NumOut(55,LCD_LINE5,right);
- }
- // Wait sample time.
- Wait(dt);
- }
- }
复制代码
|
|