小弟初学robotc 想做一个寻线加避障的小车,可是写出来的代码有问题
- #pragma config(Sensor, S4, sonarSensor, sensorSONAR)
- #pragma config(Sensor, S1,lightSensor,sensorLightActive)
- #pragma config(Sensor, S2,lightSensor,sensorLightActive)
- #pragma platform(NXT) //智能在NXT中使用
- #define Kproportional 2 // Kp, 比例因子
- #define Kintegral 0.001 // Ki,积分因子
- #define Kderivative 10 // Kd,微分因子
- #define dt 1 // 取样时间
- task main()
- {
- for(;;){
- do
- {
- motor[motorA]=100;
- motor[motorC]=100;
-
- }
- while(SensorValue(sonarSensor)>8);
- motor[motorB]=0;
- motor[motorC]=0;
- wait1Msec(1000);
-
- nMotorEncoderTarget[motorA] = 150;
- motor[motorA] = -20;
- wait1Msec(800);
- motor[motorA]=0;
- wait1Msec(1000);
- nMotorEncoder[motorB] = 0;
- nMotorEncoder[motorC] = 0;
- // reset the Motor Encoder of Motor C
- nMotorEncoderTarget[motorB] = 300; // set the target for Motor Encoder of Motor B to 360
- nMotorEncoderTarget[motorC] = -300; // set the target for Motor Encoder of Motor C to 360
- motor[motorB] = 70; // motor B is run at a power level of 75
- motor[motorC] = -70;
- // motor C is run at a power level of 75
-
- while(nMotorRunState[motorB] != runStateIdle && nMotorRunState[motorC] != runStateIdle)
- wait1Msec(1000);
- {
-
- motor[motorA]=25;
- wait1Msec(800);
- motor[motorB] = -50; // motor B is run at a power level of 75
- motor[motorC] = 50;
- wait1Msec(700);
- }
- }
- }
复制代码
这个代码能很好的执行,小车检测到前面的障碍物就把它放到旁边
但是下面这个代码加上寻线之后
- #pragma config(Sensor, S4, sonarSensor, sensorSONAR)
- #pragma config(Sensor, S1,lightSensor,sensorLightActive)
- #pragma config(Sensor, S2,lightSensor,sensorLightActive)
- #pragma platform(NXT) //智能在NXT中使用
- #define Kproportional 2 // Kp, 比例因子
- #define Kintegral 0.001 // Ki,积分因子
- #define Kderivative 10 // Kd,微分因子
- #define dt 1 // 取样时间
- task main()
- {
- for(;;){
- do
- {
- 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;
- // 设定马达初始速度
- float speed=35;
- // 开传感器
- //SetSensorColorFull(IN_3);
- // 设定初始值。
- setpoint=50;
- nxtDisplayTextLine(1,"setpoint= %d",setpoint);
- wait1Msec(20);
- // 循环
- while (true)
- {
- // 读取传感器当前值
- actual_position = SensorValue[lightSensor];
- nxtDisplayTextLine(3,"ac_pos= %d",actual_position);
- // 计算误差
- error = setpoint - actual_position;
- // 偏离航线... 发出声音示警
- //if (error <> 0) PlayTone(TONE_B7, 1);
- // 比例部分: 当前误差 x 比例因子
- proportional = Kproportional * error;
- // 积分: 误差累积
- integral = integral + error;
- // 微分: 误差变化率
- derivative = (error - previous_error) / dt;
- // 比例、积分、微分三部分加总
- output = proportional + Kintegral * dt * integral + Kderivative * derivative;
- // 保存当前误差,留作下次计算时的前次误差
- previous_error = error;
- // 控制左侧马达
- left = speed - output;
- // 控制右侧马达
- right = speed + output;
- // 判断是否越界
- if (left > 100) left = 100;
- if (left < -100) left = -100;
- if (right > 100) right = 100;
- if (right < -100) right = -100;
- motor[motorB]=left;
- motor[motorC]=right;
- nxtDisplayTextLine(5,"motorB= %d",left);
- nxtDisplayTextLine(7,"motorC= %d",right);
- // 等待取样时间
- wait1Msec(dt);
- }
-
- }
- while(SensorValue(sonarSensor)>8);
- motor[motorB]=0;
- motor[motorC]=0;
- wait1Msec(1000);
-
- nMotorEncoderTarget[motorA] = 150;
- motor[motorA] = -20;
- wait1Msec(800);
- motor[motorA]=0;
- wait1Msec(1000);
- nMotorEncoder[motorB] = 0;
- nMotorEncoder[motorC] = 0;
- // reset the Motor Encoder of Motor C
- nMotorEncoderTarget[motorB] = 300; // set the target for Motor Encoder of Motor B to 360
- nMotorEncoderTarget[motorC] = -300; // set the target for Motor Encoder of Motor C to 360
- motor[motorB] = 70; // motor B is run at a power level of 75
- motor[motorC] = -70;
- // motor C is run at a power level of 75
-
- while(nMotorRunState[motorB] != runStateIdle && nMotorRunState[motorC] != runStateIdle)
- wait1Msec(1000);
- {
-
- motor[motorA]=25;
- wait1Msec(800);
- motor[motorB] = -50; // motor B is run at a power level of 75
- motor[motorC] = 50;
- wait1Msec(700);
- }
- }
- }
复制代码
上面这个代码加上寻线之后,小车只执行寻线的那部分,没办法超声波检测了,求解 |