找回密码
 马上注册

QQ登录

只需一步,快速开始

查看: 6494|回复: 3

如何在NXC语言中使用三眼乐高颜色传感器编写走黑线程序

[复制链接]
发表于 2012-12-31 11:00:38 | 显示全部楼层 |阅读模式
谢谢,各位大大了。急!!!
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
发表于 2012-12-31 13:10:24 | 显示全部楼层
  1. /* Techbricks.nl Line Follower
  2.    Based on a PID controller, using the NXT 2.0 color sensor
  3.    NXC firmware 1.28
  4.    www.techbricks.nl
  5.    last modified 03/03/2010                                    */

  6. /* Proportional gain, straight forward reaction of the controller
  7.    Larger values typically mean faster response since the larger the error,
  8.    the larger the proportional term compensation.
  9.    An excessively large proportional gain will lead to process instability and oscillation.*/
  10. #define Kproportional 6

  11. /* Integral gain, improves the controller accuracy
  12.    Larger values imply steady state errors are eliminated more quickly.
  13.    The trade-off is larger overshoot: any negative error integrated during transient response
  14.    must be integrated away by positive error before reaching steady state.*/
  15. #define Kintegral 0.0002

  16. /* Derivative gain, improves the controller speed
  17.    Larger values decrease overshoot, but slow down transient response
  18.    and may lead to instability due to signal noise amplification in the differentiation of the error.*/
  19. #define Kderivative 100

  20. // Sample time, determined the reaction rate
  21. #define dt 25

  22. // NXT 2.0 Color sensor connected to port 2.
  23. #define COLORSENSOR SENSOR_2

  24. task main()
  25. {
  26. int error = 0;
  27. float previous_error = 0;
  28. float setpoint = 0;
  29. float actual_position = 0;
  30. float proportional = 0;
  31. int integral = 0;
  32. float derivative = 0;
  33. float output = 0;
  34. float left = 0;
  35. float right = 0;

  36. // Set the motor speed.
  37. float speed=50;

  38. // Set the color sensor light on.
  39. SetSensorColorFull(IN_2);

  40. // Read the value from the light sensor at the start position. The sensor must be place above the black line.
  41. TextOut(1,LCD_LINE1,"Setpoint");
  42. setpoint = COLORSENSOR;
  43. NumOut(50,LCD_LINE1,setpoint);

  44. // never ending loop.
  45. while (true)
  46.   {
  47.    // Read the actual color sensor value.
  48.    actual_position = COLORSENSOR;
  49.    TextOut(1,LCD_LINE2,"Actual");
  50.    NumOut(50,LCD_LINE2,actual_position);

  51.    // Calculate the error, the differance between the setpoint and actual position.
  52.    error = setpoint - actual_position;
  53.    // Play a sound when the sensor is off the line
  54.    if (error <> 0) PlayTone(TONE_B7, 1);

  55.    // Proportional term makes a change to the output that is proportional to the current error value.
  56.    proportional = Kproportional * error;

  57.    // Integrate, sum of errors
  58.    integral = integral + error;

  59.    // Derivative, rate of change of the process error is calculated by determining the slope of the error over time.
  60.    derivative = (error - previous_error) / dt;

  61.    // Calculate sum of Proportional, Integral and Derivative.
  62.    output = proportional + Kintegral * dt * integral + Kderivative * derivative;

  63.    // save error value for period.
  64.    previous_error = error;

  65.    // Controll left motor
  66.    left = speed - output;
  67.    
  68.    // Controll right motor
  69.    right = speed + output;

  70.    // Adjust the left and right motor value.
  71.    if (left >   100) left  =  100;
  72.    if (left <  -100) left  = -100;
  73.    if (right >  100) right =  100;
  74.    if (right < -100) right = -100;

  75.    if (left < 0 )
  76.      {
  77.       OnFwd(OUT_A,-left);
  78.       TextOut(1,LCD_LINE4,"Left  Rev");
  79.       NumOut(55,LCD_LINE4,-left);
  80.      }
  81.    else
  82.      {
  83.       OnRev(OUT_A,left);
  84.       TextOut(1,LCD_LINE4,"Left  Fwd");
  85.       NumOut(55,LCD_LINE4,left);
  86.      }

  87.    if (right < 0 )
  88.      {
  89.        OnFwd(OUT_B,-right);
  90.        TextOut(1,LCD_LINE5,"Right Rev");
  91.        NumOut(55,LCD_LINE5,-right);
  92.       }
  93.     else
  94.       {
  95.        OnRev(OUT_B,right);
  96.        TextOut(1,LCD_LINE5,"Right Fwd");
  97.        NumOut(55,LCD_LINE5,right);
  98.        }

  99.    // Wait sample time.
  100.    Wait(dt);
  101.    }
  102. }
复制代码
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

发表于 2012-12-31 13:11:40 | 显示全部楼层
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

 楼主| 发表于 2012-12-31 21:46:44 | 显示全部楼层
谢谢,几位大大!!
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

手机版|中文乐高 ( 桂ICP备13001575号-7 )

GMT+8, 2024-11-21 20:44 , Processed in 0.091192 second(s), 23 queries .

Powered by Discuz! X3.5

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表