Webots robot simulation platform (7) keyboard control car

In the previous blog , we used the distance sensor to enable the car to avoid obstacles in the environment. However, in actual use, we prefer to let the car follow the route we specify, that is, the remote control car walks. In this blog, we use The direction keys on the keyboard control the front, back, left, and right movement of the car to realize remote control of the robot or car.

1. Create a new C++ controller

Create a new C++ controller and name it keyboard , fill in the following code in keyboard.cpp and compile the
Insert picture description here
controller code:

#include <webots/Robot.hpp>
#include <webots/DistanceSensor.hpp>
#include <webots/Motor.hpp>
#include <webots/Keyboard.hpp>
#include <stdio.h>

#define TIME_STEP 64
// All the webots classes are defined in the "webots" namespace
using namespace webots;
 
int main(int argc, char **argv) {
    
    
  // create the Robot instance.
  Robot *robot = new Robot();
  Keyboard kb;
  
  DistanceSensor *ds[2];
  char dsNames[2][10] = {
    
    "ds_right","ds_left"};
  for (int i = 0; i < 2; i++) {
    
    
    ds[i] = robot->getDistanceSensor(dsNames[i]);
    ds[i]->enable(TIME_STEP);
  }

  // initialise motors
  Motor *wheels[4];
  char wheels_names[4][8] = {
    
    "wheel1", "wheel2", "wheel3", "wheel4"};
  for (int i = 0; i < 4; i++) {
    
    
    wheels[i] = robot->getMotor(wheels_names[i]);
    wheels[i]->setPosition(INFINITY);
    wheels[i]->setVelocity(0);
  }
  printf("init successd ...\n");
  
  kb.enable(TIME_STEP);
  double leftSpeed = 0.0;
  double rightSpeed = 0.0;

   // Main loop:
  // - perform simulation steps until Webots is stopping the controller
  while (robot->step(TIME_STEP) != -1)
   {
    
    
    int key = kb.getKey();
    if(key== 315)
    {
    
    
      leftSpeed = 3.0;
      rightSpeed = 3.0;
    }
    else if(key== 317)
    {
    
    
      leftSpeed = -3.0;
      rightSpeed = -3.0;
    }
    else if(key== 314)
    {
    
    
      leftSpeed = -3.0;
      rightSpeed = 3.0;
    }
    else if(key== 316)
    {
    
    
      leftSpeed = 3.0;
      rightSpeed = -3.0;
    }
    else  
    {
    
    
      leftSpeed = 0.0;
      rightSpeed = 0.0;
    }
    std::cout<< " Right Sensor Value:" <<ds[0]->getValue() << "  Left Sensor Value:" <<ds[1]->getValue() <<std::endl;  
     
     
     wheels[0]->setVelocity(leftSpeed);
     wheels[1]->setVelocity(rightSpeed);
     wheels[2]->setVelocity(leftSpeed);
     wheels[3]->setVelocity(rightSpeed);
  };

  // Enter here exit cleanup code.

  delete robot;
  return 0;
}

Then compile the controller. Use our previous blog ( Webot robot simulation platform (5) new four-wheeled car model ) to build a four-wheeled car to do experiments, and specify our newly constructed controller for the car

Insert picture description here

2. Code description

Here we are using the C++ version of the controller. The interface function of the controller is similar to that of C, except that it is replaced with a class variable operation method. The idea of ​​the program is to read the key value of the keyboard direction keys, determine which key is pressed, and set the speed of the left and right wheels of the car according to the key value.

The following two lines of code initialize the keyboard

 Keyboard kb;
  kb.enable(TIME_STEP);

kb.getKey() function is to read the key value

int key = kb.getKey();

You can also judge which button is pressed by the form of characters

int k = keyboard->getKey();
    switch (k) {
    
    
      case 'A':   message.assign("avoid obstacles");  break;
      case 'F':   message.assign("move forward");   break;
      case 'S':   message.assign("stop");    break;
      case 'T':   message.assign("turn");   break;
      case 'I':   displayHelp();    break;
      case 'R':   cout << "Teleport ROBOT1 at (" << x << "," << z << ")" << endl; break;
      default:    message.clear();
    }

3. Demonstration effect

The following picture is an effect of using the keyboard arrow keys to control the movement of the car back and forth, left and right
Insert picture description here

Reference

[1] 1https://cyberbotics.com/doc/reference/index?version=R2020a-rev1
[2] https://www.cyberbotics.com/doc/reference/motion

If you think the article is helpful to you, please help and like it. O(∩_∩)O

Welcome everyone to exchange and discuss in the comment area ([email protected])

Previous: Webot robot simulation platform (6) New four-wheeled car controller Next: Webot robot simulation platform (8) Add GPS sensor

Guess you like

Origin blog.csdn.net/crp997576280/article/details/105661899
Recommended