找回密码
 马上注册

QQ登录

只需一步,快速开始

查看: 6533|回复: 4

[RobotC]超声波传感器

[复制链接]
发表于 2013-8-5 09:17:17 | 显示全部楼层 |阅读模式
定义式:#pragmaconfig(Sensor, S4,     sonarSensor,         sensorSONAR)
上面的是一个示例
下面又是一段程序
前面上了那么多课,下面这段程序不难读懂
  1. #pragma config(Sensor, S4,     sonarSensor,         sensorSONAR)
  2. task main()
  3. {
  4.     do
  5.     {
  6.         motor[motorA]=100;
  7.         motor[motorC]=100;
  8.     }
  9.     while(SensorValue(sonarSensor)>20);
  10. }
复制代码
下面是两段关于超声波传感器的程序
  1. #pragma config(Sensor, S4,     sonarSensor,         sensorSONAR)
  2. task main(){
  3.     int speed = 0;

  4.     for(;;){
  5.         if(SensorValue(sonarSensor) > 35)
  6.             motor[motorC] = motor[motorB] = speed = 100;
  7.         else if(SensorValue(sonarSensor) < 35)
  8.             motor[motorC] = motor[motorB] = speed = -100;
  9.         else
  10.             motor[motorC] = motor[motorB] = speed = 0;

  11.         nxtDisplayCenteredTextLine(0, "Sonar Reading");
  12.         nxtDisplayCenteredBigTextLine(2, "%d", SensorValue(sonarSensor));

  13.         nxtDisplayCenteredTextLine(5, "%d", speed);
  14.         nxtDisplayCenteredTextLine(7, "Motor Speed");
  15.     }
  16. }
复制代码
  1. #pragma config(Sensor, S4,     sonarSensor,         sensorSONAR)
  2. task main()
  3. {
  4.     int speed = 0;
  5.     int sonarValue = 0;
  6.     int distance = 35;
  7.     while(true)
  8.     {
  9.         sonarValue = SensorValue(sonarSensor);
  10.         nxtDisplayCenteredTextLine(0, "Sonar Reading");
  11.         nxtDisplayCenteredBigTextLine(2, "%d", sonarValue);

  12.         wait1Msec(100);

  13.         speed = (SensorValue(sonarSensor) - distance);

  14.         if(speed > 100)
  15.         {
  16.             speed = 100;
  17.         }

  18.         nxtDisplayCenteredTextLine(5, "%d", speed);
  19.         nxtDisplayCenteredTextLine(7, "Motor Speed");

  20.         motor[motorC] = speed;
  21.         motor[motorB] = speed;
  22.     }
  23. }
复制代码
上面两段程序只是展示不要求读懂,之后的课会教你慢慢读懂
当然你也可以拷到NXT里,看看运行是什么情况
我都差点忘了传感器还有两个,角度传感器和颜色传感器
好吧,关于传感器还有两节课~~

如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
发表于 2013-9-9 02:58:38 | 显示全部楼层
  nxtDisplayCenteredTextLine(0, "Sonar Reading");
        nxtDisplayCenteredBigTextLine(2, "%d", SensorValue(sonarSensor));

        nxtDisplayCenteredTextLine(5, "%d", speed);
        nxtDisplayCenteredTextLine(7, "Motor Speed");
是什么意思?没看懂。
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

 楼主| 发表于 2013-9-18 21:15:27 | 显示全部楼层
