我有一辆进行Webots模拟的机器人汽车,每当我按键盘上的一个键时,都需要停在一个地方。因此,如果按1,它应该停在坐标X = 140和坐标Z = 50的位置。在此代码上,速度使用UP和DOWN箭头进行上下移动。在情况1下,我可能必须插入if语句,但不知道如何实现。机器人始终在一条线上运行,并且只有在键盘命令提示时才应停止或动作。下面给出的代码只是一个片段,如有必要,我可以提供完整的代码。我是新手,刚开始学习C语言。
// GPS
WbDeviceTag gps;
double gps_coords[3] = {0.0, 0.0, 0.0};
double gps_speed = 0.0;
// misc variables
double speed = 0.0;
double steering_angle = 0.0;
int manual_steering = 0;
bool autodrive = true;
void print_help() {
printf("You can drive this car!\n");
printf("Select the 3D window and then use the cursor keys to:\n");
printf("
/
- steer\n");
printf("[UP]/[DOWN] - accelerate/slow down\n");
}
void set_autodrive(bool onoff) {
if (autodrive == onoff)
return;
autodrive = onoff;
switch (autodrive) {
case false:
printf("switching to manual drive...\n");
printf("hit [A] to return to auto-drive.\n");
break;
case true:
if (has_camera)
printf("switching to auto-drive...\n");
else
printf("impossible to switch auto-drive on without camera...\n");
break;
}
}
void check_keyboard() {
int key = wb_keyboard_get_key();
switch (key) {
case WB_KEYBOARD_UP:
set_speed(speed + 5.0);
break;
case WB_KEYBOARD_DOWN:
set_speed(speed - 5.0);
break;
case WB_KEYBOARD_RIGHT:
change_manual_steer_angle(+1);
break;
case WB_KEYBOARD_LEFT:
change_manual_steer_angle(-1);
break;
case 'A':
set_autodrive(true);
break;
case '1':
if (gps_coords[X] == "104.9" && gps_coords[Z] == "48.9") {
set_speed(speed - speed);
}
break;
}