|
各位高手,请指教。这个程序检测传感器信息for(i=0; i<8; i++) {flagSensor[i] = sensorDat...看不懂啊?
[复制链接]
/ 检测传感器信息
if(g_enSensor) {
len = read(fd_Sensor, &sensorData, sizeof(unsigned char));
if(len > 0) {
printf("sensorData is 0x%x \n", sensorData);
} else {
perror("error to read fd_Sensor\n");
exit(1);
}
}
for(i=0; i<8; i++) {
flagSensor = sensorData & 0x80;
sensorData <<= 1;
}
swap(&flagSensor[0], &flagSensor[1]);
swap(&flagSensor[2], &flagSensor[3]);
swap(&flagSensor[4], &flagSensor[5]);
swap(&flagSensor[6], &flagSensor[7]);
sum = 0;
for(i=0; i<8; i++) {
if(flagSensor) sum ++;
}
if(sum > 4) { // 已经到了终点
break;
}
for(i=0; i<8; i++) {
if(flagSensor) temp[0] = i + 1;
}
for(i=7; i>=0; i--) {
if(flagSensor) temp[1] = i + 1;
}
biasValue = temp[0] + temp[1];
printf("Sensor = %d\n", biasValue);
temp[0] = 0;
temp[1] = 0;
// 传感器数据滤波
// 根据模型得出两轮速度值
calc_motor(pMotor_L, (int)biasValue);
calc_motor(pMotor_R, (int)biasValue);
if(pMotor_L->speed >= 0) {
wr_buf[4] = 1;
wr_buf[2] = (unsigned char)pMotor_L->speed;
} else {
wr_buf[4] = 0;
wr_buf[2] = (unsigned char)((-1)*pMotor_L->speed);
}
if(pMotor_R->speed >= 0) {
wr_buf[5] = 1;
wr_buf[3] = (unsigned char)pMotor_R->speed;
} else {
wr_buf[5] = 0;
wr_buf[3] = (unsigned char)((-1)*pMotor_R->speed);
}
}
for(i=0; i
printf("%x ", wr_buf);
printf("\n ");
ret = write(fd_com1, wr_buf, CMD_BYTES); // 将数据从串口发出去
if(ret > 0) { // 发送成功,并回显已发送的信息
//*(wr_buf+ret) = '\0';
//printf("SEND : %s\n",wr_buf);
ret = 0;
}
else
perror("error to write fd_com1");
usleep(5000);
ret = read(fd_com1, rcv_buf, CMD_BYTES); // 试图从串口接收数 据
printf("RECV:\n");
if(ret > 0) // 确实接收到了数据,并打印出来
{
for(i=0; i
printf("%h ",rcv_buf);
ret = 0;
}
else // 没有接收到数据,打印相关信息,并提示输入字符
printf("Havn't received data from uart !\n");
}
wr_buf[2] = 0;
wr_buf[3] = 0;
wr_buf[4] = 1;
wr_buf[5] = 1;
wr_buf[6] = 0;
wr_buf[7] = 0;
write(fd_com1, wr_buf, CMD_BYTES);
printf("exit\n");
close(fd_com1); // 关闭设备COM1
close(fd_Sensor); // 关闭设备Sensor
return 0; // 正常返回
|
|