063101444 发表于 2013-9-9 02:58
nxtDisplayCenteredTextLine(0, "Sonar Reading");
        nxtDisplayCenteredBigTextLine(2, "%d", Se ...

这个后面会讲到,现在说可能不好理解~~
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

发表于 2014-5-23 19:09:22 | 显示全部楼层
  1. #pragma config(Sensor, S4,     sonarSensor,         sensorSONAR)
  2. #pragma config(Sensor, S1,lightSensor,sensorLightActive)
  3. #pragma config(Sensor, S2,lightSensor,sensorLightActive)
  4. #pragma platform(NXT)          //智能在NXT中使用


  5. #define Kproportional 2         // Kp, 比例因子
  6. #define Kintegral 0.001          // Ki,积分因子
  7. #define Kderivative 10            // Kd,微分因子
  8. #define dt 1                   // 取样时间
  9. task main()
  10. {
  11.         for(;;){
  12.    do
  13.     {
  14.     int error = 0;
  15.     float previous_error = 0;
  16.     float setpoint = 0;
  17.     float actual_position = 0;
  18.     float proportional = 0;
  19.     int integral = 0;
  20.     float derivative = 0;
  21.     float output = 0;
  22.     float left = 0;
  23.     float right = 0;

  24.     // 设定马达初始速度
  25.     float speed=35;

  26.     // 开传感器
  27.     //SetSensorColorFull(IN_3);

  28.     // 设定初始值。
  29.     setpoint=50;
  30.     nxtDisplayTextLine(1,"setpoint= %d",setpoint);
  31.     wait1Msec(20);

  32.     // 循环
  33.     while (true)
  34.     {
  35.         // 读取传感器当前值
  36.         actual_position = SensorValue[lightSensor];
  37.         nxtDisplayTextLine(3,"ac_pos= %d",actual_position);

  38.         // 计算误差
  39.         error = setpoint - actual_position;
  40.         // 偏离航线... 发出声音示警
  41.         //if (error <> 0) PlayTone(TONE_B7, 1);

  42.         // 比例部分: 当前误差 x 比例因子
  43.         proportional = Kproportional * error;

  44.         // 积分: 误差累积
  45.         integral = integral + error;

  46.         // 微分: 误差变化率
  47.         derivative = (error - previous_error) / dt;

  48.         // 比例、积分、微分三部分加总
  49.         output = proportional + Kintegral * dt * integral + Kderivative * derivative;

  50.         // 保存当前误差,留作下次计算时的前次误差
  51.         previous_error = error;

  52.         // 控制左侧马达
  53.         left = speed - output;

  54.         // 控制右侧马达
  55.         right = speed + output;

  56.         // 判断是否越界
  57.         if (left >   100) left  =  100;
  58.         if (left <  -100) left  = -100;
  59.         if (right >  100) right =  100;
  60.         if (right < -100) right = -100;

  61.         motor[motorB]=left;
  62.         motor[motorC]=right;
  63.         nxtDisplayTextLine(5,"motorB= %d",left);
  64.         nxtDisplayTextLine(7,"motorC= %d",right);

  65.         // 等待取样时间
  66.         wait1Msec(dt);
  67.       }
  68.         
  69.   }
  70.     while(SensorValue(sonarSensor)>8);
  71.     motor[motorB]=0;
  72.     motor[motorC]=0;
  73.     wait1Msec(1000);
  74.    
  75.     nMotorEncoderTarget[motorA] = 150;
  76. motor[motorA] = -20;
  77. wait1Msec(800);
  78. motor[motorA]=0;
  79. wait1Msec(1000);
  80. nMotorEncoder[motorB] = 0;               
  81. nMotorEncoder[motorC] = 0;  
  82. // reset the Motor Encoder of Motor C
  83. nMotorEncoderTarget[motorB] = 300;        // set the  target for Motor Encoder of Motor B to 360
  84. nMotorEncoderTarget[motorC] = -300;        // set the  target for Motor Encoder of Motor C to 360
  85. motor[motorB] = 70;                       // motor B is run at a power level of 75
  86. motor[motorC] = -70;  
  87. // motor C is run at a power level of 75
  88.        
  89. while(nMotorRunState[motorB] != runStateIdle && nMotorRunState[motorC] != runStateIdle)
  90. wait1Msec(1000);
  91. {
  92.        
  93. motor[motorA]=25;
  94. wait1Msec(800);
  95. motor[motorB] = -50;                       // motor B is run at a power level of 75
  96. motor[motorC] = 50;
  97. wait1Msec(700);
  98. }
  99. }
  100. }
复制代码


想让小车先寻线,在线上遇到障碍物把障碍物放到旁边,现在问题是小车在一直寻线,不执行避障的程序求解
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

发表于 2014-5-23 19:36:06 | 显示全部楼层
就是只执行do....while里面的程序,不执行do....while外面的程序,为什么啊?
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-3-28 22:03 , Processed in 0.091116 second(s), 23 queries .

Powered by Discuz! X3.5

Copyright © 2001-2020, Tencent Cloud.

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